Skip to content

Commit 8ddf8b4

Browse files
authored
fix: skip promotion usage limit checks on edit flows (medusajs#14176)
* fix: skip promotion usage limit checks on edit flows * feat: add test check
1 parent 0f83538 commit 8ddf8b4

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

.changeset/nice-kids-beg.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@medusajs/promotion": patch
3+
"@medusajs/core-flows": patch
4+
"@medusajs/types": patch
5+
---
6+
7+
fix: skip promotion usage limit checks on edit flows

integration-tests/http/__tests__/exchanges/exchanges.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,16 @@ medusaIntegrationTestRunner({
14371437
adminHeaders
14381438
)
14391439

1440+
const promotionModule = getContainer().resolve(Modules.PROMOTION)
1441+
1442+
// check that adjustments are computed for promotions that exceeded usage limit (we ignore usage limits on edit flows)
1443+
// @ts-ignore
1444+
await promotionModule.updatePromotions({
1445+
id: appliedPromotion.id,
1446+
limit: 1,
1447+
used: 1,
1448+
})
1449+
14401450
let result = await api.post(
14411451
"/admin/exchanges",
14421452
{

packages/core/core-flows/src/order/workflows/compute-adjustments-for-preview.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type ComputeAdjustmentsForPreviewWorkflowInput = {
2929
/**
3030
* The order's details.
3131
*/
32-
order: OrderDTO & {
32+
order: OrderDTO & {
3333
/**
3434
* The promotions applied to the order.
3535
*/
@@ -52,7 +52,7 @@ export const computeAdjustmentsForPreviewWorkflowId =
5252
*
5353
* You can use this workflow within your customizations or your own custom workflows, allowing you to compute adjustments
5454
* in your custom flows.
55-
*
55+
*
5656
* @since v2.12.0
5757
*
5858
* @example
@@ -110,6 +110,9 @@ export const computeAdjustmentsForPreviewWorkflow = createWorkflow(
110110
const actions = getActionsToComputeFromPromotionsStep({
111111
computeActionContext: actionsToComputeItemsInput,
112112
promotionCodesToApply: orderPromotions,
113+
options: {
114+
skip_usage_limit_checks: true,
115+
},
113116
})
114117

115118
const { lineItemAdjustmentsToCreate } =

packages/core/types/src/promotion/common/compute-actions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,11 @@ export interface ComputeActionOptions {
295295
* automatically. If not provided, the automatic promotions are applied.
296296
*/
297297
prevent_auto_promotions?: boolean
298+
299+
/**
300+
* Whether to skip the usage limit checks.
301+
* Useful when recomputing adjustment for promotions that are already applied as a part of edit/exchange flows.
302+
*
303+
*/
304+
skip_usage_limit_checks?: boolean
298305
}

packages/modules/promotion/src/services/promotion-module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,10 @@ export default class PromotionModuleService
634634
options: PromotionTypes.ComputeActionOptions = {},
635635
@MedusaContext() sharedContext: Context = {}
636636
): Promise<PromotionTypes.ComputeActions[]> {
637-
const { prevent_auto_promotions: preventAutoPromotions } = options
637+
const {
638+
prevent_auto_promotions: preventAutoPromotions,
639+
skip_usage_limit_checks: skipUsageLimitChecks,
640+
} = options
638641
const computedActions: PromotionTypes.ComputeActions[] = []
639642
const { items = [], shipping_methods: shippingMethods = [] } =
640643
applicationContext
@@ -806,6 +809,7 @@ export default class PromotionModuleService
806809
} = promotion
807810

808811
if (
812+
!skipUsageLimitChecks &&
809813
promotion.campaign?.budget?.type === CampaignBudgetType.USE_BY_ATTRIBUTE
810814
) {
811815
const attribute = promotion.campaign?.budget?.attribute!
@@ -847,7 +851,7 @@ export default class PromotionModuleService
847851
}
848852

849853
// Check promotion usage limit
850-
if (typeof promotion.limit === "number") {
854+
if (!skipUsageLimitChecks && typeof promotion.limit === "number") {
851855
if ((promotion.used ?? 0) >= promotion.limit) {
852856
computedActions.push({
853857
action: ComputedActions.PROMOTION_LIMIT_EXCEEDED,

0 commit comments

Comments
 (0)