Forráskód Böngészése

fix(core): Importing custom boolean field works as intended (#1908)

Ariel Barabas 3 éve
szülő
commit
51145638f3

+ 11 - 0
packages/core/e2e/__snapshots__/import.e2e-spec.ts.snap

@@ -49,6 +49,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": null,
         "weight": 100,
       },
       "featuredAsset": null,
@@ -69,6 +70,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": null,
         "weight": 100,
       },
       "featuredAsset": null,
@@ -89,6 +91,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": null,
         "weight": 100,
       },
       "featuredAsset": null,
@@ -137,6 +140,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": null,
         "weight": 300,
       },
       "featuredAsset": null,
@@ -198,6 +202,7 @@ Object {
         },
       ],
       "customFields": Object {
+        "valid": null,
         "weight": 200,
       },
       "featuredAsset": Object {
@@ -230,6 +235,7 @@ Object {
         },
       ],
       "customFields": Object {
+        "valid": null,
         "weight": 200,
       },
       "featuredAsset": Object {
@@ -291,6 +297,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": true,
         "weight": 500,
       },
       "featuredAsset": null,
@@ -311,6 +318,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": false,
         "weight": 500,
       },
       "featuredAsset": null,
@@ -331,6 +339,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": null,
         "weight": 500,
       },
       "featuredAsset": null,
@@ -351,6 +360,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": true,
         "weight": 500,
       },
       "featuredAsset": null,
@@ -371,6 +381,7 @@ Object {
     Object {
       "assets": Array [],
       "customFields": Object {
+        "valid": false,
         "weight": null,
       },
       "featuredAsset": null,

+ 12 - 12
packages/core/e2e/fixtures/product-import.csv

@@ -1,13 +1,13 @@
-name                   ,slug                   ,description                         ,assets             ,facets                           ,optionGroups ,optionValues    ,sku   ,price,taxCategory,stockOnHand,trackInventory,variantAssets  ,variantFacets          ,product:pageType,variant:weight,product:owner,product:keywords,product:localName
-Perfect Paper Stretcher,perfect-paper-stretcher,A great device for stretching paper.,"pps1.jpg|pps2.jpg",                                 ,size         ,Half Imperial   ,PPS12 ,45.3 ,standard   ,0          ,false         ,               ,Brand:KB|Type:Accessory,default         ,100           ,"{""id"": 1}",paper|stretching|watercolor,localPPS
-                       ,                       ,                                    ,                   ,                                 ,             ,Quarter Imperial,PPS14 ,32.5 ,standard   ,0          ,false         ,               ,Brand:KB|Type:Accessory,                ,100           ,"{""id"": 1}",,
-                       ,                       ,                                    ,                   ,                                 ,             ,Full Imperial   ,PPSF  ,59.5 ,standard   ,-10        ,false         ,               ,Brand:KB|Type:Accessory,                ,100           ,"{""id"": 1}",,
-Mabef M/02 Studio Easel,                       ,Mabef description                   ,                   ,                                 ,             ,                ,M02   ,910.7,standard   ,100        ,false         ,               ,Brand:Mabef|Type:Easel ,expanded        ,300           ,"{""id"": 1}",,localMabef
-Giotto Mega Pencils    ,                       ,Really mega pencils                 ,                   ,                                 ,box size     ,Box of 8        ,225400,4.16 ,standard   ,           ,false         ,"box-of-8.jpg" ,Collection:Xmas Sale   ,default         ,200           ,"{""id"": 1}",,localGiotto
-                       ,                       ,                                    ,                   ,                                 ,             ,Box of 12       ,225600,6.24 ,standard   ,           ,false         ,"box-of-12.jpg",Collection:Xmas Sale   ,                ,200           ,"{""id"": 1}",,
+name                   ,slug                   ,description                         ,assets             ,facets                           ,optionGroups ,optionValues    ,sku   ,price,taxCategory,stockOnHand,trackInventory,variantAssets  ,variantFacets          ,product:pageType,variant:weight,product:owner,product:keywords           ,product:localName,variant:valid
+Perfect Paper Stretcher,perfect-paper-stretcher,A great device for stretching paper.,"pps1.jpg|pps2.jpg",                                 ,size         ,Half Imperial   ,PPS12 ,45.3 ,standard   ,0          ,false         ,               ,Brand:KB|Type:Accessory,default         ,100           ,"{""id"": 1}",paper|stretching|watercolor,localPPS         ,
+                       ,                       ,                                    ,                   ,                                 ,             ,Quarter Imperial,PPS14 ,32.5 ,standard   ,0          ,false         ,               ,Brand:KB|Type:Accessory,                ,100           ,"{""id"": 1}",                           ,                 ,
+                       ,                       ,                                    ,                   ,                                 ,             ,Full Imperial   ,PPSF  ,59.5 ,standard   ,-10        ,false         ,               ,Brand:KB|Type:Accessory,                ,100           ,"{""id"": 1}",                           ,                 ,
+Mabef M/02 Studio Easel,                       ,Mabef description                   ,                   ,                                 ,             ,                ,M02   ,910.7,standard   ,100        ,false         ,               ,Brand:Mabef|Type:Easel ,expanded        ,300           ,"{""id"": 1}",                           ,localMabef       ,
+Giotto Mega Pencils    ,                       ,Really mega pencils                 ,                   ,                                 ,box size     ,Box of 8        ,225400,4.16 ,standard   ,           ,false         ,"box-of-8.jpg" ,Collection:Xmas Sale   ,default         ,200           ,"{""id"": 1}",                           ,localGiotto      ,
+                       ,                       ,                                    ,                   ,                                 ,             ,Box of 12       ,225600,6.24 ,standard   ,           ,false         ,"box-of-12.jpg",Collection:Xmas Sale   ,                ,200           ,"{""id"": 1}",                           ,                 ,
 
-Artists Smock          ,                       ,Keeps the paint off the clothes     ,                   ,Material:Denim|Collection:clothes,"size|colour","small|beige"   ,10112 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",apron|clothing,localSmock
-                       ,                       ,                                    ,                   ,                                 ,             ,"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}",,
+Artists Smock          ,                       ,Keeps the paint off the clothes     ,                   ,Material:Denim|Collection:clothes,"size|colour","small|beige"   ,10112 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",apron|clothing             ,localSmock       ,true
+                       ,                       ,                                    ,                   ,                                 ,             ,"large|beige"   ,10113 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",                           ,                 ,false
+                       ,                       ,                                    ,                   ,                                 ,             ,"small|navy"    ,10114 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",                           ,                 ,
+                       ,                       ,                                    ,                   ,                                 ,             ,"large|navy"    ,10115 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,500           ,"{""id"": 1}",                           ,                 ,true
+                       ,                       ,                                    ,                   ,                                 ,             ,"large|navy"    ,10115 ,11.99,reduced    ,           ,false         ,               ,                       ,default         ,              ,"{""id"": 1}",                           ,                 ,false

+ 11 - 2
packages/core/e2e/import.e2e-spec.ts

@@ -35,7 +35,10 @@ describe('Import resolver', () => {
                     type: 'localeString',
                 },
             ],
-            ProductVariant: [{ type: 'int', name: 'weight' }],
+            ProductVariant: [
+                { type: 'boolean', name: 'valid' },
+                { type: 'int', name: 'weight' },
+            ],
         },
     });
 
