Browse Source

refactor(server): Use pipe rather than comma as separator in import csv

Michael Bromley 7 years ago
parent
commit
484ddcc917

+ 5 - 5
server/e2e/fixtures/product-import.csv

@@ -1,12 +1,12 @@
 name                    , slug                    , description                          , assets               , optionGroups   , optionValues     , sku    , price , taxCategory , variantAssets
-Perfect Paper Stretcher , perfect-paper-stretcher , A great device for stretching paper. , "pps1.jpg, pps2.jpg" , size           , Half Imperial    , PPS12  , 45.3  , standard    ,
+Perfect Paper Stretcher , perfect-paper-stretcher , A great device for stretching paper. , "pps1.jpg|pps2.jpg" , size           , Half Imperial    , PPS12  , 45.3  , standard    ,
                         ,                         ,                                      ,                      ,                , Quarter Imperial , PPS14  , 32.5  , standard    ,
                         ,                         ,                                      ,                      ,                , Full Imperial    , PPSF   , 59.5  , standard    ,
 Mabef M/02 Studio Easel ,                         , Mabef description                    ,                      ,                ,                  , M02    , 910.7 , standard    ,
 Giotto Mega Pencils     ,                         , Really mega pencils                  ,                      , box size       , Box of 8         , 225400 , 4.16  , standard    , "box-of-8.jpg"
                         ,                         ,                                      ,                      ,                , Box of 12        , 225600 , 6.24  , standard    , "box-of-12.jpg"
 
-Artists Smock           ,                         , Keeps the paint off the clothes      ,                      , "size, colour" , "small, beige"   , 10112  , 11.99 , reduced     ,
-                        ,                         ,                                      ,                      ,                , "large, beige"   , 10113  , 11.99 , reduced     ,
-                        ,                         ,                                      ,                      ,                , "small, navy"    , 10114  , 11.99 , reduced     ,
-                        ,                         ,                                      ,                      ,                , "large, navy"    , 10115  , 11.99 , reduced     ,
+Artists Smock           ,                         , Keeps the paint off the clothes      ,                      , "size|colour" , "small|beige"   , 10112  , 11.99 , reduced     ,
+                        ,                         ,                                      ,                      ,                , "large|beige"   , 10113  , 11.99 , reduced     ,
+                        ,                         ,                                      ,                      ,                , "small|navy"    , 10114  , 11.99 , reduced     ,
+                        ,                         ,                                      ,                      ,                , "large|navy"    , 10115  , 11.99 , reduced     ,

+ 11 - 8
server/src/data-import/providers/import-parser/import-parser.ts

@@ -44,6 +44,7 @@ export interface ParsedProductWithVariants {
 export interface ParseResult<T> {
     results: T[];
     errors: string[];
+    processed: number;
 }
 
 const requiredColumns: Array<keyof RawProductRecord> = [
@@ -80,9 +81,9 @@ export class ImportParser {
                     if (records) {
                         const parseResult = this.processRawRecords(records);
                         errors = errors.concat(parseResult.errors);
-                        resolve({ results: parseResult.results, errors });
+                        resolve({ results: parseResult.results, errors, processed: parseResult.processed });
                     } else {
-                        resolve({ results: [], errors });
+                        resolve({ results: [], errors, processed: 0 });
                     }
                 });
             } else {
@@ -101,7 +102,7 @@ export class ImportParser {
                 parser.on('end', () => {
                     const parseResult = this.processRawRecords(records);
                     errors = errors.concat(parseResult.errors);
-                    resolve({ results: parseResult.results, errors });
+                    resolve({ results: parseResult.results, errors, processed: parseResult.processed });
                 });
             }
         });
