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:
1 parent
54a6ef91ac
commit
caf83cf78c
16 files changed
+107
-37
No files matched your search
@@ -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(
|
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)
|
||||||
|
|||||||
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -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"
|
||||||
|
|
||||||
|
|||||||
@@ -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[]
|
||||||
|
|||||||
Reference in new issue
Block a user