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

@@ -265,21 +265,64 @@ moduleIntegrationTestRunner({
describe("update", () => {
const id = "price-set-1"
it("should throw an error when a id does not exist", async () => {
let error
it("should throw an error when an id does not exist", async () => {
let error = await service
.update("does-not-exist", {})
.catch((e) => e.message)
try {
await service.update([
{
id: "does-not-exist",
},
expect(error).toEqual(
"PriceSet with id: does-not-exist was not found"
)
})
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,
Context,
CreatePriceListRuleDTO,
CreatePriceSetDTO,
CreatePricesDTO,
DAL,
InternalModuleDeclaration,
@@ -13,6 +14,7 @@ import {
PricingRepositoryService,
PricingTypes,
RuleTypeDTO,
UpsertPriceSetDTO,
} from "@medusajs/types"
import {
arrayDifference,
@@ -22,6 +24,7 @@ import {
InjectManager,
InjectTransactionManager,
isDefined,
isString,
MedusaContext,
MedusaError,
ModulesSdkUtils,
@@ -46,6 +49,7 @@ import { validatePriceListDates } from "@utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { PriceSetIdPrefix } from "../models/price-set"
import { PriceListIdPrefix } from "../models/price-list"
import { UpdatePriceSetInput } from "src/types/services"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -235,6 +239,7 @@ export default class PricingModuleService<
const input = Array.isArray(data) ? data : [data]
const priceSets = await this.create_(input, sharedContext)
// TODO: Remove the need to refetch the data here
const dbPriceSets = await this.list(
{ id: priceSets.map((p) => p.id) },
{ relations: ["rule_types", "prices", "price_rules"] },
@@ -246,7 +251,104 @@ export default class PricingModuleService<
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(
@@ -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_")
async createPriceLists(
data: PricingTypes.CreatePriceListDTO[],
@@ -1 +1,2 @@
export * from "./price-list"
export * from "./price-set"
@@ -0,0 +1,5 @@
import { UpdatePriceSetDTO } from "@medusajs/types"
export interface UpdatePriceSetInput extends UpdatePriceSetDTO {
id: string
}