Procházet zdrojové kódy

feat(server): Generate slug on import if none specified

Michael Bromley před 7 roky
rodič
revize
d09ccbacea

+ 3 - 3
server/e2e/__snapshots__/import.e2e-spec.ts.snap

@@ -86,7 +86,7 @@ Array [
     "id": "T_2",
     "id": "T_2",
     "name": "Mabef M/02 Studio Easel",
     "name": "Mabef M/02 Studio Easel",
     "optionGroups": Array [],
     "optionGroups": Array [],
-    "slug": "",
+    "slug": "mabef-m02-studio-easel",
     "variants": Array [
     "variants": Array [
       Object {
       Object {
         "id": "T_4",
         "id": "T_4",
@@ -114,7 +114,7 @@ Array [
         "name": "box size",
         "name": "box size",
       },
       },
     ],
     ],
-    "slug": "",
+    "slug": "giotto-mega-pencils",
     "variants": Array [
     "variants": Array [
       Object {
       Object {
         "id": "T_5",
         "id": "T_5",
@@ -168,7 +168,7 @@ Array [
         "name": "colour",
         "name": "colour",
       },
       },
     ],
     ],
-    "slug": "",
+    "slug": "artists-smock",
     "variants": Array [
     "variants": Array [
       Object {
       Object {
         "id": "T_7",
         "id": "T_7",

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

@@ -55,7 +55,7 @@ Array [
       "description": "Mabef description",
       "description": "Mabef description",
       "name": "Mabef M/02 Studio Easel",
       "name": "Mabef M/02 Studio Easel",
       "optionGroups": Array [],
       "optionGroups": Array [],
-      "slug": "",
+      "slug": "mabef-m02-studio-easel",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {
@@ -81,7 +81,7 @@ Array [
           ],
           ],
         },
         },
       ],
       ],
-      "slug": "",
+      "slug": "giotto-mega-pencils",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {
@@ -125,7 +125,7 @@ Array [
           ],
           ],
         },
         },
       ],
       ],
-      "slug": "",
+      "slug": "artists-smock",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {
@@ -190,7 +190,7 @@ Array [
           ],
           ],
         },
         },
       ],
       ],
-      "slug": "",
+      "slug": "perfect-paper-stretcher",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {
@@ -306,7 +306,7 @@ Array [
       "description": "Mabef description",
       "description": "Mabef description",
       "name": "Mabef M/02 Studio Easel",
       "name": "Mabef M/02 Studio Easel",
       "optionGroups": Array [],
       "optionGroups": Array [],
-      "slug": "",
+      "slug": "mabef-m02-studio-easel",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {
@@ -332,7 +332,7 @@ Array [
           ],
           ],
         },
         },
       ],
       ],
-      "slug": "",
+      "slug": "giotto-mega-pencils",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {
@@ -376,7 +376,7 @@ Array [
           ],
           ],
         },
         },
       ],
       ],
-      "slug": "",
+      "slug": "artists-smock",
     },
     },
     "variants": Array [
     "variants": Array [
       Object {
       Object {

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

@@ -2,6 +2,8 @@ import { Injectable } from '@nestjs/common';
 import * as parse from 'csv-parse';
 import * as parse from 'csv-parse';
 import { Stream } from 'stream';
 import { Stream } from 'stream';
 
 
+import { normalizeString } from '../../../../../shared/normalize-string';
+
 export interface RawProductRecord {
 export interface RawProductRecord {
     name?: string;
     name?: string;
     slug?: string;
     slug?: string;
@@ -165,13 +167,15 @@ function validateOptionValueCount(r: RawProductRecord, currentRow?: ParsedProduc
 }
 }
 
 
 function parseProductFromRecord(r: RawProductRecord): ParsedProduct {
 function parseProductFromRecord(r: RawProductRecord): ParsedProduct {
+    const name = parseString(r.name);
+    const slug = parseString(r.slug) || normalizeString(name, '-');
     return {
     return {
-        name: parseString(r.name),
-        slug: parseString(r.slug),
+        name,
+        slug,
         description: parseString(r.description),
         description: parseString(r.description),
         assetPaths: parseStringArray(r.assets),
         assetPaths: parseStringArray(r.assets),
-        optionGroups: parseStringArray(r.optionGroups).map(name => ({
-            name,
+        optionGroups: parseStringArray(r.optionGroups).map(ogName => ({
+            name: ogName,
             values: [],
             values: [],
         })),
         })),
     };
     };

+ 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
 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" ,              ,              , PPS12 , 45.3  , standard    ,
+Perfect Paper Stretcher ,  , A great device for stretching paper. , "pps1.jpg, pps2.jpg" ,              ,              , PPS12 , 45.3  , standard    ,