feat: Implemented price set update with prices and aligned pricing API (#6872)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-03-29 10:23:24 +00:00
1 parent 85a27c3572
commit 86f499de2f
9 files changed
+326 -49

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/pricing": minor
"@medusajs/types": minor
"@medusajs/core-flows": patch
---
Aligned pricing module price set API with convention
@@ -1,26 +1,36 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPricingModuleService, UpdatePriceSetDTO } from "@medusajs/types" import { IPricingModuleService, PricingTypes } from "@medusajs/types"
import { import {
convertItemResponseToUpdateRequest, convertItemResponseToUpdateRequest,
getSelectsAndRelationsFromObjectArray, getSelectsAndRelationsFromObjectArray,
} from "@medusajs/utils" } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk" import { StepResponse, createStep } from "@medusajs/workflows-sdk"
type UpdatePriceSetsStepInput = {
selector: PricingTypes.FilterablePriceSetProps
update: PricingTypes.UpdatePriceSetDTO
}
export const updatePriceSetsStepId = "update-price-sets" export const updatePriceSetsStepId = "update-price-sets"
export const updatePriceSetsStep = createStep( export const updatePriceSetsStep = createStep(
updatePriceSetsStepId, updatePriceSetsStepId,
async (data: UpdatePriceSetDTO[], { container }) => { async (data: UpdatePriceSetsStepInput, { container }) => {
const pricingModule = container.resolve<IPricingModuleService>( const pricingModule = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING ModuleRegistrationName.PRICING
) )
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data) const { selects, relations } = getSelectsAndRelationsFromObjectArray([
const dataBeforeUpdate = await pricingModule.list( data.update,
{ id: data.map((d) => d.id) }, ])
{ relations, select: selects }
)
const updatedPriceSets = await pricingModule.update(data) const dataBeforeUpdate = await pricingModule.list(data.selector, {
select: selects,
relations,
})
const updatedPriceSets = await pricingModule.update(
data.selector,
data.update
)
return new StepResponse(updatedPriceSets, { return new StepResponse(updatedPriceSets, {
dataBeforeUpdate, dataBeforeUpdate,
@@ -29,17 +39,17 @@ export const updatePriceSetsStep = createStep(
}) })
}, },
async (revertInput, { container }) => { async (revertInput, { container }) => {
if (!revertInput) { if (!revertInput || !revertInput.dataBeforeUpdate?.length) {
return return
} }
const { dataBeforeUpdate = [], selects, relations } = revertInput const { dataBeforeUpdate, selects, relations } = revertInput
const pricingModule = container.resolve<IPricingModuleService>( const pricingModule = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING ModuleRegistrationName.PRICING
) )
await pricingModule.update( await pricingModule.upsert(
dataBeforeUpdate.map((data) => dataBeforeUpdate.map((data) =>
convertItemResponseToUpdateRequest(data, selects, relations) convertItemResponseToUpdateRequest(data, selects, relations)
) )
@@ -39,7 +39,7 @@ export const updatePricingRuleTypesStep = createStep(
ModuleRegistrationName.PRICING ModuleRegistrationName.PRICING
) )
await pricingModule.update( await pricingModule.updateRuleTypes(
dataBeforeUpdate.map((data) => dataBeforeUpdate.map((data) =>
convertItemResponseToUpdateRequest(data, selects, relations) convertItemResponseToUpdateRequest(data, selects, relations)
) )
@@ -265,21 +265,64 @@ moduleIntegrationTestRunner({
describe("update", () => { describe("update", () => {
const id = "price-set-1" const id = "price-set-1"
it("should throw an error when a id does not exist", async () => { it("should throw an error when an id does not exist", async () => {
let error let error = await service
.update("does-not-exist", {})
.catch((e) => e.message)
try { expect(error).toEqual(
await service.update([ "PriceSet with id: does-not-exist was not found"
{ )
id: "does-not-exist", })
},
it("should create, update, and delete prices to a price set", async () => {
const priceSetBefore = await service.retrieve(id, {
relations: ["prices"],
})
const updateResponse = await service.update(priceSetBefore.id, {
prices: [
{ amount: 100, currency_code: "USD" },
{ amount: 200, currency_code: "EUR" },
],
})
const priceSetAfter = await service.retrieve(id, {
relations: ["prices"],
})
expect(priceSetBefore.prices).toHaveLength(1)
expect(priceSetBefore.prices?.[0]).toEqual(
expect.objectContaining({
amount: 500,
currency_code: "USD",
})
)
expect(priceSetAfter.prices).toHaveLength(2)
expect(priceSetAfter.prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "USD",
}),
expect.objectContaining({
amount: 200,
currency_code: "EUR",
}),
])
)
expect(updateResponse.prices).toHaveLength(2)
expect(updateResponse.prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "USD",
}),
expect.objectContaining({
amount: 200,
currency_code: "EUR",
}),
]) ])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSet with id "does-not-exist" not found'
) )
}) })
}) })
+103 -13
View File
@@ -2,6 +2,7 @@ import {
AddPricesDTO, AddPricesDTO,
Context, Context,
CreatePriceListRuleDTO, CreatePriceListRuleDTO,
CreatePriceSetDTO,
CreatePricesDTO, CreatePricesDTO,
DAL, DAL,
InternalModuleDeclaration, InternalModuleDeclaration,
@@ -13,6 +14,7 @@ import {
PricingRepositoryService, PricingRepositoryService,
PricingTypes, PricingTypes,
RuleTypeDTO, RuleTypeDTO,
UpsertPriceSetDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { import {
arrayDifference, arrayDifference,
@@ -22,6 +24,7 @@ import {
InjectManager, InjectManager,
InjectTransactionManager, InjectTransactionManager,
isDefined, isDefined,
isString,
MedusaContext, MedusaContext,
MedusaError, MedusaError,
ModulesSdkUtils, ModulesSdkUtils,
@@ -46,6 +49,7 @@ import { validatePriceListDates } from "@utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config" import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { PriceSetIdPrefix } from "../models/price-set" import { PriceSetIdPrefix } from "../models/price-set"
import { PriceListIdPrefix } from "../models/price-list" import { PriceListIdPrefix } from "../models/price-list"
import { UpdatePriceSetInput } from "src/types/services"
type InjectedDependencies = { type InjectedDependencies = {
baseRepository: DAL.RepositoryService baseRepository: DAL.RepositoryService
@@ -235,6 +239,7 @@ export default class PricingModuleService<
const input = Array.isArray(data) ? data : [data] const input = Array.isArray(data) ? data : [data]
const priceSets = await this.create_(input, sharedContext) const priceSets = await this.create_(input, sharedContext)
// TODO: Remove the need to refetch the data here
const dbPriceSets = await this.list( const dbPriceSets = await this.list(
{ id: priceSets.map((p) => p.id) }, { id: priceSets.map((p) => p.id) },
{ relations: ["rule_types", "prices", "price_rules"] }, { relations: ["rule_types", "prices", "price_rules"] },
@@ -246,7 +251,104 @@ export default class PricingModuleService<
return dbPriceSets.find((p) => p.id === priceSet.id)! return dbPriceSets.find((p) => p.id === priceSet.id)!
}) })
return Array.isArray(data) ? results : results[0] return await this.baseRepository_.serialize<PriceSetDTO[] | PriceSetDTO>(
Array.isArray(data) ? results : results[0]
)
}
async upsert(
data: UpsertPriceSetDTO[],
sharedContext?: Context
): Promise<PriceSetDTO[]>
async upsert(
data: UpsertPriceSetDTO,
sharedContext?: Context
): Promise<PriceSetDTO>
@InjectManager("baseRepository_")
async upsert(
data: UpsertPriceSetDTO | UpsertPriceSetDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PriceSetDTO | PriceSetDTO[]> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(priceSet): priceSet is UpdatePriceSetInput => !!priceSet.id
)
const forCreate = input.filter(
(priceSet): priceSet is CreatePriceSetDTO => !priceSet.id
)
const operations: Promise<PriceSet[]>[] = []
if (forCreate.length) {
operations.push(this.create_(forCreate, sharedContext))
}
if (forUpdate.length) {
operations.push(this.update_(forUpdate, sharedContext))
}
const result = (await promiseAll(operations)).flat()
return await this.baseRepository_.serialize<PriceSetDTO[] | PriceSetDTO>(
Array.isArray(data) ? result : result[0]
)
}
async update(
id: string,
data: PricingTypes.UpdatePriceSetDTO,
sharedContext?: Context
): Promise<PriceSetDTO>
async update(
selector: PricingTypes.FilterablePriceSetProps,
data: PricingTypes.UpdatePriceSetDTO,
sharedContext?: Context
): Promise<PriceSetDTO[]>
@InjectManager("baseRepository_")
async update(
idOrSelector: string | PricingTypes.FilterablePriceSetProps,
data: PricingTypes.UpdatePriceSetDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<PriceSetDTO | PriceSetDTO[]> {
let normalizedInput: UpdatePriceSetInput[] = []
if (isString(idOrSelector)) {
// Check if the ID exists, it will throw if not.
await this.priceSetService_.retrieve(idOrSelector, {}, sharedContext)
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const priceSets = await this.priceSetService_.list(
idOrSelector,
{},
sharedContext
)
normalizedInput = priceSets.map((priceSet) => ({
id: priceSet.id,
...data,
}))
}
const updateResult = await this.update_(normalizedInput, sharedContext)
const priceSets = await this.baseRepository_.serialize<
PriceSetDTO[] | PriceSetDTO
>(updateResult)
return isString(idOrSelector) ? priceSets[0] : priceSets
}
@InjectTransactionManager("baseRepository_")
protected async update_(
data: UpdatePriceSetInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<PriceSet[]> {
// TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
return this.priceSetService_.upsertWithReplace(
data,
{ relations: ["prices"] },
sharedContext
)
} }
async addRules( async addRules(
@@ -356,18 +458,6 @@ export default class PricingModuleService<
) )
} }
@InjectTransactionManager("baseRepository_")
async update(
data: PricingTypes.UpdatePriceSetDTO[],
@MedusaContext() sharedContext: Context = {}
) {
const priceSets = await this.priceSetService_.update(data, sharedContext)
return await this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
priceSets
)
}
@InjectManager("baseRepository_") @InjectManager("baseRepository_")
async createPriceLists( async createPriceLists(
data: PricingTypes.CreatePriceListDTO[], data: PricingTypes.CreatePriceListDTO[],
@@ -1 +1,2 @@
export * from "./price-list" export * from "./price-list"
export * from "./price-set"
@@ -0,0 +1,5 @@
import { UpdatePriceSetDTO } from "@medusajs/types"
export interface UpdatePriceSetInput extends UpdatePriceSetDTO {
id: string
}
+24 -3
View File
@@ -55,7 +55,7 @@ export interface PriceSetDTO {
/** /**
* The prices that belong to this price set. * The prices that belong to this price set.
*/ */
money_amounts?: MoneyAmountDTO[] prices?: MoneyAmountDTO[]
/** /**
* The rule types applied on this price set. * The rule types applied on this price set.
*/ */
@@ -279,6 +279,18 @@ export interface CreatePriceSetDTO {
prices?: CreatePricesDTO[] prices?: CreatePricesDTO[]
} }
/**
* @interface
*
* The data to upsert in a price set. The `id` is used in the case we are doing an update.
*/
export interface UpsertPriceSetDTO extends UpdatePriceSetDTO {
/**
* A string indicating the ID of the price set to update.
*/
id?: string
}
/** /**
* @interface * @interface
* *
@@ -286,9 +298,18 @@ export interface CreatePriceSetDTO {
*/ */
export interface UpdatePriceSetDTO { export interface UpdatePriceSetDTO {
/** /**
* A string indicating the ID of the price set to update. * The rules to associate with the price set.
*/ */
id: string rules?: {
/**
* the value of the rule's `rule_attribute` attribute.
*/
rule_attribute: string
}[]
/**
* The prices to create and add to this price set.
*/
prices?: CreatePricesDTO[]
} }
/** /**
+108 -8
View File
@@ -31,6 +31,7 @@ import {
UpdatePriceRuleDTO, UpdatePriceRuleDTO,
UpdatePriceSetDTO, UpdatePriceSetDTO,
UpdateRuleTypeDTO, UpdateRuleTypeDTO,
UpsertPriceSetDTO,
} from "./common" } from "./common"
import { FindConfig } from "../common" import { FindConfig } from "../common"
@@ -602,18 +603,117 @@ export interface IPricingModuleService extends IModuleService {
): Promise<PriceSetDTO[]> ): Promise<PriceSetDTO[]>
/** /**
* @ignore * This method updates existing price sets, or creates new ones if they don't exist.
* @privateRemarks
* The update method shouldn't be documented at the moment
* *
* This method is used to update existing price sets. * @param {UpsertPriceSetDTO[]} data - The attributes to update or create for each price set.
*
* @param {UpdatePriceSetDTO[]} data - The price sets to update, each having the attributes that should be updated in a price set.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceSetDTO[]>} The list of updated price sets. * @returns {Promise<PriceSetDTO[]>} The updated and created price sets.
*
* @example
* import {
* initialize as initializePricingModule,
* } from "@medusajs/pricing"
*
* async function upsertPriceSet (title: string) {
* const pricingModule = await initializePricingModule()
*
* const createdPriceSets = await pricingModule.upsert([
* {
* prices: [{amount: 100, currency_code: "USD"}]
* }
* ])
*
* // do something with the price sets or return them
* }
*/
upsert(
data: UpsertPriceSetDTO[],
sharedContext?: Context
): Promise<PriceSetDTO[]>
/**
* This method updates the price set if it exists, or creates a new ones if it doesn't.
*
* @param {UpsertPriceSetDTO} data - The attributes to update or create for the new price set.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceSetDTO>} The updated or created price set.
*
* @example
* import {
* initialize as initializePricingModule,
* } from "@medusajs/pricing"
*
* async function upsertPriceSet (title: string) {
* const pricingModule = await initializePricingModule()
*
* const createdPriceSet = await pricingModule.upsert(
* {
* prices: [{amount: 100, currency_code: "USD"}]
* }
* )
*
* // do something with the price set or return it
* }
*/
upsert(data: UpsertPriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>
/**
* This method is used to update a price set.
*
* @param {string} id - The ID of the price set to be updated.
* @param {UpdatePriceSetDTO} data - The attributes of the price set to be updated
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceSetDTO>} The updated price set.
*
* @example
* import {
* initialize as initializePricingModule,
* } from "@medusajs/pricing"
*
* async function updatePriceSet (id: string, title: string) {
* const pricingtModule = await initializePricingModule()
*
* const priceSet = await pricingtModule.update(id, {
* prices: [{amount: 100, currency_code: "USD"}]
* }
* )
*
* // do something with the price set or return it
* }
*/ */
update( update(
data: UpdatePriceSetDTO[], id: string,
data: UpdatePriceSetDTO,
sharedContext?: Context
): Promise<PriceSetDTO>
/**
* This method is used to update a list of price sets determined by the selector filters.
*
* @param {FilterablePriceSetProps} selector - The filters that will determine which price sets will be updated.
* @param {UpdatePriceSetDTO} data - The attributes to be updated on the selected price sets
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceSetDTO[]>} The updated price sets.
*
* @example
* import {
* initialize as initializePricingModule,
* } from "@medusajs/pricing"
*
* async function updatePriceSet(id: string, title: string) {
* const pricingModule = await initializePricingModule()
*
* const priceSets = await pricingModule.update({id}, {
* prices: [{amount: 100, currency_code: "USD"}]
* }
* )
*
* // do something with the price sets or return them
* }
*/
update(
selector: FilterablePriceSetProps,
data: UpdatePriceSetDTO,
sharedContext?: Context sharedContext?: Context
): Promise<PriceSetDTO[]> ): Promise<PriceSetDTO[]>