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("Admin Order 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 returnShippingOption: { id: string }
37 let outboundShippingOption: { id: string }
38 let inventoryItem: { id: string }
39 let taxRate: { id: string; name: 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 // Create product with description for translation
128 product = (
129 await api.post(
130 "/admin/products",
131 {
132 title: "Medusa T-Shirt",
133 description: "A comfortable cotton t-shirt",
134 handle: "t-shirt",
135 status: ProductStatus.PUBLISHED,
136 shipping_profile_id: shippingProfile.id,
137 options: [{ title: "Size", values: ["S", "M"] }],
138 variants: [
139 {
140 title: "Small",
141 sku: "SHIRT-S",
142 options: { Size: "S" },
143 inventory_items: [
144 {
145 inventory_item_id: inventoryItem.id,
146 required_quantity: 1,
147 },
148 ],
149 prices: [{ amount: 1500, currency_code: "usd" }],
150 },
151 {
152 title: "Medium",
153 sku: "SHIRT-M",
154 options: { Size: "M" },
155 manage_inventory: false,
156 prices: [{ amount: 1500, currency_code: "usd" }],
157 },
158 ],
159 },
160 adminHeaders
161 )
162 ).data.product
163
164 // Maintain predictable variants order
165 const variantSmall = product.variants.find((v) => v.title === "Small")
166 const variantMedium = product.variants.find((v) => v.title === "Medium")
167 product.variants = [variantSmall!, variantMedium!]
168
169 // Setup fulfillment
170 const fulfillmentSets = (
171 await api.post(
172 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
173 { name: "Test", type: "test-type" },
174 adminHeaders
175 )
176 ).data.stock_location.fulfillment_sets
177
178 const fulfillmentSet = (
179 await api.post(
180 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
181 {
182 name: "Test",
183 geo_zones: [{ type: "country", country_code: "us" }],
184 },
185 adminHeaders
186 )
187 ).data.fulfillment_set
188
189 await api.post(
190 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
191 { add: ["manual_test-provider"] },
192 adminHeaders
193 )
194
195 shippingOption = (
196 await api.post(
197 `/admin/shipping-options`,
198 {
199 name: "Test shipping option",
200 service_zone_id: fulfillmentSet.service_zones[0].id,
201 shipping_profile_id: shippingProfile.id,
202 provider_id: "manual_test-provider",
203 price_type: "flat",
204 type: {
205 label: "Test type",
206 description: "Test description",
207 code: "test-code",
208 },
209 prices: [{ currency_code: "usd", amount: 1000 }],
210 rules: [],
211 },
212 adminHeaders
213 )
214 ).data.shipping_option
215
216 returnShippingOption = (
217 await api.post(
218 `/admin/shipping-options`,
219 {
220 name: "Return shipping",
221 service_zone_id: fulfillmentSet.service_zones[0].id,
222 shipping_profile_id: shippingProfile.id,
223 provider_id: "manual_test-provider",
224 price_type: "flat",
225 type: {
226 label: "Test type",
227 description: "Test description",
228 code: "test-code",
229 },
230 prices: [{ currency_code: "usd", amount: 500 }],
231 rules: [
232 {
233 operator: RuleOperator.EQ,
234 attribute: "is_return",
235 value: "true",
236 },
237 ],
238 },
239 adminHeaders
240 )
241 ).data.shipping_option
242
243 outboundShippingOption = (
244 await api.post(
245 `/admin/shipping-options`,
246 {
247 name: "Outbound shipping",
248 service_zone_id: fulfillmentSet.service_zones[0].id,
249 shipping_profile_id: shippingProfile.id,
250 provider_id: "manual_test-provider",
251 price_type: "flat",
252 type: {
253 label: "Test type",
254 description: "Test description",
255 code: "test-code",
256 },
257 prices: [{ currency_code: "usd", amount: 0 }],
258 rules: [
259 {
260 operator: RuleOperator.EQ,
261 attribute: "is_return",
262 value: "false",
263 },
264 ],
265 },
266 adminHeaders
267 )
268 ).data.shipping_option
269
270 const taxRatesResponse = await api.get(
271 `/admin/tax-rates?tax_region_id=${taxStructure.us.children.cal.province.id}`,
272 adminHeaders
273 )
274 taxRate = taxRatesResponse.data.tax_rates.find(
275 (rate: { is_default: boolean; code: string }) =>
276 rate.is_default && rate.code === "CADEFAULT"
277 )
278
279 // Create translations for product and variants
280 await api.post(
281 "/admin/translations/batch",
282 {
283 create: [
284 {
285 reference_id: product.id,
286 reference: "product",
287 locale_code: "fr-FR",
288 translations: {
289 title: "T-Shirt Medusa",
290 description: "Un t-shirt en coton confortable",
291 },
292 },
293 {
294 reference_id: product.id,
295 reference: "product",
296 locale_code: "de-DE",
297 translations: {
298 title: "Medusa T-Shirt DE",
299 description: "Ein bequemes Baumwoll-T-Shirt",
300 },
301 },
302 {
303 reference_id: product.variants[0].id,
304 reference: "product_variant",
305 locale_code: "fr-FR",
306 translations: { title: "Petit" },
307 },
308 {
309 reference_id: product.variants[0].id,
310 reference: "product_variant",
311 locale_code: "de-DE",
312 translations: { title: "Klein" },
313 },
314 {
315 reference_id: product.variants[1].id,
316 reference: "product_variant",
317 locale_code: "fr-FR",
318 translations: { title: "Moyen" },
319 },
320 {
321 reference_id: product.variants[1].id,
322 reference: "product_variant",
323 locale_code: "de-DE",
324 translations: { title: "Mittel" },
325 },
326 {
327 reference_id: shippingOption.id,
328 reference: "shipping_option",
329 locale_code: "fr-FR",
330 translations: {
331 name: "Option d'expédition de test",
332 },
333 },
334 {
335 reference_id: shippingOption.id,
336 reference: "shipping_option",
337 locale_code: "de-DE",
338 translations: {
339 name: "Test-Versandoption",
340 },
341 },
342 {
343 reference_id: taxRate.id,
344 reference: "tax_rate",
345 locale_code: "fr-FR",
346 translations: {
347 name: "Taux par défaut CA",
348 },
349 },
350 {
351 reference_id: taxRate.id,
352 reference: "tax_rate",
353 locale_code: "de-DE",
354 translations: {
355 name: "CA Standardsteuersatz",
356 },
357 },
358 ],
359 },
360 adminHeaders
361 )
362
363 await dbUtils.snapshot()
364 })
365
366 const createOrderFromCart = async (locale?: string) => {
367 const cart = (
368 await api.post(
369 `/store/carts`,
370 {
371 currency_code: "usd",
372 email: "test@example.com",
373 region_id: region.id,
374 sales_channel_id: salesChannel.id,
375 locale,
376 shipping_address: shippingAddressData,
377 billing_address: shippingAddressData,
378 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
379 },
380 storeHeaders
381 )
382 ).data.cart
383
384 await api.post(
385 `/store/carts/${cart.id}/shipping-methods`,
386 { option_id: shippingOption.id },
387 storeHeaders
388 )
389
390 const paymentCollection = (
391 await api.post(
392 `/store/payment-collections`,
393 { cart_id: cart.id },
394 storeHeaders
395 )
396 ).data.payment_collection
397
398 await api.post(
399 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
400 { provider_id: "pp_system_default" },
401 storeHeaders
402 )
403
404 const order = (
405 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
406 ).data.order
407
408 return (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data
409 .order
410 }
411
412 describe("Order creation from cart with locale", () => {
413 it("should preserve locale and translated items when order is created from cart", async () => {
414 const order = await createOrderFromCart("fr-FR")
415
416 expect(order.items[0]).toEqual(
417 expect.objectContaining({
418 product_title: "T-Shirt Medusa",
419 product_description: "Un t-shirt en coton confortable",
420 variant_title: "Petit",
421 })
422 )
423 })
424
425 it("should have original values when order is created without locale", async () => {
426 const order = await createOrderFromCart()
427
428 expect(order.items[0]).toEqual(
429 expect.objectContaining({
430 product_title: "Medusa T-Shirt",
431 product_description: "A comfortable cotton t-shirt",
432 variant_title: "Small",
433 })
434 )
435 })
436
437 it("should preserve translated shipping methods when order is created from cart with locale", async () => {
438 const order = await createOrderFromCart("fr-FR")
439
440 expect(order.shipping_methods).toHaveLength(1)
441 expect(order.shipping_methods[0]).toEqual(
442 expect.objectContaining({
443 shipping_option_id: shippingOption.id,
444 name: "Option d'expédition de test",
445 })
446 )
447 })
448
449 it("should have original shipping method name when order is created without locale", async () => {
450 const order = await createOrderFromCart()
451
452 expect(order.shipping_methods).toHaveLength(1)
453 expect(order.shipping_methods[0]).toEqual(
454 expect.objectContaining({
455 shipping_option_id: shippingOption.id,
456 name: "Test shipping option",
457 })
458 )
459 })
460 })
461
462 describe("POST /admin/orders/:id (update order locale)", () => {
463 it("should re-translate all items when locale is updated", async () => {
464 const order = await createOrderFromCart("fr-FR")
465
466 expect(order.items[0].variant_title).toEqual("Petit")
467
468 await api.post(
469 `/admin/orders/${order.id}`,
470 { locale: "de-DE" },
471 adminHeaders
472 )
473
474 const updatedOrder = (
475 await api.get(`/admin/orders/${order.id}`, adminHeaders)
476 ).data.order
477
478 expect(updatedOrder.items[0]).toEqual(
479 expect.objectContaining({
480 product_title: "Medusa T-Shirt DE",
481 product_description: "Ein bequemes Baumwoll-T-Shirt",
482 variant_title: "Klein",
483 })
484 )
485 })
486
487 it("should re-translate shipping methods when locale is updated", async () => {
488 const order = await createOrderFromCart("fr-FR")
489
490 expect(order.shipping_methods[0].name).toEqual(
491 "Option d'expédition de test"
492 )
493
494 await api.post(
495 `/admin/orders/${order.id}`,
496 { locale: "de-DE" },
497 adminHeaders
498 )
499
500 const updatedOrder = (
501 await api.get(`/admin/orders/${order.id}`, adminHeaders)
502 ).data.order
503
504 expect(updatedOrder.shipping_methods).toHaveLength(1)
505 expect(updatedOrder.shipping_methods[0]).toEqual(
506 expect.objectContaining({
507 shipping_option_id: shippingOption.id,
508 name: "Test-Versandoption",
509 })
510 )
511 })
512
513 it("should not re-translate items when updating other fields", async () => {
514 const order = await createOrderFromCart("fr-FR")
515
516 await api.post(
517 `/admin/orders/${order.id}`,
518 { email: "updated@example.com" },
519 adminHeaders
520 )
521
522 const updatedOrder = (
523 await api.get(
524 `/admin/orders/${order.id}?fields=+email`,
525 adminHeaders
526 )
527 ).data.order
528
529 expect(updatedOrder.email).toEqual("updated@example.com")
530 expect(updatedOrder.items[0]).toEqual(
531 expect.objectContaining({
532 product_title: "T-Shirt Medusa",
533 variant_title: "Petit",
534 })
535 )
536 })
537
538 it("should not re-translate shipping methods when updating other fields", async () => {
539 const order = await createOrderFromCart("fr-FR")
540
541 await api.post(
542 `/admin/orders/${order.id}`,
543 { email: "updated@example.com" },
544 adminHeaders
545 )
546
547 const updatedOrder = (
548 await api.get(
549 `/admin/orders/${order.id}?fields=+email,+shipping_methods.name`,
550 adminHeaders
551 )
552 ).data.order
553
554 expect(updatedOrder.email).toEqual("updated@example.com")
555 expect(updatedOrder.shipping_methods[0]).toEqual(
556 expect.objectContaining({
557 shipping_option_id: shippingOption.id,
558 name: "Option d'expédition de test",
559 })
560 )
561 })
562 })
563
564 describe("Tax line translations", () => {
565 it("should translate tax line descriptions when order locale is updated", async () => {
566 // Create order with French locale
567 const order = await createOrderFromCart("fr-FR")
568
569 // Verify tax lines exist and have French translation
570 expect(order.items[0].tax_lines).toBeDefined()
571 expect(order.items[0].tax_lines.length).toBe(1)
572 expect(order.items[0].tax_lines[0]).toEqual(
573 expect.objectContaining({
574 description: "Taux par défaut CA",
575 rate: 5,
576 code: "CADEFAULT",
577 })
578 )
579
580 // Update order locale to German
581 await api.post(
582 `/admin/orders/${order.id}`,
583 { locale: "de-DE" },
584 adminHeaders
585 )
586
587 // Fetch updated order
588 const updatedOrder = (
589 await api.get(`/admin/orders/${order.id}`, adminHeaders)
590 ).data.order
591
592 // Verify tax lines are translated to German
593 expect(updatedOrder.items[0].tax_lines.length).toBe(1)
594 expect(updatedOrder.items[0].tax_lines[0]).toEqual(
595 expect.objectContaining({
596 description: "CA Standardsteuersatz",
597 rate: 5,
598 code: "CADEFAULT",
599 })
600 )
601 })
602
603 it("should preserve tax line translations when order is created with locale", async () => {
604 // Create order with French locale
605 const order = await createOrderFromCart("fr-FR")
606
607 // Verify tax lines are translated from the start
608 expect(order.items[0].tax_lines).toBeDefined()
609 expect(order.items[0].tax_lines.length).toBeGreaterThan(0)
610 expect(order.items[0].tax_lines[0]).toEqual(
611 expect.objectContaining({
612 description: "Taux par défaut CA",
613 rate: 5,
614 code: "CADEFAULT",
615 })
616 )
617 })
618
619 it("should use original tax line description when order has no locale", async () => {
620 // Create order without locale
621 const order = await createOrderFromCart()
622
623 // Verify tax lines use original description
624 expect(order.items[0].tax_lines).toBeDefined()
625 expect(order.items[0].tax_lines.length).toBeGreaterThan(0)
626 expect(order.items[0].tax_lines[0]).toEqual(
627 expect.objectContaining({
628 description: "CA Default Rate",
629 rate: 5,
630 code: "CADEFAULT",
631 })
632 )
633 })
634
635 it("should translate shipping method tax lines when order locale is updated", async () => {
636 // Create order with French locale
637 const order = await createOrderFromCart("fr-FR")
638
639 // Verify shipping method tax lines exist and are translated
640 expect(order.shipping_methods).toBeDefined()
641 expect(order.shipping_methods.length).toBeGreaterThan(0)
642 if (order.shipping_methods[0].tax_lines?.length > 0) {
643 expect(order.shipping_methods[0].tax_lines[0]).toEqual(
644 expect.objectContaining({
645 description: "Taux par défaut CA",
646 rate: 5,
647 code: "CADEFAULT",
648 })
649 )
650
651 // Update order locale to German
652 await api.post(
653 `/admin/orders/${order.id}`,
654 { locale: "de-DE" },
655 adminHeaders
656 )
657
658 // Fetch updated order
659 const updatedOrder = (
660 await api.get(`/admin/orders/${order.id}`, adminHeaders)
661 ).data.order
662
663 // Verify shipping method tax lines are translated
664 if (updatedOrder.shipping_methods[0].tax_lines?.length > 0) {
665 expect(updatedOrder.shipping_methods[0].tax_lines[0]).toEqual(
666 expect.objectContaining({
667 description: "CA Standardsteuersatz",
668 rate: 5,
669 code: "CADEFAULT",
670 })
671 )
672 }
673 }
674 })
675 })
676 })
677 },
678 })