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:
Adrien de Peretti
2025-09-10 14:37:38 +02:00
committed by GitHub
parent afe21741c4
commit e8822f3e69
55 changed files with 3614 additions and 2353 deletions
@@ -219,7 +219,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "fulfillment_set",
data: {
id: expect.arrayContaining([fulfillmentSets[i].id]),
id: fulfillmentSets[i].id,
},
}),
]),
@@ -338,7 +338,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "fulfillment_set",
data: {
id: expect.arrayContaining([fulfillmentSets[i].id]),
id: fulfillmentSets[i].id,
},
}),
buildExpectedEventMessageShape({
@@ -346,9 +346,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "service_zone",
data: {
id: expect.arrayContaining([
fulfillmentSets[i].service_zones[0].id,
]),
id: fulfillmentSets[i].service_zones[0].id,
},
}),
]),
@@ -514,7 +512,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "fulfillment_set",
data: {
id: expect.arrayContaining([fulfillmentSets[i].id]),
id: fulfillmentSets[i].id,
},
}),
buildExpectedEventMessageShape({
@@ -522,9 +520,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "service_zone",
data: {
id: expect.arrayContaining([
fulfillmentSets[i].service_zones[0].id,
]),
id: fulfillmentSets[i].service_zones[0].id,
},
}),
buildExpectedEventMessageShape({
@@ -532,9 +528,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "geo_zone",
data: {
id: expect.arrayContaining([
fulfillmentSets[i].service_zones[0].geo_zones[0].id,
]),
id: fulfillmentSets[i].service_zones[0].geo_zones[0].id,
},
}),
]),
@@ -724,7 +718,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
})
expect(updatedFulfillmentSets).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(1)
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(2)
for (const data_ of updateData) {
const currentFullfillmentSet = fullfillmentSets.find(
@@ -746,7 +740,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "updated",
object: "fulfillment_set",
data: {
id: expect.arrayContaining([currentFullfillmentSet.id]),
id: currentFullfillmentSet.id,
},
}),
]),
@@ -1075,7 +1069,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
)
expect(updatedFulfillmentSets).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(5)
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(10)
for (const data_ of updateData) {
const expectedFulfillmentSet = updatedFulfillmentSets.find(
@@ -1116,7 +1110,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "updated",
object: "fulfillment_set",
data: {
id: expect.arrayContaining([expectedFulfillmentSet.id]),
id: expectedFulfillmentSet.id,
},
}),
buildExpectedEventMessageShape({
@@ -1124,9 +1118,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "service_zone",
data: {
id: expect.arrayContaining([
expectedFulfillmentSet.service_zones[0].id,
]),
id: expectedFulfillmentSet.service_zones[0].id,
},
}),
buildExpectedEventMessageShape({
@@ -1134,9 +1126,8 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "geo_zone",
data: {
id: expect.arrayContaining([
expectedFulfillmentSet.service_zones[0].geo_zones[0].id,
]),
id: expectedFulfillmentSet.service_zones[0].geo_zones[0]
.id,
},
}),
buildExpectedEventMessageShape({
@@ -1144,9 +1135,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "deleted",
object: "service_zone",
data: {
id: expect.arrayContaining([
originalFulfillmentSet.service_zones[0].id,
]),
id: originalFulfillmentSet.service_zones[0].id,
},
}),
buildExpectedEventMessageShape({
@@ -1154,9 +1143,8 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "deleted",
object: "geo_zone",
data: {
id: expect.arrayContaining([
originalFulfillmentSet.service_zones[0].geo_zones[0].id,
]),
id: originalFulfillmentSet.service_zones[0].geo_zones[0]
.id,
},
}),
]),
@@ -1245,7 +1233,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
)
expect(updatedFulfillmentSets).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(3)
expect(eventBusEmitSpy.mock.calls[1][0]).toHaveLength(6)
for (const data_ of updateData) {
const expectedFulfillmentSet = updatedFulfillmentSets.find(
@@ -1290,7 +1278,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "updated",
object: "fulfillment_set",
data: {
id: expect.arrayContaining([expectedFulfillmentSet.id]),
id: expectedFulfillmentSet.id,
},
}),
buildExpectedEventMessageShape({
@@ -1298,7 +1286,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "service_zone",
data: {
id: expect.arrayContaining([createdServiceZone.id]),
id: createdServiceZone.id,
},
}),
buildExpectedEventMessageShape({
@@ -1306,9 +1294,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "geo_zone",
data: {
id: expect.arrayContaining([
createdServiceZone.geo_zones[0].id,
]),
id: createdServiceZone.geo_zones[0].id,
},
}),
]),
@@ -152,15 +152,21 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
})
)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(5)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
expect.arrayContaining([
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_CREATED,
action: "created",
object: "fulfillment",
data: { id: fulfillment.id },
}),
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_UPDATED,
action: "updated",
object: "fulfillment",
data: { id: fulfillment.id },
}),
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED,
action: "created",
@@ -179,7 +185,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
object: "fulfillment_label",
data: { id: fulfillment.labels[0].id },
}),
],
]),
{
internal: true,
}
@@ -244,15 +250,21 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
})
)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(5)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
expect.arrayContaining([
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_CREATED,
action: "created",
object: "fulfillment",
data: { id: fulfillment.id },
}),
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_UPDATED,
action: "updated",
object: "fulfillment",
data: { id: fulfillment.id },
}),
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED,
action: "created",
@@ -271,7 +283,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
object: "fulfillment_label",
data: { id: fulfillment.labels[0].id },
}),
],
]),
{
internal: true,
}
@@ -383,15 +395,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
})
)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(3)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_UPDATED,
action: "updated",
object: "fulfillment",
data: { id: updatedFulfillment.id },
}),
expect.arrayContaining([
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.FULFILLMENT_LABEL_DELETED,
action: "deleted",
@@ -410,7 +416,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
object: "fulfillment_label",
data: { id: label4.id },
}),
],
]),
{
internal: true,
}
@@ -154,7 +154,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
const geoZones = await service.createGeoZones(data)
expect(geoZones).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
let i = 0
for (const data_ of data) {
@@ -172,7 +172,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
eventName: FulfillmentEvents.GEO_ZONE_CREATED,
action: "created",
object: "geo_zone",
data: { id: expect.arrayContaining([geoZones[i].id]) },
data: { id: geoZones[i].id },
}),
]),
{
@@ -331,7 +331,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
const updatedGeoZones = await service.updateGeoZones(updateData)
expect(updatedGeoZones).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
for (const data_ of updateData) {
const expectedGeoZone = updatedGeoZones.find(
@@ -352,7 +352,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
eventName: FulfillmentEvents.GEO_ZONE_UPDATED,
action: "updated",
object: "geo_zone",
data: { id: expect.arrayContaining([expectedGeoZone.id]) },
data: { id: expectedGeoZone.id },
}),
]),
{
@@ -371,7 +371,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
expect.arrayContaining([
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.GEO_ZONE_DELETED,
action: "deleted",
@@ -396,7 +396,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
object: "geo_zone",
data: { id: usGeoZone.id },
}),
],
]),
{
internal: true,
}
@@ -510,7 +510,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
expect(updatedServiceZones).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(3) // Since the update only calls create and update which are already tested, only check the length
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(6) // Since the update only calls create and update which are already tested, only check the length
for (const data_ of updateData) {
const expectedServiceZone = updatedServiceZones.find(
@@ -522,7 +522,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(3)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
expect.arrayContaining([
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.SHIPPING_OPTION_CREATED,
action: "created",
@@ -541,7 +541,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
object: "shipping_option_rule",
data: { id: createdShippingOption.rules[0].id },
}),
],
]),
{
internal: true,
}
@@ -582,7 +582,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
)
expect(createdShippingOptions).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(3)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(6)
let i = 0
for (const data_ of createData) {
@@ -620,9 +620,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "shipping_option",
data: {
id: expect.arrayContaining([
createdShippingOptions[i].id,
]),
id: createdShippingOptions[i].id,
},
}),
buildExpectedEventMessageShape({
@@ -630,9 +628,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "shipping_option_type",
data: {
id: expect.arrayContaining([
createdShippingOptions[i].type.id,
]),
id: createdShippingOptions[i].type.id,
},
}),
buildExpectedEventMessageShape({
@@ -640,9 +636,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "shipping_option_rule",
data: {
id: expect.arrayContaining([
createdShippingOptions[i].rules[0].id,
]),
id: createdShippingOptions[i].rules[0].id,
},
}),
]),
@@ -823,7 +817,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
])
)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(5)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
expect.arrayContaining([
buildExpectedEventMessageShape({
@@ -832,12 +826,6 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
object: "shipping_option",
data: { id: updatedShippingOption.id },
}),
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_DELETED,
action: "deleted",
object: "shipping_option_type",
data: { id: shippingOption.type.id },
}),
buildExpectedEventMessageShape({
eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED,
action: "created",
@@ -1374,14 +1362,16 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
])
// Test with full address including postal expression
const shippingOptions = await service.listShippingOptionsForContext({
address: {
country_code: "US",
province_code: "CA",
city: "Los Angeles",
postal_expression: "90210",
},
})
const shippingOptions = await service.listShippingOptionsForContext(
{
address: {
country_code: "US",
province_code: "CA",
city: "Los Angeles",
postal_expression: "90210",
},
}
)
expect(shippingOptions).toHaveLength(1)
})
@@ -1419,13 +1409,15 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
])
// Test with city but no postal expression
const shippingOptions = await service.listShippingOptionsForContext({
address: {
country_code: "US",
province_code: "CA",
city: "San Francisco",
},
})
const shippingOptions = await service.listShippingOptionsForContext(
{
address: {
country_code: "US",
province_code: "CA",
city: "San Francisco",
},
}
)
expect(shippingOptions).toHaveLength(1)
})
@@ -1462,12 +1454,14 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
])
// Test with province but no city
const shippingOptions = await service.listShippingOptionsForContext({
address: {
country_code: "US",
province_code: "NY",
},
})
const shippingOptions = await service.listShippingOptionsForContext(
{
address: {
country_code: "US",
province_code: "NY",
},
}
)
expect(shippingOptions).toHaveLength(1)
})
@@ -1503,11 +1497,13 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
])
// Test with only country
const shippingOptions = await service.listShippingOptionsForContext({
address: {
country_code: "CA",
},
})
const shippingOptions = await service.listShippingOptionsForContext(
{
address: {
country_code: "CA",
},
}
)
expect(shippingOptions).toHaveLength(1)
})
@@ -1758,14 +1754,16 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
])
// Address with undefined fields should still match if country matches
const shippingOptions = await service.listShippingOptionsForContext({
address: {
country_code: "US",
province_code: undefined,
city: undefined,
postal_expression: undefined,
},
})
const shippingOptions = await service.listShippingOptionsForContext(
{
address: {
country_code: "US",
province_code: undefined,
city: undefined,
postal_expression: undefined,
},
}
)
expect(shippingOptions).toHaveLength(1)
})
@@ -1831,26 +1829,27 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
})
// Create shipping options for each zone
const [usOption, europeOption, canadaOption] = await service.createShippingOptions([
generateCreateShippingOptionsData({
name: "US Shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "Europe Shipping",
service_zone_id: fulfillmentSet.service_zones[1].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "Canada Shipping",
service_zone_id: fulfillmentSet.service_zones[2].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
])
const [usOption, europeOption, canadaOption] =
await service.createShippingOptions([
generateCreateShippingOptionsData({
name: "US Shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "Europe Shipping",
service_zone_id: fulfillmentSet.service_zones[1].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "Canada Shipping",
service_zone_id: fulfillmentSet.service_zones[2].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
])
// Test US ZIP code - should only match US zone
let shippingOptions = await service.listShippingOptionsForContext({
@@ -1970,26 +1969,27 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
})
// Create shipping options with different prices for each zone
const [broadOption, californiaOption, laOption] = await service.createShippingOptions([
generateCreateShippingOptionsData({
name: "Standard US Shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "California Express",
service_zone_id: fulfillmentSet.service_zones[1].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "LA Premium",
service_zone_id: fulfillmentSet.service_zones[2].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
])
const [broadOption, californiaOption, laOption] =
await service.createShippingOptions([
generateCreateShippingOptionsData({
name: "Standard US Shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "California Express",
service_zone_id: fulfillmentSet.service_zones[1].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
generateCreateShippingOptionsData({
name: "LA Premium",
service_zone_id: fulfillmentSet.service_zones[2].id,
shipping_profile_id: shippingProfile.id,
provider_id: providerId,
}),
])
// Test LA ZIP code - should match all three zones
let shippingOptions = await service.listShippingOptionsForContext({
@@ -2001,7 +2001,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
},
})
expect(shippingOptions).toHaveLength(3)
const laIds = shippingOptions.map(opt => opt.id)
const laIds = shippingOptions.map((opt) => opt.id)
expect(laIds).toContain(broadOption.id)
expect(laIds).toContain(californiaOption.id)
expect(laIds).toContain(laOption.id)
@@ -2015,7 +2015,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
},
})
expect(shippingOptions).toHaveLength(2)
const caIds = shippingOptions.map(opt => opt.id)
const caIds = shippingOptions.map((opt) => opt.id)
expect(caIds).toContain(broadOption.id)
expect(caIds).toContain(californiaOption.id)
expect(caIds).not.toContain(laOption.id)
@@ -100,7 +100,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
await service.createShippingProfiles(createData)
expect(createdShippingProfiles).toHaveLength(2)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
let i = 0
for (const data_ of createData) {
@@ -118,9 +118,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
action: "created",
object: "shipping_profile",
data: {
id: expect.arrayContaining([
createdShippingProfiles[i].id,
]),
id: createdShippingProfiles[i].id,
},
}),
]),
@@ -47,18 +47,9 @@ import {
ShippingOptionType,
ShippingProfile,
} from "@models"
import {
buildCreatedFulfillmentEvents,
buildCreatedFulfillmentSetEvents,
buildCreatedServiceZoneEvents,
eventBuilders,
isContextValid,
Rule,
validateAndNormalizeRules,
} from "@utils"
import { isContextValid, Rule, validateAndNormalizeRules } from "@utils"
import { joinerConfig } from "../joiner-config"
import { UpdateShippingOptionsInput } from "../types/service"
import { buildCreatedShippingOptionEvents } from "../utils/events"
import FulfillmentProviderService from "./fulfillment-provider"
const generateMethodForModels = {
@@ -70,7 +61,7 @@ const generateMethodForModels = {
ShippingOptionRule,
ShippingOptionType,
FulfillmentProvider,
// Not adding Fulfillment to not auto generate the methods under the hood and only provide the methods we want to expose8
// Not adding Fulfillment to not auto generate the methods under the hood and only provide the methods we want to expose
}
type InjectedDependencies = {
@@ -339,11 +330,6 @@ export default class FulfillmentModuleService
sharedContext
)
buildCreatedFulfillmentSetEvents({
fulfillmentSets: createdFulfillmentSets,
sharedContext,
})
return createdFulfillmentSets
}
@@ -405,11 +391,6 @@ export default class FulfillmentModuleService
sharedContext
)
buildCreatedServiceZoneEvents({
serviceZones: createdServiceZones,
sharedContext,
})
return createdServiceZones
}
@@ -468,11 +449,6 @@ export default class FulfillmentModuleService
sharedContext
)
buildCreatedShippingOptionEvents({
shippingOptions: createdSO,
sharedContext,
})
return createdSO
}
@@ -487,7 +463,7 @@ export default class FulfillmentModuleService
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO>
@InjectTransactionManager()
@InjectManager()
@EmitEvents()
// @ts-expect-error
async createShippingProfiles(
@@ -503,11 +479,6 @@ export default class FulfillmentModuleService
sharedContext
)
eventBuilders.createdShippingProfile({
data: createdShippingProfiles,
sharedContext,
})
return await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingProfileDTO
| FulfillmentTypes.ShippingProfileDTO[]
@@ -561,11 +532,6 @@ export default class FulfillmentModuleService
sharedContext
)
eventBuilders.createdGeoZone({
data: createdGeoZones,
sharedContext,
})
return await this.baseRepository_.serialize<FulfillmentTypes.GeoZoneDTO[]>(
Array.isArray(data) ? createdGeoZones : createdGeoZones[0]
)
@@ -629,11 +595,6 @@ export default class FulfillmentModuleService
sharedContext
)
eventBuilders.createdShippingOptionRule({
data: createdSORules.map((sor) => ({ id: sor.id })),
sharedContext,
})
return createdSORules
}
@@ -679,11 +640,6 @@ export default class FulfillmentModuleService
throw error
}
buildCreatedFulfillmentEvents({
fulfillments: [fulfillment],
sharedContext,
})
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>(
fulfillment
)
@@ -754,11 +710,6 @@ export default class FulfillmentModuleService
throw error
}
buildCreatedFulfillmentEvents({
fulfillments: [fulfillment],
sharedContext,
})
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>(
fulfillment
)
@@ -943,15 +894,6 @@ export default class FulfillmentModuleService
})
if (serviceZoneIdsToDelete.length) {
eventBuilders.deletedServiceZone({
data: serviceZoneIdsToDelete.map((id) => ({ id })),
sharedContext,
})
eventBuilders.deletedGeoZone({
data: geoZoneIdsToDelete.map((id) => ({ id })),
sharedContext,
})
await promiseAll([
this.geoZoneService_.delete(
{
@@ -973,32 +915,6 @@ export default class FulfillmentModuleService
sharedContext
)
eventBuilders.updatedFulfillmentSet({
data: updatedFulfillmentSets,
sharedContext,
})
const createdServiceZoneIds: string[] = []
const createdGeoZoneIds = updatedFulfillmentSets
.flatMap((f) =>
[...f.service_zones].flatMap((serviceZone) => {
if (!existingServiceZoneIds.includes(serviceZone.id)) {
createdServiceZoneIds.push(serviceZone.id)
}
return serviceZone.geo_zones.map((g) => g.id)
})
)
.filter((id) => !existingGeoZoneIds.includes(id))
eventBuilders.createdServiceZone({
data: createdServiceZoneIds.map((id) => ({ id })),
sharedContext,
})
eventBuilders.createdGeoZone({
data: createdGeoZoneIds.map((id) => ({ id })),
sharedContext,
})
return Array.isArray(data)
? updatedFulfillmentSets
: updatedFulfillmentSets[0]
@@ -1187,11 +1103,6 @@ export default class FulfillmentModuleService
})
if (geoZoneIdsToDelete.length) {
eventBuilders.deletedGeoZone({
data: geoZoneIdsToDelete.map((id) => ({ id })),
sharedContext,
})
await this.geoZoneService_.delete(
{
id: geoZoneIdsToDelete,
@@ -1205,26 +1116,6 @@ export default class FulfillmentModuleService
sharedContext
)
eventBuilders.updatedServiceZone({
data: updatedServiceZones,
sharedContext,
})
const createdGeoZoneIds = updatedServiceZones
.flatMap((serviceZone) => {
return serviceZone.geo_zones.map((g) => g.id)
})
.filter((id) => !existingGeoZoneIds.includes(id))
eventBuilders.createdGeoZone({
data: createdGeoZoneIds.map((id) => ({ id })),
sharedContext,
})
eventBuilders.updatedGeoZone({
data: updatedGeoZoneIds.map((id) => ({ id })),
sharedContext,
})
return Array.isArray(data) ? updatedServiceZones : updatedServiceZones[0]
}
@@ -1485,11 +1376,6 @@ export default class FulfillmentModuleService
})
if (ruleIdsToDelete.length) {
eventBuilders.deletedShippingOptionRule({
data: ruleIdsToDelete.map((id) => ({ id })),
sharedContext,
})
await this.shippingOptionRuleService_.delete(
ruleIdsToDelete,
sharedContext
@@ -1501,71 +1387,11 @@ export default class FulfillmentModuleService
sharedContext
)
this.handleShippingOptionUpdateEvents({
shippingOptionsData: dataArray,
updatedShippingOptions,
optionTypeDeletedIds,
updatedRuleIds,
existingRuleIds,
sharedContext,
})
return Array.isArray(data)
? updatedShippingOptions
: updatedShippingOptions[0]
}
private handleShippingOptionUpdateEvents({
shippingOptionsData,
updatedShippingOptions,
optionTypeDeletedIds,
updatedRuleIds,
existingRuleIds,
sharedContext,
}) {
eventBuilders.updatedShippingOption({
data: updatedShippingOptions,
sharedContext,
})
eventBuilders.deletedShippingOptionType({
data: optionTypeDeletedIds.map((id) => ({ id })),
sharedContext,
})
const createdOptionTypeIds = updatedShippingOptions
.filter((so) => {
const updateData = shippingOptionsData.find((sod) => sod.id === so.id)
return isObject(updateData?.type) && !("id" in updateData.type)
})
.map((so) => so.type.id)
eventBuilders.createdShippingOptionType({
data: createdOptionTypeIds.map((id) => ({ id })),
sharedContext,
})
const createdRuleIds = updatedShippingOptions
.flatMap((so) =>
[...so.rules].map((rule) => {
if (existingRuleIds.includes(rule.id)) {
return
}
return rule.id
})
)
.filter((id): id is string => !!id)
eventBuilders.createdShippingOptionRule({
data: createdRuleIds.map((id) => ({ id })),
sharedContext,
})
eventBuilders.updatedShippingOptionRule({
data: updatedRuleIds.map((id) => ({ id })),
sharedContext,
})
}
async upsertShippingOptions(
data: FulfillmentTypes.UpsertShippingOptionDTO[],
sharedContext?: Context
@@ -1652,7 +1478,8 @@ export default class FulfillmentModuleService
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingOptionTypeDTO>
@InjectTransactionManager()
@InjectManager()
@EmitEvents()
async upsertShippingOptionTypes(
data:
| FulfillmentTypes.UpsertShippingOptionTypeDTO[]
@@ -1662,12 +1489,7 @@ export default class FulfillmentModuleService
| FulfillmentTypes.ShippingOptionTypeDTO[]
| FulfillmentTypes.ShippingOptionTypeDTO
> {
const input = Array.isArray(data) ? data : [data]
const results = await this.shippingOptionTypeService_.upsert(
input,
sharedContext
)
const results = await this.updateShippingOptionTypes_(data, sharedContext)
const allTypes = await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingOptionTypeDTO[]
@@ -1677,6 +1499,23 @@ export default class FulfillmentModuleService
return Array.isArray(data) ? allTypes : allTypes[0]
}
@InjectTransactionManager()
protected async updateShippingOptionTypes_(
data:
| FulfillmentTypes.UpsertShippingOptionTypeDTO[]
| FulfillmentTypes.UpsertShippingOptionTypeDTO,
sharedContext: Context
): Promise<InferEntityType<typeof ShippingOptionType>[]> {
const input = Array.isArray(data) ? data : [data]
const results = await this.shippingOptionTypeService_.upsert(
input,
sharedContext
)
return results
}
// @ts-expect-error
updateShippingOptionTypes(
id: string,
@@ -1691,6 +1530,7 @@ export default class FulfillmentModuleService
): Promise<FulfillmentTypes.ShippingOptionTypeDTO[]>
@InjectManager()
@EmitEvents()
// @ts-expect-error
async updateShippingOptionTypes(
idOrSelector: string | FulfillmentTypes.FilterableShippingOptionTypeProps,
@@ -1747,7 +1587,8 @@ export default class FulfillmentModuleService
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO>
@InjectTransactionManager()
@InjectManager()
@EmitEvents()
// @ts-expect-error
async updateShippingProfiles(
idOrSelector: string | FulfillmentTypes.FilterableShippingProfileProps,
@@ -1756,6 +1597,25 @@ export default class FulfillmentModuleService
): Promise<
FulfillmentTypes.ShippingProfileDTO | FulfillmentTypes.ShippingProfileDTO[]
> {
const profiles = await this.updateShippingProfiles_(
idOrSelector,
data,
sharedContext
)
const updatedProfiles = await this.baseRepository_.serialize<
FulfillmentTypes.ShippingProfileDTO[]
>(profiles)
return isString(idOrSelector) ? updatedProfiles[0] : updatedProfiles
}
@InjectTransactionManager()
protected async updateShippingProfiles_(
idOrSelector: string | FulfillmentTypes.FilterableShippingProfileProps,
data: FulfillmentTypes.UpdateShippingProfileDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<InferEntityType<typeof ShippingProfile>[]> {
let normalizedInput: ({
id: string
} & FulfillmentTypes.UpdateShippingProfileDTO)[] = []
@@ -1784,11 +1644,7 @@ export default class FulfillmentModuleService
sharedContext
)
const updatedProfiles = await this.baseRepository_.serialize<
FulfillmentTypes.ShippingProfileDTO[]
>(profiles)
return isString(idOrSelector) ? updatedProfiles[0] : updatedProfiles
return profiles
}
async upsertShippingProfiles(
@@ -1800,7 +1656,8 @@ export default class FulfillmentModuleService
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO>
@InjectTransactionManager()
@InjectManager()
@EmitEvents()
async upsertShippingProfiles(
data:
| FulfillmentTypes.UpsertShippingProfileDTO[]
@@ -1808,6 +1665,24 @@ export default class FulfillmentModuleService
@MedusaContext() sharedContext: Context = {}
): Promise<
FulfillmentTypes.ShippingProfileDTO[] | FulfillmentTypes.ShippingProfileDTO
> {
const profiles = await this.upsertShippingProfiles_(data, sharedContext)
return await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingProfileDTO[]
| FulfillmentTypes.ShippingProfileDTO
>(Array.isArray(data) ? profiles : profiles[0])
}
@InjectTransactionManager()
protected async upsertShippingProfiles_(
data:
| FulfillmentTypes.UpsertShippingProfileDTO[]
| FulfillmentTypes.UpsertShippingProfileDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<
| InferEntityType<typeof ShippingProfile>[]
| InferEntityType<typeof ShippingProfile>
> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter((prof) => !!prof.id)
@@ -1831,13 +1706,7 @@ export default class FulfillmentModuleService
)
}
const result = [...created, ...updated]
const allProfiles = await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingProfileDTO[]
| FulfillmentTypes.ShippingProfileDTO
>(result)
return Array.isArray(data) ? allProfiles : allProfiles[0]
return [...created, ...updated]
}
// @ts-expect-error
@@ -1873,11 +1742,6 @@ export default class FulfillmentModuleService
sharedContext
)
eventBuilders.updatedGeoZone({
data: updatedGeoZones,
sharedContext,
})
const serialized = await this.baseRepository_.serialize<
FulfillmentTypes.GeoZoneDTO[]
>(updatedGeoZones)
@@ -1940,11 +1804,6 @@ export default class FulfillmentModuleService
const updatedShippingOptionRules =
await this.shippingOptionRuleService_.update(data_, sharedContext)
eventBuilders.updatedShippingOptionRule({
data: updatedShippingOptionRules.map((rule) => ({ id: rule.id })),
sharedContext,
})
return Array.isArray(data)
? updatedShippingOptionRules
: updatedShippingOptionRules[0]
@@ -1980,9 +1839,6 @@ export default class FulfillmentModuleService
)
const updatedLabelIds: string[] = []
let deletedLabelIds: string[] = []
const existingLabelIds = existingFulfillment.labels.map((label) => label.id)
/**
* @note
@@ -1995,12 +1851,6 @@ export default class FulfillmentModuleService
* and we also need to emit the events later on.
*/
if (isDefined(data.labels) && isPresent(data.labels)) {
const dataLabelIds: string[] = data.labels
.filter((label): label is { id: string } => "id" in label)
.map((label) => label.id)
deletedLabelIds = arrayDifference(existingLabelIds, dataLabelIds)
for (let label of data.labels) {
if (!("id" in label)) {
continue
@@ -2029,49 +1879,9 @@ export default class FulfillmentModuleService
sharedContext
)
this.handleFulfillmentUpdateEvents(
fulfillment,
existingLabelIds,
updatedLabelIds,
deletedLabelIds,
sharedContext
)
return fulfillment
}
private handleFulfillmentUpdateEvents(
fulfillment: InferEntityType<typeof Fulfillment>,
existingLabelIds: string[],
updatedLabelIds: string[],
deletedLabelIds: string[],
sharedContext: Context
) {
eventBuilders.updatedFulfillment({
data: [{ id: fulfillment.id }],
sharedContext,
})
eventBuilders.deletedFulfillmentLabel({
data: deletedLabelIds.map((id) => ({ id })),
sharedContext,
})
eventBuilders.updatedFulfillmentLabel({
data: updatedLabelIds.map((id) => ({ id })),
sharedContext,
})
const createdLabels = fulfillment.labels.filter((label) => {
return !existingLabelIds.includes(label.id)
})
eventBuilders.createdFulfillmentLabel({
data: createdLabels.map((label) => ({ id: label.id })),
sharedContext,
})
}
@InjectManager()
@EmitEvents()
async cancelFulfillment(
@@ -2106,11 +1916,6 @@ export default class FulfillmentModuleService
},
sharedContext
)
eventBuilders.updatedFulfillment({
data: [{ id }],
sharedContext,
})
}
const result = await this.baseRepository_.serialize<FulfillmentDTO>(
@@ -2211,6 +2016,7 @@ export default class FulfillmentModuleService
}
@InjectTransactionManager()
@EmitEvents()
// @ts-expect-error
async deleteShippingProfiles(
ids: string | string[],
@@ -2226,6 +2032,7 @@ export default class FulfillmentModuleService
}
@InjectTransactionManager()
@EmitEvents()
// @ts-expect-error
async softDeleteShippingProfiles<
TReturnableLinkableKeys extends string = string