feat(fulfillment): Events management (#6730)
**What** This pr includes some cleanup and refactoring around the abstract event emitter method for the module service to not rely on a named class property but instead on resolution naming. Includes fulfillment set events on creation. The idea is that if that pr allows us to align and agreed on the approach as well as including the cleanup for other pr to use, If this gets merged I ll continue with another pr to do the rest of the event management partially fix CORE-1735
This commit is contained in:
@@ -14,6 +14,8 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
arrayDifference,
|
||||
EmitEvents,
|
||||
FulfillmentUtils,
|
||||
getSetDifference,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
@@ -36,6 +38,7 @@ import {
|
||||
import { isContextValid, validateRules } from "@utils"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
import FulfillmentProviderService from "./fulfillment-provider"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
const generateMethodForModels = [
|
||||
ServiceZone,
|
||||
@@ -242,6 +245,7 @@ export default class FulfillmentModuleService<
|
||||
): Promise<FulfillmentTypes.FulfillmentSetDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async create(
|
||||
data:
|
||||
| FulfillmentTypes.CreateFulfillmentSetDTO
|
||||
@@ -287,6 +291,11 @@ export default class FulfillmentModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
this.aggregateFulfillmentSetCreatedEvents(
|
||||
createdFulfillmentSets,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return Array.isArray(data)
|
||||
? createdFulfillmentSets
|
||||
: createdFulfillmentSets[0]
|
||||
@@ -1498,7 +1507,7 @@ export default class FulfillmentModuleService<
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
private static buildGeoZoneConstraintsFromAddress(
|
||||
protected static buildGeoZoneConstraintsFromAddress(
|
||||
address: FulfillmentTypes.FilterableShippingOptionForContextProps["address"]
|
||||
) {
|
||||
/**
|
||||
@@ -1557,4 +1566,60 @@ export default class FulfillmentModuleService<
|
||||
|
||||
return geoZoneConstraints
|
||||
}
|
||||
|
||||
protected aggregateFulfillmentSetCreatedEvents(
|
||||
createdFulfillmentSets: TEntity[],
|
||||
sharedContext: Context
|
||||
): void {
|
||||
const buildMessage = ({
|
||||
eventName,
|
||||
id,
|
||||
object,
|
||||
}: {
|
||||
eventName: string
|
||||
id: string
|
||||
object: string
|
||||
}) => {
|
||||
return {
|
||||
eventName,
|
||||
metadata: {
|
||||
object,
|
||||
service: Modules.FULFILLMENT,
|
||||
action: "created",
|
||||
eventGroupId: sharedContext.eventGroupId,
|
||||
},
|
||||
data: { id },
|
||||
}
|
||||
}
|
||||
|
||||
for (const fulfillmentSet of createdFulfillmentSets) {
|
||||
sharedContext.messageAggregator!.saveRawMessageData(
|
||||
buildMessage({
|
||||
eventName: FulfillmentUtils.FulfillmentEvents.created,
|
||||
id: fulfillmentSet.id,
|
||||
object: "fulfillment_set",
|
||||
})
|
||||
)
|
||||
|
||||
for (const serviceZone of fulfillmentSet.service_zones ?? []) {
|
||||
sharedContext.messageAggregator!.saveRawMessageData(
|
||||
buildMessage({
|
||||
eventName: FulfillmentUtils.FulfillmentEvents.service_zone_created,
|
||||
id: serviceZone.id,
|
||||
object: "service_zone",
|
||||
})
|
||||
)
|
||||
|
||||
for (const geoZone of serviceZone.geo_zones ?? []) {
|
||||
sharedContext.messageAggregator!.saveRawMessageData(
|
||||
buildMessage({
|
||||
eventName: FulfillmentUtils.FulfillmentEvents.geo_zone_created,
|
||||
id: geoZone.id,
|
||||
object: "geo_zone",
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user