@@ -82,7 +85,7 @@ describe('Import resolver', () => {
         });
 
         expect(result.importProducts.errors).toEqual([
-            'Invalid Record Length: header length is 19, got 1 on line 8',
+            'Invalid Record Length: header length is 20, got 1 on line 8',
         ]);
         expect(result.importProducts.imported).toBe(4);
         expect(result.importProducts.processed).toBe(4);
@@ -176,6 +179,7 @@ describe('Import resolver', () => {
                                     }
                                 }
                                 customFields {
+                                    valid
                                     weight
                                 }
                             }
@@ -238,10 +242,15 @@ describe('Import resolver', () => {
         expect(smock.customFields.owner.id).toBe('T_1');
 
         // Import non-list custom fields
+        expect(smock.variants[0].customFields.valid).toEqual(true);
         expect(smock.variants[0].customFields.weight).toEqual(500);
+        expect(smock.variants[1].customFields.valid).toEqual(false);
         expect(smock.variants[1].customFields.weight).toEqual(500);
+        expect(smock.variants[2].customFields.valid).toEqual(null);
         expect(smock.variants[2].customFields.weight).toEqual(500);
+        expect(smock.variants[3].customFields.valid).toEqual(true);
         expect(smock.variants[3].customFields.weight).toEqual(500);
+        expect(smock.variants[4].customFields.valid).toEqual(false);
         expect(smock.variants[4].customFields.weight).toEqual(null);
 
         // Import list custom fields

+ 4 - 1
packages/core/e2e/populate.e2e-spec.ts

@@ -57,7 +57,10 @@ describe('populate() function', () => {
                     type: 'localeString',
                 },
             ],
-            ProductVariant: [{ type: 'int', name: 'weight' }],
+            ProductVariant: [
+                { type: 'boolean', name: 'valid' },
+                { type: 'int', name: 'weight' },
+            ],
         },
     });
 

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

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