1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 ICustomerModuleService,
4 IPricingModuleService,
5 IProductModuleService,
6 IRegionModuleService,
7 } from "@medusajs/types"
8 import { Modules, PriceListStatus, PriceListType } from "@medusajs/utils"
9 import { createAdminUser } from "../../../../helpers/create-admin-user"
10 import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"
11
12 jest.setTimeout(50000)
13
14 const env = {}
15 const adminHeaders = {
16 headers: { "x-medusa-access-token": "test_token" },
17 }
18
19 medusaIntegrationTestRunner({
20 env,
21 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
22 describe("Admin: Price Lists API", () => {
23 let appContainer
24 let product
25 let variant
26 let variant2
27 let region
28 let customerGroup
29 let pricingModule: IPricingModuleService
30 let productModule: IProductModuleService
31 let customerModule: ICustomerModuleService
32 let regionModule: IRegionModuleService
33
34 beforeAll(async () => {
35 appContainer = getContainer()
36 pricingModule = appContainer.resolve(Modules.PRICING)
37 productModule = appContainer.resolve(Modules.PRODUCT)
38 customerModule = appContainer.resolve(Modules.CUSTOMER)
39 regionModule = appContainer.resolve(Modules.REGION)
40 })
41
42 beforeAll(async () => {
43 await createAdminUser(dbConnection, adminHeaders, appContainer)
44
45 customerGroup = await customerModule.createCustomerGroups({
46 name: "VIP",
47 })
48 region = await regionModule.createRegions({
49 name: "US",
50 currency_code: "USD",
51 })
52 ;[product] = await productModule.createProducts([
53 {
54 title: "test product",
55 variants: [
56 {
57 title: "test product variant",
58 },
59 {
60 title: "test product variant 2",
61 },
62 ],
63 },
64 ])
65
66 variant = product.variants[0]
67 variant2 = product.variants[1]
68
69 await dbUtils.snapshot()
70 })
71
72 describe("GET /admin/price-lists", () => {
73 it("should get all price lists and its prices with rules", async () => {
74 const priceSet = await createVariantPriceSet({
75 container: appContainer,
76 variantId: variant.id,
77 prices: [],
78 })
79
80 await pricingModule.createPriceLists([
81 {
82 title: "test price list",
83 description: "test",
84 ends_at: new Date(),
85 starts_at: new Date(),
86 status: PriceListStatus.ACTIVE,
87 type: PriceListType.OVERRIDE,
88 prices: [
89 {
90 amount: 5000,
91 currency_code: "usd",
92 price_set_id: priceSet.id,
93 rules: {
94 region_id: region.id,
95 },
96 },
97 ],
98 rules: {
99 "customer.groups.id": [customerGroup.id],
100 },
101 },
102 ])
103
104 let response = await api.get(
105 `/admin/price-lists?fields=prices.*,prices.price_rules.*,prices.price_set.*,prices.price_set.variant.*`,
106 adminHeaders
107 )
108
109 expect(response.status).toEqual(200)
110 expect(response.data.count).toEqual(1)
111 expect(response.data.price_lists).toEqual([
112 {
113 id: expect.any(String),
114 type: "override",
115 description: "test",
116 title: "test price list",
117 status: "active",
118 metadata: null,
119 starts_at: expect.any(String),
120 ends_at: expect.any(String),
121 created_at: expect.any(String),
122 updated_at: expect.any(String),
123 deleted_at: null,
124 rules: {
125 "customer.groups.id": [customerGroup.id],
126 },
127 prices: [
128 expect.objectContaining({
129 id: expect.any(String),
130 currency_code: "usd",
131 amount: 5000,
132 min_quantity: null,
133 max_quantity: null,
134 variant_id: variant.id,
135 created_at: expect.any(String),
136 updated_at: expect.any(String),
137 deleted_at: null,
138 price_set_id: expect.any(String),
139 price_list_id: expect.any(String),
140 title: null,
141 rules: {
142 region_id: region.id,
143 },
144 rules_count: 1,
145 raw_amount: expect.objectContaining({
146 value: "5000",
147 precision: 20,
148 }),
149 }),
150 ],
151 },
152 ])
153
154 response = await api.get(
155 `/admin/price-lists?fields=id,created_at,prices.amount`,
156 adminHeaders
157 )
158
159 expect(response.status).toEqual(200)
160 expect(response.data.count).toEqual(1)
161 expect(response.data.price_lists).toEqual([
162 {
163 id: expect.any(String),
164 created_at: expect.any(String),
165 prices: [
166 {
167 id: expect.any(String),
168 amount: 5000,
169 },
170 ],
171 },
172 ])
173 })
174
175 it("should support searching of price lists", async () => {
176 const priceSet = await createVariantPriceSet({
177 container: appContainer,
178 variantId: variant.id,
179 prices: [],
180 })
181
182 await pricingModule.createPriceLists([
183 {
184 title: "first price list",
185 description: "test price",
186 ends_at: new Date(),
187 starts_at: new Date(),
188 status: PriceListStatus.ACTIVE,
189 type: PriceListType.OVERRIDE,
190 prices: [
191 {
192 amount: 5000,
193 currency_code: "usd",
194 price_set_id: priceSet.id,
195 rules: {
196 region_id: region.id,
197 },
198 },
199 ],
200 rules: {
201 "customer.groups.id": [customerGroup.id],
202 },
203 },
204 {
205 title: "second price list",
206 description: "second test",
207 ends_at: new Date(),
208 starts_at: new Date(),
209 status: PriceListStatus.ACTIVE,
210 type: PriceListType.OVERRIDE,
211 prices: [
212 {
213 amount: 5000,
214 currency_code: "usd",
215 price_set_id: priceSet.id,
216 rules: {
217 region_id: region.id,
218 },
219 },
220 ],
221 rules: {
222 "customer.groups.id": [customerGroup.id],
223 },
224 },
225 ])
226
227 let response = await api.get(
228 `/admin/price-lists?q=second`,
229 adminHeaders
230 )
231
232 expect(response.status).toEqual(200)
233 expect(response.data.price_lists).toEqual([
234 expect.objectContaining({
235 title: "second price list",
236 }),
237 ])
238 })
239 })
240
241 describe("GET /admin/price-lists/:id", () => {
242 it("should retrieve a price list and its prices with rules", async () => {
243 const priceSet = await createVariantPriceSet({
244 container: appContainer,
245 variantId: variant.id,
246 prices: [],
247 })
248
249 const [priceList] = await pricingModule.createPriceLists([
250 {
251 title: "test price list",
252 description: "test",
253 ends_at: new Date(),
254 starts_at: new Date(),
255 status: PriceListStatus.ACTIVE,
256 type: PriceListType.OVERRIDE,
257 prices: [
258 {
259 amount: 5000,
260 currency_code: "usd",
261 price_set_id: priceSet.id,
262 rules: {
263 region_id: region.id,
264 },
265 },
266 ],
267 rules: {
268 "customer.groups.id": [customerGroup.id],
269 },
270 },
271 ])
272
273 let response = await api.get(
274 `/admin/price-lists/${priceList.id}?fields=prices.*,prices.price_rules.*,prices.price_set.*,prices.price_set.variant.*`,
275 adminHeaders
276 )
277
278 expect(response.status).toEqual(200)
279 expect(response.data.price_list).toEqual(
280 expect.objectContaining({
281 id: expect.any(String),
282 type: "override",
283 description: "test",
284 title: "test price list",
285 status: "active",
286 starts_at: expect.any(String),
287 ends_at: expect.any(String),
288 created_at: expect.any(String),
289 updated_at: expect.any(String),
290 deleted_at: null,
291 rules: {
292 "customer.groups.id": [customerGroup.id],
293 },
294 prices: [
295 expect.objectContaining({
296 id: expect.any(String),
297 currency_code: "usd",
298 amount: 5000,
299 min_quantity: null,
300 max_quantity: null,
301 variant_id: variant.id,
302 created_at: expect.any(String),
303 updated_at: expect.any(String),
304 price_list_id: expect.any(String),
305 price_set_id: expect.any(String),
306 deleted_at: null,
307 rules_count: 1,
308 raw_amount: expect.objectContaining({
309 value: "5000",
310 precision: 20,
311 }),
312 title: null,
313 rules: {
314 region_id: region.id,
315 },
316 }),
317 ],
318 })
319 )
320
321 response = await api.get(
322 `/admin/price-lists/${priceList.id}?fields=id,prices.id,prices.amount`,
323 adminHeaders
324 )
325
326 expect(response.data.price_list).toEqual({
327 id: expect.any(String),
328 prices: [
329 {
330 id: expect.any(String),
331 amount: 5000,
332 },
333 ],
334 })
335 })
336
337 it("should throw an error when price list is not found", async () => {
338 const error = await api
339 .get(`/admin/price-lists/does-not-exist`, adminHeaders)
340 .catch((e) => e)
341
342 expect(error.response.status).toBe(404)
343 expect(error.response.data).toEqual({
344 type: "not_found",
345 message: "Price list with id: does-not-exist was not found",
346 })
347 })
348 })
349
350 describe("POST /admin/price-lists", () => {
351 it("should create price list and prices successfully", async () => {
352 await createVariantPriceSet({
353 container: appContainer,
354 variantId: variant.id,
355 prices: [{ amount: 3000, currency_code: "usd" }],
356 })
357
358 const data = {
359 title: "test price list",
360 description: "test",
361 type: "override",
362 status: "active",
363 starts_at: new Date(),
364 rules: {
365 "customer.groups.id": [customerGroup.id],
366 },
367 prices: [
368 {
369 amount: 400,
370 variant_id: variant.id,
371 currency_code: "usd",
372 rules: { region_id: region.id },
373 },
374 ],
375 }
376
377 const response = await api.post(
378 `admin/price-lists?fields=prices.*,prices.price_rules.*,prices.price_set.*,prices.price_set.variant.*`,
379 data,
380 adminHeaders
381 )
382
383 expect(response.status).toEqual(200)
384 expect(response.data.price_list).toEqual(
385 expect.objectContaining({
386 id: expect.any(String),
387 title: "test price list",
388 description: "test",
389 type: "override",
390 status: "active",
391 starts_at: expect.any(String),
392 ends_at: null,
393 created_at: expect.any(String),
394 updated_at: expect.any(String),
395 deleted_at: null,
396 rules: {
397 "customer.groups.id": [customerGroup.id],
398 },
399 prices: [
400 expect.objectContaining({
401 id: expect.any(String),
402 currency_code: "usd",
403 amount: 400,
404 min_quantity: null,
405 max_quantity: null,
406 variant_id: variant.id,
407 created_at: expect.any(String),
408 updated_at: expect.any(String),
409 deleted_at: null,
410 price_set_id: expect.any(String),
411 rules_count: 1,
412 raw_amount: expect.objectContaining({
413 value: "400",
414 precision: 20,
415 }),
416 title: null,
417 rules: {
418 region_id: region.id,
419 },
420 }),
421 ],
422 })
423 )
424 })
425
426 it("should throw error when required attributes are not provided", async () => {
427 const data = {
428 prices: [
429 {
430 amount: 400,
431 currency_code: "usd",
432 },
433 ],
434 }
435
436 const errorResponse = await api
437 .post(`admin/price-lists`, data, adminHeaders)
438 .catch((e) => e)
439
440 expect(errorResponse.response.status).toEqual(400)
441 // TODO: reenable when this is translated
442 // expect(errorResponse.response.data.message).toEqual(
443 // "title must be a string, description must be a string, type must be one of the following values: sale, override, variant_id must be a string"
444 // )
445 })
446 })
447
448 describe("DELETE /admin/price-lists/:id", () => {
449 it("should delete price list and money amounts", async () => {
450 await createVariantPriceSet({
451 container: appContainer,
452 variantId: variant.id,
453 prices: [{ amount: 3000, currency_code: "usd" }],
454 })
455
456 const data = {
457 title: "test price list",
458 description: "test",
459 type: "override",
460 status: "active",
461 starts_at: new Date(),
462 prices: [
463 { amount: 400, variant_id: variant.id, currency_code: "usd" },
464 ],
465 }
466
467 const result = await api.post(`admin/price-lists`, data, adminHeaders)
468 const priceListId = result.data.price_list.id
469
470 let prices = await pricingModule.listPrices({
471 price_list_id: [priceListId],
472 })
473
474 expect(prices.length).toEqual(1)
475
476 const deleteRes = await api.delete(
477 `/admin/price-lists/${priceListId}`,
478 adminHeaders
479 )
480
481 expect(deleteRes.status).toEqual(200)
482
483 const afterDelete = await api
484 .get(`/admin/price-lists/${priceListId}`, adminHeaders)
485 .catch((e) => e)
486
487 expect(afterDelete.response.status).toEqual(404)
488
489 prices = await pricingModule.listPrices({
490 price_list_id: [priceListId],
491 })
492 expect(prices.length).toEqual(0)
493 })
494
495 it("should idempotently return a success even if price lists dont exist", async () => {
496 const deleteRes = await api.delete(
497 `/admin/price-lists/does-not-exist`,
498 adminHeaders
499 )
500
501 expect(deleteRes.status).toEqual(200)
502 expect(deleteRes.data).toEqual({
503 id: "does-not-exist",
504 object: "price_list",
505 deleted: true,
506 })
507 })
508 })
509
510 describe("POST /admin/price-lists/:id", () => {
511 it("should throw error when trying to update a price list that does not exist", async () => {
512 const updateRes = await api
513 .post(
514 `admin/price-lists/does-not-exist`,
515 { title: "new price list name" },
516 adminHeaders
517 )
518 .catch((e) => e)
519
520 expect(updateRes.response.status).toEqual(404)
521 expect(updateRes.response.data.message).toEqual(
522 "Price lists with id: does-not-exist was not found"
523 )
524 })
525
526 it("should update price lists", async () => {
527 await createVariantPriceSet({
528 container: appContainer,
529 variantId: variant.id,
530 prices: [{ amount: 3000, currency_code: "usd" }],
531 })
532
533 const [priceList] = await pricingModule.createPriceLists([
534 {
535 title: "test price list",
536 description: "test",
537 ends_at: new Date(),
538 starts_at: new Date(),
539 status: PriceListStatus.ACTIVE,
540 type: PriceListType.OVERRIDE,
541 },
542 ])
543
544 const data = {
545 title: "new price list name",
546 description: "new price list description",
547 rules: {
548 "customer.groups.id": [customerGroup.id],
549 },
550 }
551
552 let response = await api.post(
553 `admin/price-lists/${priceList.id}`,
554 data,
555 adminHeaders
556 )
557
558 expect(response.status).toEqual(200)
559 expect(response.data.price_list).toEqual(
560 expect.objectContaining({
561 id: expect.any(String),
562 title: "new price list name",
563 description: "new price list description",
564 rules: {
565 "customer.groups.id": [customerGroup.id],
566 },
567 })
568 )
569 })
570 })
571
572 describe("POST /admin/price-lists/:id/prices/batch", () => {
573 // TODO: This is flaky, investigate why
574 it.skip("should add, remove and delete price list prices in batch successfully", async () => {
575 const priceSet = await createVariantPriceSet({
576 container: appContainer,
577 variantId: variant.id,
578 prices: [{ amount: 3000, currency_code: "usd" }],
579 })
580
581 const [createdPriceList] = await pricingModule.createPriceLists([
582 {
583 title: "test price list",
584 description: "test",
585 prices: [
586 {
587 id: "price-to-remove",
588 amount: 5000,
589 currency_code: "usd",
590 price_set_id: priceSet.id,
591 rules: {
592 region_id: region.id,
593 },
594 },
595 {
596 id: "price-to-update",
597 amount: 5000,
598 currency_code: "usd",
599 price_set_id: priceSet.id,
600 rules: { region_id: region.id },
601 },
602 ],
603 },
604 ])
605
606 const [priceList] = await pricingModule.listPriceLists(
607 { id: [createdPriceList.id] },
608 { relations: ["prices"] }
609 )
610
611 const priceIdToDelete = priceList.prices?.find(
612 (p) => p.id === "price-to-remove"
613 )
614
615 const data = {
616 create: [
617 {
618 amount: 400,
619 variant_id: variant.id,
620 currency_code: "usd",
621 rules: { region_id: region.id },
622 },
623 ],
624 update: [
625 {
626 id: "price-to-update",
627 amount: 500,
628 variant_id: variant.id,
629 currency_code: "usd",
630 rules: { region_id: region.id },
631 },
632 ],
633 delete: [priceIdToDelete?.id],
634 }
635
636 const response = await api.post(
637 `admin/price-lists/${priceList.id}/prices/batch`,
638 data,
639 adminHeaders
640 )
641
642 expect(response.status).toEqual(200)
643 expect(response.data).toEqual({
644 created: [
645 expect.objectContaining({
646 id: expect.any(String),
647 currency_code: "usd",
648 amount: 400,
649 }),
650 ],
651 updated: [
652 expect.objectContaining({
653 id: "price-to-update",
654 currency_code: "usd",
655 amount: 500,
656 }),
657 ],
658 deleted: {
659 ids: ["price-to-remove"],
660 object: "price",
661 deleted: true,
662 },
663 })
664 })
665
666 it("should remove all price list prices of a product", async () => {
667 const priceSet = await createVariantPriceSet({
668 container: appContainer,
669 variantId: variant.id,
670 prices: [{ amount: 3000, currency_code: "usd" }],
671 })
672
673 const priceSet2 = await createVariantPriceSet({
674 container: appContainer,
675 variantId: variant2.id,
676 prices: [{ amount: 3000, currency_code: "usd" }],
677 })
678
679 const [createdPriceList] = await pricingModule.createPriceLists([
680 {
681 title: "test price list",
682 description: "test",
683 prices: [
684 {
685 id: "price-to-delete-1",
686 amount: 5000,
687 currency_code: "usd",
688 price_set_id: priceSet.id,
689 rules: {
690 region_id: region.id,
691 },
692 },
693 {
694 id: "price-to-delete-2",
695 amount: 5000,
696 currency_code: "usd",
697 price_set_id: priceSet2.id,
698 rules: { region_id: region.id },
699 },
700 ],
701 },
702 ])
703
704 const [priceList] = await pricingModule.listPriceLists(
705 { id: [createdPriceList.id] },
706 { relations: ["prices"] }
707 )
708
709 const data = { remove: [product.id] }
710 const response = await api.post(
711 `admin/price-lists/${priceList.id}/products`,
712 data,
713 adminHeaders
714 )
715
716 expect(response.status).toEqual(200)
717 expect(response.data.price_list).toEqual(
718 expect.objectContaining({
719 id: expect.any(String),
720 title: "test price list",
721 description: "test",
722 })
723 )
724 })
725 })
726 })
727 },
728 })