1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { MedusaContainer } from "@medusajs/types"
3 import { Modules, ProductStatus, RuleOperator } from "@medusajs/utils"
4 import {
5 adminHeaders,
6 createAdminUser,
7 generatePublishableKey,
8 generateStoreHeaders,
9 } from "../../../helpers/create-admin-user"
10 import { setupTaxStructure } from "../../../modules/__tests__/fixtures"
11
12 jest.setTimeout(300000)
13
14 process.env.MEDUSA_FF_TRANSLATION = "true"
15
16 const shippingAddressData = {
17 address_1: "test address 1",
18 address_2: "test address 2",
19 city: "SF",
20 country_code: "us",
21 province: "CA",
22 postal_code: "94016",
23 }
24
25 medusaIntegrationTestRunner({
26 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
27 describe("Exchange Translation API", () => {
28 let appContainer: MedusaContainer
29 let storeHeaders: { headers: { [key: string]: string } }
30 let region: { id: string }
31 let product: { id: string; variants: { id: string; title: string }[] }
32 let salesChannel: { id: string }
33 let shippingProfile: { id: string }
34 let stockLocation: { id: string }
35 let shippingOption: { id: string }
36 let outboundShippingOption: { id: string }
37 let returnShippingOption: { id: string }
38 let inventoryItem: { id: string }
39 let taxRate: { id: string }
40
41 beforeAll(async () => {
42 appContainer = getContainer()
43 })
44
45 beforeAll(async () => {
46 const taxStructure = await setupTaxStructure(
47 appContainer.resolve(Modules.TAX)
48 )
49 await createAdminUser(dbConnection, adminHeaders, appContainer)
50
51 const translationModule = appContainer.resolve(Modules.TRANSLATION)
52 await translationModule.__hooks?.onApplicationStart?.().catch(() => {})
53
54 const publishableKey = await generatePublishableKey(appContainer)
55 storeHeaders = generateStoreHeaders({ publishableKey })
56
57 salesChannel = (
58 await api.post(
59 "/admin/sales-channels",
60 { name: "Webshop", description: "channel" },
61 adminHeaders
62 )
63 ).data.sales_channel
64
65 const storeModule = appContainer.resolve(Modules.STORE)
66 const [defaultStore] = await storeModule.listStores(
67 {},
68 { select: ["id"], take: 1 }
69 )
70 await storeModule.updateStores(defaultStore.id, {
71 supported_locales: [
72 { locale_code: "en-US" },
73 { locale_code: "fr-FR" },
74 { locale_code: "de-DE" },
75 ],
76 })
77
78 region = (
79 await api.post(
80 "/admin/regions",
81 {
82 payment_providers: ["pp_system_default"],
83 name: "US",
84 currency_code: "usd",
85 countries: ["us"],
86 },
87 adminHeaders
88 )
89 ).data.region
90
91 shippingProfile = (
92 await api.post(
93 `/admin/shipping-profiles`,
94 { name: "default", type: "default" },
95 adminHeaders
96 )
97 ).data.shipping_profile
98
99 stockLocation = (
100 await api.post(
101 `/admin/stock-locations`,
102 { name: "test location" },
103 adminHeaders
104 )
105 ).data.stock_location
106
107 inventoryItem = (
108 await api.post(
109 `/admin/inventory-items`,
110 { sku: "test-variant" },
111 adminHeaders
112 )
113 ).data.inventory_item
114
115 await api.post(
116 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
117 { location_id: stockLocation.id, stocked_quantity: 100 },
118 adminHeaders
119 )
120
121 await api.post(
122 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
123 { add: [salesChannel.id] },
124 adminHeaders
125 )
126
127 product = (
128 await api.post(
129 "/admin/products",
130 {
131 title: "Medusa T-Shirt",
132 description: "A comfortable cotton t-shirt",
133 handle: "t-shirt",
134 status: ProductStatus.PUBLISHED,
135 shipping_profile_id: shippingProfile.id,
136 options: [{ title: "Size", values: ["S", "M"] }],
137 variants: [
138 {
139 title: "Small",
140 sku: "SHIRT-S",
141 options: { Size: "S" },
142 inventory_items: [
143 {
144 inventory_item_id: inventoryItem.id,
145 required_quantity: 1,
146 },
147 ],
148 prices: [{ amount: 1500, currency_code: "usd" }],
149 },
150 {
151 title: "Medium",
152 sku: "SHIRT-M",
153 options: { Size: "M" },
154 manage_inventory: false,
155 prices: [{ amount: 1500, currency_code: "usd" }],
156 },
157 ],
158 },
159 adminHeaders
160 )
161 ).data.product
162
163 const variantSmall = product.variants.find((v) => v.title === "Small")
164 const variantMedium = product.variants.find((v) => v.title === "Medium")
165 product.variants = [variantSmall!, variantMedium!]
166
167 const fulfillmentSets = (
168 await api.post(
169 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
170 { name: "Test", type: "test-type" },
171 adminHeaders
172 )
173 ).data.stock_location.fulfillment_sets
174
175 const fulfillmentSet = (
176 await api.post(
177 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
178 {
179 name: "Test",
180 geo_zones: [{ type: "country", country_code: "us" }],
181 },
182 adminHeaders
183 )
184 ).data.fulfillment_set
185
186 await api.post(
187 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
188 { add: ["manual_test-provider"] },
189 adminHeaders
190 )
191
192 shippingOption = (
193 await api.post(
194 `/admin/shipping-options`,
195 {
196 name: "Test shipping option",
197 service_zone_id: fulfillmentSet.service_zones[0].id,
198 shipping_profile_id: shippingProfile.id,
199 provider_id: "manual_test-provider",
200 price_type: "flat",
201 type: {
202 label: "Test type",
203 description: "Test description",
204 code: "test-code",
205 },
206 prices: [{ currency_code: "usd", amount: 1000 }],
207 rules: [],
208 },
209 adminHeaders
210 )
211 ).data.shipping_option
212
213 outboundShippingOption = (
214 await api.post(
215 `/admin/shipping-options`,
216 {
217 name: "Outbound shipping",
218 service_zone_id: fulfillmentSet.service_zones[0].id,
219 shipping_profile_id: shippingProfile.id,
220 provider_id: "manual_test-provider",
221 price_type: "flat",
222 type: {
223 label: "Test type",
224 description: "Test description",
225 code: "test-code",
226 },
227 prices: [{ currency_code: "usd", amount: 0 }],
228 rules: [
229 {
230 operator: RuleOperator.EQ,
231 attribute: "is_return",
232 value: "false",
233 },
234 ],
235 },
236 adminHeaders
237 )
238 ).data.shipping_option
239
240 returnShippingOption = (
241 await api.post(
242 `/admin/shipping-options`,
243 {
244 name: "Return shipping",
245 service_zone_id: fulfillmentSet.service_zones[0].id,
246 shipping_profile_id: shippingProfile.id,
247 provider_id: "manual_test-provider",
248 price_type: "flat",
249 type: {
250 label: "Test type",
251 description: "Test description",
252 code: "test-code",
253 },
254 prices: [{ currency_code: "usd", amount: 500 }],
255 rules: [
256 {
257 operator: RuleOperator.EQ,
258 attribute: "is_return",
259 value: "true",
260 },
261 ],
262 },
263 adminHeaders
264 )
265 ).data.shipping_option
266 const taxRatesResponse = await api.get(
267 `/admin/tax-rates?tax_region_id=${taxStructure.us.children.cal.province.id}`,
268 adminHeaders
269 )
270 taxRate = taxRatesResponse.data.tax_rates.find(
271 (rate: { code: string }) => rate.code === "CADEFAULT"
272 )
273
274 await api.post(
275 "/admin/translations/batch",
276 {
277 create: [
278 {
279 reference_id: product.id,
280 reference: "product",
281 locale_code: "fr-FR",
282 translations: {
283 title: "T-Shirt Medusa",
284 description: "Un t-shirt en coton confortable",
285 },
286 },
287 {
288 reference_id: product.id,
289 reference: "product",
290 locale_code: "de-DE",
291 translations: {
292 title: "Medusa T-Shirt DE",
293 description: "Ein bequemes Baumwoll-T-Shirt",
294 },
295 },
296 {
297 reference_id: product.variants[0].id,
298 reference: "product_variant",
299 locale_code: "fr-FR",
300 translations: { title: "Petit" },
301 },
302 {
303 reference_id: product.variants[0].id,
304 reference: "product_variant",
305 locale_code: "de-DE",
306 translations: { title: "Klein" },
307 },
308 {
309 reference_id: product.variants[1].id,
310 reference: "product_variant",
311 locale_code: "fr-FR",
312 translations: { title: "Moyen" },
313 },
314 {
315 reference_id: product.variants[1].id,
316 reference: "product_variant",
317 locale_code: "de-DE",
318 translations: { title: "Mittel" },
319 },
320 {
321 reference_id: outboundShippingOption.id,
322 reference: "shipping_option",
323 locale_code: "fr-FR",
324 translations: {
325 name: "Expédition sortante",
326 },
327 },
328 {
329 reference_id: outboundShippingOption.id,
330 reference: "shipping_option",
331 locale_code: "de-DE",
332 translations: {
333 name: "Ausgehende Versand",
334 },
335 },
336 {
337 reference_id: returnShippingOption.id,
338 reference: "shipping_option",
339 locale_code: "fr-FR",
340 translations: {
341 name: "Expédition de retour",
342 },
343 },
344 {
345 reference_id: returnShippingOption.id,
346 reference: "shipping_option",
347 locale_code: "de-DE",
348 translations: {
349 name: "Rückversand",
350 },
351 },
352 {
353 reference_id: taxRate.id,
354 reference: "tax_rate",
355 locale_code: "fr-FR",
356 translations: {
357 name: "Taux par défaut CA",
358 },
359 },
360 {
361 reference_id: taxRate.id,
362 reference: "tax_rate",
363 locale_code: "de-DE",
364 translations: {
365 name: "CA Standardsteuersatz",
366 },
367 },
368 ],
369 },
370 adminHeaders
371 )
372
373 await dbUtils.snapshot()
374 })
375
376 const createOrderFromCart = async (locale?: string) => {
377 const cart = (
378 await api.post(
379 `/store/carts`,
380 {
381 currency_code: "usd",
382 email: "test@example.com",
383 region_id: region.id,
384 sales_channel_id: salesChannel.id,
385 locale,
386 shipping_address: shippingAddressData,
387 billing_address: shippingAddressData,
388 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
389 },
390 storeHeaders
391 )
392 ).data.cart
393
394 await api.post(
395 `/store/carts/${cart.id}/shipping-methods`,
396 { option_id: shippingOption.id },
397 storeHeaders
398 )
399
400 const paymentCollection = (
401 await api.post(
402 `/store/payment-collections`,
403 { cart_id: cart.id },
404 storeHeaders
405 )
406 ).data.payment_collection
407
408 await api.post(
409 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
410 { provider_id: "pp_system_default" },
411 storeHeaders
412 )
413
414 const order = (
415 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
416 ).data.order
417
418 return (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data
419 .order
420 }
421
422 describe("Exchange items translation", () => {
423 it("should translate new items in exchange using order locale", async () => {
424 const order = await createOrderFromCart("fr-FR")
425
426 await api.post(
427 `/admin/orders/${order.id}/fulfillments`,
428 {
429 location_id: stockLocation.id,
430 items: [{ id: order.items[0].id, quantity: 1 }],
431 },
432 adminHeaders
433 )
434
435 const exchange = (
436 await api.post(
437 "/admin/exchanges",
438 { order_id: order.id, description: "Test exchange" },
439 adminHeaders
440 )
441 ).data.exchange
442
443 // Add inbound item (item being returned)
444 await api.post(
445 `/admin/exchanges/${exchange.id}/inbound/items`,
446 {
447 items: [{ id: order.items[0].id, quantity: 1 }],
448 },
449 adminHeaders
450 )
451
452 // Add outbound item (new item being sent)
453 await api.post(
454 `/admin/exchanges/${exchange.id}/outbound/items`,
455 {
456 items: [{ variant_id: product.variants[1].id, quantity: 1 }],
457 },
458 adminHeaders
459 )
460
461 await api.post(
462 `/admin/exchanges/${exchange.id}/outbound/shipping-method`,
463 { shipping_option_id: outboundShippingOption.id },
464 adminHeaders
465 )
466
467 await api.post(
468 `/admin/exchanges/${exchange.id}/request`,
469 {},
470 adminHeaders
471 )
472
473 const updatedOrder = (
474 await api.get(`/admin/orders/${order.id}`, adminHeaders)
475 ).data.order
476
477 const newItem = updatedOrder.items.find(
478 (item: any) => item.variant_id === product.variants[1].id
479 )
480
481 expect(newItem).toEqual(
482 expect.objectContaining({
483 product_title: "T-Shirt Medusa",
484 variant_title: "Moyen",
485 })
486 )
487
488 expect(newItem.tax_lines.length).toBeGreaterThan(0)
489 const taxLine = newItem.tax_lines.find(
490 (tl) => tl.code === "CADEFAULT"
491 )
492 expect(taxLine.description).toEqual("Taux par défaut CA")
493
494 const outboundShippingMethod = updatedOrder.shipping_methods.find(
495 (sm) => sm.shipping_option_id === outboundShippingOption.id
496 )
497 expect(outboundShippingMethod.tax_lines.length).toBeGreaterThan(0)
498 const shippingTaxLine = outboundShippingMethod.tax_lines.find(
499 (tl) => tl.code === "CADEFAULT"
500 )
501 expect(shippingTaxLine.description).toEqual("Taux par défaut CA")
502 })
503
504 it("should have original values when order has no locale", async () => {
505 const order = await createOrderFromCart()
506
507 await api.post(
508 `/admin/orders/${order.id}/fulfillments`,
509 {
510 location_id: stockLocation.id,
511 items: [{ id: order.items[0].id, quantity: 1 }],
512 },
513 adminHeaders
514 )
515
516 const exchange = (
517 await api.post(
518 "/admin/exchanges",
519 { order_id: order.id, description: "Test exchange" },
520 adminHeaders
521 )
522 ).data.exchange
523
524 await api.post(
525 `/admin/exchanges/${exchange.id}/inbound/items`,
526 {
527 items: [{ id: order.items[0].id, quantity: 1 }],
528 },
529 adminHeaders
530 )
531
532 // Add outbound item (new item being sent)
533 await api.post(
534 `/admin/exchanges/${exchange.id}/outbound/items`,
535 {
536 items: [{ variant_id: product.variants[1].id, quantity: 1 }],
537 },
538 adminHeaders
539 )
540
541 await api.post(
542 `/admin/exchanges/${exchange.id}/outbound/shipping-method`,
543 { shipping_option_id: outboundShippingOption.id },
544 adminHeaders
545 )
546
547 await api.post(
548 `/admin/exchanges/${exchange.id}/request`,
549 {},
550 adminHeaders
551 )
552
553 const updatedOrder = (
554 await api.get(`/admin/orders/${order.id}`, adminHeaders)
555 ).data.order
556
557 const newItem = updatedOrder.items.find(
558 (item: any) => item.variant_id === product.variants[1].id
559 )
560
561 expect(newItem).toEqual(
562 expect.objectContaining({
563 product_title: "Medusa T-Shirt",
564 product_description: "A comfortable cotton t-shirt",
565 variant_title: "Medium",
566 })
567 )
568
569 expect(newItem.tax_lines.length).toBeGreaterThan(0)
570 const taxLine = newItem.tax_lines.find(
571 (tl) => tl.code === "CADEFAULT"
572 )
573 expect(taxLine.description).toEqual("CA Default Rate")
574
575 const outboundShippingMethod = updatedOrder.shipping_methods.find(
576 (sm) => sm.shipping_option_id === outboundShippingOption.id
577 )
578 expect(outboundShippingMethod.tax_lines.length).toBeGreaterThan(0)
579 const shippingTaxLine = outboundShippingMethod.tax_lines.find(
580 (tl) => tl.code === "CADEFAULT"
581 )
582 expect(shippingTaxLine.description).toEqual("CA Default Rate")
583 })
584 })
585
586 describe("Exchange shipping method translation", () => {
587 it("should translate outbound and inbound shipping methods added during exchange using order locale", async () => {
588 const order = await createOrderFromCart("fr-FR")
589
590 await api.post(
591 `/admin/orders/${order.id}/fulfillments`,
592 {
593 location_id: stockLocation.id,
594 items: [{ id: order.items[0].id, quantity: 1 }],
595 },
596 adminHeaders
597 )
598
599 const exchange = (
600 await api.post(
601 "/admin/exchanges",
602 { order_id: order.id, description: "Test exchange" },
603 adminHeaders
604 )
605 ).data.exchange
606
607 await api.post(
608 `/admin/exchanges/${exchange.id}/inbound/items`,
609 {
610 items: [{ id: order.items[0].id, quantity: 1 }],
611 },
612 adminHeaders
613 )
614
615 await api.post(
616 `/admin/exchanges/${exchange.id}/inbound/shipping-method`,
617 { shipping_option_id: returnShippingOption.id },
618 adminHeaders
619 )
620
621 await api.post(
622 `/admin/exchanges/${exchange.id}/outbound/items`,
623 {
624 items: [{ variant_id: product.variants[1].id, quantity: 1 }],
625 },
626 adminHeaders
627 )
628
629 await api.post(
630 `/admin/exchanges/${exchange.id}/outbound/shipping-method`,
631 { shipping_option_id: outboundShippingOption.id },
632 adminHeaders
633 )
634
635 const exchangeResult = (
636 await api.post(
637 `/admin/exchanges/${exchange.id}/request`,
638 {},
639 adminHeaders
640 )
641 ).data.exchange
642
643 const updatedOrder = (
644 await api.get(`/admin/orders/${order.id}`, adminHeaders)
645 ).data.order
646
647 const outboundShippingMethod = updatedOrder.shipping_methods.find(
648 (sm: any) => sm.shipping_option_id === outboundShippingOption.id
649 )
650
651 expect(outboundShippingMethod).toEqual(
652 expect.objectContaining({
653 shipping_option_id: outboundShippingOption.id,
654 name: "Expédition sortante",
655 })
656 )
657
658 const orderReturn = (
659 await api.get(
660 `/admin/returns/${exchangeResult.return_id}?fields=*shipping_methods`,
661 adminHeaders
662 )
663 ).data.return
664
665 const inboundShippingMethod = orderReturn.shipping_methods.find(
666 (sm: any) => sm.shipping_option_id === returnShippingOption.id
667 )
668
669 expect(inboundShippingMethod).toEqual(
670 expect.objectContaining({
671 shipping_option_id: returnShippingOption.id,
672 name: "Expédition de retour",
673 })
674 )
675 })
676
677 it("should translate outbound and inbound shipping methods using German locale", async () => {
678 const order = await createOrderFromCart("de-DE")
679
680 await api.post(
681 `/admin/orders/${order.id}/fulfillments`,
682 {
683 location_id: stockLocation.id,
684 items: [{ id: order.items[0].id, quantity: 1 }],
685 },
686 adminHeaders
687 )
688
689 const exchange = (
690 await api.post(
691 "/admin/exchanges",
692 { order_id: order.id, description: "Test exchange" },
693 adminHeaders
694 )
695 ).data.exchange
696
697 await api.post(
698 `/admin/exchanges/${exchange.id}/inbound/items`,
699 {
700 items: [{ id: order.items[0].id, quantity: 1 }],
701 },
702 adminHeaders
703 )
704
705 await api.post(
706 `/admin/exchanges/${exchange.id}/inbound/shipping-method`,
707 { shipping_option_id: returnShippingOption.id },
708 adminHeaders
709 )
710
711 await api.post(
712 `/admin/exchanges/${exchange.id}/outbound/items`,
713 {
714 items: [{ variant_id: product.variants[1].id, quantity: 1 }],
715 },
716 adminHeaders
717 )
718
719 await api.post(
720 `/admin/exchanges/${exchange.id}/outbound/shipping-method`,
721 { shipping_option_id: outboundShippingOption.id },
722 adminHeaders
723 )
724
725 const exchangeResult = (
726 await api.post(
727 `/admin/exchanges/${exchange.id}/request`,
728 {},
729 adminHeaders
730 )
731 ).data.exchange
732
733 const updatedOrder = (
734 await api.get(`/admin/orders/${order.id}`, adminHeaders)
735 ).data.order
736
737 const outboundShippingMethod = updatedOrder.shipping_methods.find(
738 (sm: any) => sm.shipping_option_id === outboundShippingOption.id
739 )
740
741 expect(outboundShippingMethod).toEqual(
742 expect.objectContaining({
743 shipping_option_id: outboundShippingOption.id,
744 name: "Ausgehende Versand",
745 })
746 )
747
748 const orderReturn = (
749 await api.get(
750 `/admin/returns/${exchangeResult.return_id}?fields=*shipping_methods`,
751 adminHeaders
752 )
753 ).data.return
754
755 const inboundShippingMethod = orderReturn.shipping_methods.find(
756 (sm: any) => sm.shipping_option_id === returnShippingOption.id
757 )
758
759 expect(inboundShippingMethod).toEqual(
760 expect.objectContaining({
761 shipping_option_id: returnShippingOption.id,
762 name: "Rückversand",
763 })
764 )
765 })
766
767 it("should have original shipping method names when order has no locale", async () => {
768 const order = await createOrderFromCart()
769
770 await api.post(
771 `/admin/orders/${order.id}/fulfillments`,
772 {
773 location_id: stockLocation.id,
774 items: [{ id: order.items[0].id, quantity: 1 }],
775 },
776 adminHeaders
777 )
778
779 const exchange = (
780 await api.post(
781 "/admin/exchanges",
782 { order_id: order.id, description: "Test exchange" },
783 adminHeaders
784 )
785 ).data.exchange
786
787 await api.post(
788 `/admin/exchanges/${exchange.id}/inbound/items`,
789 {
790 items: [{ id: order.items[0].id, quantity: 1 }],
791 },
792 adminHeaders
793 )
794
795 await api.post(
796 `/admin/exchanges/${exchange.id}/inbound/shipping-method`,
797 { shipping_option_id: returnShippingOption.id },
798 adminHeaders
799 )
800
801 await api.post(
802 `/admin/exchanges/${exchange.id}/outbound/items`,
803 {
804 items: [{ variant_id: product.variants[1].id, quantity: 1 }],
805 },
806 adminHeaders
807 )
808
809 await api.post(
810 `/admin/exchanges/${exchange.id}/outbound/shipping-method`,
811 { shipping_option_id: outboundShippingOption.id },
812 adminHeaders
813 )
814
815 const exchangeResult = (
816 await api.post(
817 `/admin/exchanges/${exchange.id}/request`,
818 {},
819 adminHeaders
820 )
821 ).data.exchange
822
823 const updatedOrder = (
824 await api.get(`/admin/orders/${order.id}`, adminHeaders)
825 ).data.order
826
827 const outboundShippingMethod = updatedOrder.shipping_methods.find(
828 (sm: any) => sm.shipping_option_id === outboundShippingOption.id
829 )
830
831 expect(outboundShippingMethod).toEqual(
832 expect.objectContaining({
833 shipping_option_id: outboundShippingOption.id,
834 name: "Outbound shipping",
835 })
836 )
837
838 const orderReturn = (
839 await api.get(
840 `/admin/returns/${exchangeResult.return_id}?fields=*shipping_methods`,
841 adminHeaders
842 )
843 ).data.return
844
845 const inboundShippingMethod = orderReturn.shipping_methods.find(
846 (sm: any) => sm.shipping_option_id === returnShippingOption.id
847 )
848
849 expect(inboundShippingMethod).toEqual(
850 expect.objectContaining({
851 shipping_option_id: returnShippingOption.id,
852 name: "Return shipping",
853 })
854 )
855 })
856 })
857 })
858 },
859 })