Browse Source

fix(core): Truthy check for custom fields in importer

Added a truthy check in the processCustomFieldValues function
in the data importer so that no attempts are made to write empty strings
into the database (e.g. for float or int fields)
Daniel Stehlik 3 years ago
parent
commit
a8c44d1e67

+ 23 - 3
packages/core/e2e/__snapshots__/import.e2e-spec.ts.snap

@@ -368,6 +368,26 @@ Object {
       },
       "trackInventory": "FALSE",
     },
+    Object {
+      "assets": Array [],
+      "customFields": Object {
+        "weight": null,
+      },
+      "featuredAsset": null,
+      "id": "T_11",
+      "name": "Artists Smock large navy",
+      "price": 1199,
+      "sku": "10115",
+      "stockMovements": Object {
+        "items": Array [],
+      },
+      "stockOnHand": 0,
+      "taxCategory": Object {
+        "id": "T_2",
+        "name": "Reduced Tax",
+      },
+      "trackInventory": "FALSE",
+    },
   ],
 }
 `;
@@ -402,7 +422,7 @@ Object {
         "weight": 243,
       },
       "featuredAsset": null,
-      "id": "T_11",
+      "id": "T_12",
       "name": "奇妙的纸张拉伸器 半英制",
       "price": 4530,
       "sku": "PPS12",
@@ -428,7 +448,7 @@ Object {
         "weight": 344,
       },
       "featuredAsset": null,
-      "id": "T_12",
+      "id": "T_13",
       "name": "奇妙的纸张拉伸器 四分之一英制",
       "price": 3250,
       "sku": "PPS14",
@@ -454,7 +474,7 @@ Object {
         "weight": 656,
       },
       "featuredAsset": null,
-      "id": "T_13",
+      "id": "T_14",
       "name": "奇妙的纸张拉伸器 全英制",
       "price": 5950,
       "sku": "PPSF",

+ 1 - 0
packages/core/e2e/fixtures/product-import.csv

@@ -10,3 +10,4 @@ Artists Smock          ,                       ,Keeps the paint off the clothes
                        ,                       ,                                    ,                   ,                                 ,             ,"large|beige"   ,10113 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",,
                        ,                       ,                                    ,                   ,                                 ,             ,"small|navy"    ,10114 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",,
                        ,                       ,                                    ,                   ,                                 ,             ,"large|navy"    ,10115 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",,
+                       ,                       ,                                    ,                   ,                                 ,             ,"large|navy"    ,10115 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,              ,"{""id"": 1}",,

+ 7 - 0
packages/core/e2e/import.e2e-spec.ts

@@ -235,6 +235,13 @@ describe('Import resolver', () => {
         expect(pencils.customFields.owner.id).toBe('T_1');
         expect(smock.customFields.owner.id).toBe('T_1');
 
+        // Import non-list custom fields
+        expect(smock.variants[0].customFields.weight).toEqual(500);
+        expect(smock.variants[1].customFields.weight).toEqual(500);
+        expect(smock.variants[2].customFields.weight).toEqual(500);
+        expect(smock.variants[3].customFields.weight).toEqual(500);
+        expect(smock.variants[4].customFields.weight).toEqual(null);
+
         // Import list custom fields
         expect(paperStretcher.customFields.keywords).toEqual(['paper', 'stretching', 'watercolor']);
         expect(easel.customFields.keywords).toEqual([]);

+ 6 - 3
packages/core/src/data-import/providers/importer/importer.ts

@@ -363,11 +363,14 @@ export class Importer {
     }
 
     private processCustomFieldValues(customFields: { [field: string]: string }, config: CustomFieldConfig[]) {
-        const processed: { [field: string]: string | string[] } = {};
+        const processed: { [field: string]: string | string[] | undefined } = {};
         for (const fieldDef of config) {
             const value = customFields[fieldDef.name];
-            processed[fieldDef.name] =
-                fieldDef.list === true ? value?.split('|').filter(val => val.trim() !== '') : value;
+            if (fieldDef.list === true) {
+                processed[fieldDef.name] = value?.split('|').filter(val => val.trim() !== '');
+            } else {
+                processed[fieldDef.name] = value ? value : undefined;
+            }
         }
         return processed;
     }