Browse Source

fix(core): Fix fetching entities with a single localeString custom field

Michael Bromley 6 years ago
parent
commit
0d0545f8db

+ 30 - 13
packages/core/e2e/custom-fields.e2e-spec.ts

@@ -90,6 +90,12 @@ describe('Custom fields', () => {
                             public: true,
                         },
                     ],
+                    Facet: [
+                        {
+                            name: 'translated',
+                            type: 'localeString',
+                        },
+                    ],
                 },
             },
         );
@@ -165,6 +171,28 @@ describe('Custom fields', () => {
         });
     });
 
+    it('get entity with localeString only', async () => {
+        const { facet } = await adminClient.query(gql`
+            query {
+                facet(id: "T_1") {
+                    id
+                    name
+                    customFields {
+                        translated
+                    }
+                }
+            }
+        `);
+
+        expect(facet).toEqual({
+            id: 'T_1',
+            name: 'category',
+            customFields: {
+                translated: null,
+            },
+        });
+    });
+
     it('get fields with default values', async () => {
         const { product } = await adminClient.query(gql`
             query {
@@ -380,15 +408,10 @@ describe('Custom fields', () => {
     });
 
     describe('sort & filter', () => {
-
         it('can sort by custom fields', async () => {
             const { products } = await adminClient.query(gql`
                 query {
-                    products(options: {
-                        sort: {
-                            nullable: ASC
-                        }
-                    }) {
+                    products(options: { sort: { nullable: ASC } }) {
                         totalItems
                     }
                 }
@@ -400,13 +423,7 @@ describe('Custom fields', () => {
         it('can filter by custom fields', async () => {
             const { products } = await adminClient.query(gql`
                 query {
-                    products(options: {
-                        filter: {
-                            stringWithDefault: {
-                                contains: "hello"
-                            }
-                        }
-                    }) {
+                    products(options: { filter: { stringWithDefault: { contains: "hello" } } }) {
                         totalItems
                     }
                 }

+ 4 - 1
packages/core/src/service/helpers/utils/translate-entity.ts

@@ -58,7 +58,10 @@ export function translateEntity<T extends Translatable>(
 
     for (const [key, value] of Object.entries(translation)) {
         if (key === 'customFields') {
-            Object.assign(translated[key], value);
+            if (!translated.customFields) {
+                translated.customFields = {};
+            }
+            Object.assign(translated.customFields, value);
         } else if (key !== 'base' && key !== 'id') {
             translated[key] = value;
         }