1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { DmlEntity, Modules } from "@medusajs/utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7
8 jest.setTimeout(100000)
9
10 process.env.MEDUSA_FF_TRANSLATION = "true"
11
12 medusaIntegrationTestRunner({
13 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
14 describe("Admin Translation Settings Batch API", () => {
15 let mockGetTranslatableEntities: jest.SpyInstance
16
17 beforeAll(async () => {
18 mockGetTranslatableEntities = jest.spyOn(
19 DmlEntity,
20 "getTranslatableEntities"
21 )
22 mockGetTranslatableEntities.mockReturnValue([
23 { entity: "ProductVariant", fields: ["title", "material"] },
24 { entity: "ProductCategory", fields: ["name", "description"] },
25 { entity: "ProductCollection", fields: ["title"] },
26 ])
27 await createAdminUser(dbConnection, adminHeaders, getContainer())
28
29 const appContainer = getContainer()
30
31 const translationModuleService = appContainer.resolve(
32 Modules.TRANSLATION
33 )
34 await translationModuleService.__hooks
35 ?.onApplicationStart?.()
36 .catch(() => {})
37
38 // Delete all translation settings to be able to test the create operation
39 const settings =
40 await translationModuleService.listTranslationSettings()
41 await translationModuleService.deleteTranslationSettings(
42 settings.map((s) => s.id)
43 )
44
45 await dbUtils.snapshot()
46 })
47
48 afterAll(async () => {
49 delete process.env.MEDUSA_FF_TRANSLATION
50 mockGetTranslatableEntities.mockRestore()
51 })
52
53 describe("POST /admin/translations/settings/batch", () => {
54 describe("create", () => {
55 it("should create a single translation setting", async () => {
56 const response = await api.post(
57 "/admin/translations/settings/batch",
58 {
59 create: [
60 {
61 entity_type: "product_variant",
62 fields: ["title", "material"],
63 is_active: true,
64 },
65 ],
66 },
67 adminHeaders
68 )
69
70 expect(response.status).toEqual(200)
71 expect(response.data.created).toHaveLength(1)
72 expect(response.data.created[0]).toEqual(
73 expect.objectContaining({
74 id: expect.any(String),
75 entity_type: "product_variant",
76 fields: ["title", "material"],
77 is_active: true,
78 created_at: expect.any(String),
79 updated_at: expect.any(String),
80 })
81 )
82 })
83
84 it("should create multiple translation settings", async () => {
85 const response = await api.post(
86 "/admin/translations/settings/batch",
87 {
88 create: [
89 {
90 entity_type: "product_variant",
91 fields: ["title", "material"],
92 is_active: true,
93 },
94 {
95 entity_type: "product_category",
96 fields: ["name", "description"],
97 is_active: true,
98 },
99 {
100 entity_type: "product_collection",
101 fields: ["title"],
102 is_active: false,
103 },
104 ],
105 },
106 adminHeaders
107 )
108
109 expect(response.status).toEqual(200)
110 expect(response.data.created).toHaveLength(3)
111 expect(response.data.created).toEqual(
112 expect.arrayContaining([
113 expect.objectContaining({
114 entity_type: "product_variant",
115 fields: ["title", "material"],
116 is_active: true,
117 }),
118 expect.objectContaining({
119 entity_type: "product_category",
120 fields: ["name", "description"],
121 is_active: true,
122 }),
123 expect.objectContaining({
124 entity_type: "product_collection",
125 fields: ["title"],
126 is_active: false,
127 }),
128 ])
129 )
130 })
131 })
132
133 describe("update", () => {
134 it("should update an existing translation setting", async () => {
135 const createResponse = await api.post(
136 "/admin/translations/settings/batch",
137 {
138 create: [
139 {
140 entity_type: "product_variant",
141 fields: ["title"],
142 is_active: true,
143 },
144 ],
145 },
146 adminHeaders
147 )
148
149 const settingId = createResponse.data.created[0].id
150
151 const updateResponse = await api.post(
152 "/admin/translations/settings/batch",
153 {
154 update: [
155 {
156 id: settingId,
157 entity_type: "product_variant",
158 fields: ["title", "material"],
159 },
160 ],
161 },
162 adminHeaders
163 )
164
165 expect(updateResponse.status).toEqual(200)
166 expect(updateResponse.data.updated).toHaveLength(1)
167 expect(updateResponse.data.updated[0]).toEqual(
168 expect.objectContaining({
169 id: settingId,
170 entity_type: "product_variant",
171 fields: ["title", "material"],
172 is_active: true,
173 })
174 )
175 })
176
177 it("should update multiple translation settings", async () => {
178 const createResponse = await api.post(
179 "/admin/translations/settings/batch",
180 {
181 create: [
182 {
183 entity_type: "product_variant",
184 fields: ["title"],
185 is_active: true,
186 },
187 {
188 entity_type: "product_category",
189 fields: ["name"],
190 is_active: true,
191 },
192 ],
193 },
194 adminHeaders
195 )
196
197 const [settingId1, settingId2] = createResponse.data.created.map(
198 (s) => s.id
199 )
200
201 const updateResponse = await api.post(
202 "/admin/translations/settings/batch",
203 {
204 update: [
205 {
206 id: settingId1,
207 entity_type: "product_variant",
208 fields: ["title", "material"],
209 },
210 {
211 id: settingId2,
212 entity_type: "product_category",
213 is_active: false,
214 },
215 ],
216 },
217 adminHeaders
218 )
219
220 expect(updateResponse.status).toEqual(200)
221 expect(updateResponse.data.updated).toHaveLength(2)
222 expect(updateResponse.data.updated).toEqual(
223 expect.arrayContaining([
224 expect.objectContaining({
225 entity_type: "product_variant",
226 fields: ["title", "material"],
227 }),
228 expect.objectContaining({
229 entity_type: "product_category",
230 is_active: false,
231 }),
232 ])
233 )
234 })
235 })
236
237 describe("delete", () => {
238 it("should delete a translation setting", async () => {
239 const createResponse = await api.post(
240 "/admin/translations/settings/batch",
241 {
242 create: [
243 {
244 entity_type: "product_variant",
245 fields: ["title"],
246 is_active: true,
247 },
248 ],
249 },
250 adminHeaders
251 )
252
253 const settingId = createResponse.data.created[0].id
254
255 const deleteResponse = await api.post(
256 "/admin/translations/settings/batch",
257 {
258 delete: [settingId],
259 },
260 adminHeaders
261 )
262
263 expect(deleteResponse.status).toEqual(200)
264 expect(deleteResponse.data.deleted).toEqual({
265 ids: [settingId],
266 object: "translation_settings",
267 deleted: true,
268 })
269 })
270
271 it("should delete multiple translation settings", async () => {
272 const createResponse = await api.post(
273 "/admin/translations/settings/batch",
274 {
275 create: [
276 {
277 entity_type: "product_variant",
278 fields: ["title"],
279 is_active: true,
280 },
281 {
282 entity_type: "product_category",
283 fields: ["name"],
284 is_active: true,
285 },
286 {
287 entity_type: "product_collection",
288 fields: ["title"],
289 is_active: true,
290 },
291 ],
292 },
293 adminHeaders
294 )
295
296 const ids = createResponse.data.created.map((s) => s.id)
297
298 const deleteResponse = await api.post(
299 "/admin/translations/settings/batch",
300 {
301 delete: ids,
302 },
303 adminHeaders
304 )
305
306 expect(deleteResponse.status).toEqual(200)
307 expect(deleteResponse.data.deleted).toEqual({
308 ids: expect.arrayContaining(ids),
309 object: "translation_settings",
310 deleted: true,
311 })
312 expect(deleteResponse.data.deleted.ids).toHaveLength(3)
313 })
314
315 it("should handle deleting with empty array", async () => {
316 const response = await api.post(
317 "/admin/translations/settings/batch",
318 {
319 delete: [],
320 },
321 adminHeaders
322 )
323
324 expect(response.status).toEqual(200)
325 expect(response.data.deleted).toEqual({
326 ids: [],
327 object: "translation_settings",
328 deleted: true,
329 })
330 })
331 })
332
333 describe("combined operations", () => {
334 it("should handle create, update, and delete in a single batch", async () => {
335 const setupResponse = await api.post(
336 "/admin/translations/settings/batch",
337 {
338 create: [
339 {
340 entity_type: "product_variant",
341 fields: ["title"],
342 is_active: true,
343 },
344 {
345 entity_type: "product_category",
346 fields: ["name"],
347 is_active: true,
348 },
349 ],
350 },
351 adminHeaders
352 )
353
354 const [settingId1, settingId2] = setupResponse.data.created.map(
355 (s) => s.id
356 )
357
358 const batchResponse = await api.post(
359 "/admin/translations/settings/batch",
360 {
361 create: [
362 {
363 entity_type: "product_collection",
364 fields: ["title"],
365 is_active: true,
366 },
367 ],
368 update: [
369 {
370 id: settingId1,
371 entity_type: "product_variant",
372 fields: ["title", "material"],
373 is_active: false,
374 },
375 ],
376 delete: [settingId2],
377 },
378 adminHeaders
379 )
380
381 expect(batchResponse.status).toEqual(200)
382 expect(batchResponse.data.created).toHaveLength(1)
383 expect(batchResponse.data.updated).toHaveLength(1)
384 expect(batchResponse.data.deleted.ids).toContain(settingId2)
385
386 expect(batchResponse.data.created[0]).toEqual(
387 expect.objectContaining({
388 entity_type: "product_collection",
389 fields: ["title"],
390 is_active: true,
391 })
392 )
393
394 expect(batchResponse.data.updated[0]).toEqual(
395 expect.objectContaining({
396 id: settingId1,
397 fields: ["title", "material"],
398 is_active: false,
399 })
400 )
401 })
402
403 it("should handle empty batch request", async () => {
404 const response = await api.post(
405 "/admin/translations/settings/batch",
406 {
407 create: [],
408 update: [],
409 delete: [],
410 },
411 adminHeaders
412 )
413
414 expect(response.status).toEqual(200)
415 expect(response.data.created).toEqual([])
416 expect(response.data.updated).toEqual([])
417 expect(response.data.deleted.ids).toEqual([])
418 })
419 })
420
421 describe("validation", () => {
422 it("should reject non-translatable entity types", async () => {
423 const error = await api
424 .post(
425 "/admin/translations/settings/batch",
426 {
427 create: [
428 {
429 entity_type: "NonExistentEntity",
430 fields: ["title"],
431 is_active: true,
432 },
433 ],
434 },
435 adminHeaders
436 )
437 .catch((e) => e)
438
439 expect(error.response.status).toEqual(400)
440 expect(error.response.data.message).toContain(
441 "NonExistentEntity is not a translatable entity"
442 )
443 })
444
445 it("should reject invalid fields for translatable entities", async () => {
446 const error = await api
447 .post(
448 "/admin/translations/settings/batch",
449 {
450 create: [
451 {
452 entity_type: "product_variant",
453 fields: ["title", "invalid_field", "another_invalid"],
454 is_active: true,
455 },
456 ],
457 },
458 adminHeaders
459 )
460 .catch((e) => e)
461
462 expect(error.response.status).toEqual(400)
463 expect(error.response.data.message).toContain("product_variant")
464 expect(error.response.data.message).toContain("invalid_field")
465 expect(error.response.data.message).toContain("another_invalid")
466 })
467
468 it("should reject multiple invalid settings in a single batch", async () => {
469 const error = await api
470 .post(
471 "/admin/translations/settings/batch",
472 {
473 create: [
474 {
475 entity_type: "NonExistentEntity",
476 fields: ["title"],
477 is_active: true,
478 },
479 {
480 entity_type: "product_variant",
481 fields: ["title", "invalid_field"],
482 is_active: true,
483 },
484 ],
485 },
486 adminHeaders
487 )
488 .catch((e) => e)
489
490 expect(error.response.status).toEqual(400)
491 expect(error.response.data.message).toContain(
492 "NonExistentEntity is not a translatable entity"
493 )
494 expect(error.response.data.message).toContain("product_variant")
495 expect(error.response.data.message).toContain("invalid_field")
496 })
497
498 it("should accept valid fields for translatable entities", async () => {
499 const response = await api.post(
500 "/admin/translations/settings/batch",
501 {
502 create: [
503 {
504 entity_type: "product_variant",
505 fields: ["title", "material"],
506 is_active: true,
507 },
508 {
509 entity_type: "product_category",
510 fields: ["name", "description"],
511 is_active: true,
512 },
513 {
514 entity_type: "product_collection",
515 fields: ["title"],
516 is_active: true,
517 },
518 ],
519 },
520 adminHeaders
521 )
522
523 expect(response.status).toEqual(200)
524 expect(response.data.created).toHaveLength(3)
525 })
526 })
527 })
528
529 describe("GET /admin/translations/settings", () => {
530 beforeEach(async () => {
531 // Set up some translation settings for testing
532 await api.post(
533 "/admin/translations/settings/batch",
534 {
535 create: [
536 {
537 entity_type: "product_variant",
538 fields: ["title"],
539 is_active: true,
540 },
541 {
542 entity_type: "product_category",
543 fields: ["name", "description"],
544 is_active: true,
545 },
546 {
547 entity_type: "product_collection",
548 fields: ["title"],
549 is_active: false,
550 },
551 ],
552 },
553 adminHeaders
554 )
555 })
556
557 it("should return all translation settings", async () => {
558 const response = await api.get(
559 "/admin/translations/settings",
560 adminHeaders
561 )
562
563 expect(response.status).toEqual(200)
564 expect(response.data.translation_settings).toEqual({
565 product_variant: expect.objectContaining({
566 id: expect.any(String),
567 fields: ["title"],
568 inactive_fields: ["material"],
569 is_active: true,
570 }),
571 product_category: expect.objectContaining({
572 id: expect.any(String),
573 fields: ["name", "description"],
574 inactive_fields: [],
575 is_active: true,
576 }),
577 product_collection: expect.objectContaining({
578 id: expect.any(String),
579 fields: [],
580 inactive_fields: ["title"],
581 is_active: false,
582 }),
583 })
584 })
585
586 it("should return translation settings for a specific entity type", async () => {
587 const response = await api.get(
588 "/admin/translations/settings?entity_type=product_variant",
589 adminHeaders
590 )
591
592 expect(response.status).toEqual(200)
593 expect(response.data.translation_settings).toEqual({
594 product_variant: expect.objectContaining({
595 id: expect.any(String),
596 fields: ["title"],
597 inactive_fields: ["material"],
598 is_active: true,
599 }),
600 })
601 })
602 })
603
604 describe("translated_field_count recomputation", () => {
605 let translationModuleService: any
606
607 beforeEach(async () => {
608 translationModuleService = getContainer().resolve(Modules.TRANSLATION)
609 })
610
611 it("should recompute translated_field_count when setting is activated", async () => {
612 // Create a translation setting that is inactive
613 const createResponse = await api.post(
614 "/admin/translations/settings/batch",
615 {
616 create: [
617 {
618 entity_type: "product_variant",
619 fields: ["title", "material"],
620 is_active: false,
621 },
622 ],
623 },
624 adminHeaders
625 )
626
627 const settingId = createResponse.data.created[0].id
628
629 // Create translations with both fields translated
630 const translationResponse = await api.post(
631 "/admin/translations/batch",
632 {
633 create: [
634 {
635 reference_id: "variant_1",
636 reference: "product_variant",
637 locale_code: "fr-FR",
638 translations: {
639 title: "Titre français",
640 material: "Matériau français",
641 },
642 },
643 ],
644 },
645 adminHeaders
646 )
647
648 const translationId = translationResponse.data.created[0].id
649
650 // Initially, translated_field_count should be 0 since setting is inactive
651 const initialTranslation =
652 await translationModuleService.retrieveTranslation(translationId)
653 expect(initialTranslation.translated_field_count).toEqual(0)
654
655 // Activate the setting
656 await api.post(
657 "/admin/translations/settings/batch",
658 {
659 update: [
660 {
661 id: settingId,
662 is_active: true,
663 },
664 ],
665 },
666 adminHeaders
667 )
668
669 // After activation, translated_field_count should be 2 (both fields are translated)
670 const updatedTranslation =
671 await translationModuleService.retrieveTranslation(translationId)
672 expect(updatedTranslation.translated_field_count).toEqual(2)
673 })
674
675 it("should recompute translated_field_count when setting is deactivated", async () => {
676 // Create an active translation setting
677 const createResponse = await api.post(
678 "/admin/translations/settings/batch",
679 {
680 create: [
681 {
682 entity_type: "product_variant",
683 fields: ["title", "material"],
684 is_active: true,
685 },
686 ],
687 },
688 adminHeaders
689 )
690
691 const settingId = createResponse.data.created[0].id
692
693 // Create translations with both fields translated
694 const translationResponse = await api.post(
695 "/admin/translations/batch",
696 {
697 create: [
698 {
699 reference_id: "variant_2",
700 reference: "product_variant",
701 locale_code: "fr-FR",
702 translations: {
703 title: "Titre français",
704 material: "Matériau français",
705 },
706 },
707 ],
708 },
709 adminHeaders
710 )
711
712 const translationId = translationResponse.data.created[0].id
713
714 // Initially, translated_field_count should be 2 since setting is active
715 const initialTranslation =
716 await translationModuleService.retrieveTranslation(translationId)
717 expect(initialTranslation.translated_field_count).toEqual(2)
718
719 // Deactivate the setting
720 await api.post(
721 "/admin/translations/settings/batch",
722 {
723 update: [
724 {
725 id: settingId,
726 is_active: false,
727 },
728 ],
729 },
730 adminHeaders
731 )
732
733 // After deactivation, translated_field_count should be 0
734 const updatedTranslation =
735 await translationModuleService.retrieveTranslation(translationId)
736 expect(updatedTranslation.translated_field_count).toEqual(0)
737 })
738
739 it("should recompute translated_field_count when fields are added to setting", async () => {
740 // Create a translation setting with only one field
741 const createResponse = await api.post(
742 "/admin/translations/settings/batch",
743 {
744 create: [
745 {
746 entity_type: "product_variant",
747 fields: ["title"],
748 is_active: true,
749 },
750 ],
751 },
752 adminHeaders
753 )
754
755 const settingId = createResponse.data.created[0].id
756
757 // Create translations with both fields translated
758 const translationResponse = await api.post(
759 "/admin/translations/batch",
760 {
761 create: [
762 {
763 reference_id: "variant_3",
764 reference: "product_variant",
765 locale_code: "fr-FR",
766 translations: {
767 title: "Titre français",
768 material: "Matériau français",
769 },
770 },
771 ],
772 },
773 adminHeaders
774 )
775
776 const translationId = translationResponse.data.created[0].id
777
778 // Initially, translated_field_count should be 1 (only title is in active fields)
779 const initialTranslation =
780 await translationModuleService.retrieveTranslation(translationId)
781 expect(initialTranslation.translated_field_count).toEqual(1)
782
783 // Add material field to the setting
784 await api.post(
785 "/admin/translations/settings/batch",
786 {
787 update: [
788 {
789 id: settingId,
790 fields: ["title", "material"],
791 },
792 ],
793 },
794 adminHeaders
795 )
796
797 // After adding material, translated_field_count should be 2
798 const updatedTranslation =
799 await translationModuleService.retrieveTranslation(translationId)
800 expect(updatedTranslation.translated_field_count).toEqual(2)
801 })
802
803 it("should recompute translated_field_count when fields are removed from setting", async () => {
804 // Create a translation setting with both fields
805 const createResponse = await api.post(
806 "/admin/translations/settings/batch",
807 {
808 create: [
809 {
810 entity_type: "product_variant",
811 fields: ["title", "material"],
812 is_active: true,
813 },
814 ],
815 },
816 adminHeaders
817 )
818
819 const settingId = createResponse.data.created[0].id
820
821 // Create translations with both fields translated
822 const translationResponse = await api.post(
823 "/admin/translations/batch",
824 {
825 create: [
826 {
827 reference_id: "variant_4",
828 reference: "product_variant",
829 locale_code: "fr-FR",
830 translations: {
831 title: "Titre français",
832 material: "Matériau français",
833 },
834 },
835 ],
836 },
837 adminHeaders
838 )
839
840 const translationId = translationResponse.data.created[0].id
841
842 // Initially, translated_field_count should be 2
843 const initialTranslation =
844 await translationModuleService.retrieveTranslation(translationId)
845 expect(initialTranslation.translated_field_count).toEqual(2)
846
847 // Remove material field from the setting
848 await api.post(
849 "/admin/translations/settings/batch",
850 {
851 update: [
852 {
853 id: settingId,
854 fields: ["title"],
855 },
856 ],
857 },
858 adminHeaders
859 )
860
861 // After removing material, translated_field_count should be 1
862 const updatedTranslation =
863 await translationModuleService.retrieveTranslation(translationId)
864 expect(updatedTranslation.translated_field_count).toEqual(1)
865 })
866
867 it("should recompute translated_field_count for multiple translations when setting is updated", async () => {
868 // Create a translation setting
869 const createResponse = await api.post(
870 "/admin/translations/settings/batch",
871 {
872 create: [
873 {
874 entity_type: "product_variant",
875 fields: ["title"],
876 is_active: true,
877 },
878 ],
879 },
880 adminHeaders
881 )
882
883 const settingId = createResponse.data.created[0].id
884
885 // Create multiple translations
886 const translationResponse = await api.post(
887 "/admin/translations/batch",
888 {
889 create: [
890 {
891 reference_id: "variant_5",
892 reference: "product_variant",
893 locale_code: "fr-FR",
894 translations: {
895 title: "Titre français",
896 material: "Matériau français",
897 },
898 },
899 {
900 reference_id: "variant_6",
901 reference: "product_variant",
902 locale_code: "de-DE",
903 translations: {
904 title: "Deutscher Titel",
905 material: "Deutsches Material",
906 },
907 },
908 {
909 reference_id: "variant_7",
910 reference: "product_variant",
911 locale_code: "fr-FR",
912 translations: {
913 title: "Autre titre",
914 // material not translated
915 },
916 },
917 ],
918 },
919 adminHeaders
920 )
921
922 // Store translation IDs by reference_id and locale for easy lookup
923 const translationIds = {
924 variant5: translationResponse.data.created.find(
925 (t: any) =>
926 t.reference_id === "variant_5" && t.locale_code === "fr-FR"
927 )!.id,
928 variant6: translationResponse.data.created.find(
929 (t: any) =>
930 t.reference_id === "variant_6" && t.locale_code === "de-DE"
931 )!.id,
932 variant7: translationResponse.data.created.find(
933 (t: any) =>
934 t.reference_id === "variant_7" && t.locale_code === "fr-FR"
935 )!.id,
936 }
937
938 // Initially, all should have translated_field_count = 1 (only title)
939 const initialVariant5 =
940 await translationModuleService.retrieveTranslation(
941 translationIds.variant5
942 )
943 const initialVariant6 =
944 await translationModuleService.retrieveTranslation(
945 translationIds.variant6
946 )
947 const initialVariant7 =
948 await translationModuleService.retrieveTranslation(
949 translationIds.variant7
950 )
951
952 expect(initialVariant5.translated_field_count).toEqual(1)
953 expect(initialVariant6.translated_field_count).toEqual(1)
954 expect(initialVariant7.translated_field_count).toEqual(1)
955
956 // Add material field to the setting
957 const updateResponse = await api.post(
958 "/admin/translations/settings/batch",
959 {
960 update: [
961 {
962 id: settingId,
963 fields: ["title", "material"],
964 },
965 ],
966 },
967 adminHeaders
968 )
969
970 // Verify the setting was updated correctly
971 expect(updateResponse.data.updated[0].fields).toEqual([
972 "title",
973 "material",
974 ])
975
976 // After update, translations with material should have count = 2, others = 1
977 // Use listTranslations to get fresh data (may be filtered but count should be correct)
978 const allUpdatedTranslations =
979 await translationModuleService.listTranslations({
980 reference: "product_variant",
981 })
982
983 const updatedVariant5 = allUpdatedTranslations.find(
984 (t: any) => t.id === translationIds.variant5
985 )
986 const updatedVariant6 = allUpdatedTranslations.find(
987 (t: any) => t.id === translationIds.variant6
988 )
989 const updatedVariant7 = allUpdatedTranslations.find(
990 (t: any) => t.id === translationIds.variant7
991 )
992
993 // variant_5 and variant_6 have both fields translated
994 expect(updatedVariant5.translated_field_count).toEqual(2)
995 expect(updatedVariant6.translated_field_count).toEqual(2)
996 // variant_7 only has title translated
997 expect(updatedVariant7.translated_field_count).toEqual(1)
998 })
999
1000 it("should recompute translated_field_count when both is_active and fields are updated", async () => {
1001 // Create a translation setting that is inactive
1002 const createResponse = await api.post(
1003 "/admin/translations/settings/batch",
1004 {
1005 create: [
1006 {
1007 entity_type: "product_category",
1008 fields: ["name"],
1009 is_active: false,
1010 },
1011 ],
1012 },
1013 adminHeaders
1014 )
1015
1016 const settingId = createResponse.data.created[0].id
1017
1018 // Create translations with both fields translated
1019 const translationResponse = await api.post(
1020 "/admin/translations/batch",
1021 {
1022 create: [
1023 {
1024 reference_id: "category_1",
1025 reference: "product_category",
1026 locale_code: "fr-FR",
1027 translations: {
1028 name: "Nom français",
1029 description: "Description française",
1030 },
1031 },
1032 ],
1033 },
1034 adminHeaders
1035 )
1036
1037 const translationId = translationResponse.data.created[0].id
1038
1039 // Initially, translated_field_count should be 0 since setting is inactive
1040 const initialTranslation =
1041 await translationModuleService.retrieveTranslation(translationId)
1042 expect(initialTranslation.translated_field_count).toEqual(0)
1043
1044 // Activate the setting and add description field
1045 await api.post(
1046 "/admin/translations/settings/batch",
1047 {
1048 update: [
1049 {
1050 id: settingId,
1051 fields: ["name", "description"],
1052 is_active: true,
1053 },
1054 ],
1055 },
1056 adminHeaders
1057 )
1058
1059 // After update, translated_field_count should be 2 (both fields are translated)
1060 const updatedTranslation =
1061 await translationModuleService.retrieveTranslation(translationId)
1062 expect(updatedTranslation.translated_field_count).toEqual(2)
1063 })
1064
1065 it("should handle translations with partial field translations correctly", async () => {
1066 // Create a translation setting with two fields
1067 const createResponse = await api.post(
1068 "/admin/translations/settings/batch",
1069 {
1070 create: [
1071 {
1072 entity_type: "product_variant",
1073 fields: ["title", "material"],
1074 is_active: true,
1075 },
1076 ],
1077 },
1078 adminHeaders
1079 )
1080
1081 const settingId = createResponse.data.created[0].id
1082
1083 // Create translation with only one field translated
1084 const translationResponse = await api.post(
1085 "/admin/translations/batch",
1086 {
1087 create: [
1088 {
1089 reference_id: "variant_8",
1090 reference: "product_variant",
1091 locale_code: "fr-FR",
1092 translations: {
1093 title: "Titre français",
1094 // material not translated
1095 },
1096 },
1097 ],
1098 },
1099 adminHeaders
1100 )
1101
1102 const translationId = translationResponse.data.created[0].id
1103
1104 // Initially, translated_field_count should be 1 (only title is translated)
1105 const initialTranslation =
1106 await translationModuleService.retrieveTranslation(translationId)
1107 expect(initialTranslation.translated_field_count).toEqual(1)
1108
1109 // Remove title from the setting (only material remains)
1110 await api.post(
1111 "/admin/translations/settings/batch",
1112 {
1113 update: [
1114 {
1115 id: settingId,
1116 fields: ["material"],
1117 },
1118 ],
1119 },
1120 adminHeaders
1121 )
1122
1123 // After update, translated_field_count should be 0 (material is not translated)
1124 const updatedTranslation =
1125 await translationModuleService.retrieveTranslation(translationId)
1126 expect(updatedTranslation.translated_field_count).toEqual(0)
1127 })
1128 })
1129 })
1130 },
1131 })