|
1
|
import { updateCartPromotionsWorkflow } from "@medusajs/core-flows" |
|
2
|
import { StepResponse } from "@medusajs/framework/workflows-sdk" |
|
3
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { ICartModuleService, IPromotionModuleService } from "@medusajs/types" |
|
5
|
import { |
|
6
|
Modules, |
|
7
|
PromotionActions, |
|
8
|
PromotionStatus, |
|
9
|
PromotionType, |
|
10
|
} from "@medusajs/utils" |
|
11
|
import { |
|
12
|
adminHeaders, |
|
13
|
createAdminUser, |
|
14
|
generatePublishableKey, |
|
15
|
generateStoreHeaders, |
|
16
|
} from "../../../../helpers/create-admin-user" |
|
17
|
|
|
18
|
jest.setTimeout(50000) |
|
19
|
|
|
20
|
const env = {} |
|
21
|
|
|
22
|
medusaIntegrationTestRunner({ |
|
23
|
env, |
|
24
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
25
|
describe("updateCartPromotionsWorkflow: setPromotionContext hook", () => { |
|
26
|
let appContainer |
|
27
|
let cartModuleService: ICartModuleService |
|
28
|
let promotionModuleService: IPromotionModuleService |
|
29
|
let storeHeaders |
|
30
|
|
|
31
|
// Toggled per test so we can register the hook once globally and |
|
32
|
// opt into different behaviour per case without re-registering. |
|
33
|
let setPromotionContextHook: |
|
34
|
| ((input: any) => StepResponse<Record<string, unknown> | undefined>) |
|
35
|
| undefined |
|
36
|
|
|
37
|
beforeAll(async () => { |
|
38
|
appContainer = getContainer() |
|
39
|
cartModuleService = appContainer.resolve(Modules.CART) |
|
40
|
promotionModuleService = appContainer.resolve(Modules.PROMOTION) |
|
41
|
|
|
42
|
updateCartPromotionsWorkflow.hooks.setPromotionContext( |
|
43
|
(input) => { |
|
44
|
if (setPromotionContextHook) { |
|
45
|
return setPromotionContextHook(input) |
|
46
|
} |
|
47
|
}, |
|
48
|
() => {} |
|
49
|
) |
|
50
|
}) |
|
51
|
|
|
52
|
beforeAll(async () => { |
|
53
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
54
|
const publishableKey = await generatePublishableKey(appContainer) |
|
55
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
56
|
|
|
57
|
await dbUtils.snapshot() |
|
58
|
}) |
|
59
|
|
|
60
|
afterEach(() => { |
|
61
|
setPromotionContextHook = undefined |
|
62
|
}) |
|
63
|
|
|
64
|
it("should expose the cart, action and promo_codes to the hook", async () => { |
|
65
|
const promotion = await promotionModuleService.createPromotions({ |
|
66
|
code: "HOOK_INPUT_SHAPE", |
|
67
|
type: PromotionType.STANDARD, |
|
68
|
status: PromotionStatus.ACTIVE, |
|
69
|
application_method: { |
|
70
|
type: "fixed", |
|
71
|
target_type: "items", |
|
72
|
allocation: "across", |
|
73
|
value: 100, |
|
74
|
apply_to_quantity: 1, |
|
75
|
currency_code: "usd", |
|
76
|
}, |
|
77
|
}) |
|
78
|
|
|
79
|
const cart = await cartModuleService.createCarts({ |
|
80
|
currency_code: "usd", |
|
81
|
items: [ |
|
82
|
{ |
|
83
|
unit_price: 2000, |
|
84
|
quantity: 1, |
|
85
|
title: "Test item", |
|
86
|
} as any, |
|
87
|
], |
|
88
|
}) |
|
89
|
|
|
90
|
let receivedInput: any |
|
91
|
setPromotionContextHook = (input) => { |
|
92
|
receivedInput = input |
|
93
|
return new StepResponse(undefined) |
|
94
|
} |
|
95
|
|
|
96
|
const response = await api.post( |
|
97
|
`/store/carts/${cart.id}/promotions`, |
|
98
|
{ promo_codes: [promotion.code] }, |
|
99
|
storeHeaders |
|
100
|
) |
|
101
|
|
|
102
|
expect(response.status).toEqual(200) |
|
103
|
expect(receivedInput).toEqual( |
|
104
|
expect.objectContaining({ |
|
105
|
cart: expect.objectContaining({ id: cart.id }), |
|
106
|
action: PromotionActions.ADD, |
|
107
|
promo_codes: [promotion.code], |
|
108
|
}) |
|
109
|
) |
|
110
|
}) |
|
111
|
|
|
112
|
it("should apply a promotion whose rule depends on a custom context attribute supplied by the hook", async () => { |
|
113
|
const companyId = "comp_acme" |
|
114
|
const promotion = await promotionModuleService.createPromotions({ |
|
115
|
code: "B2B_DISCOUNT", |
|
116
|
type: PromotionType.STANDARD, |
|
117
|
status: PromotionStatus.ACTIVE, |
|
118
|
rules: [ |
|
119
|
{ |
|
120
|
attribute: "company_id", |
|
121
|
operator: "eq", |
|
122
|
values: [companyId], |
|
123
|
}, |
|
124
|
], |
|
125
|
application_method: { |
|
126
|
type: "fixed", |
|
127
|
target_type: "items", |
|
128
|
allocation: "across", |
|
129
|
value: 500, |
|
130
|
apply_to_quantity: 1, |
|
131
|
currency_code: "usd", |
|
132
|
}, |
|
133
|
}) |
|
134
|
|
|
135
|
const cart = await cartModuleService.createCarts({ |
|
136
|
currency_code: "usd", |
|
137
|
items: [ |
|
138
|
{ |
|
139
|
unit_price: 2000, |
|
140
|
quantity: 1, |
|
141
|
title: "Test item", |
|
142
|
} as any, |
|
143
|
], |
|
144
|
}) |
|
145
|
|
|
146
|
setPromotionContextHook = () => |
|
147
|
new StepResponse({ company_id: companyId }) |
|
148
|
|
|
149
|
const response = await api.post( |
|
150
|
`/store/carts/${cart.id}/promotions`, |
|
151
|
{ promo_codes: [promotion.code] }, |
|
152
|
storeHeaders |
|
153
|
) |
|
154
|
|
|
155
|
expect(response.status).toEqual(200) |
|
156
|
expect(response.data.cart.items[0].adjustments).toEqual( |
|
157
|
expect.arrayContaining([ |
|
158
|
expect.objectContaining({ |
|
159
|
code: promotion.code, |
|
160
|
amount: 500, |
|
161
|
}), |
|
162
|
]) |
|
163
|
) |
|
164
|
}) |
|
165
|
|
|
166
|
it("should not apply the promotion when the hook does not supply the attribute its rule depends on", async () => { |
|
167
|
const promotion = await promotionModuleService.createPromotions({ |
|
168
|
code: "B2B_DISCOUNT_UNMATCHED", |
|
169
|
type: PromotionType.STANDARD, |
|
170
|
status: PromotionStatus.ACTIVE, |
|
171
|
rules: [ |
|
172
|
{ |
|
173
|
attribute: "company_id", |
|
174
|
operator: "eq", |
|
175
|
values: ["comp_acme"], |
|
176
|
}, |
|
177
|
], |
|
178
|
application_method: { |
|
179
|
type: "fixed", |
|
180
|
target_type: "items", |
|
181
|
allocation: "across", |
|
182
|
value: 500, |
|
183
|
apply_to_quantity: 1, |
|
184
|
currency_code: "usd", |
|
185
|
}, |
|
186
|
}) |
|
187
|
|
|
188
|
const cart = await cartModuleService.createCarts({ |
|
189
|
currency_code: "usd", |
|
190
|
items: [ |
|
191
|
{ |
|
192
|
unit_price: 2000, |
|
193
|
quantity: 1, |
|
194
|
title: "Test item", |
|
195
|
} as any, |
|
196
|
], |
|
197
|
}) |
|
198
|
|
|
199
|
// No hook is registered for this case — the cart context lacks |
|
200
|
// `company_id`, so the rule cannot be satisfied and the promotion |
|
201
|
// is not applied. |
|
202
|
const response = await api.post( |
|
203
|
`/store/carts/${cart.id}/promotions`, |
|
204
|
{ promo_codes: [promotion.code] }, |
|
205
|
storeHeaders |
|
206
|
) |
|
207
|
|
|
208
|
expect(response.status).toEqual(200) |
|
209
|
expect(response.data.cart.items[0].adjustments).toEqual([]) |
|
210
|
}) |
|
211
|
|
|
212
|
it("should resolve nested attributes from the hook context (dot-path)", async () => { |
|
213
|
const promotion = await promotionModuleService.createPromotions({ |
|
214
|
code: "B2B_TIER_DISCOUNT", |
|
215
|
type: PromotionType.STANDARD, |
|
216
|
status: PromotionStatus.ACTIVE, |
|
217
|
rules: [ |
|
218
|
{ |
|
219
|
attribute: "company.tier", |
|
220
|
operator: "eq", |
|
221
|
values: ["gold"], |
|
222
|
}, |
|
223
|
], |
|
224
|
application_method: { |
|
225
|
type: "fixed", |
|
226
|
target_type: "items", |
|
227
|
allocation: "across", |
|
228
|
value: 700, |
|
229
|
apply_to_quantity: 1, |
|
230
|
currency_code: "usd", |
|
231
|
}, |
|
232
|
}) |
|
233
|
|
|
234
|
const cart = await cartModuleService.createCarts({ |
|
235
|
currency_code: "usd", |
|
236
|
items: [ |
|
237
|
{ |
|
238
|
unit_price: 2000, |
|
239
|
quantity: 1, |
|
240
|
title: "Test item", |
|
241
|
} as any, |
|
242
|
], |
|
243
|
}) |
|
244
|
|
|
245
|
setPromotionContextHook = () => |
|
246
|
new StepResponse({ |
|
247
|
company: { id: "comp_acme", tier: "gold" }, |
|
248
|
}) |
|
249
|
|
|
250
|
const response = await api.post( |
|
251
|
`/store/carts/${cart.id}/promotions`, |
|
252
|
{ promo_codes: [promotion.code] }, |
|
253
|
storeHeaders |
|
254
|
) |
|
255
|
|
|
256
|
expect(response.status).toEqual(200) |
|
257
|
expect(response.data.cart.items[0].adjustments).toEqual( |
|
258
|
expect.arrayContaining([ |
|
259
|
expect.objectContaining({ |
|
260
|
code: promotion.code, |
|
261
|
amount: 700, |
|
262
|
}), |
|
263
|
]) |
|
264
|
) |
|
265
|
}) |
|
266
|
|
|
267
|
it("should not apply the promotion when the hook supplies the attribute but with a non-matching value", async () => { |
|
268
|
const promotion = await promotionModuleService.createPromotions({ |
|
269
|
code: "B2B_DISCOUNT_WRONG_VALUE", |
|
270
|
type: PromotionType.STANDARD, |
|
271
|
status: PromotionStatus.ACTIVE, |
|
272
|
rules: [ |
|
273
|
{ |
|
274
|
attribute: "company_id", |
|
275
|
operator: "eq", |
|
276
|
values: ["comp_acme"], |
|
277
|
}, |
|
278
|
], |
|
279
|
application_method: { |
|
280
|
type: "fixed", |
|
281
|
target_type: "items", |
|
282
|
allocation: "across", |
|
283
|
value: 500, |
|
284
|
apply_to_quantity: 1, |
|
285
|
currency_code: "usd", |
|
286
|
}, |
|
287
|
}) |
|
288
|
|
|
289
|
const cart = await cartModuleService.createCarts({ |
|
290
|
currency_code: "usd", |
|
291
|
items: [ |
|
292
|
{ |
|
293
|
unit_price: 2000, |
|
294
|
quantity: 1, |
|
295
|
title: "Test item", |
|
296
|
} as any, |
|
297
|
], |
|
298
|
}) |
|
299
|
|
|
300
|
// Hook supplies company_id but with a value that does not satisfy the rule. |
|
301
|
setPromotionContextHook = () => |
|
302
|
new StepResponse({ company_id: "comp_other" }) |
|
303
|
|
|
304
|
const response = await api.post( |
|
305
|
`/store/carts/${cart.id}/promotions`, |
|
306
|
{ promo_codes: [promotion.code] }, |
|
307
|
storeHeaders |
|
308
|
) |
|
309
|
|
|
310
|
expect(response.status).toEqual(200) |
|
311
|
expect(response.data.cart.items[0].adjustments).toEqual([]) |
|
312
|
}) |
|
313
|
|
|
314
|
it("should use the hook context value over the existing cart attribute when keys conflict", async () => { |
|
315
|
const promotion = await promotionModuleService.createPromotions({ |
|
316
|
code: "EMAIL_DISCOUNT_OVERRIDE", |
|
317
|
type: PromotionType.STANDARD, |
|
318
|
status: PromotionStatus.ACTIVE, |
|
319
|
rules: [ |
|
320
|
{ |
|
321
|
attribute: "email", |
|
322
|
operator: "eq", |
|
323
|
values: ["cart@example.com"], |
|
324
|
}, |
|
325
|
], |
|
326
|
application_method: { |
|
327
|
type: "fixed", |
|
328
|
target_type: "items", |
|
329
|
allocation: "across", |
|
330
|
value: 300, |
|
331
|
apply_to_quantity: 1, |
|
332
|
currency_code: "usd", |
|
333
|
}, |
|
334
|
}) |
|
335
|
|
|
336
|
const cart = await cartModuleService.createCarts({ |
|
337
|
currency_code: "usd", |
|
338
|
email: "cart@example.com", |
|
339
|
items: [ |
|
340
|
{ |
|
341
|
unit_price: 2000, |
|
342
|
quantity: 1, |
|
343
|
title: "Test item", |
|
344
|
} as any, |
|
345
|
], |
|
346
|
}) |
|
347
|
|
|
348
|
setPromotionContextHook = () => |
|
349
|
new StepResponse({ email: "hook@example.com" }) |
|
350
|
|
|
351
|
const response = await api.post( |
|
352
|
`/store/carts/${cart.id}/promotions`, |
|
353
|
{ promo_codes: [promotion.code] }, |
|
354
|
storeHeaders |
|
355
|
) |
|
356
|
|
|
357
|
expect(response.status).toEqual(200) |
|
358
|
expect(response.data.cart.items[0].adjustments).toEqual([]) |
|
359
|
}) |
|
360
|
|
|
361
|
it("should reject a non-object response from the hook", async () => { |
|
362
|
const cart = await cartModuleService.createCarts({ |
|
363
|
currency_code: "usd", |
|
364
|
items: [ |
|
365
|
{ |
|
366
|
unit_price: 2000, |
|
367
|
quantity: 1, |
|
368
|
title: "Test item", |
|
369
|
} as any, |
|
370
|
], |
|
371
|
}) |
|
372
|
|
|
373
|
setPromotionContextHook = () => |
|
374
|
new StepResponse([1] as unknown as Record<string, unknown>) |
|
375
|
|
|
376
|
const { errors } = await updateCartPromotionsWorkflow(appContainer).run( |
|
377
|
{ |
|
378
|
throwOnError: false, |
|
379
|
input: { |
|
380
|
cart_id: cart.id, |
|
381
|
promo_codes: [], |
|
382
|
action: PromotionActions.ADD, |
|
383
|
}, |
|
384
|
} |
|
385
|
) |
|
386
|
|
|
387
|
expect(errors).toHaveLength(1) |
|
388
|
expect(errors[0]).toEqual( |
|
389
|
expect.objectContaining({ |
|
390
|
action: "get-setPromotionContext-result", |
|
391
|
handlerType: "invoke", |
|
392
|
error: expect.objectContaining({ |
|
393
|
issues: [ |
|
394
|
{ |
|
395
|
code: "invalid_type", |
|
396
|
expected: "record", |
|
397
|
message: "Invalid input: expected record, received array", |
|
398
|
path: [], |
|
399
|
}, |
|
400
|
], |
|
401
|
}), |
|
402
|
}) |
|
403
|
) |
|
404
|
}) |
|
405
|
}) |
|
406
|
}, |
|
407
|
}) |