-
-
Notifications
You must be signed in to change notification settings - Fork 934
/
import-parser.ts
712 lines (669 loc) · 24.3 KB
/
import-parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import { Injectable } from '@nestjs/common';
import { GlobalFlag, LanguageCode } from '@vendure/common/lib/generated-types';
import { normalizeString } from '@vendure/common/lib/normalize-string';
import { unique } from '@vendure/common/lib/unique';
import { parse, Options } from 'csv-parse';
import { Stream } from 'stream';
import { InternalServerError } from '../../../common/error/errors';
import { ConfigService } from '../../../config/config.service';
import { CustomFieldConfig } from '../../../config/custom-field/custom-field-types';
const baseTranslatableColumns = [
'name',
'slug',
'description',
'facets',
'optionGroups',
'optionValues',
'variantFacets',
];
const requiredColumns: string[] = [
'name',
'slug',
'description',
'assets',
'facets',
'optionGroups',
'optionValues',
'sku',
'price',
'taxCategory',
'variantAssets',
'variantFacets',
];
/**
* @description
* The intermediate representation of an OptionGroup after it has been parsed
* by the {@link ImportParser}.
*
* @docsCategory import-export
* @docsPage ImportParser
*/
export interface ParsedOptionGroup {
translations: Array<{
languageCode: LanguageCode;
name: string;
values: string[];
}>;
}
/**
* @description
* The intermediate representation of a Facet after it has been parsed
* by the {@link ImportParser}.
*
* @docsCategory import-export
* @docsPage ImportParser
*/
export interface ParsedFacet {
translations: Array<{
languageCode: LanguageCode;
facet: string;
value: string;
}>;
}
/**
* @description
* The intermediate representation of a ProductVariant after it has been parsed
* by the {@link ImportParser}.
*
* @docsCategory import-export
* @docsPage ImportParser
*/
export interface ParsedProductVariant {
sku: string;
price: number;
taxCategory: string;
stockOnHand: number;
trackInventory: GlobalFlag;
assetPaths: string[];
facets: ParsedFacet[];
translations: Array<{
languageCode: LanguageCode;
optionValues: string[];
customFields: {
[name: string]: string;
};
}>;
}
/**
* @description
* The intermediate representation of a Product after it has been parsed
* by the {@link ImportParser}.
*
* @docsCategory import-export
* @docsPage ImportParser
*/
export interface ParsedProduct {
assetPaths: string[];
optionGroups: ParsedOptionGroup[];
facets: ParsedFacet[];
translations: Array<{
languageCode: LanguageCode;
name: string;
slug: string;
description: string;
customFields: {
[name: string]: string;
};
}>;
}
/**
* @description
* The data structure into which an import CSV file is parsed by the
* {@link ImportParser} `parseProducts()` method.
*
* @docsCategory import-export
* @docsPage ImportParser
*/
export interface ParsedProductWithVariants {
product: ParsedProduct;
variants: ParsedProductVariant[];
}
/**
* @description
* The result returned by the {@link ImportParser} `parseProducts()` method.
*
* @docsCategory import-export
* @docsPage ImportParser
*/
export interface ParseResult<T> {
results: T[];
errors: string[];
processed: number;
}
/**
* @description
* Validates and parses CSV files into a data structure which can then be used to created new entities.
* This is used internally by the {@link Importer}.
*
* @docsCategory import-export
* @docsPage ImportParser
* @docsWeight 0
*/
@Injectable()
export class ImportParser {
/** @internal */
constructor(private configService: ConfigService) {}
/**
* @description
* Parses the contents of the [product import CSV file](/guides/developer-guide/importing-data/#product-import-format) and
* returns a data structure which can then be used to populate Vendure using the {@link FastImporterService}.
*/
async parseProducts(
input: string | Stream,
mainLanguage: LanguageCode = this.configService.defaultLanguageCode,
): Promise<ParseResult<ParsedProductWithVariants>> {
const options: Options = {
trim: true,
relax_column_count: true,
};
return new Promise<ParseResult<ParsedProductWithVariants>>((resolve, reject) => {
let errors: string[] = [];
if (typeof input === 'string') {
parse(input, options, (err: any, records: string[][]) => {
if (err) {
errors = errors.concat(err);
}
if (records) {
const parseResult = this.processRawRecords(records, mainLanguage);
errors = errors.concat(parseResult.errors);
resolve({ results: parseResult.results, errors, processed: parseResult.processed });
} else {
resolve({ results: [], errors, processed: 0 });
}
});
} else {
const parser = parse(options);
const records: string[][] = [];
// input.on('open', () => input.pipe(parser));
input.pipe(parser);
parser.on('readable', () => {
let record;
// eslint-disable-next-line no-cond-assign
while ((record = parser.read())) {
records.push(record);
}
});
parser.on('error', reject);
parser.on('end', () => {
const parseResult = this.processRawRecords(records, mainLanguage);
errors = errors.concat(parseResult.errors);
resolve({ results: parseResult.results, errors, processed: parseResult.processed });
});
}
});
}
private processRawRecords(
records: string[][],
mainLanguage: LanguageCode,
): ParseResult<ParsedProductWithVariants> {
const results: ParsedProductWithVariants[] = [];
const errors: string[] = [];
let currentRow: ParsedProductWithVariants | undefined;
const headerRow = records[0];
const rest = records.slice(1);
const totalProducts = rest.map(row => row[0]).filter(name => name.trim() !== '').length;
const customFieldErrors = this.validateCustomFields(headerRow);
if (customFieldErrors.length > 0) {
return { results: [], errors: customFieldErrors, processed: 0 };
}
const translationError = this.validateHeaderTranslations(headerRow);
if (translationError) {
return { results: [], errors: [translationError], processed: 0 };
}
const columnError = validateRequiredColumns(headerRow);
if (columnError) {
return { results: [], errors: [columnError], processed: 0 };
}
const usedLanguages = usedLanguageCodes(headerRow);
let line = 1;
for (const record of rest) {
line++;
const columnCountError = validateColumnCount(headerRow, record);
if (columnCountError) {
errors.push(columnCountError + ` on line ${line}`);
continue;
}
const r = mapRowToObject(headerRow, record);
if (getRawMainTranslation(r, 'name', mainLanguage)) {
if (currentRow) {
populateOptionGroupValues(currentRow);
results.push(currentRow);
}
currentRow = {
product: this.parseProductFromRecord(r, usedLanguages, mainLanguage),
variants: [this.parseVariantFromRecord(r, usedLanguages, mainLanguage)],
};
} else {
if (currentRow) {
currentRow.variants.push(this.parseVariantFromRecord(r, usedLanguages, mainLanguage));
}
}
const optionError = validateOptionValueCount(r, currentRow);
if (optionError) {
errors.push(optionError + ` on line ${line}`);
}
}
if (currentRow) {
populateOptionGroupValues(currentRow);
results.push(currentRow);
}
return { results, errors, processed: totalProducts };
}
private validateCustomFields(rowKeys: string[]): string[] {
const errors: string[] = [];
for (const rowKey of rowKeys) {
const baseKey = getBaseKey(rowKey);
const parts = baseKey.split(':');
if (parts.length === 1) {
continue;
}
if (parts.length === 2) {
let customFieldConfigs: CustomFieldConfig[] = [];
if (parts[0] === 'product') {
customFieldConfigs = this.configService.customFields.Product;
} else if (parts[0] === 'variant') {
customFieldConfigs = this.configService.customFields.ProductVariant;
} else {
continue;
}
const customFieldConfig = customFieldConfigs.find(config => config.name === parts[1]);
if (customFieldConfig) {
continue;
}
}
errors.push(`Invalid custom field: ${rowKey}`);
}
return errors;
}
private isTranslatable(baseKey: string): boolean {
const parts = baseKey.split(':');
if (parts.length === 1) {
return baseTranslatableColumns.includes(baseKey);
}
if (parts.length === 2) {
let customFieldConfigs: CustomFieldConfig[];
if (parts[0] === 'product') {
customFieldConfigs = this.configService.customFields.Product;
} else if (parts[0] === 'variant') {
customFieldConfigs = this.configService.customFields.ProductVariant;
} else {
throw new InternalServerError(`Invalid column header '${baseKey}'`);
}
const customFieldConfig = customFieldConfigs.find(config => config.name === parts[1]);
if (!customFieldConfig) {
throw new InternalServerError(
`Could not find custom field config for column header '${baseKey}'`,
);
}
return customFieldConfig.type === 'localeString';
}
throw new InternalServerError(`Invalid column header '${baseKey}'`);
}
private validateHeaderTranslations(rowKeys: string[]): string | undefined {
const missing: string[] = [];
const languageCodes = usedLanguageCodes(rowKeys);
const baseKeys = usedBaseKeys(rowKeys);
for (const baseKey of baseKeys) {
const translatedKeys = languageCodes.map(code => [baseKey, code].join(':'));
if (rowKeys.includes(baseKey)) {
// Untranslated column header is used -> there should be no translated ones
if (rowKeys.some(key => translatedKeys.includes(key))) {
return `The import file must not contain both translated and untranslated columns for field '${baseKey}'`;
}
} else {
if (!this.isTranslatable(baseKey) && translatedKeys.some(key => rowKeys.includes(key))) {
return `The '${baseKey}' field is not translatable.`;
}
// All column headers must exist for all translations
for (const translatedKey of translatedKeys) {
if (!rowKeys.includes(translatedKey)) {
missing.push(translatedKey);
}
}
}
}
if (missing.length) {
return `The import file is missing the following translations: ${missing
.map(m => `"${m}"`)
.join(', ')}`;
}
}
private parseProductFromRecord(
r: { [key: string]: string },
usedLanguages: LanguageCode[],
mainLanguage: LanguageCode,
): ParsedProduct {
const translationCodes = usedLanguages.length === 0 ? [mainLanguage] : usedLanguages;
const optionGroups: ParsedOptionGroup[] = [];
for (const languageCode of translationCodes) {
const rawTranslOptionGroups = r.hasOwnProperty(`optionGroups:${languageCode}`)
? r[`optionGroups:${languageCode}`]
: r.optionGroups;
const translatedOptionGroups = parseStringArray(rawTranslOptionGroups);
if (optionGroups.length === 0) {
for (const translatedOptionGroup of translatedOptionGroups) {
optionGroups.push({ translations: [] });
}
}
for (const i of optionGroups.map((optionGroup, index) => index)) {
optionGroups[i].translations.push({
languageCode,
name: translatedOptionGroups[i],
values: [],
});
}
}
const facets: ParsedFacet[] = [];
for (const languageCode of translationCodes) {
const rawTranslatedFacets = r.hasOwnProperty(`facets:${languageCode}`)
? r[`facets:${languageCode}`]
: r.facets;
const translatedFacets = parseStringArray(rawTranslatedFacets);
if (facets.length === 0) {
for (const translatedFacet of translatedFacets) {
facets.push({ translations: [] });
}
}
for (const i of facets.map((facet, index) => index)) {
const [facet, value] = translatedFacets[i].split(':');
facets[i].translations.push({
languageCode,
facet,
value,
});
}
}
const translations = translationCodes.map(languageCode => {
const translatedFields = getRawTranslatedFields(r, languageCode);
const parsedTranslatedCustomFields = parseCustomFields('product', translatedFields);
const parsedUntranslatedCustomFields = parseCustomFields('product', getRawUntranslatedFields(r));
const parsedCustomFields = {
...parsedUntranslatedCustomFields,
...parsedTranslatedCustomFields,
};
const name = translatedFields.hasOwnProperty('name')
? parseString(translatedFields.name)
: r.name;
let slug: string;
if (translatedFields.hasOwnProperty('slug')) {
slug = parseString(translatedFields.slug);
} else {
slug = parseString(r.slug);
}
if (slug.length === 0) {
slug = normalizeString(name, '-');
}
return {
languageCode,
name,
slug,
description: translatedFields.hasOwnProperty('description')
? parseString(translatedFields.description)
: r.description,
customFields: parsedCustomFields,
};
});
const parsedProduct: ParsedProduct = {
assetPaths: parseStringArray(r.assets),
optionGroups,
facets,
translations,
};
return parsedProduct;
}
private parseVariantFromRecord(
r: { [key: string]: string },
usedLanguages: LanguageCode[],
mainLanguage: LanguageCode,
): ParsedProductVariant {
const translationCodes = usedLanguages.length === 0 ? [mainLanguage] : usedLanguages;
const facets: ParsedFacet[] = [];
for (const languageCode of translationCodes) {
const rawTranslatedFacets = r.hasOwnProperty(`variantFacets:${languageCode}`)
? r[`variantFacets:${languageCode}`]
: r.variantFacets;
const translatedFacets = parseStringArray(rawTranslatedFacets);
if (facets.length === 0) {
for (const translatedFacet of translatedFacets) {
facets.push({ translations: [] });
}
}
for (const i of facets.map((facet, index) => index)) {
const [facet, value] = translatedFacets[i].split(':');
facets[i].translations.push({
languageCode,
facet,
value,
});
}
}
const translations = translationCodes.map(languageCode => {
const rawTranslOptionValues = r.hasOwnProperty(`optionValues:${languageCode}`)
? r[`optionValues:${languageCode}`]
: r.optionValues;
const translatedOptionValues = parseStringArray(rawTranslOptionValues);
const translatedFields = getRawTranslatedFields(r, languageCode);
const parsedTranslatedCustomFields = parseCustomFields('variant', translatedFields);
const parsedUntranslatedCustomFields = parseCustomFields('variant', getRawUntranslatedFields(r));
const parsedCustomFields = {
...parsedUntranslatedCustomFields,
...parsedTranslatedCustomFields,
};
return {
languageCode,
optionValues: translatedOptionValues,
customFields: parsedCustomFields,
};
});
const parsedVariant: ParsedProductVariant = {
sku: parseString(r.sku),
price: parseNumber(r.price),
taxCategory: parseString(r.taxCategory),
stockOnHand: parseNumber(r.stockOnHand),
trackInventory:
r.trackInventory == null || r.trackInventory === ''
? GlobalFlag.INHERIT
: parseBoolean(r.trackInventory)
? GlobalFlag.TRUE
: GlobalFlag.FALSE,
assetPaths: parseStringArray(r.variantAssets),
facets,
translations,
};
return parsedVariant;
}
}
function populateOptionGroupValues(currentRow: ParsedProductWithVariants) {
for (const translation of currentRow.product.translations) {
const values = currentRow.variants.map(variant => {
const variantTranslation = variant.translations.find(
t => t.languageCode === translation.languageCode,
);
if (!variantTranslation) {
throw new InternalServerError(
`No translation '${translation.languageCode}' for variant SKU '${variant.sku}'`,
);
}
return variantTranslation.optionValues;
});
currentRow.product.optionGroups.forEach((og, i) => {
const ogTranslation = og.translations.find(t => t.languageCode === translation.languageCode);
if (!ogTranslation) {
throw new InternalServerError(
`No translation '${translation.languageCode}' for option groups'`,
);
}
ogTranslation.values = unique(values.map(v => v[i]));
});
}
}
function getLanguageCode(rowKey: string): LanguageCode | undefined {
const parts = rowKey.split(':');
if (parts.length === 2) {
if (parts[1] in LanguageCode) {
return parts[1] as LanguageCode;
}
}
if (parts.length === 3) {
if (['product', 'productVariant'].includes(parts[0]) && parts[2] in LanguageCode) {
return parts[2] as LanguageCode;
}
}
}
function getBaseKey(rowKey: string): string {
const parts = rowKey.split(':');
if (getLanguageCode(rowKey)) {
parts.pop();
return parts.join(':');
} else {
return rowKey;
}
}
function usedLanguageCodes(rowKeys: string[]): LanguageCode[] {
const languageCodes: LanguageCode[] = [];
for (const rowKey of rowKeys) {
const languageCode = getLanguageCode(rowKey);
if (languageCode && !languageCodes.includes(languageCode)) {
languageCodes.push(languageCode);
}
}
return languageCodes;
}
function usedBaseKeys(rowKeys: string[]): string[] {
const baseKeys: string[] = [];
for (const rowKey of rowKeys) {
const baseKey = getBaseKey(rowKey);
if (!baseKeys.includes(baseKey)) {
baseKeys.push(baseKey);
}
}
return baseKeys;
}
function validateRequiredColumns(r: string[]): string | undefined {
const rowKeys = r;
const missing: string[] = [];
const languageCodes = usedLanguageCodes(rowKeys);
for (const col of requiredColumns) {
if (!rowKeys.includes(col)) {
if (languageCodes.length > 0 && rowKeys.includes(`${col}:${languageCodes[0]}`)) {
continue; // If one translation is present, they are all present (we did 'validateHeaderTranslations' before)
}
missing.push(col);
}
}
if (missing.length) {
return `The import file is missing the following columns: ${missing.map(m => `"${m}"`).join(', ')}`;
}
}
function validateColumnCount(columns: string[], row: string[]): string | undefined {
if (columns.length !== row.length) {
return `Invalid Record Length: header length is ${columns.length}, got ${row.length}`;
}
}
function mapRowToObject(columns: string[], row: string[]): { [key: string]: string } {
return row.reduce((obj, val, i) => {
return { ...obj, [columns[i]]: val };
}, {});
}
function validateOptionValueCount(
r: { [key: string]: string },
currentRow?: ParsedProductWithVariants,
): string | undefined {
if (!currentRow) {
return;
}
const optionValueKeys = Object.keys(r).filter(key => key.startsWith('optionValues'));
for (const key of optionValueKeys) {
const optionValues = parseStringArray(r[key]);
if (currentRow.product.optionGroups.length !== optionValues.length) {
return `The number of optionValues in column '${key}' must match the number of optionGroups`;
}
}
}
function getRawMainTranslation(
r: { [key: string]: string },
field: string,
mainLanguage: LanguageCode,
): string {
if (r.hasOwnProperty(field)) {
return r[field];
} else {
return r[`${field}:${mainLanguage}`];
}
}
function getRawTranslatedFields(
r: { [key: string]: string },
languageCode: LanguageCode,
): { [key: string]: string } {
return Object.entries(r)
.filter(([key, value]) => key.endsWith(`:${languageCode}`))
.reduce((output, [key, value]) => {
const fieldName = key.replace(`:${languageCode}`, '');
return {
...output,
[fieldName]: value,
};
}, {});
}
function getRawUntranslatedFields(r: { [key: string]: string }): { [key: string]: string } {
return Object.entries(r)
.filter(([key, value]) => {
return !getLanguageCode(key);
})
.reduce((output, [key, value]) => {
return {
...output,
[key]: value,
};
}, {});
}
function isRelationObject(value: string) {
try {
const parsed = JSON.parse(value);
return parsed && parsed.hasOwnProperty('id');
} catch (e: any) {
return false;
}
}
function parseCustomFields(
prefix: 'product' | 'variant',
r: { [key: string]: string },
): { [name: string]: string } {
return Object.entries(r)
.filter(([key, value]) => {
return key.indexOf(`${prefix}:`) === 0;
})
.reduce((output, [key, value]) => {
const fieldName = key.replace(`${prefix}:`, '');
return {
...output,
[fieldName]: isRelationObject(value) ? JSON.parse(value) : value,
};
}, {});
}
function parseString(input?: string): string {
return (input || '').trim();
}
function parseNumber(input?: string): number {
return +(input || '').trim();
}
function parseBoolean(input?: string): boolean {
if (input == null) {
return false;
}
switch (input.toLowerCase()) {
case 'true':
case '1':
case 'yes':
return true;
default:
return false;
}
}
function parseStringArray(input?: string, separator = '|'): string[] {
return (input || '')
.trim()
.split(separator)
.map(s => s.trim())
.filter(s => s !== '');
}