Просмотр исходного кода

feat(server): Add product Facets to import format

Closes #46
Michael Bromley 7 лет назад
Родитель
Сommit
ba2644495c

+ 21 - 0
server/e2e/__snapshots__/import.e2e-spec.ts.snap

@@ -14,6 +14,7 @@ Array [
       },
     ],
     "description": "A great device for stretching paper.",
+    "facetValues": Array [],
     "featuredAsset": Object {
       "id": "T_6",
       "name": "pps1.jpg",
@@ -148,6 +149,7 @@ Array [
   Object {
     "assets": Array [],
     "description": "Mabef description",
+    "facetValues": Array [],
     "featuredAsset": null,
     "id": "T_2",
     "name": "Mabef M/02 Studio Easel",
@@ -192,6 +194,7 @@ Array [
   Object {
     "assets": Array [],
     "description": "Really mega pencils",
+    "facetValues": Array [],
     "featuredAsset": null,
     "id": "T_3",
     "name": "Giotto Mega Pencils",
@@ -283,6 +286,24 @@ Array [
   Object {
     "assets": Array [],
     "description": "Keeps the paint off the clothes",
+    "facetValues": Array [
+      Object {
+        "facet": Object {
+          "id": "T_4",
+          "name": "Material",
+        },
+        "id": "T_16",
+        "name": "Denim",
+      },
+      Object {
+        "facet": Object {
+          "id": "T_3",
+          "name": "Collection",
+        },
+        "id": "T_17",
+        "name": "clothes",
+      },
+    ],
     "featuredAsset": null,
     "id": "T_4",
     "name": "Artists Smock",

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

@@ -1,12 +1,12 @@
-name                    , slug                    , description                          , assets              , optionGroups  , optionValues     , sku    , price , taxCategory , variantAssets   , facets
-Perfect Paper Stretcher , perfect-paper-stretcher , A great device for stretching paper. , "pps1.jpg|pps2.jpg" , size          , Half Imperial    , PPS12  , 45.3  , standard    ,                 , Brand:KB|Type:Accessory
-                        ,                         ,                                      ,                     ,               , Quarter Imperial , PPS14  , 32.5  , standard    ,                 , Brand:KB|Type:Accessory
-                        ,                         ,                                      ,                     ,               , Full Imperial    , PPSF   , 59.5  , standard    ,                 , Brand:KB|Type:Accessory
-Mabef M/02 Studio Easel ,                         , Mabef description                    ,                     ,               ,                  , M02    , 910.7 , standard    ,                 , Brand:Mabef|Type:Easel
-Giotto Mega Pencils     ,                         , Really mega pencils                  ,                     , box size      , Box of 8         , 225400 , 4.16  , standard    , "box-of-8.jpg"  , Collection:Xmas Sale
-                        ,                         ,                                      ,                     ,               , Box of 12        , 225600 , 6.24  , standard    , "box-of-12.jpg" , Collection:Xmas Sale
+name                    , slug                    , description                          , assets              , facets                            , optionGroups  , optionValues     , sku    , price , taxCategory , variantAssets   , variantFacets
+Perfect Paper Stretcher , perfect-paper-stretcher , A great device for stretching paper. , "pps1.jpg|pps2.jpg" ,                                   , size          , Half Imperial    , PPS12  , 45.3  , standard    ,                 , Brand:KB|Type:Accessory
+                        ,                         ,                                      ,                     ,                                   ,               , Quarter Imperial , PPS14  , 32.5  , standard    ,                 , Brand:KB|Type:Accessory
+                        ,                         ,                                      ,                     ,                                   ,               , Full Imperial    , PPSF   , 59.5  , standard    ,                 , Brand:KB|Type:Accessory
+Mabef M/02 Studio Easel ,                         , Mabef description                    ,                     ,                                   ,               ,                  , M02    , 910.7 , standard    ,                 , Brand:Mabef|Type:Easel
+Giotto Mega Pencils     ,                         , Really mega pencils                  ,                     ,                                   , box size      , Box of 8         , 225400 , 4.16  , standard    , "box-of-8.jpg"  , Collection:Xmas Sale
+                        ,                         ,                                      ,                     ,                                   ,               , Box of 12        , 225600 , 6.24  , standard    , "box-of-12.jpg" , Collection:Xmas Sale
 
-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      ,                     , Material:Denim|Collection: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     ,                 ,

+ 9 - 1
server/e2e/import.e2e-spec.ts

@@ -39,7 +39,7 @@ describe('Import resolver', () => {
         const result = await client.importProducts(csvFile);
 
         expect(result.importProducts.errors).toEqual([
-            'Invalid Record Length: header length is 11, got 1 on line 8',
+            'Invalid Record Length: header length is 12, got 1 on line 8',
         ]);
         expect(result.importProducts.imported).toBe(4);
         expect(result.importProducts.processed).toBe(4);
@@ -67,6 +67,14 @@ describe('Import resolver', () => {
                                 code
                                 name
                             }
+                            facetValues {
+                                id
+                                name
+                                facet {
+                                    id
+                                    name
+                                }
+                            }
                             variants {
                                 id
                                 name

+ 22 - 6
server/src/data-import/providers/import-parser/__snapshots__/import-parser.spec.ts.snap

@@ -10,6 +10,7 @@ Array [
         "keywords": "paper, stretch",
       },
       "description": "A great device for stretching paper.",
+      "facets": Array [],
       "name": "Perfect Paper Stretcher",
       "optionGroups": Array [
         Object {
@@ -75,6 +76,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "A great device for stretching paper.",
+      "facets": Array [],
       "name": "Perfect Paper Stretcher",
       "optionGroups": Array [
         Object {
@@ -129,6 +131,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "Mabef description",
+      "facets": Array [],
       "name": "Mabef M/02 Studio Easel",
       "optionGroups": Array [],
       "slug": "mabef-m02-studio-easel",
@@ -150,6 +153,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "Really mega pencils",
+      "facets": Array [],
       "name": "Giotto Mega Pencils",
       "optionGroups": Array [
         Object {
@@ -192,6 +196,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "Keeps the paint off the clothes",
+      "facets": Array [],
       "name": "Artists Smock",
       "optionGroups": Array [
         Object {
@@ -272,6 +277,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "A great device for stretching paper.",
+      "facets": Array [],
       "name": "Perfect Paper Stretcher",
       "optionGroups": Array [
         Object {
@@ -334,6 +340,16 @@ Array [
       ],
       "customFields": Object {},
       "description": "A great device for stretching paper.",
+      "facets": Array [
+        Object {
+          "facet": "brand",
+          "value": "KB",
+        },
+        Object {
+          "facet": "type",
+          "value": "Accessory",
+        },
+      ],
       "name": "Perfect Paper Stretcher",
       "optionGroups": Array [],
       "slug": "perfect-paper-stretcher",
@@ -344,12 +360,8 @@ Array [
         "customFields": Object {},
         "facets": Array [
           Object {
-            "facet": "brand",
-            "value": "KB",
-          },
-          Object {
-            "facet": "type",
-            "value": "Accessory",
+            "facet": "material",
+            "value": "Wood",
           },
         ],
         "optionValues": Array [],
@@ -369,6 +381,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "A great device for stretching paper.",
+      "facets": Array [],
       "name": "Perfect Paper Stretcher",
       "optionGroups": Array [
         Object {
@@ -423,6 +436,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "Mabef description",
+      "facets": Array [],
       "name": "Mabef M/02 Studio Easel",
       "optionGroups": Array [],
       "slug": "mabef-m02-studio-easel",
@@ -444,6 +458,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "Really mega pencils",
+      "facets": Array [],
       "name": "Giotto Mega Pencils",
       "optionGroups": Array [
         Object {
@@ -486,6 +501,7 @@ Array [
       "assetPaths": Array [],
       "customFields": Object {},
       "description": "Keeps the paint off the clothes",
+      "facets": Array [],
       "name": "Artists Smock",
       "optionGroups": Array [
         Object {

+ 3 - 3
server/src/data-import/providers/import-parser/import-parser.spec.ts

@@ -74,7 +74,7 @@ describe('ImportParser', () => {
 
                 expect(result.results).toEqual([]);
                 expect(result.errors).toEqual([
-                    'The import file is missing the following columns: "slug", "assets"',
+                    'The import file is missing the following columns: "slug", "assets", "variantFacets"',
                 ]);
             });
 
@@ -85,8 +85,8 @@ describe('ImportParser', () => {
                 const result = await importParser.parseProducts(input);
 
                 expect(result.errors).toEqual([
-                    'Invalid Record Length: header length is 11, got 9 on line 3',
-                    'Invalid Record Length: header length is 11, got 1 on line 4',
+                    'Invalid Record Length: header length is 12, got 10 on line 3',
+                    'Invalid Record Length: header length is 12, got 1 on line 4',
                 ]);
                 expect(result.results.length).toBe(2);
             });

+ 13 - 3
server/src/data-import/providers/import-parser/import-parser.ts

@@ -10,13 +10,14 @@ export type BaseProductRecord = {
     slug?: string;
     description?: string;
     assets?: string;
+    facets?: string;
     optionGroups?: string;
     optionValues?: string;
     sku?: string;
     price?: string;
     taxCategory?: string;
     variantAssets?: string;
-    facets?: string;
+    variantFacets?: string;
 };
 
 export type RawProductRecord = BaseProductRecord & { [customFieldName: string]: string };
@@ -45,6 +46,10 @@ export interface ParsedProduct {
         name: string;
         values: string[];
     }>;
+    facets: Array<{
+        facet: string;
+        value: string;
+    }>;
     customFields: {
         [name: string]: string;
     };
@@ -66,13 +71,14 @@ const requiredColumns: Array<keyof BaseProductRecord> = [
     'slug',
     'description',
     'assets',
+    'facets',
     'optionGroups',
     'optionValues',
     'sku',
     'price',
     'taxCategory',
     'variantAssets',
-    'facets',
+    'variantFacets',
 ];
 
 /**
@@ -227,6 +233,10 @@ function parseProductFromRecord(r: RawProductRecord): ParsedProduct {
             name: ogName,
             values: [],
         })),
+        facets: parseStringArray(r.facets).map(pair => {
+            const [facet, value] = pair.split(':');
+            return { facet, value };
+        }),
         customFields: parseCustomFields('product', r),
     };
 }
@@ -238,7 +248,7 @@ function parseVariantFromRecord(r: RawProductRecord): ParsedProductVariant {
         price: parseNumber(r.price),
         taxCategory: parseString(r.taxCategory),
         assetPaths: parseStringArray(r.variantAssets),
-        facets: parseStringArray(r.facets).map(pair => {
+        facets: parseStringArray(r.variantFacets).map(pair => {
             const [facet, value] = pair.split(':');
             return { facet, value };
         }),

+ 4 - 4
server/src/data-import/providers/import-parser/test-fixtures/custom-fields.csv

@@ -1,4 +1,4 @@
-name                    , slug , description                          , assets , optionGroups , optionValues     , sku   , price , taxCategory , variantAssets , facets , product:keywords , product:customPage , variant:volumetric
-Perfect Paper Stretcher ,      , A great device for stretching paper. ,        , size         , Half Imperial    , PPS12 , 45.3  , standard    ,               ,        , "paper, stretch" , grid-view          , 243
-                        ,      ,                                      ,        ,              , Quarter Imperial , PPS14 , 32.5  , standard    ,               ,        ,                  ,                    , 344
-                        ,      ,                                      ,        ,              , Full Imperial    , PPSF  , 59.5  , standard    ,               ,        ,                  ,                    , 656
+name                    , slug , description                          , assets , facets , optionGroups , optionValues     , sku   , price , taxCategory , variantAssets , variantFacets , product:keywords , product:customPage , variant:volumetric
+Perfect Paper Stretcher ,      , A great device for stretching paper. ,        ,        , size         , Half Imperial    , PPS12 , 45.3  , standard    ,               ,               , "paper, stretch" , grid-view          , 243
+                        ,      ,                                      ,        ,        ,              , Quarter Imperial , PPS14 , 32.5  , standard    ,               ,               ,                  ,                    , 344
+                        ,      ,                                      ,        ,        ,              , Full Imperial    , PPSF  , 59.5  , standard    ,               ,               ,                  ,                    , 656

+ 5 - 5
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" , facets
-"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"    , ""              ,
+"name"          , "slug" , "description"                     , "assets" , facets , "optionGroups" , "optionValues" , "sku"   , "price" , "taxCategory" , "variantAssets" , variantFacets
+"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/invalid-row-length.csv

@@ -1,5 +1,5 @@
-name                    , slug , description                     , assets , optionGroups , optionValues , sku      , price , taxCategory , variantAssets , facets
-Mabef M/02 Studio Easel ,      , Mabef description               ,        ,              ,              , M02      , 910.7 , standard    ,               ,
-Giotto Mega Pencils     ,      , Really mega pencils             ,        , 225400       , 4.16         , standard ,       ,
+name                    , slug , description                     , assets , facets , optionGroups , optionValues , sku      , price , taxCategory , variantAssets , variantFacets
+Mabef M/02 Studio Easel ,      , Mabef description               ,        ,        ,              ,              , M02      , 910.7 , standard    ,               ,
+Giotto Mega Pencils     ,      , Really mega pencils             ,        ,        , 225400       , 4.16         , standard ,       ,
 
-Artists Smock           ,      , Keeps the paint off the clothes ,        ,              ,              , 10112    , 11.99 , standard    ,               ,
+Artists Smock           ,      , Keeps the paint off the clothes ,        ,        ,              ,              , 10112    , 11.99 , standard    ,               ,

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

@@ -1,11 +1,11 @@
-name                    , slug                    , description                          , assets , optionGroups  , optionValues     , sku    , price , taxCategory , variantAssets , facets
-Perfect Paper Stretcher , Perfect-paper-stretcher , A great device for stretching paper. ,        , 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 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    ,               ,
+name                    , slug                    , description                          , assets , facets , optionGroups  , optionValues     , sku    , price , taxCategory , variantAssets , variantFacets
+Perfect Paper Stretcher , Perfect-paper-stretcher , A great device for stretching paper. ,        ,        , 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 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    ,               ,

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

@@ -1,4 +1,4 @@
-name                    , slug , description                          , assets , optionGroups , optionValues     , sku   , price , taxCategory , variantAssets , facets
-Perfect Paper Stretcher ,      , A great device for stretching paper. ,        , size         , Half Imperial    , PPS12 , 45.3  , standard    ,               ,
-                        ,      ,                                      ,        ,              , Quarter Imperial , PPS14 , 32.5  , standard    ,               ,
-                        ,      ,                                      ,        ,              , Full Imperial    , PPSF  , 59.5  , standard    ,               ,
+name                    , slug , description                          , assets , facets , optionGroups , optionValues     , sku   , price , taxCategory , variantAssets , variantFacets
+Perfect Paper Stretcher ,      , A great device for stretching paper. ,        ,        , size         , Half Imperial    , PPS12 , 45.3  , standard    ,               ,
+                        ,      ,                                      ,        ,        ,              , Quarter Imperial , PPS14 , 32.5  , standard    ,               ,
+                        ,      ,                                      ,        ,        ,              , Full Imperial    , PPSF  , 59.5  , standard    ,               ,

+ 2 - 2
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 , facets
-Perfect Paper Stretcher ,      , A great device for stretching paper. , "pps1.jpg|pps2.jpg" ,              ,              , PPS12 , 45.3  , standard    ,               , brand:KB|type:Accessory
+name                    , slug , description                          , assets              , facets                  , optionGroups , optionValues , sku   , price , taxCategory , variantAssets , variantFacets
+Perfect Paper Stretcher ,      , A great device for stretching paper. , "pps1.jpg|pps2.jpg" , brand:KB|type:Accessory ,              ,              , PPS12 , 45.3  , standard    ,               , material:Wood

+ 1 - 0
server/src/data-import/providers/importer/importer.ts

@@ -142,6 +142,7 @@ export class Importer {
             const createdProduct = await this.productService.create(ctx, {
                 featuredAssetId: productAssets.length ? (productAssets[0].id as string) : undefined,
                 assetIds: productAssets.map(a => a.id) as string[],
+                facetValueIds: await this.getFacetValueIds(product.facets, languageCode),
                 translations: [
                     {
                         languageCode,