import-parser.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { Injectable } from '@nestjs/common';
  2. import * as parse from 'csv-parse';
  3. import { Stream } from 'stream';
  4. export interface RawProductRecord {
  5. name?: string;
  6. slug?: string;
  7. description?: string;
  8. assets?: string;
  9. optionGroups?: string;
  10. optionValues?: string;
  11. sku?: string;
  12. price?: string;
  13. taxCategory?: string;
  14. variantAssets?: string;
  15. }
  16. export interface ParsedProductVariant {
  17. optionValues: string[];
  18. sku: string;
  19. price: number;
  20. taxCategory: string;
  21. assetPaths: string[];
  22. }
  23. export interface ParsedProduct {
  24. name: string;
  25. slug: string;
  26. description: string;
  27. assetPaths: string[];
  28. optionGroups: Array<{
  29. name: string;
  30. values: string[];
  31. }>;
  32. }
  33. export interface ParsedProductWithVariants {
  34. product: ParsedProduct;
  35. variants: ParsedProductVariant[];
  36. }
  37. /**
  38. * Validates and parses CSV files into a data structure which can then be used to created new entities.
  39. */
  40. @Injectable()
  41. export class ImportParser {
  42. async parseProducts(input: string | Stream): Promise<ParsedProductWithVariants[]> {
  43. const options: parse.Options = {
  44. columns: true,
  45. trim: true,
  46. };
  47. return new Promise<ParsedProductWithVariants[]>((resolve, reject) => {
  48. if (typeof input === 'string') {
  49. parse(input, options, (err, records: RawProductRecord[]) => {
  50. if (err) {
  51. reject(err);
  52. }
  53. try {
  54. const output = this.processRawRecords(records);
  55. resolve(output);
  56. } catch (err) {
  57. reject(err);
  58. }
  59. });
  60. } else {
  61. const parser = parse(options);
  62. const records: RawProductRecord[] = [];
  63. // input.on('open', () => input.pipe(parser));
  64. input.pipe(parser);
  65. parser.on('readable', () => {
  66. let record;
  67. // tslint:disable-next-line:no-conditional-assignment
  68. while ((record = parser.read())) {
  69. records.push(record);
  70. }
  71. });
  72. parser.on('error', reject);
  73. parser.on('end', () => {
  74. try {
  75. const output = this.processRawRecords(records);
  76. resolve(output);
  77. } catch (err) {
  78. reject(err);
  79. }
  80. });
  81. }
  82. });
  83. }
  84. private processRawRecords(records: RawProductRecord[]): ParsedProductWithVariants[] {
  85. const output: ParsedProductWithVariants[] = [];
  86. let currentRow: ParsedProductWithVariants | undefined;
  87. validateColumns(records[0]);
  88. for (const r of records) {
  89. if (r.name) {
  90. if (currentRow) {
  91. populateOptionGroupValues(currentRow);
  92. output.push(currentRow);
  93. }
  94. currentRow = {
  95. product: parseProductFromRecord(r),
  96. variants: [parseVariantFromRecord(r)],
  97. };
  98. } else {
  99. if (currentRow) {
  100. currentRow.variants.push(parseVariantFromRecord(r));
  101. }
  102. }
  103. validateOptionValueCount(r, currentRow);
  104. }
  105. if (currentRow) {
  106. populateOptionGroupValues(currentRow);
  107. output.push(currentRow);
  108. }
  109. return output;
  110. }
  111. }
  112. function populateOptionGroupValues(currentRow: ParsedProductWithVariants) {
  113. const values = currentRow.variants.map(v => v.optionValues);
  114. currentRow.product.optionGroups.forEach((og, i) => {
  115. const uniqueValues = Array.from(new Set(values.map(v => v[i])));
  116. og.values = uniqueValues;
  117. });
  118. }
  119. function validateColumns(r: RawProductRecord) {
  120. const requiredColumns: Array<keyof RawProductRecord> = [
  121. 'name',
  122. 'slug',
  123. 'description',
  124. 'assets',
  125. 'optionGroups',
  126. 'optionValues',
  127. 'sku',
  128. 'price',
  129. 'taxCategory',
  130. 'variantAssets',
  131. ];
  132. const rowKeys = Object.keys(r);
  133. const missing: string[] = [];
  134. for (const col of requiredColumns) {
  135. if (!rowKeys.includes(col)) {
  136. missing.push(col);
  137. }
  138. }
  139. if (missing.length) {
  140. throw new Error(
  141. `The import file is missing the following columns: ${missing.map(m => `"${m}"`).join(', ')}`,
  142. );
  143. }
  144. }
  145. function validateOptionValueCount(r: RawProductRecord, currentRow?: ParsedProductWithVariants) {
  146. if (!currentRow) {
  147. return;
  148. }
  149. const optionValues = parseStringArray(r.optionValues);
  150. if (currentRow.product.optionGroups.length !== optionValues.length) {
  151. throw new Error(
  152. `The number of optionValues must match the number of optionGroups for the product "${r.name}"`,
  153. );
  154. }
  155. }
  156. function parseProductFromRecord(r: RawProductRecord): ParsedProduct {
  157. return {
  158. name: parseString(r.name),
  159. slug: parseString(r.slug),
  160. description: parseString(r.description),
  161. assetPaths: parseStringArray(r.assets),
  162. optionGroups: parseStringArray(r.optionGroups).map(name => ({
  163. name,
  164. values: [],
  165. })),
  166. };
  167. }
  168. function parseVariantFromRecord(r: RawProductRecord): ParsedProductVariant {
  169. return {
  170. optionValues: parseStringArray(r.optionValues),
  171. sku: parseString(r.sku),
  172. price: parseNumber(r.price),
  173. taxCategory: parseString(r.taxCategory),
  174. assetPaths: parseStringArray(r.variantAssets),
  175. };
  176. }
  177. function parseString(input?: string): string {
  178. return (input || '').trim();
  179. }
  180. function parseNumber(input?: string): number {
  181. return +(input || '').trim();
  182. }
  183. function parseStringArray(input?: string, separator = ','): string[] {
  184. return (input || '')
  185. .trim()
  186. .split(separator)
  187. .map(s => s.trim())
  188. .filter(s => s !== '');
  189. }