Przeglądaj źródła

fix(admin-ui): Fix saving relation custom fields on ProductVariants

Michael Bromley 4 lat temu
rodzic
commit
fb38c68cba

+ 62 - 0
packages/admin-ui/src/lib/core/src/data/utils/transform-relation-custom-field-inputs.spec.ts

@@ -88,4 +88,66 @@ describe('transformRelationCustomFieldInput()', () => {
             },
         } as any);
     });
+
+    it('transforms input object', () => {
+        const config: CustomFieldConfig[] = [
+            { name: 'weight', type: 'int', list: false },
+            { name: 'avatar', type: 'relation', list: false, entity: 'Asset' },
+        ];
+        const entity = {
+            id: 1,
+            name: 'test',
+            customFields: {
+                weight: 500,
+                avatar: {
+                    id: 123,
+                    preview: '...',
+                },
+            },
+        };
+
+        const result = transformRelationCustomFieldInputs({ input: entity }, config);
+        expect(result).toEqual({
+            input: {
+                id: 1,
+                name: 'test',
+                customFields: {
+                    weight: 500,
+                    avatarId: 123,
+                },
+            },
+        } as any);
+    });
+
+    it('transforms input array (as in UpdateProductVariantsInput)', () => {
+        const config: CustomFieldConfig[] = [
+            { name: 'weight', type: 'int', list: false },
+            { name: 'avatar', type: 'relation', list: false, entity: 'Asset' },
+        ];
+        const entity = {
+            id: 1,
+            name: 'test',
+            customFields: {
+                weight: 500,
+                avatar: {
+                    id: 123,
+                    preview: '...',
+                },
+            },
+        };
+
+        const result = transformRelationCustomFieldInputs({ input: [entity] }, config);
+        expect(result).toEqual({
+            input: [
+                {
+                    id: 1,
+                    name: 'test',
+                    customFields: {
+                        weight: 500,
+                        avatarId: 123,
+                    },
+                },
+            ],
+        } as any);
+    });
 });

+ 11 - 6
packages/admin-ui/src/lib/core/src/data/utils/transform-relation-custom-field-inputs.ts

@@ -7,12 +7,17 @@ import { CustomFieldConfig } from '../../common/generated-types';
  * Transforms any custom field "relation" type inputs into the corresponding `<name>Id` format,
  * as expected by the server.
  */
-export function transformRelationCustomFieldInputs<T extends { input?: any } & Record<string, any> = any>(
-    variables: T,
-    customFieldConfig: CustomFieldConfig[],
-): T {
+export function transformRelationCustomFieldInputs<
+    T extends { input?: Record<string, any> | Array<Record<string, any>> } & Record<string, any> = any
+>(variables: T, customFieldConfig: CustomFieldConfig[]): T {
     if (variables.input) {
-        transformRelations(variables.input, customFieldConfig);
+        if (Array.isArray(variables.input)) {
+            for (const item of variables.input) {
+                transformRelations(item, customFieldConfig);
+            }
+        } else {
+            transformRelations(variables.input, customFieldConfig);
+        }
     }
     return transformRelations(variables, customFieldConfig);
 }
@@ -22,7 +27,7 @@ export function transformRelationCustomFieldInputs<T extends { input?: any } & R
  * When persisting custom fields, we need to send just the IDs of the relations,
  * rather than the objects themselves.
  */
-function transformRelations(input: any, customFieldConfig: CustomFieldConfig[]) {
+function transformRelations<T>(input: T, customFieldConfig: CustomFieldConfig[]) {
     for (const field of customFieldConfig) {
         if (field.type === 'relation') {
             if (hasCustomFields(input)) {