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
@@ -18,7 +18,7 @@ export interface AddShippingMethodToCartStepInput {
export const addShippingMethodToCartStepId = "add-shipping-method-to-cart-step" export const addShippingMethodToCartStepId = "add-shipping-method-to-cart-step"
/** /**
* This step adds shipping methods to a cart. * This step adds shipping methods to a cart.
* *
* @example * @example
* const data = addShippingMethodToCartStep({ * const data = addShippingMethodToCartStep({
* shipping_methods: [ * shipping_methods: [
@@ -33,8 +33,11 @@ export const addShippingMethodToCartStepId = "add-shipping-method-to-cart-step"
export const addShippingMethodToCartStep = createStep( export const addShippingMethodToCartStep = createStep(
addShippingMethodToCartStepId, addShippingMethodToCartStepId,
async (data: AddShippingMethodToCartStepInput, { container }) => { 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) const methods = await cartService.addShippingMethods(data.shipping_methods)
return new StepResponse(methods, methods) return new StepResponse(methods, methods)
@@ -43,7 +43,7 @@ export const confirmInventoryStepId = "confirm-inventory-step"
/** /**
* This step validates that items in the cart have sufficient inventory quantity. * This step validates that items in the cart have sufficient inventory quantity.
* If an item doesn't have sufficient inventory, an error is thrown. * If an item doesn't have sufficient inventory, an error is thrown.
* *
* @example * @example
* confirmInventoryStep({ * confirmInventoryStep({
* items: [ * items: [
@@ -60,6 +60,10 @@ export const confirmInventoryStepId = "confirm-inventory-step"
export const confirmInventoryStep = createStep( export const confirmInventoryStep = createStep(
confirmInventoryStepId, confirmInventoryStepId,
async (data: ConfirmVariantInventoryStepInput, { container }) => { async (data: ConfirmVariantInventoryStepInput, { container }) => {
if (!data.items?.length) {
return new StepResponse([], [])
}
const inventoryService = container.resolve<IInventoryService>( const inventoryService = container.resolve<IInventoryService>(
Modules.INVENTORY Modules.INVENTORY
) )
@@ -18,7 +18,7 @@ export interface CreateLineItemAdjustmentsCartStepInput {
export const createLineItemAdjustmentsStepId = "create-line-item-adjustments" export const createLineItemAdjustmentsStepId = "create-line-item-adjustments"
/** /**
* This step creates line item adjustments in a cart, such as when a promotion is applied. * This step creates line item adjustments in a cart, such as when a promotion is applied.
* *
* @example * @example
* createLineItemAdjustmentsStep({ * createLineItemAdjustmentsStep({
* lineItemAdjustmentsToCreate: [ * lineItemAdjustmentsToCreate: [
@@ -34,6 +34,11 @@ export const createLineItemAdjustmentsStep = createStep(
createLineItemAdjustmentsStepId, createLineItemAdjustmentsStepId,
async (data: CreateLineItemAdjustmentsCartStepInput, { container }) => { async (data: CreateLineItemAdjustmentsCartStepInput, { container }) => {
const { lineItemAdjustmentsToCreate = [] } = data const { lineItemAdjustmentsToCreate = [] } = data
if (!lineItemAdjustmentsToCreate?.length) {
return new StepResponse([], [])
}
const cartModuleService: ICartModuleService = container.resolve( const cartModuleService: ICartModuleService = container.resolve(
Modules.CART Modules.CART
) )
@@ -22,7 +22,7 @@ export interface CreateLineItemsCartStepInput {
export const createLineItemsStepId = "create-line-items-step" export const createLineItemsStepId = "create-line-items-step"
/** /**
* This step creates line item in a cart. * This step creates line item in a cart.
* *
* @example * @example
* const data = createLineItemsStep({ * const data = createLineItemsStep({
* "id": "cart_123", * "id": "cart_123",
@@ -33,6 +33,10 @@ export const createPaymentCollectionsStepId = "create-payment-collections"
export const createPaymentCollectionsStep = createStep( export const createPaymentCollectionsStep = createStep(
createPaymentCollectionsStepId, createPaymentCollectionsStepId,
async (data: CreatePaymentCollectionCartStepInput, { container }) => { async (data: CreatePaymentCollectionCartStepInput, { container }) => {
if (!data?.length) {
return new StepResponse([], [])
}
const service = container.resolve<IPaymentModuleService>(Modules.PAYMENT) const service = container.resolve<IPaymentModuleService>(Modules.PAYMENT)
const created = await service.createPaymentCollections(data) const created = await service.createPaymentCollections(data)
@@ -19,7 +19,7 @@ export const createShippingMethodAdjustmentsStepId =
"create-shipping-method-adjustments" "create-shipping-method-adjustments"
/** /**
* This step creates shipping method adjustments for a cart. * This step creates shipping method adjustments for a cart.
* *
* @example * @example
* const data = createShippingMethodAdjustmentsStep({ * const data = createShippingMethodAdjustmentsStep({
* "shippingMethodAdjustmentsToCreate": [{ * "shippingMethodAdjustmentsToCreate": [{
@@ -33,6 +33,11 @@ export const createShippingMethodAdjustmentsStep = createStep(
createShippingMethodAdjustmentsStepId, createShippingMethodAdjustmentsStepId,
async (data: CreateShippingMethodAdjustmentsStepInput, { container }) => { async (data: CreateShippingMethodAdjustmentsStepInput, { container }) => {
const { shippingMethodAdjustmentsToCreate = [] } = data const { shippingMethodAdjustmentsToCreate = [] } = data
if (!shippingMethodAdjustmentsToCreate?.length) {
return new StepResponse(void 0, [])
}
const cartModuleService: ICartModuleService = container.resolve( const cartModuleService: ICartModuleService = container.resolve(
Modules.CART Modules.CART
) )
@@ -21,13 +21,13 @@ export const getActionsToComputeFromPromotionsStepId =
/** /**
* This step retrieves the actions to compute based on the promotions * This step retrieves the actions to compute based on the promotions
* applied on a cart. * applied on a cart.
* *
* :::tip * :::tip
* *
* You can use the {@link retrieveCartStep} to retrieve a cart's details. * You can use the {@link retrieveCartStep} to retrieve a cart's details.
* *
* ::: * :::
* *
* @example * @example
* const data = getActionsToComputeFromPromotionsStep({ * const data = getActionsToComputeFromPromotionsStep({
* // retrieve the details of the cart from another workflow * // retrieve the details of the cart from another workflow
@@ -40,6 +40,7 @@ export const getActionsToComputeFromPromotionsStep = createStep(
getActionsToComputeFromPromotionsStepId, getActionsToComputeFromPromotionsStepId,
async (data: GetActionsToComputeFromPromotionsStepInput, { container }) => { async (data: GetActionsToComputeFromPromotionsStepInput, { container }) => {
const { cart, promotionCodesToApply = [] } = data const { cart, promotionCodesToApply = [] } = data
const promotionService = container.resolve<IPromotionModuleService>( const promotionService = container.resolve<IPromotionModuleService>(
Modules.PROMOTION Modules.PROMOTION
) )
@@ -41,7 +41,7 @@ export const getLineItemActionsStepId = "get-line-item-actions-step"
/** /**
* This step returns lists of cart line items to create or update based on the * This step returns lists of cart line items to create or update based on the
* provided input. * provided input.
* *
* @example * @example
* const data = getLineItemActionsStep({ * const data = getLineItemActionsStep({
* "id": "cart_123", * "id": "cart_123",
@@ -56,11 +56,16 @@ export const getLineItemActionsStepId = "get-line-item-actions-step"
export const getLineItemActionsStep = createStep( export const getLineItemActionsStep = createStep(
getLineItemActionsStepId, getLineItemActionsStepId,
async (data: GetLineItemActionsStepInput, { container }) => { async (data: GetLineItemActionsStepInput, { container }) => {
if (!data.items.length) {
return new StepResponse({ itemsToCreate: [], itemsToUpdate: [] }, null)
}
const cartModule = container.resolve<ICartModuleService>(Modules.CART) const cartModule = container.resolve<ICartModuleService>(Modules.CART)
const variantIds = data.items.map((d) => d.variant_id!)
const existingVariantItems = await cartModule.listLineItems({ const existingVariantItems = await cartModule.listLineItems({
cart_id: data.id, cart_id: data.id,
variant_id: data.items.map((d) => d.variant_id!), variant_id: variantIds,
}) })
const variantItemMap = new Map<string, CartLineItemDTO>( const variantItemMap = new Map<string, CartLineItemDTO>(
@@ -99,7 +104,8 @@ export const getLineItemActionsStep = createStep(
} }
return new StepResponse( return new StepResponse(
{ itemsToCreate, itemsToUpdate } as GetLineItemActionsStepOutput { itemsToCreate, itemsToUpdate } as GetLineItemActionsStepOutput,
, null) null
)
} }
) )
@@ -34,7 +34,7 @@ export interface GetPromotionCodesToApplyStepInput {
/** /**
* The promotion codes to apply on the cart. * The promotion codes to apply on the cart.
* *
* @example ["PRO10", "SHIPFREE", "NEWYEAR20"] * @example ["PRO10", "SHIPFREE", "NEWYEAR20"]
*/ */
export type GetPromotionCodesToApplyStepOutput = string[] export type GetPromotionCodesToApplyStepOutput = string[]
@@ -42,7 +42,7 @@ export type GetPromotionCodesToApplyStepOutput = string[]
export const getPromotionCodesToApplyId = "get-promotion-codes-to-apply" export const getPromotionCodesToApplyId = "get-promotion-codes-to-apply"
/** /**
* This step retrieves the promotion codes to apply on a cart. * This step retrieves the promotion codes to apply on a cart.
* *
* @example * @example
* const data = getPromotionCodesToApply( * const data = getPromotionCodesToApply(
* { * {
@@ -84,12 +84,14 @@ export const getPromotionCodesToApply = createStep(
}) })
const promotionCodesToApply: Set<string> = new Set( const promotionCodesToApply: Set<string> = new Set(
( adjustmentCodes.length
await promotionService.listPromotions( ? (
{ code: adjustmentCodes }, await promotionService.listPromotions(
{ select: ["code"] } { code: adjustmentCodes },
) { select: ["code"] }
).map((p) => p.code!) )
).map((p) => p.code!)
: []
) )
if (action === PromotionActions.ADD) { 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 { MedusaError, Modules } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk" import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
@@ -12,7 +15,7 @@ export interface GetVariantPriceSetsStepInput {
variantIds: string[] variantIds: string[]
/** /**
* The context to use when calculating the price sets. * The context to use when calculating the price sets.
* *
* Learn more in [this documentation](https://docs.medusajs.com/resources/commerce-modules/product/guides/price#retrieve-calculated-price-for-a-context). * Learn more in [this documentation](https://docs.medusajs.com/resources/commerce-modules/product/guides/price#retrieve-calculated-price-for-a-context).
*/ */
context?: Record<string, unknown> context?: Record<string, unknown>
@@ -28,18 +31,18 @@ export interface GetVariantPriceSetsStepOutput {
export const getVariantPriceSetsStepId = "get-variant-price-sets" export const getVariantPriceSetsStepId = "get-variant-price-sets"
/** /**
* This step retrieves the calculated price sets of the specified variants. * This step retrieves the calculated price sets of the specified variants.
* *
* @example * @example
* To retrieve a variant's price sets: * To retrieve a variant's price sets:
* *
* ```ts * ```ts
* const data = getVariantPriceSetsStep({ * const data = getVariantPriceSetsStep({
* variantIds: ["variant_123"], * variantIds: ["variant_123"],
* }) * })
* ``` * ```
* *
* To retrieve the calculated price sets of a variant: * To retrieve the calculated price sets of a variant:
* *
* ```ts * ```ts
* const data = getVariantPriceSetsStep({ * const data = getVariantPriceSetsStep({
* variantIds: ["variant_123"], * variantIds: ["variant_123"],
@@ -85,7 +85,7 @@ export const prepareAdjustmentsFromPromotionActionsStepId =
/** /**
* This step prepares the line item or shipping method adjustments using * This step prepares the line item or shipping method adjustments using
* actions computed by the Promotion Module. * actions computed by the Promotion Module.
* *
* @example * @example
* const data = prepareAdjustmentsFromPromotionActionsStep({ * const data = prepareAdjustmentsFromPromotionActionsStep({
* "actions": [{ * "actions": [{
@@ -107,6 +107,17 @@ export const prepareAdjustmentsFromPromotionActionsStep = createStep(
) )
const { actions = [] } = data const { actions = [] } = data
if (!actions.length) {
return new StepResponse({
lineItemAdjustmentsToCreate: [],
lineItemAdjustmentIdsToRemove: [],
shippingMethodAdjustmentsToCreate: [],
shippingMethodAdjustmentIdsToRemove: [],
computedPromotionCodes: [],
} as PrepareAdjustmentsFromPromotionActionsStepOutput)
}
const promotions = await promotionModuleService.listPromotions( const promotions = await promotionModuleService.listPromotions(
{ code: actions.map((a) => a.code) }, { code: actions.map((a) => a.code) },
{ select: ["id", "code"] } { select: ["id", "code"] }
@@ -20,6 +20,11 @@ export const removeLineItemAdjustmentsStep = createStep(
removeLineItemAdjustmentsStepId, removeLineItemAdjustmentsStepId,
async (data: RemoveLineItemAdjustmentsStepInput, { container }) => { async (data: RemoveLineItemAdjustmentsStepInput, { container }) => {
const { lineItemAdjustmentIdsToRemove = [] } = data const { lineItemAdjustmentIdsToRemove = [] } = data
if (!lineItemAdjustmentIdsToRemove?.length) {
return new StepResponse(void 0, [])
}
const cartModuleService: ICartModuleService = container.resolve( const cartModuleService: ICartModuleService = container.resolve(
Modules.CART Modules.CART
) )
@@ -21,6 +21,11 @@ export const removeShippingMethodAdjustmentsStep = createStep(
removeShippingMethodAdjustmentsStepId, removeShippingMethodAdjustmentsStepId,
async (data: RemoveShippingMethodAdjustmentsStepInput, { container }) => { async (data: RemoveShippingMethodAdjustmentsStepInput, { container }) => {
const { shippingMethodAdjustmentIdsToRemove = [] } = data const { shippingMethodAdjustmentIdsToRemove = [] } = data
if (!shippingMethodAdjustmentIdsToRemove?.length) {
return new StepResponse(void 0, [])
}
const cartModuleService: ICartModuleService = container.resolve( const cartModuleService: ICartModuleService = container.resolve(
Modules.CART Modules.CART
) )
@@ -61,8 +61,14 @@ export const reserveInventoryStepId = "reserve-inventory-step"
export const reserveInventoryStep = createStep( export const reserveInventoryStep = createStep(
reserveInventoryStepId, reserveInventoryStepId,
async (data: ReserveVariantInventoryStepInput, { container }) => { 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 locking = container.resolve(Modules.LOCKING)
const inventoryItemIds: string[] = [] const inventoryItemIds: string[] = []
@@ -35,6 +35,7 @@ export const updateCartPromotionsStep = createStep(
updateCartPromotionsStepId, updateCartPromotionsStepId,
async (data: UpdateCartPromotionStepInput, { container }) => { async (data: UpdateCartPromotionStepInput, { container }) => {
const { promo_codes = [], id, action = PromotionActions.ADD } = data const { promo_codes = [], id, action = PromotionActions.ADD } = data
const remoteLink = container.resolve(ContainerRegistrationKeys.LINK) const remoteLink = container.resolve(ContainerRegistrationKeys.LINK)
const remoteQuery = container.resolve( const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY ContainerRegistrationKeys.REMOTE_QUERY
@@ -55,29 +56,31 @@ export const updateCartPromotionsStep = createStep(
existingCartPromotionLinks.map((link) => [link.promotion_id, link]) existingCartPromotionLinks.map((link) => [link.promotion_id, link])
) )
const promotions = await promotionService.listPromotions(
{ code: promo_codes },
{ select: ["id"] }
)
const linksToCreate: any[] = [] const linksToCreate: any[] = []
const linksToDismiss: any[] = [] const linksToDismiss: any[] = []
for (const promotion of promotions) { if (promo_codes?.length) {
const linkObject = { const promotions = await promotionService.listPromotions(
[Modules.CART]: { cart_id: id }, { code: promo_codes },
[Modules.PROMOTION]: { promotion_id: promotion.id }, { select: ["id"] }
} )
if ([PromotionActions.ADD, PromotionActions.REPLACE].includes(action)) { for (const promotion of promotions) {
linksToCreate.push(linkObject) const linkObject = {
} [Modules.CART]: { cart_id: id },
[Modules.PROMOTION]: { promotion_id: promotion.id },
}
if (action === PromotionActions.REMOVE) { if ([PromotionActions.ADD, PromotionActions.REPLACE].includes(action)) {
const link = promotionLinkMap.get(promotion.id) linksToCreate.push(linkObject)
}
if (link) { if (action === PromotionActions.REMOVE) {
linksToDismiss.push(linkObject) 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( const result = taxLines.length
taxLines, ? await this.lineItemTaxLineService_.upsert(taxLines, sharedContext)
sharedContext : []
)
return await this.baseRepository_.serialize<CartTypes.LineItemTaxLineDTO[]>( return await this.baseRepository_.serialize<CartTypes.LineItemTaxLineDTO[]>(
result, result,
@@ -1201,10 +1200,12 @@ export default class CartModuleService
) )
} }
const result = await this.shippingMethodTaxLineService_.upsert( const result = taxLines.length
taxLines as UpdateShippingMethodTaxLineDTO[], ? await this.shippingMethodTaxLineService_.upsert(
sharedContext taxLines as UpdateShippingMethodTaxLineDTO[],
) sharedContext
)
: []
return await this.baseRepository_.serialize< return await this.baseRepository_.serialize<
CartTypes.ShippingMethodTaxLineDTO[] CartTypes.ShippingMethodTaxLineDTO[]