chore: prevent workflow steps to call modules when not necessary (#11632)

**What**
Some steps were calling the modules even when nothing was needed which for some operations would create transaction for nothing leading to extra execution time that add up very quickly on cloud network

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2025-02-26 17:42:48 +00:00
committed by GitHub
co-authored by Carlos R. L. Rodrigues
parent 54a6ef91ac
commit caf83cf78c
17 changed files with 125 additions and 55 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/core-flows": patch
"@medusajs/cart": patch
---
chore(): Prevent workflow steps to even call modules when not necessary
@@ -33,8 +33,11 @@ export const addShippingMethodToCartStepId = "add-shipping-method-to-cart-step"
export const addShippingMethodToCartStep = createStep(
addShippingMethodToCartStepId,
async (data: AddShippingMethodToCartStepInput, { container }) => {
const cartService = container.resolve<ICartModuleService>(Modules.CART)
if (!data.shipping_methods?.length) {
return new StepResponse([], [])
}
const cartService = container.resolve<ICartModuleService>(Modules.CART)
const methods = await cartService.addShippingMethods(data.shipping_methods)
return new StepResponse(methods, methods)
@@ -60,6 +60,10 @@ export const confirmInventoryStepId = "confirm-inventory-step"
export const confirmInventoryStep = createStep(
confirmInventoryStepId,
async (data: ConfirmVariantInventoryStepInput, { container }) => {
if (!data.items?.length) {
return new StepResponse([], [])
}
const inventoryService = container.resolve<IInventoryService>(
Modules.INVENTORY
)
@@ -34,6 +34,11 @@ export const createLineItemAdjustmentsStep = createStep(
createLineItemAdjustmentsStepId,
async (data: CreateLineItemAdjustmentsCartStepInput, { container }) => {
const { lineItemAdjustmentsToCreate = [] } = data
if (!lineItemAdjustmentsToCreate?.length) {
return new StepResponse([], [])
}
const cartModuleService: ICartModuleService = container.resolve(
Modules.CART
)
@@ -33,6 +33,10 @@ export const createPaymentCollectionsStepId = "create-payment-collections"
export const createPaymentCollectionsStep = createStep(
createPaymentCollectionsStepId,
async (data: CreatePaymentCollectionCartStepInput, { container }) => {
if (!data?.length) {
return new StepResponse([], [])
}
const service = container.resolve<IPaymentModuleService>(Modules.PAYMENT)
const created = await service.createPaymentCollections(data)
@@ -33,6 +33,11 @@ export const createShippingMethodAdjustmentsStep = createStep(
createShippingMethodAdjustmentsStepId,
async (data: CreateShippingMethodAdjustmentsStepInput, { container }) => {
const { shippingMethodAdjustmentsToCreate = [] } = data
if (!shippingMethodAdjustmentsToCreate?.length) {
return new StepResponse(void 0, [])
}
const cartModuleService: ICartModuleService = container.resolve(
Modules.CART
)
@@ -40,6 +40,7 @@ export const getActionsToComputeFromPromotionsStep = createStep(
getActionsToComputeFromPromotionsStepId,
async (data: GetActionsToComputeFromPromotionsStepInput, { container }) => {
const { cart, promotionCodesToApply = [] } = data
const promotionService = container.resolve<IPromotionModuleService>(
Modules.PROMOTION
)
@@ -56,11 +56,16 @@ export const getLineItemActionsStepId = "get-line-item-actions-step"
export const getLineItemActionsStep = createStep(
getLineItemActionsStepId,
async (data: GetLineItemActionsStepInput, { container }) => {
if (!data.items.length) {
return new StepResponse({ itemsToCreate: [], itemsToUpdate: [] }, null)
}
const cartModule = container.resolve<ICartModuleService>(Modules.CART)
const variantIds = data.items.map((d) => d.variant_id!)
const existingVariantItems = await cartModule.listLineItems({
cart_id: data.id,
variant_id: data.items.map((d) => d.variant_id!),
variant_id: variantIds,
})
const variantItemMap = new Map<string, CartLineItemDTO>(
@@ -99,7 +104,8 @@ export const getLineItemActionsStep = createStep(
}
return new StepResponse(
{ itemsToCreate, itemsToUpdate } as GetLineItemActionsStepOutput
, null)
{ itemsToCreate, itemsToUpdate } as GetLineItemActionsStepOutput,
null
)
}
)
@@ -84,12 +84,14 @@ export const getPromotionCodesToApply = createStep(
})
const promotionCodesToApply: Set<string> = new Set(
(
await promotionService.listPromotions(
{ code: adjustmentCodes },
{ select: ["code"] }
)
).map((p) => p.code!)
adjustmentCodes.length
? (
await promotionService.listPromotions(
{ code: adjustmentCodes },
{ select: ["code"] }
)
).map((p) => p.code!)
: []
)
if (action === PromotionActions.ADD) {
@@ -1,4 +1,7 @@
import { CalculatedPriceSet, IPricingModuleService } from "@medusajs/framework/types"
import {
CalculatedPriceSet,
IPricingModuleService,
} from "@medusajs/framework/types"
import { MedusaError, Modules } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
@@ -107,6 +107,17 @@ export const prepareAdjustmentsFromPromotionActionsStep = createStep(
)
const { actions = [] } = data
if (!actions.length) {
return new StepResponse({
lineItemAdjustmentsToCreate: [],
lineItemAdjustmentIdsToRemove: [],
shippingMethodAdjustmentsToCreate: [],
shippingMethodAdjustmentIdsToRemove: [],
computedPromotionCodes: [],
} as PrepareAdjustmentsFromPromotionActionsStepOutput)
}
const promotions = await promotionModuleService.listPromotions(
{ code: actions.map((a) => a.code) },
{ select: ["id", "code"] }
@@ -20,6 +20,11 @@ export const removeLineItemAdjustmentsStep = createStep(
removeLineItemAdjustmentsStepId,
async (data: RemoveLineItemAdjustmentsStepInput, { container }) => {
const { lineItemAdjustmentIdsToRemove = [] } = data
if (!lineItemAdjustmentIdsToRemove?.length) {
return new StepResponse(void 0, [])
}
const cartModuleService: ICartModuleService = container.resolve(
Modules.CART
)
@@ -21,6 +21,11 @@ export const removeShippingMethodAdjustmentsStep = createStep(
removeShippingMethodAdjustmentsStepId,
async (data: RemoveShippingMethodAdjustmentsStepInput, { container }) => {
const { shippingMethodAdjustmentIdsToRemove = [] } = data
if (!shippingMethodAdjustmentIdsToRemove?.length) {
return new StepResponse(void 0, [])
}
const cartModuleService: ICartModuleService = container.resolve(
Modules.CART
)
@@ -61,8 +61,14 @@ export const reserveInventoryStepId = "reserve-inventory-step"
export const reserveInventoryStep = createStep(
reserveInventoryStepId,
async (data: ReserveVariantInventoryStepInput, { container }) => {
const inventoryService = container.resolve(Modules.INVENTORY)
if (!data.items.length) {
return new StepResponse([], {
reservations: [],
inventoryItemIds: [],
})
}
const inventoryService = container.resolve(Modules.INVENTORY)
const locking = container.resolve(Modules.LOCKING)
const inventoryItemIds: string[] = []
@@ -35,6 +35,7 @@ export const updateCartPromotionsStep = createStep(
updateCartPromotionsStepId,
async (data: UpdateCartPromotionStepInput, { container }) => {
const { promo_codes = [], id, action = PromotionActions.ADD } = data
const remoteLink = container.resolve(ContainerRegistrationKeys.LINK)
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
@@ -55,29 +56,31 @@ export const updateCartPromotionsStep = createStep(
existingCartPromotionLinks.map((link) => [link.promotion_id, link])
)
const promotions = await promotionService.listPromotions(
{ code: promo_codes },
{ select: ["id"] }
)
const linksToCreate: any[] = []
const linksToDismiss: any[] = []
for (const promotion of promotions) {
const linkObject = {
[Modules.CART]: { cart_id: id },
[Modules.PROMOTION]: { promotion_id: promotion.id },
}
if (promo_codes?.length) {
const promotions = await promotionService.listPromotions(
{ code: promo_codes },
{ select: ["id"] }
)
if ([PromotionActions.ADD, PromotionActions.REPLACE].includes(action)) {
linksToCreate.push(linkObject)
}
for (const promotion of promotions) {
const linkObject = {
[Modules.CART]: { cart_id: id },
[Modules.PROMOTION]: { promotion_id: promotion.id },
}
if (action === PromotionActions.REMOVE) {
const link = promotionLinkMap.get(promotion.id)
if ([PromotionActions.ADD, PromotionActions.REPLACE].includes(action)) {
linksToCreate.push(linkObject)
}
if (link) {
linksToDismiss.push(linkObject)
if (action === PromotionActions.REMOVE) {
const link = promotionLinkMap.get(promotion.id)
if (link) {
linksToDismiss.push(linkObject)
}
}
}
}
@@ -1083,10 +1083,9 @@ export default class CartModuleService
)
}
const result = await this.lineItemTaxLineService_.upsert(
taxLines,
sharedContext
)
const result = taxLines.length
? await this.lineItemTaxLineService_.upsert(taxLines, sharedContext)
: []
return await this.baseRepository_.serialize<CartTypes.LineItemTaxLineDTO[]>(
result,
@@ -1201,10 +1200,12 @@ export default class CartModuleService
)
}
const result = await this.shippingMethodTaxLineService_.upsert(
taxLines as UpdateShippingMethodTaxLineDTO[],
sharedContext
)
const result = taxLines.length
? await this.shippingMethodTaxLineService_.upsert(
taxLines as UpdateShippingMethodTaxLineDTO[],
sharedContext
)
: []
return await this.baseRepository_.serialize<
CartTypes.ShippingMethodTaxLineDTO[]