feat(core-flows,dashboard,js-sdk,promotion,medusa,types,utils): limit promotion usage per customer (#13451)

**What**
- implement promotion usage limits per customer/email
- fix registering spend usage over the limit
- fix type errors in promotion module tests

**How**
- introduce a new type of campaign budget that can be defined by an attribute such as customer id or email
- add `CampaignBudgetUsage` entity to keep track of the number of uses per attribute value
- update `registerUsage` and `computeActions` in the promotion module to work with the new type
- update `core-flows` to pass context needed for usage calculation to the promotion module

**Breaking**
- registering promotion usage now throws (and cart complete fails) if the budget limit is exceeded or if the cart completion would result in a breached limit

---

CLOSES CORE-1172
CLOSES CORE-1173
CLOSES CORE-1174
CLOSES CORE-1175


Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Frane Polić
2025-10-09 14:35:54 +02:00
committed by GitHub
parent 924564bee5
commit 7dc3b0c5ff
36 changed files with 2390 additions and 190 deletions

View File

@@ -36,6 +36,7 @@ const CreateCampaignBudget = z
type: z.nativeEnum(CampaignBudgetType),
limit: z.number().nullish(),
currency_code: z.string().nullish(),
attribute: z.string().nullish(),
})
.strict()
.refine(
@@ -54,6 +55,18 @@ const CreateCampaignBudget = z
message: `currency_code should not be present when budget type is ${CampaignBudgetType.USAGE}`,
}
)
.refine(
(data) =>
isPresent(data.attribute) ||
![
CampaignBudgetType.USE_BY_ATTRIBUTE,
CampaignBudgetType.SPEND_BY_ATTRIBUTE,
].includes(data.type),
(data) => ({
path: ["attribute"],
message: `campaign budget attribute is required when budget type is ${data.type}`,
})
)
export const UpdateCampaignBudget = z
.object({