@medusajs/medusa 1.3.6 → 1.3.8-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +19 -0
  3. package/dist/api/routes/admin/discounts/create-condition.d.ts +2 -2
  4. package/dist/api/routes/admin/discounts/create-condition.js +2 -2
  5. package/dist/api/routes/admin/discounts/update-condition.d.ts +2 -2
  6. package/dist/api/routes/admin/discounts/update-condition.js +2 -2
  7. package/dist/api/routes/admin/gift-cards/update-gift-card.d.ts +2 -2
  8. package/dist/api/routes/admin/gift-cards/update-gift-card.js +2 -2
  9. package/dist/api/routes/admin/orders/create-swap-shipment.d.ts +1 -1
  10. package/dist/api/routes/admin/orders/create-swap-shipment.js +1 -1
  11. package/dist/api/routes/admin/orders/fulfill-swap.d.ts +1 -1
  12. package/dist/api/routes/admin/orders/fulfill-swap.js.map +1 -1
  13. package/dist/api/routes/admin/products/create-product.d.ts +1 -1
  14. package/dist/api/routes/admin/products/create-product.js +1 -1
  15. package/dist/api/routes/admin/products/update-variant.d.ts +1 -1
  16. package/dist/api/routes/admin/products/update-variant.js +1 -1
  17. package/dist/api/routes/admin/swaps/list-swaps.js +1 -1
  18. package/dist/api/routes/admin/swaps/list-swaps.js.map +1 -1
  19. package/dist/api/routes/store/customers/list-orders.d.ts +94 -7
  20. package/dist/api/routes/store/customers/list-orders.js +96 -9
  21. package/dist/api/routes/store/customers/list-orders.js.map +1 -1
  22. package/dist/api/routes/store/swaps/index.d.ts +2 -1
  23. package/dist/api/routes/store/swaps/index.js.map +1 -1
  24. package/dist/commands/develop.js +3 -0
  25. package/dist/commands/develop.js.map +1 -1
  26. package/dist/loaders/strategies.js +9 -1
  27. package/dist/loaders/strategies.js.map +1 -1
  28. package/dist/migrations/1661863940645-swap_fulfillment_status_requires_action.d.ts +6 -0
  29. package/dist/migrations/1661863940645-swap_fulfillment_status_requires_action.js +89 -0
  30. package/dist/migrations/1661863940645-swap_fulfillment_status_requires_action.js.map +1 -0
  31. package/dist/models/shipping-method-tax-line.d.ts +4 -4
  32. package/dist/models/shipping-method-tax-line.js +4 -4
  33. package/dist/models/shipping-method.d.ts +1 -1
  34. package/dist/models/shipping-method.js +1 -1
  35. package/dist/models/swap.d.ts +2 -1
  36. package/dist/models/swap.js +2 -1
  37. package/dist/models/swap.js.map +1 -1
  38. package/dist/repositories/sales-channel.d.ts +3 -1
  39. package/dist/repositories/sales-channel.js +123 -0
  40. package/dist/repositories/sales-channel.js.map +1 -1
  41. package/dist/services/batch-job.js.map +1 -1
  42. package/dist/services/cart.d.ts +0 -7
  43. package/dist/services/cart.js +10 -58
  44. package/dist/services/cart.js.map +1 -1
  45. package/dist/services/csv-parser.d.ts +1 -3
  46. package/dist/services/csv-parser.js +4 -4
  47. package/dist/services/csv-parser.js.map +1 -1
  48. package/dist/services/event-bus.d.ts +4 -4
  49. package/dist/services/line-item.d.ts +8 -0
  50. package/dist/services/line-item.js +9 -0
  51. package/dist/services/line-item.js.map +1 -1
  52. package/dist/services/product-variant.d.ts +2 -2
  53. package/dist/services/product-variant.js +43 -35
  54. package/dist/services/product-variant.js.map +1 -1
  55. package/dist/services/product.d.ts +9 -1
  56. package/dist/services/product.js +36 -15
  57. package/dist/services/product.js.map +1 -1
  58. package/dist/services/region.d.ts +7 -0
  59. package/dist/services/region.js +22 -0
  60. package/dist/services/region.js.map +1 -1
  61. package/dist/services/sales-channel.d.ts +19 -1
  62. package/dist/services/sales-channel.js +64 -14
  63. package/dist/services/sales-channel.js.map +1 -1
  64. package/dist/services/swap.d.ts +152 -168
  65. package/dist/services/swap.js +898 -878
  66. package/dist/services/swap.js.map +1 -1
  67. package/dist/services/tax-provider.d.ts +1 -1
  68. package/dist/services/user.d.ts +1 -7
  69. package/dist/services/user.js +3 -15
  70. package/dist/services/user.js.map +1 -1
  71. package/dist/strategies/batch-jobs/product/import.d.ts +140 -0
  72. package/dist/strategies/batch-jobs/product/import.js +1162 -0
  73. package/dist/strategies/batch-jobs/product/import.js.map +1 -0
  74. package/dist/strategies/batch-jobs/product/types.d.ts +47 -0
  75. package/dist/strategies/batch-jobs/product/types.js +14 -0
  76. package/dist/strategies/batch-jobs/product/types.js.map +1 -0
  77. package/dist/strategies/batch-jobs/product/utils.d.ts +15 -0
  78. package/dist/strategies/batch-jobs/product/utils.js +44 -0
  79. package/dist/strategies/batch-jobs/product/utils.js.map +1 -0
  80. package/dist/subscribers/batch-job.js +28 -8
  81. package/dist/subscribers/batch-job.js.map +1 -1
  82. package/dist/types/fulfillment.d.ts +4 -2
  83. package/dist/utils/is-email.d.ts +6 -0
  84. package/dist/utils/is-email.js +19 -0
  85. package/dist/utils/is-email.js.map +1 -0
  86. package/package.json +3 -5
