1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6 import {
7 getPricelistFixture,
8 getProductFixture,
9 } from "../../../../helpers/fixtures"
10
11 jest.setTimeout(30000)
12
13 medusaIntegrationTestRunner({
14 env: {},
15 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
16 let pricelist1
17 let pricelist1Prices
18 let pricelist2
19 let region1
20 let product1
21 let customerGroup1
22 let shippingProfile
23
24 beforeAll(async () => {
25 const container = getContainer()
26 await createAdminUser(dbConnection, adminHeaders, container)
27
28 region1 = (
29 await api.post(
30 "/admin/regions",
31 {
32 name: "Test Region",
33 currency_code: "usd",
34 },
35 adminHeaders
36 )
37 ).data.region
38
39 shippingProfile = (
40 await api.post(
41 `/admin/shipping-profiles`,
42 { name: "Test", type: "default" },
43 adminHeaders
44 )
45 ).data.shipping_profile
46
47 product1 = (
48 await api.post(
49 "/admin/products",
50 getProductFixture({
51 title: "Test product",
52 shipping_profile_id: shippingProfile.id,
53 }),
54 adminHeaders
55 )
56 ).data.product
57
58 customerGroup1 = (
59 await api.post(
60 "/admin/customer-groups",
61 {
62 name: "vip-customers",
63 metadata: {
64 data1: "value1",
65 },
66 },
67 adminHeaders
68 )
69 ).data.customer_group
70
71 pricelist1 = (
72 await api.post(
73 "/admin/price-lists",
74 getPricelistFixture({
75 title: "New sale",
76 prices: [
77 {
78 amount: 100,
79 currency_code: "usd",
80 variant_id: product1.variants[0].id,
81 min_quantity: 1,
82 max_quantity: 100,
83 },
84 {
85 amount: 80,
86 currency_code: "usd",
87 variant_id: product1.variants[0].id,
88 min_quantity: 101,
89 max_quantity: 500,
90 },
91 ],
92 }),
93 adminHeaders
94 )
95 ).data.price_list
96
97 pricelist1Prices = await api
98 .get(`/admin/price-lists/${pricelist1.id}/prices`, adminHeaders)
99 .then((res) => res.data.prices)
100
101 pricelist2 = (
102 await api.post(
103 "/admin/price-lists",
104
105 getPricelistFixture({
106 title: "Another sale",
107 description: "50% off on all items",
108 }),
109 adminHeaders
110 )
111 ).data.price_list
112
113 await dbUtils.snapshot()
114 })
115
116 describe("/admin/price-lists", () => {
117 describe("POST /admin/price-list", () => {
118 it("creates a price list", async () => {
119 const payload = getPricelistFixture({
120 title: "VIP Summer sale",
121 description:
122 "Summer sale for VIP customers. 25% off selected items.",
123 rules: {
124 "customer.groups.id": [customerGroup1.id],
125 },
126 prices: [
127 {
128 amount: 85,
129 currency_code: "usd",
130 variant_id: product1.variants[0].id,
131 },
132 {
133 amount: 105,
134 currency_code: region1.currency_code,
135 rules: {
136 region_id: region1.id,
137 },
138 variant_id: product1.variants[0].id,
139 },
140 ],
141 })
142
143 const response = await api.post(
144 "/admin/price-lists?fields=prices.*,prices.price_set.*,prices.price_set.variant.*",
145 payload,
146 adminHeaders
147 )
148
149 expect(response.status).toEqual(200)
150 expect(response.data.price_list).toEqual(
151 expect.objectContaining({
152 id: expect.any(String),
153 title: "VIP Summer sale",
154 description:
155 "Summer sale for VIP customers. 25% off selected items.",
156 type: "sale",
157 status: "active",
158 starts_at: "2022-07-01T00:00:00.000Z",
159 ends_at: "2022-07-31T00:00:00.000Z",
160 rules: {
161 "customer.groups.id": [expect.stringContaining("cusgroup_")],
162 },
163 prices: [
164 expect.objectContaining({
165 id: expect.any(String),
166 amount: 85,
167 currency_code: "usd",
168 variant_id: product1.variants[0].id,
169 }),
170 expect.objectContaining({
171 id: expect.any(String),
172 amount: 105,
173 currency_code: region1.currency_code,
174 variant_id: product1.variants[0].id,
175 }),
176 ],
177 })
178 )
179 })
180 })
181
182 describe("GET /admin/price-lists", () => {
183 it("returns a price list by :id", async () => {
184 const response = await api.get(
185 `/admin/price-lists/${pricelist1.id}`,
186 adminHeaders
187 )
188
189 expect(response.status).toEqual(200)
190 expect(response.data.price_list).toEqual(
191 expect.objectContaining({
192 id: expect.any(String),
193 title: pricelist1.title,
194 description: pricelist1.description,
195 type: pricelist1.type,
196 status: pricelist1.status,
197 starts_at: pricelist1.starts_at,
198 ends_at: pricelist1.ends_at,
199 created_at: expect.any(String),
200 updated_at: expect.any(String),
201 })
202 )
203 // BREAKING: Prices are not returned as part of the price list if not specified in fields
204 expect(response.data.price_list).not.toHaveProperty("prices")
205 })
206
207 it("returns a list of price lists", async () => {
208 const response = await api.get("/admin/price-lists", adminHeaders)
209
210 expect(response.status).toEqual(200)
211 expect(response.data.price_lists).toHaveLength(2)
212 expect(response.data.price_lists).toEqual(
213 expect.arrayContaining([
214 expect.objectContaining({
215 id: pricelist1.id,
216 }),
217 expect.objectContaining({
218 id: pricelist2.id,
219 }),
220 ])
221 )
222 })
223
224 it("given a search query, returns matching results by name", async () => {
225 const response = await api.get(
226 "/admin/price-lists?q=new",
227 adminHeaders
228 )
229
230 expect(response.status).toEqual(200)
231 expect(response.data.price_lists).toEqual([
232 expect.objectContaining({
233 title: pricelist1.title,
234 }),
235 ])
236 expect(response.data.count).toEqual(1)
237 })
238
239 it("given a search query, returns matching results by description", async () => {
240 const response = await api.get(
241 "/admin/price-lists?q=50%",
242 adminHeaders
243 )
244
245 expect(response.status).toEqual(200)
246 expect(response.data.price_lists).toEqual([
247 expect.objectContaining({
248 id: pricelist2.id,
249 }),
250 ])
251 expect(response.data.count).toEqual(1)
252 })
253
254 it("given a search query, returns empty list when does not exist", async () => {
255 const response = await api.get(
256 "/admin/price-lists?q=blablabla",
257 adminHeaders
258 )
259
260 expect(response.status).toEqual(200)
261 expect(response.data.price_lists).toEqual([])
262 expect(response.data.count).toEqual(0)
263 })
264
265 it("given a search query and a status filter not matching any price list, returns an empty set", async () => {
266 const response = await api.get(
267 "/admin/price-lists?q=new&status[]=draft",
268 adminHeaders
269 )
270
271 expect(response.status).toEqual(200)
272 expect(response.data.price_lists).toEqual([])
273 expect(response.data.count).toEqual(0)
274 })
275
276 it("given a search query and a status filter matching a price list, returns a price list", async () => {
277 const response = await api.get(
278 "/admin/price-lists?q=new&status[]=active",
279 adminHeaders
280 )
281
282 expect(response.status).toEqual(200)
283 expect(response.data.price_lists).toEqual(
284 expect.arrayContaining([
285 expect.objectContaining({
286 title: "New sale",
287 status: "active",
288 }),
289 ])
290 )
291 expect(response.data.count).toEqual(1)
292 })
293
294 it.skip("lists only price lists with customer_group", async () => {
295 // BREAKING: You can no longer filter by customer groups
296 const response = await api.get(
297 `/admin/price-lists?customer_groups[]=customer-group-1,customer-group-2`,
298 adminHeaders
299 )
300
301 expect(response.status).toEqual(200)
302 expect(response.data.price_lists.length).toEqual(2)
303 expect(response.data.price_lists).toHaveLength(2)
304 expect(response.data.price_lists).toEqual(
305 expect.arrayContaining([
306 expect.objectContaining({ id: "test-list-cgroup-1" }),
307 expect.objectContaining({ id: "test-list-cgroup-2" }),
308 ])
309 )
310 })
311 })
312
313 describe("POST /admin/price-lists/:id", () => {
314 it("removes configuration with update", async () => {
315 const updateResult = await api.post(
316 `/admin/price-lists/${pricelist1.id}`,
317 { ends_at: null, starts_at: null, rules: {} },
318 adminHeaders
319 )
320 expect(updateResult.status).toEqual(200)
321 expect(updateResult.data.price_list.starts_at).toBeFalsy()
322 expect(updateResult.data.price_list.ends_at).toBeFalsy()
323 expect(updateResult.data.price_list.rules).toEqual({})
324 })
325
326 it("updates a price list", async () => {
327 const payload = {
328 title: "Loyalty Reward - Winter Sale",
329 description: "Winter sale for our most loyal customers",
330 type: "sale",
331 status: "draft",
332 starts_at: "2022-09-01T00:00:00.000Z",
333 ends_at: "2022-12-31T00:00:00.000Z",
334 rules: {
335 "customer.groups.id": [customerGroup1.id],
336 },
337 }
338
339 const response = await api.post(
340 `/admin/price-lists/${pricelist1.id}`,
341 payload,
342 adminHeaders
343 )
344
345 expect(response.status).toEqual(200)
346 expect(response.data.price_list).toEqual(
347 expect.objectContaining({
348 id: pricelist1.id,
349 title: "Loyalty Reward - Winter Sale",
350 description: "Winter sale for our most loyal customers",
351 type: "sale",
352 status: "draft",
353 starts_at: "2022-09-01T00:00:00.000Z",
354 ends_at: "2022-12-31T00:00:00.000Z",
355 rules: {
356 "customer.groups.id": [customerGroup1.id],
357 },
358 created_at: expect.any(String),
359 updated_at: expect.any(String),
360 })
361 )
362 })
363
364 it("updates the amount and currency of a price in the price list", async () => {
365 const priceToUpdate = pricelist1Prices.find((p) => p.amount === 80)
366 const payload = {
367 // BREAKING: Updates of prices happen through the batch endpoint, and doing it through the price list update endpoint is no longer supported
368 update: [
369 {
370 id: priceToUpdate.id,
371 amount: 250,
372 currency_code: "eur",
373 variant_id: product1.variants[0].id,
374 },
375 ],
376 }
377
378 await api.post(
379 `/admin/price-lists/${pricelist1.id}/prices/batch`,
380 payload,
381 adminHeaders
382 )
383 const response = await api.get(
384 `/admin/price-lists/${pricelist1.id}/prices`,
385 adminHeaders
386 )
387
388 expect(response.status).toEqual(200)
389 expect(response.data.prices).toEqual(
390 expect.arrayContaining([
391 expect.objectContaining({
392 amount: 100,
393 currency_code: "usd",
394 id: expect.any(String),
395 max_quantity: 100,
396 min_quantity: 1,
397 }),
398 expect.objectContaining({
399 amount: 250,
400 currency_code: "eur",
401 id: expect.any(String),
402 max_quantity: 500,
403 min_quantity: 101,
404 }),
405 ])
406 )
407 })
408 })
409
410 describe("POST /admin/price-lists/:id/prices/batch", () => {
411 it("Adds a batch of new prices to a price list without overriding existing prices", async () => {
412 // BREAKING: The payload of the batch request changed
413 const payload = {
414 create: [
415 {
416 amount: 45,
417 currency_code: "usd",
418 variant_id: product1.variants[0].id,
419 min_quantity: 1001,
420 max_quantity: 2000,
421 },
422 {
423 amount: 35,
424 currency_code: "usd",
425 variant_id: product1.variants[0].id,
426 min_quantity: 2001,
427 max_quantity: 3000,
428 },
429 {
430 amount: 25,
431 currency_code: "usd",
432 variant_id: product1.variants[0].id,
433 min_quantity: 3001,
434 max_quantity: 4000,
435 },
436 ],
437 }
438
439 await api.post(
440 `/admin/price-lists/${pricelist1.id}/prices/batch`,
441 payload,
442 adminHeaders
443 )
444 const response = await api.get(
445 `/admin/price-lists/${pricelist1.id}/prices`,
446 adminHeaders
447 )
448
449 expect(response.status).toEqual(200)
450 expect(response.data.prices.length).toEqual(5)
451 expect(response.data.prices).toEqual(
452 expect.arrayContaining([
453 expect.objectContaining({
454 id: expect.any(String),
455 amount: 100,
456 currency_code: "usd",
457 min_quantity: 1,
458 max_quantity: 100,
459 price_set: expect.objectContaining({
460 id: expect.any(String),
461 variant: {
462 id: product1.variants[0].id,
463 },
464 }),
465 }),
466 expect.objectContaining({
467 id: expect.any(String),
468 amount: 80,
469 currency_code: "usd",
470 min_quantity: 101,
471 max_quantity: 500,
472 price_set: expect.objectContaining({
473 id: expect.any(String),
474 variant: {
475 id: product1.variants[0].id,
476 },
477 }),
478 }),
479 expect.objectContaining({
480 id: expect.any(String),
481 amount: 45,
482 currency_code: "usd",
483 price_set: expect.objectContaining({
484 id: expect.any(String),
485 variant: {
486 id: product1.variants[0].id,
487 },
488 }),
489 min_quantity: 1001,
490 max_quantity: 2000,
491 }),
492 expect.objectContaining({
493 id: expect.any(String),
494 amount: 35,
495 currency_code: "usd",
496 price_set: expect.objectContaining({
497 id: expect.any(String),
498 variant: {
499 id: product1.variants[0].id,
500 },
501 }),
502 min_quantity: 2001,
503 max_quantity: 3000,
504 }),
505 expect.objectContaining({
506 id: expect.any(String),
507 amount: 25,
508 currency_code: "usd",
509 price_set: expect.objectContaining({
510 id: expect.any(String),
511 variant: {
512 id: product1.variants[0].id,
513 },
514 }),
515 min_quantity: 3001,
516 max_quantity: 4000,
517 }),
518 ])
519 )
520 })
521
522 it("Adds a batch of new prices to a price list overriding existing prices", async () => {
523 // BREAKING: The payload of the batch request changed
524 // BREAKING: The create dataset does an upsert (before an explicit `override` flag was passed)
525 const payload = {
526 create: [
527 {
528 amount: 45,
529 currency_code: "usd",
530 variant_id: product1.variants[0].id,
531 min_quantity: 1,
532 max_quantity: 100,
533 },
534 {
535 amount: 35,
536 currency_code: "usd",
537 variant_id: product1.variants[0].id,
538 min_quantity: 101,
539 max_quantity: 500,
540 },
541 ],
542 }
543
544 await api.post(
545 `/admin/price-lists/${pricelist1.id}/prices/batch`,
546 payload,
547 adminHeaders
548 )
549
550 const response = await api.get(
551 `/admin/price-lists/${pricelist1.id}/prices`,
552 adminHeaders
553 )
554
555 expect(response.status).toEqual(200)
556 expect(response.data.prices.length).toEqual(2)
557 expect(response.data.prices).toEqual(
558 expect.arrayContaining([
559 expect.objectContaining({
560 id: expect.any(String),
561 amount: 45,
562 currency_code: "usd",
563 min_quantity: 1,
564 max_quantity: 100,
565 price_set: expect.objectContaining({
566 id: expect.any(String),
567 variant: {
568 id: product1.variants[0].id,
569 },
570 }),
571 }),
572 expect.objectContaining({
573 id: expect.any(String),
574 amount: 35,
575 currency_code: "usd",
576 min_quantity: 101,
577 max_quantity: 500,
578 price_set: expect.objectContaining({
579 id: expect.any(String),
580 variant: {
581 id: product1.variants[0].id,
582 },
583 }),
584 }),
585 ])
586 )
587 })
588
589 it("Adds a batch of new prices where a MA record have a `region_id` instead of `currency_code`", async () => {
590 // BREAKING: As mentioned, currency code is required now
591 const payload = {
592 create: [
593 {
594 amount: 100,
595 variant_id: product1.variants[0].id,
596 currency_code: "eur",
597 rules: {
598 region_id: region1.id,
599 },
600 },
601 {
602 amount: 200,
603 variant_id: product1.variants[0].id,
604 currency_code: "eur",
605 },
606 ],
607 }
608
609 await api.post(
610 `/admin/price-lists/${pricelist1.id}/prices/batch`,
611 payload,
612 adminHeaders
613 )
614 const response = await api.get(
615 `/admin/price-lists/${pricelist1.id}/prices`,
616 adminHeaders
617 )
618
619 expect(response.status).toEqual(200)
620 expect(response.data.prices.length).toEqual(4)
621 expect(response.data.prices).toEqual(
622 expect.arrayContaining([
623 expect.objectContaining({
624 id: expect.any(String),
625 amount: 100,
626 currency_code: "usd",
627 min_quantity: 1,
628 max_quantity: 100,
629 price_set: expect.objectContaining({
630 id: expect.any(String),
631 variant: {
632 id: product1.variants[0].id,
633 },
634 }),
635 }),
636 expect.objectContaining({
637 id: expect.any(String),
638 amount: 80,
639 currency_code: "usd",
640 min_quantity: 101,
641 max_quantity: 500,
642 price_set: expect.objectContaining({
643 id: expect.any(String),
644 variant: {
645 id: product1.variants[0].id,
646 },
647 }),
648 }),
649 expect.objectContaining({
650 id: expect.any(String),
651 amount: 100,
652 currency_code: "eur",
653 price_rules: expect.arrayContaining([
654 expect.objectContaining({
655 attribute: "region_id",
656 value: region1.id,
657 }),
658 ]),
659 price_set: expect.objectContaining({
660 id: expect.any(String),
661 variant: {
662 id: product1.variants[0].id,
663 },
664 }),
665 }),
666 expect.objectContaining({
667 id: expect.any(String),
668 amount: 200,
669 currency_code: "eur",
670 price_set: expect.objectContaining({
671 id: expect.any(String),
672 variant: {
673 id: product1.variants[0].id,
674 },
675 }),
676 }),
677 ])
678 )
679 })
680 })
681
682 describe("DELETE /admin/price-lists/:id", () => {
683 it("Deletes a price list", async () => {
684 const response = await api.delete(
685 `/admin/price-lists/${pricelist1.id}`,
686 adminHeaders
687 )
688
689 expect(response.status).toEqual(200)
690 expect(response.data).toEqual({
691 id: pricelist1.id,
692 object: "price_list",
693 deleted: true,
694 })
695
696 const err = await api
697 .get(`/admin/price-lists/${pricelist1.id}`, adminHeaders)
698 .catch((e) => e)
699
700 expect(err.response.status).toBe(404)
701 expect(err.response.data.message).toEqual(
702 `Price list with id: ${pricelist1.id} was not found`
703 )
704 })
705 })
706
707 describe("tests cascade on delete", () => {
708 it("Deletes a variant and ensures that prices associated with the variant are deleted from PriceList", async () => {
709 await api.delete(
710 `/admin/products/${product1.id}/variants/${product1.variants[0].id}`,
711 adminHeaders
712 )
713
714 const response = await api.get(
715 `/admin/price-lists/${pricelist1.id}/prices`,
716 adminHeaders
717 )
718
719 expect(response.status).toEqual(200)
720 expect(response.data.prices.length).toEqual(0)
721 })
722 })
723
724 describe("DELETE /admin/price-lists/:id/prices/batch", () => {
725 // BREAKING: The batch method signature changed
726 it("Deletes several prices associated with a price list", async () => {
727 const priceToDelete = pricelist1Prices.find((p) => p.amount === 100)
728 await api.post(
729 `/admin/price-lists/${pricelist1.id}/prices/batch`,
730 {
731 delete: [priceToDelete.id],
732 },
733 adminHeaders
734 )
735
736 const response = await api.get(
737 `/admin/price-lists/${pricelist1.id}/prices`,
738 adminHeaders
739 )
740
741 expect(response.status).toEqual(200)
742 expect(response.data.prices).toEqual(
743 expect.arrayContaining([
744 expect.objectContaining({
745 amount: 80,
746 currency_code: "usd",
747 min_quantity: 101,
748 max_quantity: 500,
749 price_set: expect.objectContaining({
750 variant: expect.objectContaining({
751 id: product1.variants[0].id,
752 }),
753 }),
754 }),
755 ])
756 )
757 })
758 })
759
760 // BREAKING: There is no longer a GET /admin/price-lists/:id/products endpoint. Get products through `/admin/products` endpoint instead
761 // BREAKING: There is no longer a DELETE /admin/price-lists/test-list/products/${product.id}/prices endpoint
762 // BREAKING: There is no longer a DELETE /admin/price-lists/test-list/variants/${variant.id}/prices endpoint
763 })
764 },
765 })
766
767 // TODO: Revisit tax inclusive pricing
768 // describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/price-lists", () => {
769 // let medusaProcess
770 // let dbConnection
771
772 // beforeAll(async () => {
773 // const cwd = path.resolve(path.join(__dirname, "..", ".."))
774 // const [process, connection] = await startServerWithEnvironment({
775 // cwd,
776 // env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
777 // })
778 // dbConnection = connection
779 // medusaProcess = process
780 // })
781
782 // afterAll(async () => {
783 // const db = useDb()
784 // await db.shutdown()
785
786 // medusaProcess.kill()
787 // })
788
789 // describe("POST /admin/price-list", () => {
790 // const priceListIncludesTaxId = "price-list-1-includes-tax"
791
792 // beforeEach(async () => {
793 // try {
794 // await adminSeeder(dbConnection)
795 // await customerSeeder(dbConnection)
796 // await productSeeder(dbConnection)
797 // await simplePriceListFactory(dbConnection, {
798 // id: priceListIncludesTaxId,
799 // })
800 // } catch (err) {
801 // console.log(err)
802 // throw err
803 // }
804 // })
805
806 // afterEach(async () => {
807 // const db = useDb()
808 // await db.teardown()
809 // })
810
811 // it("should creates a price list that includes tax", async () => {
812 // const payload = {
813 // name: "VIP Summer sale",
814 // description: "Summer sale for VIP customers. 25% off selected items.",
815 // type: "sale",
816 // status: "active",
817 // starts_at: "2022-07-01T00:00:00.000Z",
818 // ends_at: "2022-07-31T00:00:00.000Z",
819 // customer_groups: [
820 // {
821 // id: "customer-group-1",
822 // },
823 // ],
824 // prices: [
825 // {
826 // amount: 85,
827 // currency_code: "usd",
828 // variant_id: "test-variant",
829 // },
830 // ],
831 // includes_tax: true,
832 // }
833
834 // const response = await api
835 // .post("/admin/price-lists", payload, adminHeaders)
836 // .catch((err) => {
837 // console.warn(err.response.data)
838 // })
839
840 // expect(response.status).toEqual(200)
841 // expect(response.data.price_list).toEqual(
842 // expect.objectContaining({
843 // id: expect.any(String),
844 // includes_tax: true,
845 // })
846 // )
847 // })
848
849 // it("should update a price list that include_tax", async () => {
850 // let response = await api
851 // .get(`/admin/price-lists/${priceListIncludesTaxId}`, adminHeaders)
852 // .catch((err) => {
853 // console.log(err)
854 // })
855
856 // expect(response.data.price_list.includes_tax).toBe(false)
857
858 // response = await api
859 // .post(
860 // `/admin/price-lists/${priceListIncludesTaxId}`,
861 // { includes_tax: true },
862 // adminHeaders
863 // )
864 // .catch((err) => {
865 // console.log(err)
866 // })
867
868 // expect(response.data.price_list.includes_tax).toBe(true)
869 // })
870 // })
871 // })