chore(): Module Internal Events (#13296)
* chore(): Ensure the product module emits all necessary events * chore(): Ensure the product module emits all necessary events * Update events tests * more events and fixes * more tests and category fixes * more tests and category fixes * Add todo * update updateProduct_ event emitting and adjust test * Adjust update products implementation to rely on already computed events * rm unnecessary update variants events * Fix formatting in changeset for product events * refactor: Manage event emitting automatically (WIP) * refactor: Manage event emitting automatically (WIP) * chore(api-key): Add missing emit events and refactoring * chore(cart): Add missing emit events and refactoring * chore(customer): Add missing emit events and refactoring * chore(fufillment, utils): Add missing emit events and refactoring * chore(fufillment, utils): Add missing emit events and refactoring * chore(inventory): Add missing emit events and refactoring * chore(notification): Add missing emit events and refactoring * chore(utils): Remove medusa service event handling legacy * chore(product): Add missing emit events and refactoring * chore(order): Add missing emit events and refactoring * chore(payment): Add missing emit events and refactoring * chore(pricing, util): Add missing emit events and refactoring, fix internal service upsertWithReplace event dispatching * chore(promotions): Add missing emit events and refactoring * chore(region): Add missing emit events and refactoring * chore(sales-channel): Add missing emit events and refactoring * chore(settings): Add missing emit events and refactoring * chore(stock-location): Add missing emit events and refactoring * chore(store): Add missing emit events and refactoring * chore(taxes): Add missing emit events and refactoring * chore(user): Add missing emit events and refactoring * fix unit tests * rm changeset for regeneration * Create changeset for Medusa.js patch updates Add a changeset for patch updates to multiple Medusa.js modules. * rm unused product event builders * address feedback * remove old changeset * fix event action for token generated * fix user module events * fix import * fix promotion events * add new module integration tests shard * fix medusa service * revert shard * fix event action * fix pipeline * fix pipeline --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
afe21741c4
commit
e8822f3e69
@@ -461,35 +461,41 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
)
|
||||
|
||||
const events = eventBusEmitSpy.mock.calls[0][0]
|
||||
expect(events).toHaveLength(3)
|
||||
expect(events[0]).toEqual(
|
||||
composeMessage(PricingEvents.PRICE_LIST_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list",
|
||||
data: { id: priceList.id },
|
||||
})
|
||||
)
|
||||
expect(events[1]).toEqual(
|
||||
composeMessage(PricingEvents.PRICE_LIST_RULE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list_rule",
|
||||
data: {
|
||||
id: [
|
||||
priceList.price_list_rules?.[0].id,
|
||||
priceList.price_list_rules?.[1].id,
|
||||
],
|
||||
},
|
||||
})
|
||||
)
|
||||
expect(events[2]).toEqual(
|
||||
composeMessage(PricingEvents.PRICE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price",
|
||||
data: { id: priceList.prices![0].id },
|
||||
})
|
||||
|
||||
// 4 events: 1 price list created, 2 price list rules created, 1 price created
|
||||
expect(events).toHaveLength(4)
|
||||
|
||||
expect(events).toEqual(
|
||||
expect.arrayContaining([
|
||||
composeMessage(PricingEvents.PRICE_LIST_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list",
|
||||
data: { id: priceList.id },
|
||||
}),
|
||||
composeMessage(PricingEvents.PRICE_LIST_RULE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list_rule",
|
||||
data: {
|
||||
id: priceList.price_list_rules?.[1].id,
|
||||
},
|
||||
}),
|
||||
composeMessage(PricingEvents.PRICE_LIST_RULE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list_rule",
|
||||
data: {
|
||||
id: priceList.price_list_rules?.[0].id,
|
||||
},
|
||||
}),
|
||||
composeMessage(PricingEvents.PRICE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price",
|
||||
data: { id: priceList.prices![0].id },
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
@@ -789,7 +795,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
it("should update a price to a priceList successfully", async () => {
|
||||
const [priceSet] = await service.createPriceSets([{}])
|
||||
|
||||
await service.addPriceListPrices([
|
||||
const [priceBeforeUpdate] = await service.addPriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
prices: [
|
||||
@@ -806,7 +812,9 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
},
|
||||
])
|
||||
|
||||
await service.updatePriceListPrices([
|
||||
eventBusEmitSpy.mockClear()
|
||||
|
||||
const [price] = await service.updatePriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
prices: [
|
||||
@@ -822,28 +830,37 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
},
|
||||
])
|
||||
|
||||
const events = eventBusEmitSpy.mock.calls[2][0]
|
||||
const events = eventBusEmitSpy.mock.calls[0][0]
|
||||
|
||||
// 4 events: 2 price rules created, 1 price updated, 1 price rule updated, 1 price rule deleted
|
||||
expect(events).toHaveLength(4)
|
||||
expect(events).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "pricing.price-rule.created",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "pricing.price-rule.created",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "pricing.price.updated",
|
||||
metadata: {
|
||||
source: "pricing",
|
||||
expect.objectContaining(
|
||||
composeMessage(PricingEvents.PRICE_UPDATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.UPDATED,
|
||||
object: "price",
|
||||
action: "updated",
|
||||
},
|
||||
data: {
|
||||
id: "test-price-id",
|
||||
},
|
||||
data: { id: price.id },
|
||||
})
|
||||
),
|
||||
composeMessage(PricingEvents.PRICE_RULE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_rule",
|
||||
data: { id: price.price_rules![0].id },
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "pricing.price-rule.deleted",
|
||||
composeMessage(PricingEvents.PRICE_RULE_CREATED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_rule",
|
||||
data: { id: price.price_rules![1].id },
|
||||
}),
|
||||
composeMessage(PricingEvents.PRICE_RULE_DELETED, {
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.DELETED,
|
||||
object: "price_rule",
|
||||
data: { id: priceBeforeUpdate.price_rules![0].id },
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
@@ -41,7 +41,6 @@ import {
|
||||
promiseAll,
|
||||
removeNullish,
|
||||
simpleHash,
|
||||
upperCaseFirst,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
import {
|
||||
@@ -55,7 +54,7 @@ import {
|
||||
|
||||
import { Collection } from "@mikro-orm/core"
|
||||
import { ServiceTypes } from "@types"
|
||||
import { eventBuilders, validatePriceListDates } from "@utils"
|
||||
import { validatePriceListDates } from "@utils"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -169,6 +168,8 @@ export default class PricingModuleService
|
||||
return pricingContext
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@EmitEvents()
|
||||
// @ts-expect-error
|
||||
async createPriceRules(
|
||||
...args: Parameters<PricingTypes.IPricingModuleService["createPriceRules"]>
|
||||
@@ -180,6 +181,8 @@ export default class PricingModuleService
|
||||
}
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@EmitEvents()
|
||||
// @ts-expect-error
|
||||
async updatePriceRules(
|
||||
...args: Parameters<PricingTypes.IPricingModuleService["updatePriceRules"]>
|
||||
@@ -191,6 +194,8 @@ export default class PricingModuleService
|
||||
}
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@EmitEvents()
|
||||
// @ts-expect-error
|
||||
async createPriceListRules(
|
||||
...args: any[]
|
||||
@@ -203,6 +208,8 @@ export default class PricingModuleService
|
||||
}
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@EmitEvents()
|
||||
// @ts-expect-error
|
||||
async updatePriceListRules(
|
||||
...args: any[]
|
||||
@@ -269,7 +276,7 @@ export default class PricingModuleService
|
||||
priceSet.calculated_price = calculatedPrice ?? null
|
||||
}
|
||||
|
||||
return priceSets
|
||||
return await this.baseRepository_.serialize<PriceSetDTO[]>(priceSets)
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
@@ -291,7 +298,10 @@ export default class PricingModuleService
|
||||
sharedContext
|
||||
)
|
||||
if (!pricingContext || !priceSets.length) {
|
||||
return [priceSets, count]
|
||||
const serializedPriceSets = await this.baseRepository_.serialize<
|
||||
PriceSetDTO[]
|
||||
>(priceSets)
|
||||
return [serializedPriceSets, count]
|
||||
}
|
||||
|
||||
const calculatedPrices = await this.calculatePrices(
|
||||
@@ -310,7 +320,11 @@ export default class PricingModuleService
|
||||
priceSet.calculated_price = calculatedPrice ?? null
|
||||
}
|
||||
|
||||
return [priceSets, count]
|
||||
const serializedPriceSets = await this.baseRepository_.serialize<
|
||||
PriceSetDTO[]
|
||||
>(priceSets)
|
||||
|
||||
return [serializedPriceSets, count]
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
@@ -405,73 +419,73 @@ export default class PricingModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedPrices: PricingTypes.CalculatedPriceSet[] =
|
||||
pricingFilters.id
|
||||
.map((priceSetId: string): PricingTypes.CalculatedPriceSet | null => {
|
||||
const prices = pricesSetPricesMap.get(priceSetId)
|
||||
if (!prices) {
|
||||
return null
|
||||
}
|
||||
const {
|
||||
calculatedPrice,
|
||||
originalPrice,
|
||||
}: {
|
||||
calculatedPrice: PricingTypes.CalculatedPriceSetDTO
|
||||
originalPrice: PricingTypes.CalculatedPriceSetDTO | undefined
|
||||
} = prices
|
||||
const calculatedPrices: PricingTypes.CalculatedPriceSet[] = []
|
||||
|
||||
return {
|
||||
id: priceSetId,
|
||||
is_calculated_price_price_list: !!calculatedPrice?.price_list_id,
|
||||
is_calculated_price_tax_inclusive: isTaxInclusive(
|
||||
priceRulesPriceMap.get(calculatedPrice.id),
|
||||
for (const priceSetId of pricingFilters.id) {
|
||||
const prices = pricesSetPricesMap.get(priceSetId)
|
||||
if (!prices) {
|
||||
continue
|
||||
}
|
||||
|
||||
const {
|
||||
calculatedPrice,
|
||||
originalPrice,
|
||||
}: {
|
||||
calculatedPrice: PricingTypes.CalculatedPriceSetDTO
|
||||
originalPrice: PricingTypes.CalculatedPriceSetDTO | undefined
|
||||
} = prices
|
||||
|
||||
const calculatedPrice_: PricingTypes.CalculatedPriceSet = {
|
||||
id: priceSetId,
|
||||
is_calculated_price_price_list: !!calculatedPrice?.price_list_id,
|
||||
is_calculated_price_tax_inclusive: isTaxInclusive(
|
||||
priceRulesPriceMap.get(calculatedPrice.id),
|
||||
pricingPreferences,
|
||||
calculatedPrice.currency_code!,
|
||||
pricingContext.context?.region_id as string
|
||||
),
|
||||
calculated_amount: isPresent(calculatedPrice?.amount)
|
||||
? parseFloat(calculatedPrice?.amount as string)
|
||||
: null,
|
||||
raw_calculated_amount: calculatedPrice?.raw_amount || null,
|
||||
|
||||
is_original_price_price_list: !!originalPrice?.price_list_id,
|
||||
is_original_price_tax_inclusive: originalPrice?.id
|
||||
? isTaxInclusive(
|
||||
priceRulesPriceMap.get(originalPrice.id),
|
||||
pricingPreferences,
|
||||
calculatedPrice.currency_code!,
|
||||
originalPrice.currency_code || calculatedPrice.currency_code!,
|
||||
pricingContext.context?.region_id as string
|
||||
),
|
||||
calculated_amount: isPresent(calculatedPrice?.amount)
|
||||
? parseFloat(calculatedPrice?.amount as string)
|
||||
: null,
|
||||
raw_calculated_amount: calculatedPrice?.raw_amount || null,
|
||||
)
|
||||
: false,
|
||||
original_amount: isPresent(originalPrice?.amount)
|
||||
? parseFloat(originalPrice?.amount as string)
|
||||
: null,
|
||||
raw_original_amount: originalPrice?.raw_amount || null,
|
||||
|
||||
is_original_price_price_list: !!originalPrice?.price_list_id,
|
||||
is_original_price_tax_inclusive: originalPrice?.id
|
||||
? isTaxInclusive(
|
||||
priceRulesPriceMap.get(originalPrice.id),
|
||||
pricingPreferences,
|
||||
originalPrice.currency_code || calculatedPrice.currency_code!,
|
||||
pricingContext.context?.region_id as string
|
||||
)
|
||||
: false,
|
||||
original_amount: isPresent(originalPrice?.amount)
|
||||
? parseFloat(originalPrice?.amount as string)
|
||||
: null,
|
||||
raw_original_amount: originalPrice?.raw_amount || null,
|
||||
currency_code: calculatedPrice?.currency_code || null,
|
||||
|
||||
currency_code: calculatedPrice?.currency_code || null,
|
||||
calculated_price: {
|
||||
id: calculatedPrice?.id || null,
|
||||
price_list_id: calculatedPrice?.price_list_id || null,
|
||||
price_list_type: calculatedPrice?.price_list_type || null,
|
||||
min_quantity: parseInt(calculatedPrice?.min_quantity || "") || null,
|
||||
max_quantity: parseInt(calculatedPrice?.max_quantity || "") || null,
|
||||
},
|
||||
|
||||
calculated_price: {
|
||||
id: calculatedPrice?.id || null,
|
||||
price_list_id: calculatedPrice?.price_list_id || null,
|
||||
price_list_type: calculatedPrice?.price_list_type || null,
|
||||
min_quantity:
|
||||
parseInt(calculatedPrice?.min_quantity || "") || null,
|
||||
max_quantity:
|
||||
parseInt(calculatedPrice?.max_quantity || "") || null,
|
||||
},
|
||||
original_price: {
|
||||
id: originalPrice?.id || null,
|
||||
price_list_id: originalPrice?.price_list_id || null,
|
||||
price_list_type: originalPrice?.price_list_type || null,
|
||||
min_quantity: parseInt(originalPrice?.min_quantity || "") || null,
|
||||
max_quantity: parseInt(originalPrice?.max_quantity || "") || null,
|
||||
},
|
||||
}
|
||||
|
||||
original_price: {
|
||||
id: originalPrice?.id || null,
|
||||
price_list_id: originalPrice?.price_list_id || null,
|
||||
price_list_type: originalPrice?.price_list_type || null,
|
||||
min_quantity: parseInt(originalPrice?.min_quantity || "") || null,
|
||||
max_quantity: parseInt(originalPrice?.max_quantity || "") || null,
|
||||
},
|
||||
}
|
||||
})
|
||||
.filter(Boolean) as PricingTypes.CalculatedPriceSet[]
|
||||
calculatedPrices.push(calculatedPrice_)
|
||||
}
|
||||
|
||||
return JSON.parse(JSON.stringify(calculatedPrices))
|
||||
return calculatedPrices
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -534,6 +548,22 @@ export default class PricingModuleService
|
||||
data: UpsertPriceSetDTO | UpsertPriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PriceSetDTO | PriceSetDTO[]> {
|
||||
const result = await this.upsertPriceSets_(data, sharedContext)
|
||||
|
||||
try {
|
||||
return await this.baseRepository_.serialize<PriceSetDTO[] | PriceSetDTO>(
|
||||
Array.isArray(data) ? result : result[0]
|
||||
)
|
||||
} finally {
|
||||
this.pricingRepository_.clearAvailableAttributes?.()
|
||||
}
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
protected async upsertPriceSets_(
|
||||
data: UpsertPriceSetDTO | UpsertPriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<InferEntityType<typeof PriceSet>[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const forUpdate = input.filter(
|
||||
@@ -554,13 +584,7 @@ export default class PricingModuleService
|
||||
|
||||
const result = (await promiseAll(operations)).flat()
|
||||
|
||||
try {
|
||||
return await this.baseRepository_.serialize<PriceSetDTO[] | PriceSetDTO>(
|
||||
Array.isArray(data) ? result : result[0]
|
||||
)
|
||||
} finally {
|
||||
this.pricingRepository_.clearAvailableAttributes?.()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -606,12 +630,13 @@ export default class PricingModuleService
|
||||
normalizedInput,
|
||||
sharedContext
|
||||
)
|
||||
const priceSets = await this.baseRepository_.serialize<
|
||||
|
||||
const serializedUpdateResult = await this.baseRepository_.serialize<
|
||||
PriceSetDTO[] | PriceSetDTO
|
||||
>(updateResult)
|
||||
>(isString(idOrSelector) ? updateResult[0] : updateResult)
|
||||
|
||||
try {
|
||||
return isString(idOrSelector) ? priceSets[0] : priceSets
|
||||
return serializedUpdateResult
|
||||
} finally {
|
||||
this.pricingRepository_.clearAvailableAttributes?.()
|
||||
}
|
||||
@@ -707,10 +732,6 @@ export default class PricingModuleService
|
||||
})),
|
||||
sharedContext
|
||||
)
|
||||
eventBuilders.createdPriceRule({
|
||||
data: createdPriceRules.map((r) => ({ id: r.id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
if (priceRulesToUpdate.length > 0) {
|
||||
@@ -718,10 +739,6 @@ export default class PricingModuleService
|
||||
priceRulesToUpdate,
|
||||
sharedContext
|
||||
)
|
||||
eventBuilders.updatedPriceRule({
|
||||
data: updatedPriceRules.map((r) => ({ id: r.id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
if (priceRulesToDelete.length > 0) {
|
||||
@@ -729,10 +746,6 @@ export default class PricingModuleService
|
||||
priceRulesToDelete,
|
||||
sharedContext
|
||||
)
|
||||
eventBuilders.deletedPriceRule({
|
||||
data: priceRulesToDelete.map((id) => ({ id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
const upsertedPriceRules = [
|
||||
@@ -755,10 +768,6 @@ export default class PricingModuleService
|
||||
priceRuleToDelete,
|
||||
sharedContext
|
||||
)
|
||||
eventBuilders.deletedPriceRule({
|
||||
data: priceRuleToDelete.map((id) => ({ id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
;(priceToUpdate as InferEntityType<typeof Price>).rules_count = 0
|
||||
@@ -780,27 +789,6 @@ export default class PricingModuleService
|
||||
await this.priceService_.delete(pricesToDelete, sharedContext)
|
||||
}
|
||||
|
||||
if (createdPrices.length > 0) {
|
||||
eventBuilders.createdPrice({
|
||||
data: createdPrices.map((p) => ({ id: p.id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
if (updatedPrices.length > 0) {
|
||||
eventBuilders.updatedPrice({
|
||||
data: updatedPrices.map((p) => ({ id: p.id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
if (pricesToDelete.length > 0) {
|
||||
eventBuilders.deletedPrice({
|
||||
data: pricesToDelete.map((id) => ({ id })),
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
|
||||
const priceSets = await this.priceSetService_.list(
|
||||
{ id: normalizedData.map(({ id }) => id) },
|
||||
{
|
||||
@@ -825,11 +813,6 @@ export default class PricingModuleService
|
||||
ps.prices = upsertedPricesMap.get(ps.id) || []
|
||||
})
|
||||
|
||||
eventBuilders.updatedPriceSet({
|
||||
data: priceSets.map((ps) => ({ id: ps.id })),
|
||||
sharedContext,
|
||||
})
|
||||
|
||||
return priceSets
|
||||
}
|
||||
|
||||
@@ -952,8 +935,12 @@ export default class PricingModuleService
|
||||
return dbPrices.find((p) => p.id === inputItem.priceSetId)!
|
||||
})
|
||||
|
||||
const serializedOrderedPriceSets = await this.baseRepository_.serialize<
|
||||
PricingTypes.PriceSetDTO[]
|
||||
>(Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0])
|
||||
|
||||
try {
|
||||
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0]
|
||||
return serializedOrderedPriceSets
|
||||
} finally {
|
||||
this.pricingRepository_.clearAvailableAttributes?.()
|
||||
}
|
||||
@@ -977,7 +964,7 @@ export default class PricingModuleService
|
||||
}
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@InjectManager()
|
||||
@EmitEvents()
|
||||
// @ts-ignore
|
||||
async updatePriceLists(
|
||||
@@ -1117,6 +1104,18 @@ export default class PricingModuleService
|
||||
data: UpsertPricePreferenceDTO | UpsertPricePreferenceDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricePreferenceDTO | PricePreferenceDTO[]> {
|
||||
const result = await this.upsertPricePreferences_(data, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
PricePreferenceDTO[] | PricePreferenceDTO
|
||||
>(Array.isArray(data) ? result : result[0])
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
protected async upsertPricePreferences_(
|
||||
data: UpsertPricePreferenceDTO | UpsertPricePreferenceDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<InferEntityType<typeof PricePreference>[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const forUpdate = input.filter(
|
||||
(
|
||||
@@ -1139,9 +1138,8 @@ export default class PricingModuleService
|
||||
}
|
||||
|
||||
const result = (await promiseAll(operations)).flat()
|
||||
return await this.baseRepository_.serialize<
|
||||
PricePreferenceDTO[] | PricePreferenceDTO
|
||||
>(Array.isArray(data) ? result : result[0])
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
@@ -1158,6 +1156,7 @@ export default class PricingModuleService
|
||||
): Promise<PricePreferenceDTO[]>
|
||||
|
||||
@InjectManager()
|
||||
@EmitEvents()
|
||||
// @ts-expect-error
|
||||
async updatePricePreferences(
|
||||
idOrSelector: string | PricingTypes.FilterablePricePreferenceProps,
|
||||
@@ -1248,49 +1247,6 @@ export default class PricingModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const eventsData = priceSets.reduce(
|
||||
(eventsData, priceSet) => {
|
||||
eventsData.priceSets.push({
|
||||
id: priceSet.id,
|
||||
})
|
||||
|
||||
priceSet.prices.map((price) => {
|
||||
eventsData.prices.push({
|
||||
id: price.id,
|
||||
})
|
||||
price.price_rules.map((priceRule) => {
|
||||
eventsData.priceRules.push({
|
||||
id: priceRule.id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return eventsData
|
||||
},
|
||||
{
|
||||
priceSets: [],
|
||||
priceRules: [],
|
||||
prices: [],
|
||||
} as {
|
||||
priceSets: { id: string }[]
|
||||
priceRules: { id: string }[]
|
||||
prices: { id: string }[]
|
||||
}
|
||||
)
|
||||
|
||||
eventBuilders.createdPriceSet({
|
||||
data: eventsData.priceSets,
|
||||
sharedContext,
|
||||
})
|
||||
eventBuilders.createdPrice({
|
||||
data: eventsData.prices,
|
||||
sharedContext,
|
||||
})
|
||||
eventBuilders.createdPriceRule({
|
||||
data: eventsData.priceRules,
|
||||
sharedContext,
|
||||
})
|
||||
|
||||
return priceSets
|
||||
}
|
||||
|
||||
@@ -1336,18 +1292,11 @@ export default class PricingModuleService
|
||||
}
|
||||
})
|
||||
|
||||
const { entities, performedActions } =
|
||||
await this.priceService_.upsertWithReplace(
|
||||
pricesToUpsert,
|
||||
{ relations: ["price_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
composeAllEvents({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
})
|
||||
const { entities } = await this.priceService_.upsertWithReplace(
|
||||
pricesToUpsert,
|
||||
{ relations: ["price_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return entities
|
||||
}
|
||||
@@ -1401,67 +1350,6 @@ export default class PricingModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
/**
|
||||
* Preparing data for emitting events
|
||||
*/
|
||||
const eventsData = priceLists.reduce(
|
||||
(eventsData, priceList) => {
|
||||
eventsData.priceList.push({
|
||||
id: priceList.id,
|
||||
})
|
||||
|
||||
priceList.price_list_rules.map((listRule) => {
|
||||
eventsData.priceListRules.push({
|
||||
id: listRule.id,
|
||||
})
|
||||
})
|
||||
|
||||
priceList.prices.map((price) => {
|
||||
eventsData.prices.push({
|
||||
id: price.id,
|
||||
})
|
||||
price.price_rules.map((priceRule) => {
|
||||
eventsData.priceRules.push({
|
||||
id: priceRule.id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return eventsData
|
||||
},
|
||||
{
|
||||
priceList: [],
|
||||
priceListRules: [],
|
||||
priceRules: [],
|
||||
prices: [],
|
||||
} as {
|
||||
priceList: { id: string }[]
|
||||
priceListRules: { id: string }[]
|
||||
priceRules: { id: string }[]
|
||||
prices: { id: string }[]
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* Emitting events for all created entities
|
||||
*/
|
||||
eventBuilders.createdPriceList({
|
||||
data: eventsData.priceList,
|
||||
sharedContext,
|
||||
})
|
||||
eventBuilders.createdPriceListRule({
|
||||
data: eventsData.priceListRules,
|
||||
sharedContext,
|
||||
})
|
||||
eventBuilders.createdPrice({
|
||||
data: eventsData.prices,
|
||||
sharedContext,
|
||||
})
|
||||
eventBuilders.createdPriceRule({
|
||||
data: eventsData.priceRules,
|
||||
sharedContext,
|
||||
})
|
||||
|
||||
return priceLists
|
||||
}
|
||||
|
||||
@@ -1519,16 +1407,12 @@ export default class PricingModuleService
|
||||
}
|
||||
)
|
||||
|
||||
const { entities, performedActions } =
|
||||
await this.priceListService_.upsertWithReplace(normalizedData, {
|
||||
const { entities } = await this.priceListService_.upsertWithReplace(
|
||||
normalizedData,
|
||||
{
|
||||
relations: ["price_list_rules"],
|
||||
})
|
||||
|
||||
composeAllEvents({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
return entities
|
||||
}
|
||||
@@ -1572,18 +1456,11 @@ export default class PricingModuleService
|
||||
}
|
||||
}
|
||||
|
||||
const { entities, performedActions } =
|
||||
await this.priceService_.upsertWithReplace(
|
||||
pricesToUpsert,
|
||||
{ relations: ["price_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
composeAllEvents({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
})
|
||||
const { entities } = await this.priceService_.upsertWithReplace(
|
||||
pricesToUpsert,
|
||||
{ relations: ["price_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return entities
|
||||
}
|
||||
@@ -1634,18 +1511,11 @@ export default class PricingModuleService
|
||||
}
|
||||
})
|
||||
|
||||
const { entities, performedActions } =
|
||||
await this.priceService_.upsertWithReplace(
|
||||
pricesToUpsert,
|
||||
{ relations: ["price_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
composeAllEvents({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
})
|
||||
const { entities } = await this.priceService_.upsertWithReplace(
|
||||
pricesToUpsert,
|
||||
{ relations: ["price_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return entities
|
||||
}
|
||||
@@ -1705,18 +1575,11 @@ export default class PricingModuleService
|
||||
})
|
||||
.filter(Boolean)
|
||||
|
||||
const { entities, performedActions } =
|
||||
await this.priceListService_.upsertWithReplace(
|
||||
priceListsUpsert,
|
||||
{ relations: ["price_list_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
composeAllEvents({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
})
|
||||
const { entities } = await this.priceListService_.upsertWithReplace(
|
||||
priceListsUpsert,
|
||||
{ relations: ["price_list_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return entities
|
||||
}
|
||||
@@ -1779,18 +1642,11 @@ export default class PricingModuleService
|
||||
})
|
||||
.filter(Boolean)
|
||||
|
||||
const { entities, performedActions } =
|
||||
await this.priceListService_.upsertWithReplace(
|
||||
priceListsUpsert,
|
||||
{ relations: ["price_list_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
composeAllEvents({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
})
|
||||
const { entities } = await this.priceListService_.upsertWithReplace(
|
||||
priceListsUpsert,
|
||||
{ relations: ["price_list_rules"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return entities
|
||||
}
|
||||
@@ -1829,26 +1685,6 @@ export default class PricingModuleService
|
||||
}
|
||||
}
|
||||
|
||||
const composeAllEvents = ({
|
||||
eventBuilders,
|
||||
performedActions,
|
||||
sharedContext,
|
||||
}) => {
|
||||
for (const action of Object.keys(performedActions)) {
|
||||
for (const entity of Object.keys(performedActions[action])) {
|
||||
const eventName = action + upperCaseFirst(entity)
|
||||
if (!eventBuilders[eventName]) {
|
||||
continue
|
||||
}
|
||||
|
||||
eventBuilders[eventName]({
|
||||
data: performedActions[action][entity] ?? [],
|
||||
sharedContext,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const isTaxInclusive = (
|
||||
priceRules: InferEntityType<typeof PriceRule>[],
|
||||
preferences: InferEntityType<typeof PricePreference>[],
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
import {
|
||||
CommonEvents,
|
||||
moduleEventBuilderFactory,
|
||||
Modules,
|
||||
PricingEvents,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
export const eventBuilders = {
|
||||
createdPriceSet: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_set",
|
||||
eventName: PricingEvents.PRICE_SET_CREATED,
|
||||
}),
|
||||
createdPrice: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price",
|
||||
eventName: PricingEvents.PRICE_CREATED,
|
||||
}),
|
||||
createdPriceRule: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_rule",
|
||||
eventName: PricingEvents.PRICE_RULE_CREATED,
|
||||
}),
|
||||
createdPriceList: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list",
|
||||
eventName: PricingEvents.PRICE_LIST_CREATED,
|
||||
}),
|
||||
createdPriceListRule: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_list_rule",
|
||||
eventName: PricingEvents.PRICE_LIST_RULE_CREATED,
|
||||
}),
|
||||
updatedPriceSet: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.UPDATED,
|
||||
object: "price_set",
|
||||
eventName: PricingEvents.PRICE_SET_UPDATED,
|
||||
}),
|
||||
updatedPrice: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.UPDATED,
|
||||
object: "price",
|
||||
eventName: PricingEvents.PRICE_UPDATED,
|
||||
}),
|
||||
updatedPriceList: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.UPDATED,
|
||||
object: "price_list",
|
||||
eventName: PricingEvents.PRICE_LIST_UPDATED,
|
||||
}),
|
||||
updatedPriceListRule: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.UPDATED,
|
||||
object: "price_list_rule",
|
||||
eventName: PricingEvents.PRICE_LIST_RULE_UPDATED,
|
||||
}),
|
||||
updatedPriceRule: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.UPDATED,
|
||||
object: "price_rule",
|
||||
eventName: PricingEvents.PRICE_RULE_UPDATED,
|
||||
}),
|
||||
deletedPriceSet: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.DELETED,
|
||||
object: "price_set",
|
||||
eventName: PricingEvents.PRICE_SET_DELETED,
|
||||
}),
|
||||
deletedPrice: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.DELETED,
|
||||
object: "price",
|
||||
eventName: PricingEvents.PRICE_DELETED,
|
||||
}),
|
||||
deletedPriceList: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.DELETED,
|
||||
object: "price_list",
|
||||
eventName: PricingEvents.PRICE_LIST_DELETED,
|
||||
}),
|
||||
deletedPriceListRule: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.DELETED,
|
||||
object: "price_list_rule",
|
||||
eventName: PricingEvents.PRICE_LIST_RULE_DELETED,
|
||||
}),
|
||||
deletedPriceRule: moduleEventBuilderFactory({
|
||||
source: Modules.PRICING,
|
||||
action: CommonEvents.DELETED,
|
||||
object: "price_rule",
|
||||
eventName: PricingEvents.PRICE_RULE_DELETED,
|
||||
}),
|
||||
}
|
||||
@@ -1,2 +1 @@
|
||||
export * from "./validate-price-list-dates"
|
||||
export * from "./events"
|
||||
|
||||
Reference in New Issue
Block a user