@@ -0,0 +1,1162 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (_) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ var __read = (this && this.__read) || function (o, n) {
65
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
66
+ if (!m) return o;
67
+ var i = m.call(o), r, ar = [], e;
68
+ try {
69
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
70
+ }
71
+ catch (error) { e = { error: error }; }
72
+ finally {
73
+ try {
74
+ if (r && !r.done && (m = i["return"])) m.call(i);
75
+ }
76
+ finally { if (e) throw e.error; }
77
+ }
78
+ return ar;
79
+ };
80
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
81
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
82
+ if (ar || !(i in from)) {
83
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
84
+ ar[i] = from[i];
85
+ }
86
+ }
87
+ return to.concat(ar || Array.prototype.slice.call(from));
88
+ };
89
+ var __values = (this && this.__values) || function(o) {
90
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
91
+ if (m) return m.call(o);
92
+ if (o && typeof o.length === "number") return {
93
+ next: function () {
94
+ if (o && i >= o.length) o = void 0;
95
+ return { value: o && o[i++], done: !o };
96
+ }
97
+ };
98
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
99
+ };
100
+ var __importDefault = (this && this.__importDefault) || function (mod) {
101
+ return (mod && mod.__esModule) ? mod : { "default": mod };
102
+ };
103
+ Object.defineProperty(exports, "__esModule", { value: true });
104
+ var medusa_core_utils_1 = require("medusa-core-utils");
105
+ var interfaces_1 = require("../../../interfaces");
106
+ var csv_parser_1 = __importDefault(require("../../../services/csv-parser"));
107
+ var types_1 = require("./types");
108
+ var utils_1 = require("./utils");
109
+ var sales_channels_1 = __importDefault(require("../../../loaders/feature-flags/sales-channels"));
110
+ /**
111
+ * Process this many variant rows before reporting progress.
112
+ */
113
+ var BATCH_SIZE = 100;
114
+ /**
115
+ * Default strategy class used for a batch import of products/variants.
116
+ */
117
+ var ProductImportStrategy = /** @class */ (function (_super) {
118
+ __extends(ProductImportStrategy, _super);
119
+ function ProductImportStrategy(_a) {
120
+ var batchJobService = _a.batchJobService, productService = _a.productService, salesChannelService = _a.salesChannelService, productVariantService = _a.productVariantService, shippingProfileService = _a.shippingProfileService, regionService = _a.regionService, fileService = _a.fileService, manager = _a.manager, featureFlagRouter = _a.featureFlagRouter;
121
+ var _this =
122
+ // eslint-disable-next-line prefer-rest-params
123
+ _super.call(this, arguments[0]) || this;
124
+ _this.processedCounter = {};
125
+ var isSalesChannelsFeatureOn = featureFlagRouter.isFeatureEnabled(sales_channels_1.default.key);
126
+ _this.csvParser_ = new csv_parser_1.default(__assign(__assign({}, CSVSchema), { columns: __spreadArray(__spreadArray([], __read(CSVSchema.columns), false), __read((isSalesChannelsFeatureOn ? SalesChannelsSchema.columns : [])), false) }));
127
+ _this.featureFlagRouter_ = featureFlagRouter;
128
+ _this.manager_ = manager;
129
+ _this.fileService_ = fileService;
130
+ _this.batchJobService_ = batchJobService;
131
+ _this.productService_ = productService;
132
+ _this.salesChannelService_ = salesChannelService;
133
+ _this.productVariantService_ = productVariantService;
134
+ _this.shippingProfileService_ = shippingProfileService;
135
+ _this.regionService_ = regionService;
136
+ return _this;
137
+ }
138
+ ProductImportStrategy.prototype.buildTemplate = function () {
139
+ throw new Error("Not implemented!");
140
+ };
141
+ /**
142
+ * Create a description of a row on which the error occurred and throw a Medusa error.
143
+ *
144
+ * @param row - Parsed CSV row data
145
+ * @param errorDescription - Concrete error
146
+ */
147
+ ProductImportStrategy.throwDescriptiveError = function (row, errorDescription) {
148
+ var message = "Error while processing row with:\n product id: ".concat(row["product.id"], ",\n product handle: ").concat(row["product.handle"], ",\n variant id: ").concat(row["variant.id"], "\n variant sku: ").concat(row["variant.sku"], "\n ").concat(errorDescription);
149
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, message);
150
+ };
151
+ /**
152
+ * Generate instructions for update/create of products/variants from parsed CSV rows.
153
+ *
154
+ * @param csvData - An array of parsed CSV rows.
155
+ */
156
+ ProductImportStrategy.prototype.getImportInstructions = function (csvData) {
157
+ return __awaiter(this, void 0, void 0, function () {
158
+ var shippingProfile, seenProducts, productsCreate, productsUpdate, variantsCreate, variantsUpdate, csvData_1, csvData_1_1, row, e_1_1;
159
+ var e_1, _a, _b;
160
+ return __generator(this, function (_c) {
161
+ switch (_c.label) {
162
+ case 0: return [4 /*yield*/, this.shippingProfileService_.retrieveDefault()];
163
+ case 1:
164
+ shippingProfile = _c.sent();
165
+ seenProducts = {};
166
+ productsCreate = [];
167
+ productsUpdate = [];
168
+ variantsCreate = [];
169
+ variantsUpdate = [];
170
+ _c.label = 2;
171
+ case 2:
172
+ _c.trys.push([2, 8, 9, 10]);
173
+ csvData_1 = __values(csvData), csvData_1_1 = csvData_1.next();
174
+ _c.label = 3;
175
+ case 3:
176
+ if (!!csvData_1_1.done) return [3 /*break*/, 7];
177
+ row = csvData_1_1.value;
178
+ if (!row["variant.prices"].length) return [3 /*break*/, 5];
179
+ return [4 /*yield*/, this.prepareVariantPrices(row)];
180
+ case 4:
181
+ _c.sent();
182
+ _c.label = 5;
183
+ case 5:
184
+ if (row["variant.id"]) {
185
+ variantsUpdate.push(row);
186
+ }
187
+ else {
188
+ variantsCreate.push(row);
189
+ }
190
+ // save only first occurrence
191
+ if (!seenProducts[row["product.handle"]]) {
192
+ row["product.profile_id"] = shippingProfile.id;
193
+ if (row["product.product.id"]) {
194
+ productsUpdate.push(row);
195
+ }
196
+ else {
197
+ productsCreate.push(row);
198
+ }
199
+ seenProducts[row["product.handle"]] = true;
200
+ }
201
+ _c.label = 6;
202
+ case 6:
203
+ csvData_1_1 = csvData_1.next();
204
+ return [3 /*break*/, 3];
205
+ case 7: return [3 /*break*/, 10];
206
+ case 8:
207
+ e_1_1 = _c.sent();
208
+ e_1 = { error: e_1_1 };
209
+ return [3 /*break*/, 10];
210
+ case 9:
211
+ try {
212
+ if (csvData_1_1 && !csvData_1_1.done && (_a = csvData_1.return)) _a.call(csvData_1);
213
+ }
214
+ finally { if (e_1) throw e_1.error; }
215
+ return [7 /*endfinally*/];
216
+ case 10: return [2 /*return*/, (_b = {},
217
+ _b[types_1.OperationType.ProductCreate] = productsCreate,
218
+ _b[types_1.OperationType.VariantCreate] = variantsCreate,
219
+ _b[types_1.OperationType.ProductUpdate] = productsUpdate,
220
+ _b[types_1.OperationType.VariantUpdate] = variantsUpdate,
221
+ _b)];
222
+ }
223
+ });
224
+ });
225
+ };
226
+ /**
227
+ * Prepare prices records for insert - find and append region ids to records that contain a region name.
228
+ *
229
+ * @param row - An object containing parsed row data.
230
+ */
231
+ ProductImportStrategy.prototype.prepareVariantPrices = function (row) {
232
+ var _a, _b;
233
+ return __awaiter(this, void 0, void 0, function () {
234
+ var transactionManager, prices, _c, _d, price, record, _e, e_2, e_3_1;
235
+ var e_3, _f;
236
+ return __generator(this, function (_g) {
237
+ switch (_g.label) {
238
+ case 0:
239
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
240
+ prices = [];
241
+ _g.label = 1;
242
+ case 1:
243
+ _g.trys.push([1, 11, 12, 13]);
244
+ _c = __values(row["variant.prices"]), _d = _c.next();
245
+ _g.label = 2;
246
+ case 2:
247
+ if (!!_d.done) return [3 /*break*/, 10];
248
+ price = _d.value;
249
+ record = {
250
+ amount: price.amount,
251
+ };
252
+ if (!price.regionName) return [3 /*break*/, 7];
253
+ _g.label = 3;
254
+ case 3:
255
+ _g.trys.push([3, 5, , 6]);
256
+ _e = record;
257
+ return [4 /*yield*/, this.regionService_
258
+ .withTransaction(transactionManager)
259
+ .retrieveByName(price.regionName)];
260
+ case 4:
261
+ _e.region_id = (_b = (_g.sent())) === null || _b === void 0 ? void 0 : _b.id;
262
+ return [3 /*break*/, 6];
263
+ case 5:
264
+ e_2 = _g.sent();
265
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Trying to set a price for a region ".concat(price.regionName, " that doesn't exist"));
266
+ case 6: return [3 /*break*/, 8];
267
+ case 7:
268
+ record.currency_code = price.currency_code;
269
+ _g.label = 8;
270
+ case 8:
271
+ prices.push(record);
272
+ _g.label = 9;
273
+ case 9:
274
+ _d = _c.next();
275
+ return [3 /*break*/, 2];
276
+ case 10: return [3 /*break*/, 13];
277
+ case 11:
278
+ e_3_1 = _g.sent();
279
+ e_3 = { error: e_3_1 };
280
+ return [3 /*break*/, 13];
281
+ case 12:
282
+ try {
283
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
284
+ }
285
+ finally { if (e_3) throw e_3.error; }
286
+ return [7 /*endfinally*/];
287
+ case 13:
288
+ row["variant.prices"] = prices;
289
+ return [2 /*return*/];
290
+ }
291
+ });
292
+ });
293
+ };
294
+ /**
295
+ * A worker method called after a batch job has been created.
296
+ * The method parses a CSV file, generates sets of instructions
297
+ * for processing and stores these instructions to a JSON file
298
+ * which is uploaded to a bucket.
299
+ *
300
+ * @param batchJobId - An id of a job that is being preprocessed.
301
+ */
302
+ ProductImportStrategy.prototype.preProcessBatchJob = function (batchJobId) {
303
+ return __awaiter(this, void 0, void 0, function () {
304
+ var batchJob, csvFileKey, csvStream, parsedData, builtData, ops;
305
+ return __generator(this, function (_a) {
306
+ switch (_a.label) {
307
+ case 0: return [4 /*yield*/, this.batchJobService_.retrieve(batchJobId)];
308
+ case 1:
309
+ batchJob = _a.sent();
310
+ csvFileKey = batchJob.context.fileKey;
311
+ return [4 /*yield*/, this.fileService_.getDownloadStream({
312
+ fileKey: csvFileKey,
313
+ })];
314
+ case 2:
315
+ csvStream = _a.sent();
316
+ return [4 /*yield*/, this.csvParser_.parse(csvStream)];
317
+ case 3:
318
+ parsedData = _a.sent();
319
+ return [4 /*yield*/, this.csvParser_.buildData(parsedData)];
320
+ case 4:
321
+ builtData = _a.sent();
322
+ return [4 /*yield*/, this.getImportInstructions(builtData)];
323
+ case 5:
324
+ ops = _a.sent();
325
+ return [4 /*yield*/, this.uploadImportOpsFile(batchJobId, ops)];
326
+ case 6:
327
+ _a.sent();
328
+ return [4 /*yield*/, this.batchJobService_.update(batchJobId, {
329
+ result: {
330
+ advancement_count: 0,
331
+ // number of update/create operations to execute
332
+ count: Object.keys(ops).reduce(function (acc, k) { return acc + ops[k].length; }, 0),
333
+ stat_descriptors: [
334
+ {
335
+ key: "product-import-count",
336
+ name: "Products/variants to import",
337
+ message: "There will be ".concat(ops[types_1.OperationType.ProductCreate].length, " products created (").concat(ops[types_1.OperationType.ProductUpdate].length, " updated).\n ").concat(ops[types_1.OperationType.VariantCreate].length, " variants will be created and ").concat(ops[types_1.OperationType.VariantUpdate].length, " updated"),
338
+ },
339
+ ],
340
+ },
341
+ })];
342
+ case 7:
343
+ _a.sent();
344
+ return [2 /*return*/];
345
+ }
346
+ });
347
+ });
348
+ };
349
+ /**
350
+ * The main processing method called after a batch job
351
+ * is ready/confirmed for processing.
352
+ *
353
+ * @param batchJobId - An id of a batch job that is being processed.
354
+ */
355
+ ProductImportStrategy.prototype.processJob = function (batchJobId) {
356
+ return __awaiter(this, void 0, void 0, function () {
357
+ var _this = this;
358
+ return __generator(this, function (_a) {
359
+ switch (_a.label) {
360
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
361
+ return __generator(this, function (_a) {
362
+ switch (_a.label) {
363
+ case 0: return [4 /*yield*/, this.createProducts(batchJobId)];
364
+ case 1:
365
+ _a.sent();
366
+ return [4 /*yield*/, this.updateProducts(batchJobId)];
367
+ case 2:
368
+ _a.sent();
369
+ return [4 /*yield*/, this.createVariants(batchJobId)];
370
+ case 3:
371
+ _a.sent();
372
+ return [4 /*yield*/, this.updateVariants(batchJobId)];
373
+ case 4:
374
+ _a.sent();
375
+ this.finalize(batchJobId);
376
+ return [2 /*return*/];
377
+ }
378
+ });
379
+ }); })];
380
+ case 1: return [2 /*return*/, _a.sent()];
381
+ }
382
+ });
383
+ });
384
+ };
385
+ /**
386
+ * Create, or retrieve by name, sales channels from the input data.
387
+ *
388
+ * NOTE: Sales channel names provided in the CSV must exist in the DB.
389
+ * New sales channels will not be created.
390
+ *
391
+ * @param data an array of sales channels partials
392
+ * @return an array of sales channels created or retrieved by name
393
+ */
394
+ ProductImportStrategy.prototype.processSalesChannels = function (data) {
395
+ var _a;
396
+ return __awaiter(this, void 0, void 0, function () {
397
+ var transactionManager, salesChannelServiceTx, salesChannels, data_1, data_1_1, input, channel, e_4, e_5_1;
398
+ var e_5, _b;
399
+ return __generator(this, function (_c) {
400
+ switch (_c.label) {
401
+ case 0:
402
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
403
+ salesChannelServiceTx = this.salesChannelService_.withTransaction(transactionManager);
404
+ salesChannels = [];
405
+ _c.label = 1;
406
+ case 1:
407
+ _c.trys.push([1, 11, 12, 13]);
408
+ data_1 = __values(data), data_1_1 = data_1.next();
409
+ _c.label = 2;
410
+ case 2:
411
+ if (!!data_1_1.done) return [3 /*break*/, 10];
412
+ input = data_1_1.value;
413
+ channel = null;
414
+ if (!input.id) return [3 /*break*/, 6];
415
+ _c.label = 3;
416
+ case 3:
417
+ _c.trys.push([3, 5, , 6]);
418
+ return [4 /*yield*/, salesChannelServiceTx.retrieve(input.id, {
419
+ select: ["id"],
420
+ })];
421
+ case 4:
422
+ channel = _c.sent();
423
+ return [3 /*break*/, 6];
424
+ case 5:
425
+ e_4 = _c.sent();
426
+ return [3 /*break*/, 6];
427
+ case 6:
428
+ if (!!channel) return [3 /*break*/, 8];
429
+ return [4 /*yield*/, salesChannelServiceTx.retrieveByName(input.name, {
430
+ select: ["id"],
431
+ })];
432
+ case 7:
433
+ channel = (_c.sent());
434
+ _c.label = 8;
435
+ case 8:
436
+ salesChannels.push(channel);
437
+ _c.label = 9;
438
+ case 9:
439
+ data_1_1 = data_1.next();
440
+ return [3 /*break*/, 2];
441
+ case 10: return [3 /*break*/, 13];
442
+ case 11:
443
+ e_5_1 = _c.sent();
444
+ e_5 = { error: e_5_1 };
445
+ return [3 /*break*/, 13];
446
+ case 12:
447
+ try {
448
+ if (data_1_1 && !data_1_1.done && (_b = data_1.return)) _b.call(data_1);
449
+ }
450
+ finally { if (e_5) throw e_5.error; }
451
+ return [7 /*endfinally*/];
452
+ case 13: return [2 /*return*/, salesChannels];
453
+ }
454
+ });
455
+ });
456
+ };
457
+ /**
458
+ * Method creates products using `ProductService` and parsed data from a CSV row.
459
+ *
460
+ * @param batchJobId - An id of the current batch job being processed.
461
+ */
462
+ ProductImportStrategy.prototype.createProducts = function (batchJobId) {
463
+ var _a;
464
+ return __awaiter(this, void 0, void 0, function () {
465
+ var transactionManager, productOps, productServiceTx, isSalesChannelsFeatureOn, productOps_1, productOps_1_1, productOp, productData, _b, _c, e_6, e_7_1;
466
+ var e_7, _d;
467
+ return __generator(this, function (_e) {
468
+ switch (_e.label) {
469
+ case 0:
470
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
471
+ return [4 /*yield*/, this.downloadImportOpsFile(batchJobId, types_1.OperationType.ProductCreate)];
472
+ case 1:
473
+ productOps = _e.sent();
474
+ productServiceTx = this.productService_.withTransaction(transactionManager);
475
+ isSalesChannelsFeatureOn = this.featureFlagRouter_.isFeatureEnabled(sales_channels_1.default.key);
476
+ _e.label = 2;
477
+ case 2:
478
+ _e.trys.push([2, 12, 13, 14]);
479
+ productOps_1 = __values(productOps), productOps_1_1 = productOps_1.next();
480
+ _e.label = 3;
481
+ case 3:
482
+ if (!!productOps_1_1.done) return [3 /*break*/, 11];
483
+ productOp = productOps_1_1.value;
484
+ productData = (0, utils_1.transformProductData)(productOp);
485
+ _e.label = 4;
486
+ case 4:
487
+ _e.trys.push([4, 8, , 9]);
488
+ if (!isSalesChannelsFeatureOn) return [3 /*break*/, 6];
489
+ _b = productData;
490
+ _c = "sales_channels";
491
+ return [4 /*yield*/, this.processSalesChannels(productOp["product.sales_channels"])];
492
+ case 5:
493
+ _b[_c] = _e.sent();
494
+ _e.label = 6;
495
+ case 6: return [4 /*yield*/, productServiceTx.create(productData)];
496
+ case 7:
497
+ _e.sent();
498
+ return [3 /*break*/, 9];
499
+ case 8:
500
+ e_6 = _e.sent();
501
+ ProductImportStrategy.throwDescriptiveError(productOp, e_6.message);
502
+ return [3 /*break*/, 9];
503
+ case 9:
504
+ this.updateProgress(batchJobId);
505
+ _e.label = 10;
506
+ case 10:
507
+ productOps_1_1 = productOps_1.next();
508
+ return [3 /*break*/, 3];
509
+ case 11: return [3 /*break*/, 14];
510
+ case 12:
511
+ e_7_1 = _e.sent();
512
+ e_7 = { error: e_7_1 };
513
+ return [3 /*break*/, 14];
514
+ case 13:
515
+ try {
516
+ if (productOps_1_1 && !productOps_1_1.done && (_d = productOps_1.return)) _d.call(productOps_1);
517
+ }
518
+ finally { if (e_7) throw e_7.error; }
519
+ return [7 /*endfinally*/];
520
+ case 14: return [2 /*return*/];
521
+ }
522
+ });
523
+ });
524
+ };
525
+ /**
526
+ * Method updates existing products in the DB using a CSV row data.
527
+ *
528
+ * @param batchJobId - An id of the current batch job being processed.
529
+ */
530
+ ProductImportStrategy.prototype.updateProducts = function (batchJobId) {
531
+ var _a;
532
+ return __awaiter(this, void 0, void 0, function () {
533
+ var transactionManager, productOps, productServiceTx, isSalesChannelsFeatureOn, productOps_2, productOps_2_1, productOp, productData, _b, _c, e_8, e_9_1;
534
+ var e_9, _d;
535
+ return __generator(this, function (_e) {
536
+ switch (_e.label) {
537
+ case 0:
538
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
539
+ return [4 /*yield*/, this.downloadImportOpsFile(batchJobId, types_1.OperationType.ProductUpdate)];
540
+ case 1:
541
+ productOps = _e.sent();
542
+ productServiceTx = this.productService_.withTransaction(transactionManager);
543
+ isSalesChannelsFeatureOn = this.featureFlagRouter_.isFeatureEnabled(sales_channels_1.default.key);
544
+ _e.label = 2;
545
+ case 2:
546
+ _e.trys.push([2, 12, 13, 14]);
547
+ productOps_2 = __values(productOps), productOps_2_1 = productOps_2.next();
548
+ _e.label = 3;
549
+ case 3:
550
+ if (!!productOps_2_1.done) return [3 /*break*/, 11];
551
+ productOp = productOps_2_1.value;
552
+ productData = (0, utils_1.transformProductData)(productOp);
553
+ _e.label = 4;
554
+ case 4:
555
+ _e.trys.push([4, 8, , 9]);
556
+ if (!isSalesChannelsFeatureOn) return [3 /*break*/, 6];
557
+ _b = productData;
558
+ _c = "sales_channels";
559
+ return [4 /*yield*/, this.processSalesChannels(productOp["product.sales_channels"])];
560
+ case 5:
561
+ _b[_c] = _e.sent();
562
+ _e.label = 6;
563
+ case 6: return [4 /*yield*/, productServiceTx.update(productOp["product.id"], productData)];
564
+ case 7:
565
+ _e.sent();
566
+ return [3 /*break*/, 9];
567
+ case 8:
568
+ e_8 = _e.sent();
569
+ ProductImportStrategy.throwDescriptiveError(productOp, e_8.message);
570
+ return [3 /*break*/, 9];
571
+ case 9:
572
+ this.updateProgress(batchJobId);
573
+ _e.label = 10;
574
+ case 10:
575
+ productOps_2_1 = productOps_2.next();
576
+ return [3 /*break*/, 3];
577
+ case 11: return [3 /*break*/, 14];
578
+ case 12:
579
+ e_9_1 = _e.sent();
580
+ e_9 = { error: e_9_1 };
581
+ return [3 /*break*/, 14];
582
+ case 13:
583
+ try {
584
+ if (productOps_2_1 && !productOps_2_1.done && (_d = productOps_2.return)) _d.call(productOps_2);
585
+ }
586
+ finally { if (e_9) throw e_9.error; }
587
+ return [7 /*endfinally*/];
588
+ case 14: return [2 /*return*/];
589
+ }
590
+ });
591
+ });
592
+ };
593
+ /**
594
+ * Method creates product variants from a CSV data.
595
+ * Method also handles processing of variant options.
596
+ *
597
+ * @param batchJobId - An id of the current batch job being processed.
598
+ */
599
+ ProductImportStrategy.prototype.createVariants = function (batchJobId) {
600
+ var _a, _b, _c;
601
+ return __awaiter(this, void 0, void 0, function () {
602
+ var transactionManager, variantOps, _loop_1, this_1, variantOps_1, variantOps_1_1, variantOp, e_10_1;
603
+ var e_10, _d;
604
+ return __generator(this, function (_e) {
605
+ switch (_e.label) {
606
+ case 0:
607
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
608
+ return [4 /*yield*/, this.downloadImportOpsFile(batchJobId, types_1.OperationType.VariantCreate)];
609
+ case 1:
610
+ variantOps = _e.sent();
611
+ _loop_1 = function (variantOp) {
612
+ var variant, product_1, optionIds_1, e_11;
613
+ return __generator(this, function (_f) {
614
+ switch (_f.label) {
615
+ case 0:
616
+ _f.trys.push([0, 3, , 4]);
617
+ variant = (0, utils_1.transformVariantData)(variantOp);
618
+ return [4 /*yield*/, this_1.productService_
619
+ .withTransaction(transactionManager)
620
+ .retrieveByHandle(variantOp["product.handle"], {
621
+ relations: ["variants", "variants.options", "options"],
622
+ })];
623
+ case 1:
624
+ product_1 = _f.sent();
625
+ optionIds_1 = ((_b = variantOp["product.options"]) === null || _b === void 0 ? void 0 : _b.map(function (variantOption) {
626
+ return product_1.options.find(function (createdProductOption) {
627
+ return createdProductOption.title === variantOption.title;
628
+ }).id;
629
+ })) || [];
630
+ variant.options =
631
+ ((_c = variant.options) === null || _c === void 0 ? void 0 : _c.map(function (o, index) { return (__assign(__assign({}, o), { option_id: optionIds_1[index] })); })) || [];
632
+ return [4 /*yield*/, this_1.productVariantService_
633
+ .withTransaction(transactionManager)
634
+ .create(product_1, variant)];
635
+ case 2:
636
+ _f.sent();
637
+ this_1.updateProgress(batchJobId);
638
+ return [3 /*break*/, 4];
639
+ case 3:
640
+ e_11 = _f.sent();
641
+ ProductImportStrategy.throwDescriptiveError(variantOp, e_11.message);
642
+ return [3 /*break*/, 4];
643
+ case 4: return [2 /*return*/];
644
+ }
645
+ });
646
+ };
647
+ this_1 = this;
648
+ _e.label = 2;
649
+ case 2:
650
+ _e.trys.push([2, 7, 8, 9]);
651
+ variantOps_1 = __values(variantOps), variantOps_1_1 = variantOps_1.next();
652
+ _e.label = 3;
653
+ case 3:
654
+ if (!!variantOps_1_1.done) return [3 /*break*/, 6];
655
+ variantOp = variantOps_1_1.value;
656
+ return [5 /*yield**/, _loop_1(variantOp)];
657
+ case 4:
658
+ _e.sent();
659
+ _e.label = 5;
660
+ case 5:
661
+ variantOps_1_1 = variantOps_1.next();
662
+ return [3 /*break*/, 3];
663
+ case 6: return [3 /*break*/, 9];
664
+ case 7:
665
+ e_10_1 = _e.sent();
666
+ e_10 = { error: e_10_1 };
667
+ return [3 /*break*/, 9];
668
+ case 8:
669
+ try {
670
+ if (variantOps_1_1 && !variantOps_1_1.done && (_d = variantOps_1.return)) _d.call(variantOps_1);
671
+ }
672
+ finally { if (e_10) throw e_10.error; }
673
+ return [7 /*endfinally*/];
674
+ case 9: return [2 /*return*/];
675
+ }
676
+ });
677
+ });
678
+ };
679
+ /**
680
+ * Method updates product variants from a CSV data.
681
+ *
682
+ * @param batchJobId - An id of the current batch job being processed.
683
+ */
684
+ ProductImportStrategy.prototype.updateVariants = function (batchJobId) {
685
+ var _a;
686
+ return __awaiter(this, void 0, void 0, function () {
687
+ var transactionManager, variantOps, productServiceTx, variantOps_2, variantOps_2_1, variantOp, product, e_12, e_13_1;
688
+ var e_13, _b;
689
+ return __generator(this, function (_c) {
690
+ switch (_c.label) {
691
+ case 0:
692
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
693
+ return [4 /*yield*/, this.downloadImportOpsFile(batchJobId, types_1.OperationType.VariantUpdate)];
694
+ case 1:
695
+ variantOps = _c.sent();
696
+ productServiceTx = this.productService_.withTransaction(transactionManager);
697
+ _c.label = 2;
698
+ case 2:
699
+ _c.trys.push([2, 12, 13, 14]);
700
+ variantOps_2 = __values(variantOps), variantOps_2_1 = variantOps_2.next();
701
+ _c.label = 3;
702
+ case 3:
703
+ if (!!variantOps_2_1.done) return [3 /*break*/, 11];
704
+ variantOp = variantOps_2_1.value;
705
+ _c.label = 4;
706
+ case 4:
707
+ _c.trys.push([4, 8, , 9]);
708
+ return [4 /*yield*/, productServiceTx.retrieveByHandle(variantOp["product.handle"])];
709
+ case 5:
710
+ product = _c.sent();
711
+ return [4 /*yield*/, this.prepareVariantOptions(variantOp, product.id)];
712
+ case 6:
713
+ _c.sent();
714
+ return [4 /*yield*/, this.productVariantService_
715
+ .withTransaction(transactionManager)
716
+ .update(variantOp["variant.id"], (0, utils_1.transformVariantData)(variantOp))];
717
+ case 7:
718
+ _c.sent();
719
+ return [3 /*break*/, 9];
720
+ case 8:
721
+ e_12 = _c.sent();
722
+ ProductImportStrategy.throwDescriptiveError(variantOp, e_12.message);
723
+ return [3 /*break*/, 9];
724
+ case 9:
725
+ this.updateProgress(batchJobId);
726
+ _c.label = 10;
727
+ case 10:
728
+ variantOps_2_1 = variantOps_2.next();
729
+ return [3 /*break*/, 3];
730
+ case 11: return [3 /*break*/, 14];
731
+ case 12:
732
+ e_13_1 = _c.sent();
733
+ e_13 = { error: e_13_1 };
734
+ return [3 /*break*/, 14];
735
+ case 13:
736
+ try {
737
+ if (variantOps_2_1 && !variantOps_2_1.done && (_b = variantOps_2.return)) _b.call(variantOps_2);
738
+ }
739
+ finally { if (e_13) throw e_13.error; }
740
+ return [7 /*endfinally*/];
741
+ case 14: return [2 /*return*/];
742
+ }
743
+ });
744
+ });
745
+ };
746
+ /**
747
+ * Extend records used for creating variant options with corresponding product option ids.
748
+ *
749
+ * @param variantOp - Parsed row data form CSV
750
+ * @param productId - id of variant's product
751
+ */
752
+ ProductImportStrategy.prototype.prepareVariantOptions = function (variantOp, productId) {
753
+ return __awaiter(this, void 0, void 0, function () {
754
+ var productOptions, productOptions_1, productOptions_1_1, o, option, e_14_1;
755
+ var e_14, _a;
756
+ return __generator(this, function (_b) {
757
+ switch (_b.label) {
758
+ case 0:
759
+ productOptions = variantOp["variant.options"] || [];
760
+ _b.label = 1;
761
+ case 1:
762
+ _b.trys.push([1, 6, 7, 8]);
763
+ productOptions_1 = __values(productOptions), productOptions_1_1 = productOptions_1.next();
764
+ _b.label = 2;
765
+ case 2:
766
+ if (!!productOptions_1_1.done) return [3 /*break*/, 5];
767
+ o = productOptions_1_1.value;
768
+ return [4 /*yield*/, this.productService_.retrieveOptionByTitle(o._title, productId)];
769
+ case 3:
770
+ option = _b.sent();
771
+ o.option_id = option === null || option === void 0 ? void 0 : option.id;
772
+ _b.label = 4;
773
+ case 4:
774
+ productOptions_1_1 = productOptions_1.next();
775
+ return [3 /*break*/, 2];
776
+ case 5: return [3 /*break*/, 8];
777
+ case 6:
778
+ e_14_1 = _b.sent();
779
+ e_14 = { error: e_14_1 };
780
+ return [3 /*break*/, 8];
781
+ case 7:
782
+ try {
783
+ if (productOptions_1_1 && !productOptions_1_1.done && (_a = productOptions_1.return)) _a.call(productOptions_1);
784
+ }
785
+ finally { if (e_14) throw e_14.error; }
786
+ return [7 /*endfinally*/];
787
+ case 8: return [2 /*return*/];
788
+ }
789
+ });
790
+ });
791
+ };
792
+ /**
793
+ * Store import ops JSON file to a bucket.
794
+ *
795
+ * @param batchJobId - An id of the current batch job being processed.
796
+ * @param results - An object containing parsed CSV data.
797
+ */
798
+ ProductImportStrategy.prototype.uploadImportOpsFile = function (batchJobId, results) {
799
+ var _a, _b;
800
+ return __awaiter(this, void 0, void 0, function () {
801
+ var uploadPromises, transactionManager, _c, _d, _i, op, _e, writeStream, promise;
802
+ return __generator(this, function (_f) {
803
+ switch (_f.label) {
804
+ case 0:
805
+ uploadPromises = [];
806
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
807
+ _c = [];
808
+ for (_d in results)
809
+ _c.push(_d);
810
+ _i = 0;
811
+ _f.label = 1;
812
+ case 1:
813
+ if (!(_i < _c.length)) return [3 /*break*/, 4];
814
+ op = _c[_i];
815
+ if (!((_b = results[op]) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 3];
816
+ return [4 /*yield*/, this.fileService_
817
+ .withTransaction(transactionManager)
818
+ .getUploadStreamDescriptor({
819
+ name: "imports/products/ops/".concat(batchJobId, "-").concat(op),
820
+ ext: "json",
821
+ })];
822
+ case 2:
823
+ _e = _f.sent(), writeStream = _e.writeStream, promise = _e.promise;
824
+ uploadPromises.push(promise);
825
+ writeStream.write(JSON.stringify(results[op]));
826
+ writeStream.end();
827
+ _f.label = 3;
828
+ case 3:
829
+ _i++;
830
+ return [3 /*break*/, 1];
831
+ case 4: return [4 /*yield*/, Promise.all(uploadPromises)];
832
+ case 5:
833
+ _f.sent();
834
+ return [2 /*return*/];
835
+ }
836
+ });
837
+ });
838
+ };
839
+ /**
840
+ * Remove parsed ops JSON file.
841
+ *
842
+ * @param batchJobId - An id of the current batch job being processed.
843
+ * @param op - Type of import operation.
844
+ */
845
+ ProductImportStrategy.prototype.downloadImportOpsFile = function (batchJobId, op) {
846
+ var _a;
847
+ return __awaiter(this, void 0, void 0, function () {
848
+ var data, transactionManager, readableStream;
849
+ return __generator(this, function (_b) {
850
+ switch (_b.label) {
851
+ case 0:
852
+ data = "";
853
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
854
+ return [4 /*yield*/, this.fileService_
855
+ .withTransaction(transactionManager)
856
+ .getDownloadStream({
857
+ fileKey: "imports/products/ops/".concat(batchJobId, "-").concat(op, ".json"),
858
+ })];
859
+ case 1:
860
+ readableStream = _b.sent();
861
+ return [4 /*yield*/, new Promise(function (resolve) {
862
+ readableStream.on("data", function (chunk) {
863
+ data += chunk;
864
+ });
865
+ readableStream.on("end", function () {
866
+ resolve(JSON.parse(data));
867
+ });
868
+ readableStream.on("error", function () {
869
+ // TODO: maybe should throw
870
+ resolve([]);
871
+ });
872
+ })];
873
+ case 2: return [2 /*return*/, _b.sent()];
874
+ }
875
+ });
876
+ });
877
+ };
878
+ /**
879
+ * Delete parsed CSV ops files.
880
+ *
881
+ * @param batchJobId - An id of the current batch job being processed.
882
+ */
883
+ ProductImportStrategy.prototype.deleteOpsFiles = function (batchJobId) {
884
+ var _a;
885
+ return __awaiter(this, void 0, void 0, function () {
886
+ var transactionManager, _b, _c, op;
887
+ var e_15, _d;
888
+ return __generator(this, function (_e) {
889
+ transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
890
+ try {
891
+ for (_b = __values(Object.keys(types_1.OperationType)), _c = _b.next(); !_c.done; _c = _b.next()) {
892
+ op = _c.value;
893
+ try {
894
+ this.fileService_.withTransaction(transactionManager).delete({
895
+ fileKey: "imports/products/ops/-".concat(batchJobId, "-").concat(op),
896
+ });
897
+ }
898
+ catch (e) {
899
+ // noop
900
+ }
901
+ }
902
+ }
903
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
904
+ finally {
905
+ try {
906
+ if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
907
+ }
908
+ finally { if (e_15) throw e_15.error; }
909
+ }
910
+ return [2 /*return*/];
911
+ });
912
+ });
913
+ };
914
+ /**
915
+ * Update count of processed data in the batch job `result` column
916
+ * and cleanup temp JSON files.
917
+ *
918
+ * @param batchJobId - An id of the current batch job being processed.
919
+ */
920
+ ProductImportStrategy.prototype.finalize = function (batchJobId) {
921
+ return __awaiter(this, void 0, void 0, function () {
922
+ var batchJob, fileKey;
923
+ return __generator(this, function (_a) {
924
+ switch (_a.label) {
925
+ case 0: return [4 /*yield*/, this.batchJobService_.retrieve(batchJobId)];
926
+ case 1:
927
+ batchJob = _a.sent();
928
+ delete this.processedCounter[batchJobId];
929
+ return [4 /*yield*/, this.batchJobService_.update(batchJobId, {
930
+ result: { advancement_count: batchJob.result.count },
931
+ })];
932
+ case 2:
933
+ _a.sent();
934
+ fileKey = batchJob.context.fileKey;
935
+ return [4 /*yield*/, this.fileService_.delete({ fileKey: fileKey })];
936
+ case 3:
937
+ _a.sent();
938
+ return [4 /*yield*/, this.deleteOpsFiles(batchJobId)];
939
+ case 4:
940
+ _a.sent();
941
+ return [2 /*return*/];
942
+ }
943
+ });
944
+ });
945
+ };
946
+ /**
947
+ * Store the progress in the batch job `result` column.
948
+ * Method is called after every update/create operation,
949
+ * but after every `BATCH_SIZE` processed rows info is written to the DB.
950
+ *
951
+ * @param batchJobId - An id of the current batch job being processed.
952
+ */
953
+ ProductImportStrategy.prototype.updateProgress = function (batchJobId) {
954
+ return __awaiter(this, void 0, void 0, function () {
955
+ var newCount;
956
+ return __generator(this, function (_a) {
957
+ switch (_a.label) {
958
+ case 0:
959
+ newCount = (this.processedCounter[batchJobId] || 0) + 1;
960
+ this.processedCounter[batchJobId] = newCount;
961
+ if (newCount % BATCH_SIZE !== 0) {
962
+ return [2 /*return*/];
963
+ }
964
+ return [4 /*yield*/, this.batchJobService_.update(batchJobId, {
965
+ result: {
966
+ advancement_count: newCount,
967
+ },
968
+ })];
969
+ case 1:
970
+ _a.sent();
971
+ return [2 /*return*/];
972
+ }
973
+ });
974
+ });
975
+ };
976
+ ProductImportStrategy.identifier = "product-import-strategy";
977
+ ProductImportStrategy.batchType = "product-import";
978
+ return ProductImportStrategy;
979
+ }(interfaces_1.AbstractBatchJobStrategy));
980
+ exports.default = ProductImportStrategy;
981
+ /**
982
+ * Schema definition for the CSV parser.
983
+ */
984
+ var CSVSchema = {
985
+ columns: [
986
+ // PRODUCT
987
+ {
988
+ name: "Product id",
989
+ mapTo: "product.id",
990
+ },
991
+ {
992
+ name: "Product Handle",
993
+ mapTo: "product.handle",
994
+ required: true,
995
+ },
996
+ { name: "Product Title", mapTo: "product.title" },
997
+ { name: "Product Subtitle", mapTo: "product.subtitle" },
998
+ { name: "Product Description", mapTo: "product.description" },
999
+ { name: "Product Status", mapTo: "product.status" },
1000
+ { name: "Product Thumbnail", mapTo: "product.thumbnail" },
1001
+ { name: "Product Weight", mapTo: "product.weight" },
1002
+ { name: "Product Length", mapTo: "product.length" },
1003
+ { name: "Product Width", mapTo: "product.width" },
1004
+ { name: "Product Height", mapTo: "product.height" },
1005
+ { name: "Product HS Code", mapTo: "product.hs_code" },
1006
+ { name: "Product Origin Country", mapTo: "product.origin_country" },
1007
+ { name: "Product Mid Code", mapTo: "product.mid_code" },
1008
+ { name: "Product Material", mapTo: "product.material" },
1009
+ // PRODUCT-COLLECTION
1010
+ { name: "Product Collection Title", mapTo: "product.collection.title" },
1011
+ { name: "Product Collection Handle", mapTo: "product.collection.handle" },
1012
+ // PRODUCT-TYPE
1013
+ { name: "Product Type", mapTo: "product.type.value" },
1014
+ // PRODUCT-TAGS
1015
+ {
1016
+ name: "Product Tags",
1017
+ mapTo: "product.tags",
1018
+ transform: function (value) {
1019
+ return "".concat(value).split(",").map(function (v) { return ({ value: v }); });
1020
+ },
1021
+ },
1022
+ //
1023
+ { name: "Product Discountable", mapTo: "product.discountable" },
1024
+ { name: "Product External ID", mapTo: "product.external_id" },
1025
+ // PRODUCT-SHIPPING_PROFILE
1026
+ { name: "Product Profile Name", mapTo: "product.profile.name" },
1027
+ { name: "Product Profile Type", mapTo: "product.profile.type" },
1028
+ // VARIANTS
1029
+ {
1030
+ name: "Variant id",
1031
+ mapTo: "variant.id",
1032
+ },
1033
+ { name: "Variant Title", mapTo: "variant.title" },
1034
+ { name: "Variant SKU", mapTo: "variant.sku" },
1035
+ { name: "Variant Barcode", mapTo: "variant.barcode" },
1036
+ { name: "Variant Inventory Quantity", mapTo: "variant.inventory_quantity" },
1037
+ { name: "Variant Allow backorder", mapTo: "variant.allow_backorder" },
1038
+ { name: "Variant Manage inventory", mapTo: "variant.manage_inventory" },
1039
+ { name: "Variant Weight", mapTo: "variant.weight" },
1040
+ { name: "Variant Length", mapTo: "variant.length" },
1041
+ { name: "Variant Width", mapTo: "variant.width" },
1042
+ { name: "Variant Height", mapTo: "variant.height" },
1043
+ { name: "Variant HS Code", mapTo: "variant.hs_code" },
1044
+ { name: "Variant Origin Country", mapTo: "variant.origin_country" },
1045
+ { name: "Variant Mid Code", mapTo: "variant.mid_code" },
1046
+ { name: "Variant Material", mapTo: "variant.material" },
1047
+ // ==== DYNAMIC FIELDS ====
1048
+ // PRODUCT_OPTIONS
1049
+ {
1050
+ name: "Option Name",
1051
+ match: /Option \d+ Name/,
1052
+ reducer: function (builtLine, key, value) {
1053
+ builtLine["product.options"] = builtLine["product.options"] || [];
1054
+ if (typeof value === "undefined" || value === null) {
1055
+ return builtLine;
1056
+ }
1057
+ ;
1058
+ builtLine["product.options"].push({ title: value });
1059
+ return builtLine;
1060
+ },
1061
+ },
1062
+ {
1063
+ name: "Option Value",
1064
+ match: /Option \d+ Value/,
1065
+ reducer: function (builtLine, key, value, context) {
1066
+ builtLine["variant.options"] = builtLine["variant.options"] || [];
1067
+ if (typeof value === "undefined" || value === null) {
1068
+ return builtLine;
1069
+ }
1070
+ ;
1071
+ builtLine["variant.options"].push({
1072
+ value: value,
1073
+ _title: context.line[key.slice(0, -6) + " Name"],
1074
+ });
1075
+ return builtLine;
1076
+ },
1077
+ },
1078
+ // PRICES
1079
+ {
1080
+ name: "Price Region",
1081
+ match: /Price .* \[([A-Z]{2,4})\]/,
1082
+ reducer: function (builtLine, key, value) {
1083
+ builtLine["variant.prices"] = builtLine["variant.prices"] || [];
1084
+ if (typeof value === "undefined" || value === null) {
1085
+ return builtLine;
1086
+ }
1087
+ var regionName = key.split(" ")[1];
1088
+ builtLine["variant.prices"].push({
1089
+ amount: value,
1090
+ regionName: regionName,
1091
+ });
1092
+ return builtLine;
1093
+ },
1094
+ },
1095
+ {
1096
+ name: "Price Currency",
1097
+ match: /Price [A-Z]{2,4}/,
1098
+ reducer: function (builtLine, key, value) {
1099
+ builtLine["variant.prices"] = builtLine["variant.prices"] || [];
1100
+ if (typeof value === "undefined" || value === null) {
1101
+ return builtLine;
1102
+ }
1103
+ var currency = key.split(" ")[1];
1104
+ builtLine["variant.prices"].push({
1105
+ amount: value,
1106
+ currency_code: currency,
1107
+ });
1108
+ return builtLine;
1109
+ },
1110
+ },
1111
+ // IMAGES
1112
+ {
1113
+ name: "Image Url",
1114
+ match: /Image \d+ Url/,
1115
+ reducer: function (builtLine, key, value) {
1116
+ builtLine["product.images"] = builtLine["product.images"] || [];
1117
+ if (typeof value === "undefined" || value === null) {
1118
+ return builtLine;
1119
+ }
1120
+ builtLine["product.images"].push(value);
1121
+ return builtLine;
1122
+ },
1123
+ },
1124
+ ],
1125
+ };
1126
+ var SalesChannelsSchema = {
1127
+ columns: [
1128
+ {
1129
+ name: "Sales Channel Name",
1130
+ match: /Sales Channel \d+ Name/,
1131
+ reducer: function (builtLine, key, value) {
1132
+ builtLine["product.sales_channels"] =
1133
+ builtLine["product.sales_channels"] || [];
1134
+ if (typeof value === "undefined" || value === null) {
1135
+ return builtLine;
1136
+ }
1137
+ ;
1138
+ builtLine["product.sales_channels"].push({
1139
+ name: value,
1140
+ });
1141
+ return builtLine;
1142
+ },
1143
+ },
1144
+ {
1145
+ name: "Sales Channel Id",
1146
+ match: /Sales Channel \d+ Id/,
1147
+ reducer: function (builtLine, key, value) {
1148
+ builtLine["product.sales_channels"] =
1149
+ builtLine["product.sales_channels"] || [];
1150
+ if (typeof value === "undefined" || value === null) {
1151
+ return builtLine;
1152
+ }
1153
+ ;
1154
+ builtLine["product.sales_channels"].push({
1155
+ id: value,
1156
+ });
1157
+ return builtLine;
1158
+ },
1159
+ },
1160
+ ],
1161
+ };
1162
+ //# sourceMappingURL=import.js.map