@@ -111,13 +112,15 @@ export class ImportParser {
         const results: ParsedProductWithVariants[] = [];
         const errors: string[] = [];
         let currentRow: ParsedProductWithVariants | undefined;
-        const headerRow = records.shift() as string[];
+        const headerRow = records[0];
+        const rest = records.slice(1);
+        const totalProducts = rest.map(row => row[0]).filter(name => name.trim() !== '').length;
         const columnError = validateRequiredColumns(headerRow);
         if (columnError) {
-            return { results: [], errors: [columnError] };
+            return { results: [], errors: [columnError], processed: 0 };
         }
         let line = 1;
-        for (const record of records) {
+        for (const record of rest) {
             line++;
             const columnCountError = validateColumnCount(headerRow, record);
             if (columnCountError) {
@@ -148,7 +151,7 @@ export class ImportParser {
             populateOptionGroupValues(currentRow);
             results.push(currentRow);
         }
-        return { results, errors };
+        return { results, errors, processed: totalProducts };
     }
 }
 
@@ -231,7 +234,7 @@ function parseNumber(input?: string): number {
     return +(input || '').trim();
 }
 
-function parseStringArray(input?: string, separator = ','): string[] {
+function parseStringArray(input?: string, separator = '|'): string[] {
     return (input || '')
         .trim()
         .split(separator)

+ 4 - 4
server/src/data-import/providers/import-parser/test-fixtures/invalid-columns.csv

@@ -1,5 +1,5 @@
 "name"          , "description"                     , "optionGroups" , "optionValues" , "sku"   , "price" , "taxCategory" , "variantAssets"
-"Artists Smock" , "Keeps the paint off the clothes" , "size, colour" , "small, beige" , "10112" , "11.99" , "standard"    , ""
-""              , ""                                , ""             , "large, beige" , "10113" , "11.99" , "standard"    , ""
-""              , ""                                , ""             , "small, navy"  , "10114" , "11.99" , "standard"    , ""
-""              , ""                                , ""             , "large, navy"  , "10115" , "11.99" , "standard"    , ""
+"Artists Smock" , "Keeps the paint off the clothes" , "size|colour" , "small|beige" , "10112" , "11.99" , "standard"    , ""
+""              , ""                                , ""             , "large|beige" , "10113" , "11.99" , "standard"    , ""
+""              , ""                                , ""             , "small|navy"  , "10114" , "11.99" , "standard"    , ""
+""              , ""                                , ""             , "large|navy"  , "10115" , "11.99" , "standard"    , ""

+ 4 - 4
server/src/data-import/providers/import-parser/test-fixtures/invalid-option-values.csv

@@ -1,5 +1,5 @@
 "name"          , "slug" , "description"                     , "assets" , "optionGroups" , "optionValues" , "sku"   , "price" , "taxCategory" , "variantAssets"
-"Artists Smock" , ""     , "Keeps the paint off the clothes" , ""       , "size"         , "small, beige" , "10112" , "11.99" , "standard"    , ""
-""              , ""     , ""                                , ""       , ""             , "large, beige" , "10113" , "11.99" , "standard"    , ""
-""              , ""     , ""                                , ""       , ""             , "small, navy"  , "10114" , "11.99" , "standard"    , ""
-""              , ""     , ""                                , ""       , ""             , "large, navy"  , "10115" , "11.99" , "standard"    , ""
+"Artists Smock" , ""     , "Keeps the paint off the clothes" , ""       , "size"         , "small|beige" , "10112" , "11.99" , "standard"    , ""
+""              , ""     , ""                                , ""       , ""             , "large|beige" , "10113" , "11.99" , "standard"    , ""
+""              , ""     , ""                                , ""       , ""             , "small|navy"  , "10114" , "11.99" , "standard"    , ""
+""              , ""     , ""                                , ""       , ""             , "large|navy"  , "10115" , "11.99" , "standard"    , ""

+ 4 - 4
server/src/data-import/providers/import-parser/test-fixtures/multiple-products-multiple-variants.csv

@@ -5,7 +5,7 @@ Perfect Paper Stretcher , Perfect-paper-stretcher , A great device for stretchin
 Mabef M/02 Studio Easel ,                         , Mabef description                    ,        ,                ,                  , M02    , 910.7 , standard    ,
 Giotto Mega Pencils     ,                         , Really mega pencils                  ,        , box size       , Box of 8         , 225400 , 4.16  , standard    ,
                         ,                         ,                                      ,        ,                , Box of 12        , 225600 , 6.24  , standard    ,
-Artists Smock           ,                         , Keeps the paint off the clothes      ,        , "size, colour" , "small, beige"   , 10112  , 11.99 , standard    ,
-                        ,                         ,                                      ,        ,                , "large, beige"   , 10113  , 11.99 , standard    ,
-                        ,                         ,                                      ,        ,                , "small, navy"    , 10114  , 11.99 , standard    ,
-                        ,                         ,                                      ,        ,                , "large, navy"    , 10115  , 11.99 , standard    ,
+Artists Smock           ,                         , Keeps the paint off the clothes      ,        , "size|colour" ,  "small|beige"   , 10112  , 11.99 , standard    ,
+                        ,                         ,                                      ,        ,                , "large|beige"   , 10113  , 11.99 , standard    ,
+                        ,                         ,                                      ,        ,                , "small|navy"    , 10114  , 11.99 , standard    ,
+                        ,                         ,                                      ,        ,                , "large|navy"    , 10115  , 11.99 , standard    ,

+ 1 - 1
server/src/data-import/providers/import-parser/test-fixtures/single-product-single-variant.csv

@@ -1,2 +1,2 @@
 name                    , slug                    , description                          , assets , optionGroups , optionValues , sku   , price , taxCategory , variantAssets
-Perfect Paper Stretcher ,  , A great device for stretching paper. , "pps1.jpg, pps2.jpg" ,              ,              , PPS12 , 45.3  , standard    ,
+Perfect Paper Stretcher ,  , A great device for stretching paper. , "pps1.jpg|pps2.jpg" ,              ,              , PPS12 , 45.3  , standard    ,