@medusajs/medusa 1.7.13 → 1.7.14

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 (49) hide show
  1. package/dist/api/routes/admin/inventory-items/create-inventory-item.d.ts +136 -0
  2. package/dist/api/routes/admin/inventory-items/create-inventory-item.js +315 -0
  3. package/dist/api/routes/admin/inventory-items/create-inventory-item.js.map +1 -0
  4. package/dist/api/routes/admin/inventory-items/transaction/create-inventory-item.d.ts +25 -0
  5. package/dist/api/routes/admin/inventory-items/transaction/create-inventory-item.js +200 -0
  6. package/dist/api/routes/admin/inventory-items/transaction/create-inventory-item.js.map +1 -0
  7. package/dist/api/routes/admin/products/update-product.d.ts +1 -1
  8. package/dist/api/routes/admin/products/update-product.js +116 -143
  9. package/dist/api/routes/admin/products/update-product.js.map +1 -1
  10. package/dist/helpers/test-request.js +26 -3
  11. package/dist/helpers/test-request.js.map +1 -1
  12. package/dist/loaders/models.d.ts +2 -1
  13. package/dist/loaders/models.js +2 -2
  14. package/dist/loaders/models.js.map +1 -1
  15. package/dist/loaders/repositories.d.ts +2 -1
  16. package/dist/loaders/repositories.js +2 -2
  17. package/dist/loaders/repositories.js.map +1 -1
  18. package/dist/repositories/image.d.ts +3 -1
  19. package/dist/repositories/image.js +71 -49
  20. package/dist/repositories/image.js.map +1 -1
  21. package/dist/repositories/money-amount.d.ts +8 -2
  22. package/dist/repositories/money-amount.js +84 -33
  23. package/dist/repositories/money-amount.js.map +1 -1
  24. package/dist/repositories/staged-job.d.ts +1 -1
  25. package/dist/repositories/staged-job.js +3 -3
  26. package/dist/repositories/staged-job.js.map +1 -1
  27. package/dist/services/discount.js +4 -3
  28. package/dist/services/discount.js.map +1 -1
  29. package/dist/services/product-variant.d.ts +31 -6
  30. package/dist/services/product-variant.js +431 -231
  31. package/dist/services/product-variant.js.map +1 -1
  32. package/dist/services/product.js +30 -37
  33. package/dist/services/product.js.map +1 -1
  34. package/dist/services/staged-job.d.ts +3 -1
  35. package/dist/services/staged-job.js +46 -0
  36. package/dist/services/staged-job.js.map +1 -1
  37. package/dist/strategies/batch-jobs/product/import.js +5 -2
  38. package/dist/strategies/batch-jobs/product/import.js.map +1 -1
  39. package/dist/types/product-variant.d.ts +22 -1
  40. package/dist/types/product-variant.js.map +1 -1
  41. package/dist/utils/has-changes.d.ts +6 -0
  42. package/dist/utils/has-changes.js +60 -0
  43. package/dist/utils/has-changes.js.map +1 -0
  44. package/dist/utils/index.d.ts +2 -1
  45. package/dist/utils/index.js +2 -1
  46. package/dist/utils/index.js.map +1 -1
  47. package/dist/utils/product-category/index.js +1 -14
  48. package/dist/utils/product-category/index.js.map +1 -1
  49. package/package.json +2 -2
@@ -206,28 +206,27 @@ var ProductVariantService = /** @class */ (function (_super) {
206
206
  return __generator(this, function (_a) {
207
207
  switch (_a.label) {
208
208
  case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
209
- var productRepo, variantRepo, prices, rest, product, variantExists, toCreate, productVariant, result, prices_1, prices_1_1, price, region, e_1_1;
210
- var e_1, _a;
211
- return __generator(this, function (_b) {
212
- switch (_b.label) {
209
+ var productRepo, variantRepo, prices, rest, product, variantExists, toCreate, productVariant, result;
210
+ return __generator(this, function (_a) {
211
+ switch (_a.label) {
213
212
  case 0:
214
213
  productRepo = manager.getCustomRepository(this.productRepository_);
215
214
  variantRepo = manager.getCustomRepository(this.productVariantRepository_);
216
215
  prices = variant.prices, rest = __rest(variant, ["prices"]);
217
216
  product = productOrProductId;
218
- if (!(typeof product === "string")) return [3 /*break*/, 2];
217
+ if (!(0, utils_1.isString)(product)) return [3 /*break*/, 2];
219
218
  return [4 /*yield*/, productRepo.findOne({
220
219
  where: { id: productOrProductId },
221
220
  relations: ["variants", "variants.options", "options"],
222
221
  })];
223
222
  case 1:
224
- product = (_b.sent());
223
+ product = (_a.sent());
225
224
  return [3 /*break*/, 3];
226
225
  case 2:
227
226
  if (!product.id) {
228
227
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product id missing");
229
228
  }
230
- _b.label = 3;
229
+ _a.label = 3;
231
230
  case 3:
232
231
  if (product.options.length !== variant.options.length) {
233
232
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product options length does not match variant options length. Product has ".concat(product.options.length, " and variant has ").concat(variant.options.length, "."));
@@ -253,54 +252,25 @@ var ProductVariantService = /** @class */ (function (_super) {
253
252
  productVariant = variantRepo.create(toCreate);
254
253
  return [4 /*yield*/, variantRepo.save(productVariant)];
255
254
  case 4:
256
- result = _b.sent();
257
- if (!prices) return [3 /*break*/, 15];
258
- _b.label = 5;
255
+ result = _a.sent();
256
+ if (!prices) return [3 /*break*/, 6];
257
+ return [4 /*yield*/, this.updateVariantPrices([
258
+ {
259
+ variantId: result.id,
260
+ prices: prices,
261
+ },
262
+ ])];
259
263
  case 5:
260
- _b.trys.push([5, 13, 14, 15]);
261
- prices_1 = __values(prices), prices_1_1 = prices_1.next();
262
- _b.label = 6;
263
- case 6:
264
- if (!!prices_1_1.done) return [3 /*break*/, 12];
265
- price = prices_1_1.value;
266
- if (!price.region_id) return [3 /*break*/, 9];
267
- return [4 /*yield*/, this.regionService_.retrieve(price.region_id)];
268
- case 7:
269
- region = _b.sent();
270
- return [4 /*yield*/, this.setRegionPrice(result.id, {
271
- amount: price.amount,
272
- region_id: price.region_id,
273
- currency_code: region.currency_code,
274
- })];
275
- case 8:
276
- _b.sent();
277
- return [3 /*break*/, 11];
278
- case 9: return [4 /*yield*/, this.setCurrencyPrice(result.id, price)];
279
- case 10:
280
- _b.sent();
281
- _b.label = 11;
282
- case 11:
283
- prices_1_1 = prices_1.next();
284
- return [3 /*break*/, 6];
285
- case 12: return [3 /*break*/, 15];
286
- case 13:
287
- e_1_1 = _b.sent();
288
- e_1 = { error: e_1_1 };
289
- return [3 /*break*/, 15];
290
- case 14:
291
- try {
292
- if (prices_1_1 && !prices_1_1.done && (_a = prices_1.return)) _a.call(prices_1);
293
- }
294
- finally { if (e_1) throw e_1.error; }
295
- return [7 /*endfinally*/];
296
- case 15: return [4 /*yield*/, this.eventBus_
264
+ _a.sent();
265
+ _a.label = 6;
266
+ case 6: return [4 /*yield*/, this.eventBus_
297
267
  .withTransaction(manager)
298
268
  .emit(ProductVariantService.Events.CREATED, {
299
269
  id: result.id,
300
270
  product_id: result.product_id,
301
271
  })];
302
- case 16:
303
- _b.sent();
272
+ case 7:
273
+ _a.sent();
304
274
  return [2 /*return*/, result];
305
275
  }
306
276
  });
@@ -310,114 +280,174 @@ var ProductVariantService = /** @class */ (function (_super) {
310
280
  });
311
281
  });
312
282
  };
313
- /**
314
- * Updates a variant.
315
- * Price updates should use dedicated methods.
316
- * The function will throw, if price updates are attempted.
317
- * @param variantOrVariantId - variant or id of a variant.
318
- * @param update - an object with the update values.
319
- * @param config - an object with the config values for returning the variant.
320
- * @return resolves to the update result.
321
- */
322
- ProductVariantService.prototype.update = function (variantOrVariantId, update) {
283
+ ProductVariantService.prototype.update = function (variantOrVariantIdOrData, updateData) {
284
+ return __awaiter(this, void 0, void 0, function () {
285
+ var data;
286
+ var _this = this;
287
+ return __generator(this, function (_a) {
288
+ switch (_a.label) {
289
+ case 0:
290
+ data = Array.isArray(variantOrVariantIdOrData)
291
+ ? variantOrVariantIdOrData
292
+ : [];
293
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
294
+ var variantRepo, variant, result;
295
+ return __generator(this, function (_a) {
296
+ switch (_a.label) {
297
+ case 0:
298
+ variantRepo = manager.getCustomRepository(this.productVariantRepository_);
299
+ if (!updateData) return [3 /*break*/, 3];
300
+ variant = variantOrVariantIdOrData;
301
+ if (!(0, utils_1.isString)(variantOrVariantIdOrData)) return [3 /*break*/, 2];
302
+ return [4 /*yield*/, this.retrieve(variantOrVariantIdOrData, {
303
+ select: variantRepo.metadata.columns.map(function (c) { return c.propertyName; }),
304
+ })];
305
+ case 1:
306
+ variant = _a.sent();
307
+ _a.label = 2;
308
+ case 2:
309
+ if (!(variant === null || variant === void 0 ? void 0 : variant.id)) {
310
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Variant id missing");
311
+ }
312
+ data = [{ variant: variant, updateData: updateData }];
313
+ _a.label = 3;
314
+ case 3: return [4 /*yield*/, this.updateBatch(data)];
315
+ case 4:
316
+ result = _a.sent();
317
+ return [2 /*return*/, (Array.isArray(variantOrVariantIdOrData)
318
+ ? result
319
+ : result[0])];
320
+ }
321
+ });
322
+ }); })];
323
+ case 1: return [2 /*return*/, _a.sent()];
324
+ }
325
+ });
326
+ });
327
+ };
328
+ ProductVariantService.prototype.updateBatch = function (variantData) {
323
329
  return __awaiter(this, void 0, void 0, function () {
324
330
  var _this = this;
325
331
  return __generator(this, function (_a) {
326
332
  switch (_a.label) {
327
333
  case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
328
- var variantRepo, variant, variantRes, prices, options, metadata, inventory_quantity, rest, options_1, options_1_1, option, e_2_1, _a, _b, _c, key, value, result;
329
- var e_2, _d, e_3, _e;
330
- return __generator(this, function (_f) {
331
- switch (_f.label) {
334
+ var variantRepo, variantPriceUpdateData, results, events;
335
+ var _this = this;
336
+ return __generator(this, function (_a) {
337
+ switch (_a.label) {
332
338
  case 0:
333
339
  variantRepo = manager.getCustomRepository(this.productVariantRepository_);
334
- variant = variantOrVariantId;
335
- if (!(typeof variant === "string")) return [3 /*break*/, 2];
336
- return [4 /*yield*/, variantRepo.findOne({
337
- where: { id: variantOrVariantId },
338
- })];
340
+ variantPriceUpdateData = variantData
341
+ .filter(function (data) { return (0, medusa_core_utils_1.isDefined)(data.updateData.prices); })
342
+ .map(function (data) { return ({
343
+ variantId: data.variant.id,
344
+ prices: data.updateData.prices,
345
+ }); });
346
+ if (!variantPriceUpdateData.length) return [3 /*break*/, 2];
347
+ return [4 /*yield*/, this.updateVariantPrices(variantPriceUpdateData)];
339
348
  case 1:
340
- variantRes = _f.sent();
341
- if (!(0, medusa_core_utils_1.isDefined)(variantRes)) {
342
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Variant with id ".concat(variantOrVariantId, " was not found"));
343
- }
344
- else {
345
- variant = variantRes;
346
- }
347
- return [3 /*break*/, 3];
348
- case 2:
349
- if (!variant.id) {
350
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Variant id missing");
351
- }
352
- _f.label = 3;
349
+ _a.sent();
350
+ _a.label = 2;
351
+ case 2: return [4 /*yield*/, Promise.all(variantData.map(function (_a) {
352
+ var variant = _a.variant, updateData = _a.updateData;
353
+ return __awaiter(_this, void 0, void 0, function () {
354
+ var prices, options, rest, shouldUpdate, shouldEmitUpdateEvent, _b, _c, option, e_1_1, toUpdate, _d, _e, _f, key, value, result, id, rawResult;
355
+ var e_1, _g, e_2, _h;
356
+ return __generator(this, function (_j) {
357
+ switch (_j.label) {
358
+ case 0:
359
+ prices = updateData.prices, options = updateData.options, rest = __rest(updateData, ["prices", "options"]);
360
+ shouldUpdate = (0, utils_1.hasChanges)(variant, rest);
361
+ shouldEmitUpdateEvent = shouldUpdate || !!(options === null || options === void 0 ? void 0 : options.length) || !!(prices === null || prices === void 0 ? void 0 : prices.length);
362
+ _j.label = 1;
363
+ case 1:
364
+ _j.trys.push([1, 6, 7, 8]);
365
+ _b = __values(options !== null && options !== void 0 ? options : []), _c = _b.next();
366
+ _j.label = 2;
367
+ case 2:
368
+ if (!!_c.done) return [3 /*break*/, 5];
369
+ option = _c.value;
370
+ return [4 /*yield*/, this.updateOptionValue(variant.id, option.option_id, option.value)];
371
+ case 3:
372
+ _j.sent();
373
+ _j.label = 4;
374
+ case 4:
375
+ _c = _b.next();
376
+ return [3 /*break*/, 2];
377
+ case 5: return [3 /*break*/, 8];
378
+ case 6:
379
+ e_1_1 = _j.sent();
380
+ e_1 = { error: e_1_1 };
381
+ return [3 /*break*/, 8];
382
+ case 7:
383
+ try {
384
+ if (_c && !_c.done && (_g = _b.return)) _g.call(_b);
385
+ }
386
+ finally { if (e_1) throw e_1.error; }
387
+ return [7 /*endfinally*/];
388
+ case 8:
389
+ toUpdate = {};
390
+ if ((0, utils_1.isObject)(rest.metadata)) {
391
+ toUpdate["metadata"] = (0, utils_1.setMetadata)(variant, rest.metadata);
392
+ delete rest.metadata;
393
+ }
394
+ if (Object.keys(rest).length) {
395
+ try {
396
+ for (_d = __values(Object.entries(rest)), _e = _d.next(); !_e.done; _e = _d.next()) {
397
+ _f = __read(_e.value, 2), key = _f[0], value = _f[1];
398
+ if (variant[key] !== value) {
399
+ toUpdate[key] = value;
400
+ }
401
+ }
402
+ }
403
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
404
+ finally {
405
+ try {
406
+ if (_e && !_e.done && (_h = _d.return)) _h.call(_d);
407
+ }
408
+ finally { if (e_2) throw e_2.error; }
409
+ }
410
+ }
411
+ result = variant;
412
+ if (!shouldUpdate) return [3 /*break*/, 10];
413
+ id = variant.id;
414
+ return [4 /*yield*/, variantRepo.update({ id: id }, toUpdate)];
415
+ case 9:
416
+ rawResult = _j.sent();
417
+ result = variantRepo.create(__assign(__assign({}, variant), rawResult.generatedMaps[0]));
418
+ _j.label = 10;
419
+ case 10: return [2 /*return*/, [result, updateData, shouldEmitUpdateEvent]];
420
+ }
421
+ });
422
+ });
423
+ }))];
353
424
  case 3:
354
- prices = update.prices, options = update.options, metadata = update.metadata, inventory_quantity = update.inventory_quantity, rest = __rest(update, ["prices", "options", "metadata", "inventory_quantity"]);
355
- if (!prices) return [3 /*break*/, 5];
356
- return [4 /*yield*/, this.updateVariantPrices(variant.id, prices)];
425
+ results = _a.sent();
426
+ events = results
427
+ .filter(function (_a) {
428
+ var _b = __read(_a, 3), shouldEmitUpdateEvent = _b[2];
429
+ return shouldEmitUpdateEvent;
430
+ })
431
+ .map(function (_a) {
432
+ var _b = __read(_a, 2), result = _b[0], updatedData = _b[1];
433
+ return {
434
+ eventName: ProductVariantService.Events.UPDATED,
435
+ data: {
436
+ id: result.id,
437
+ product_id: result.product_id,
438
+ fields: Object.keys(updatedData),
439
+ },
440
+ };
441
+ });
442
+ if (!events.length) return [3 /*break*/, 5];
443
+ return [4 /*yield*/, this.eventBus_.withTransaction(manager).emit(events)];
357
444
  case 4:
358
- _f.sent();
359
- _f.label = 5;
360
- case 5:
361
- if (!options) return [3 /*break*/, 13];
362
- _f.label = 6;
363
- case 6:
364
- _f.trys.push([6, 11, 12, 13]);
365
- options_1 = __values(options), options_1_1 = options_1.next();
366
- _f.label = 7;
367
- case 7:
368
- if (!!options_1_1.done) return [3 /*break*/, 10];
369
- option = options_1_1.value;
370
- return [4 /*yield*/, this.updateOptionValue(variant.id, option.option_id, option.value)];
371
- case 8:
372
- _f.sent();
373
- _f.label = 9;
374
- case 9:
375
- options_1_1 = options_1.next();
376
- return [3 /*break*/, 7];
377
- case 10: return [3 /*break*/, 13];
378
- case 11:
379
- e_2_1 = _f.sent();
380
- e_2 = { error: e_2_1 };
381
- return [3 /*break*/, 13];
382
- case 12:
383
- try {
384
- if (options_1_1 && !options_1_1.done && (_d = options_1.return)) _d.call(options_1);
385
- }
386
- finally { if (e_2) throw e_2.error; }
387
- return [7 /*endfinally*/];
388
- case 13:
389
- if (typeof metadata === "object") {
390
- variant.metadata = (0, utils_1.setMetadata)(variant, metadata);
391
- }
392
- if (typeof inventory_quantity === "number") {
393
- variant.inventory_quantity = inventory_quantity;
394
- }
395
- try {
396
- for (_a = __values(Object.entries(rest)), _b = _a.next(); !_b.done; _b = _a.next()) {
397
- _c = __read(_b.value, 2), key = _c[0], value = _c[1];
398
- variant[key] = value;
399
- }
400
- }
401
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
402
- finally {
403
- try {
404
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
405
- }
406
- finally { if (e_3) throw e_3.error; }
407
- }
408
- return [4 /*yield*/, variantRepo.save(variant)];
409
- case 14:
410
- result = _f.sent();
411
- return [4 /*yield*/, this.eventBus_
412
- .withTransaction(manager)
413
- .emit(ProductVariantService.Events.UPDATED, {
414
- id: result.id,
415
- product_id: result.product_id,
416
- fields: Object.keys(update),
417
- })];
418
- case 15:
419
- _f.sent();
420
- return [2 /*return*/, result];
445
+ _a.sent();
446
+ _a.label = 5;
447
+ case 5: return [2 /*return*/, results.map(function (_a) {
448
+ var _b = __read(_a, 1), variant = _b[0];
449
+ return variant;
450
+ })];
421
451
  }
422
452
  });
423
453
  }); })];
@@ -426,70 +456,231 @@ var ProductVariantService = /** @class */ (function (_super) {
426
456
  });
427
457
  });
428
458
  };
429
- /**
430
- * Updates a variant's prices.
431
- * Deletes any prices that are not in the update object, and is not associated with a price list.
432
- * @param variantId - the id of variant
433
- * @param prices - the update prices
434
- * @returns empty promise
435
- */
436
- ProductVariantService.prototype.updateVariantPrices = function (variantId, prices) {
459
+ ProductVariantService.prototype.updateVariantPrices = function (variantIdOrData, prices) {
460
+ return __awaiter(this, void 0, void 0, function () {
461
+ var data;
462
+ return __generator(this, function (_a) {
463
+ switch (_a.label) {
464
+ case 0:
465
+ data = !(0, utils_1.isString)(variantIdOrData)
466
+ ? variantIdOrData
467
+ : [];
468
+ if (prices && (0, utils_1.isString)(variantIdOrData)) {
469
+ data = [{ variantId: variantIdOrData, prices: prices }];
470
+ }
471
+ return [4 /*yield*/, this.updateVariantPricesBatch(data)];
472
+ case 1: return [2 /*return*/, _a.sent()];
473
+ }
474
+ });
475
+ });
476
+ };
477
+ ProductVariantService.prototype.updateVariantPricesBatch = function (data) {
437
478
  return __awaiter(this, void 0, void 0, function () {
438
479
  var _this = this;
439
480
  return __generator(this, function (_a) {
440
481
  switch (_a.label) {
441
482
  case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
442
- var moneyAmountRepo, regionsServiceTx, prices_2, prices_2_1, price, region, e_4_1;
443
- var e_4, _a;
444
- return __generator(this, function (_b) {
445
- switch (_b.label) {
483
+ var moneyAmountRepo, regionIdsSet, regions, regionsMap, dataRegionPrices, dataCurrencyPrices, promises;
484
+ return __generator(this, function (_a) {
485
+ switch (_a.label) {
446
486
  case 0:
447
487
  moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepository_);
448
488
  // Delete obsolete prices
449
- return [4 /*yield*/, moneyAmountRepo.deleteVariantPricesNotIn(variantId, prices)];
489
+ return [4 /*yield*/, moneyAmountRepo.deleteVariantPricesNotIn(data)];
450
490
  case 1:
451
491
  // Delete obsolete prices
452
- _b.sent();
453
- regionsServiceTx = this.regionService_.withTransaction(manager);
454
- _b.label = 2;
492
+ _a.sent();
493
+ regionIdsSet = new Set(data
494
+ .map(function (data_) {
495
+ return data_.prices
496
+ .filter(function (price) { return price.region_id; })
497
+ .map(function (price) { return price.region_id; });
498
+ })
499
+ .flat());
500
+ return [4 /*yield*/, this.regionService_.withTransaction(manager).list({
501
+ id: __spreadArray([], __read(regionIdsSet), false),
502
+ }, {
503
+ select: ["id", "currency_code"],
504
+ })];
455
505
  case 2:
456
- _b.trys.push([2, 10, 11, 12]);
457
- prices_2 = __values(prices), prices_2_1 = prices_2.next();
458
- _b.label = 3;
506
+ regions = _a.sent();
507
+ regionsMap = new Map(regions.map(function (r) { return [r.id, r]; }));
508
+ dataRegionPrices = [];
509
+ dataCurrencyPrices = [];
510
+ data.forEach(function (_a) {
511
+ var prices = _a.prices, variantId = _a.variantId;
512
+ prices.forEach(function (price) {
513
+ if (price.region_id) {
514
+ var region = regionsMap.get(price.region_id);
515
+ dataRegionPrices.push({
516
+ variantId: variantId,
517
+ price: {
518
+ currency_code: region.currency_code,
519
+ region_id: price.region_id,
520
+ amount: price.amount,
521
+ },
522
+ });
523
+ }
524
+ else {
525
+ dataCurrencyPrices.push({
526
+ variantId: variantId,
527
+ price: __assign(__assign({}, price), { currency_code: price.currency_code }),
528
+ });
529
+ }
530
+ });
531
+ });
532
+ promises = [];
533
+ if (dataRegionPrices.length) {
534
+ promises.push(this.addOrUpdateRegionPrices(dataRegionPrices));
535
+ }
536
+ if (dataCurrencyPrices.length) {
537
+ promises.push(this.addOrUpdateCurrencyPrices(dataCurrencyPrices));
538
+ }
539
+ return [4 /*yield*/, Promise.all(promises)];
459
540
  case 3:
460
- if (!!prices_2_1.done) return [3 /*break*/, 9];
461
- price = prices_2_1.value;
462
- if (!price.region_id) return [3 /*break*/, 6];
463
- return [4 /*yield*/, regionsServiceTx.retrieve(price.region_id)];
464
- case 4:
465
- region = _b.sent();
466
- return [4 /*yield*/, this.setRegionPrice(variantId, {
467
- currency_code: region.currency_code,
468
- region_id: price.region_id,
469
- amount: price.amount,
541
+ _a.sent();
542
+ return [2 /*return*/];
543
+ }
544
+ });
545
+ }); })];
546
+ case 1: return [2 /*return*/, _a.sent()];
547
+ }
548
+ });
549
+ });
550
+ };
551
+ ProductVariantService.prototype.addOrUpdateRegionPrices = function (data) {
552
+ return __awaiter(this, void 0, void 0, function () {
553
+ var _this = this;
554
+ return __generator(this, function (_a) {
555
+ switch (_a.label) {
556
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
557
+ var moneyAmountRepo, where, moneyAmounts, moneyAmountsMapToVariantId, dataToCreate, dataToUpdate, promises;
558
+ return __generator(this, function (_a) {
559
+ switch (_a.label) {
560
+ case 0:
561
+ moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepository_);
562
+ where = data.map(function (data_) { return ({
563
+ variant_id: data_.variantId,
564
+ region_id: data_.price.region_id,
565
+ price_list_id: (0, typeorm_1.IsNull)(),
566
+ }); });
567
+ return [4 /*yield*/, moneyAmountRepo.find({
568
+ where: where,
470
569
  })];
471
- case 5:
472
- _b.sent();
473
- return [3 /*break*/, 8];
474
- case 6: return [4 /*yield*/, this.setCurrencyPrice(variantId, price)];
475
- case 7:
476
- _b.sent();
477
- _b.label = 8;
478
- case 8:
479
- prices_2_1 = prices_2.next();
480
- return [3 /*break*/, 3];
481
- case 9: return [3 /*break*/, 12];
482
- case 10:
483
- e_4_1 = _b.sent();
484
- e_4 = { error: e_4_1 };
485
- return [3 /*break*/, 12];
486
- case 11:
487
- try {
488
- if (prices_2_1 && !prices_2_1.done && (_a = prices_2.return)) _a.call(prices_2);
570
+ case 1:
571
+ moneyAmounts = _a.sent();
572
+ moneyAmountsMapToVariantId = new Map();
573
+ moneyAmounts.map(function (d) {
574
+ var _a;
575
+ var moneyAmounts = (_a = moneyAmountsMapToVariantId.get(d.variant_id)) !== null && _a !== void 0 ? _a : [];
576
+ moneyAmounts.push(d);
577
+ moneyAmountsMapToVariantId.set(d.variant_id, moneyAmounts);
578
+ });
579
+ dataToCreate = [];
580
+ dataToUpdate = [];
581
+ data.forEach(function (_a) {
582
+ var _b;
583
+ var price = _a.price, variantId = _a.variantId;
584
+ var variantMoneyAmounts = (_b = moneyAmountsMapToVariantId.get(variantId)) !== null && _b !== void 0 ? _b : [];
585
+ var moneyAmount = variantMoneyAmounts.find(function (ma) { return ma.region_id === price.region_id; });
586
+ if (moneyAmount) {
587
+ // No need to update if the amount is the same
588
+ if (moneyAmount.amount !== price.amount) {
589
+ dataToUpdate.push({
590
+ id: moneyAmount.id,
591
+ amount: price.amount,
592
+ });
593
+ }
594
+ }
595
+ else {
596
+ dataToCreate.push(moneyAmountRepo.create(__assign(__assign({}, price), { variant_id: variantId })));
597
+ }
598
+ });
599
+ promises = [];
600
+ if (dataToCreate.length) {
601
+ promises.push(moneyAmountRepo.insertBulk(dataToCreate));
602
+ }
603
+ if (dataToUpdate.length) {
604
+ dataToUpdate.forEach(function (data) {
605
+ var id = data.id, rest = __rest(data, ["id"]);
606
+ promises.push(moneyAmountRepo.update({ id: data.id }, rest));
607
+ });
489
608
  }
490
- finally { if (e_4) throw e_4.error; }
491
- return [7 /*endfinally*/];
492
- case 12: return [2 /*return*/];
609
+ return [4 /*yield*/, Promise.all(promises)];
610
+ case 2:
611
+ _a.sent();
612
+ return [2 /*return*/];
613
+ }
614
+ });
615
+ }); })];
616
+ case 1: return [2 /*return*/, _a.sent()];
617
+ }
618
+ });
619
+ });
620
+ };
621
+ ProductVariantService.prototype.addOrUpdateCurrencyPrices = function (data) {
622
+ return __awaiter(this, void 0, void 0, function () {
623
+ var _this = this;
624
+ return __generator(this, function (_a) {
625
+ switch (_a.label) {
626
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
627
+ var moneyAmountRepo, where, moneyAmounts, moneyAmountsMapToVariantId, dataToCreate, dataToUpdate, promises;
628
+ return __generator(this, function (_a) {
629
+ switch (_a.label) {
630
+ case 0:
631
+ moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepository_);
632
+ where = data.map(function (data_) { return ({
633
+ variant_id: data_.variantId,
634
+ currency_code: data_.price.currency_code,
635
+ region_id: (0, typeorm_1.IsNull)(),
636
+ price_list_id: (0, typeorm_1.IsNull)(),
637
+ }); });
638
+ return [4 /*yield*/, moneyAmountRepo.find({
639
+ where: where,
640
+ })];
641
+ case 1:
642
+ moneyAmounts = _a.sent();
643
+ moneyAmountsMapToVariantId = new Map();
644
+ moneyAmounts.map(function (d) {
645
+ var _a;
646
+ var moneyAmounts = (_a = moneyAmountsMapToVariantId.get(d.variant_id)) !== null && _a !== void 0 ? _a : [];
647
+ moneyAmounts.push(d);
648
+ moneyAmountsMapToVariantId.set(d.variant_id, moneyAmounts);
649
+ });
650
+ dataToCreate = [];
651
+ dataToUpdate = [];
652
+ data.forEach(function (_a) {
653
+ var _b;
654
+ var price = _a.price, variantId = _a.variantId;
655
+ var variantMoneyAmounts = (_b = moneyAmountsMapToVariantId.get(variantId)) !== null && _b !== void 0 ? _b : [];
656
+ var moneyAmount = variantMoneyAmounts.find(function (ma) { return ma.currency_code === price.currency_code; });
657
+ if (moneyAmount) {
658
+ // No need to update if the amount is the same
659
+ if (moneyAmount.amount !== price.amount) {
660
+ dataToUpdate.push({
661
+ id: moneyAmount.id,
662
+ amount: price.amount,
663
+ });
664
+ }
665
+ }
666
+ else {
667
+ dataToCreate.push(moneyAmountRepo.create(__assign(__assign({}, price), { variant_id: variantId, currency_code: price.currency_code.toLowerCase() })));
668
+ }
669
+ });
670
+ promises = [];
671
+ if (dataToCreate.length) {
672
+ promises.push(moneyAmountRepo.insertBulk(dataToCreate));
673
+ }
674
+ if (dataToUpdate.length) {
675
+ dataToUpdate.forEach(function (data) {
676
+ var id = data.id, rest = __rest(data, ["id"]);
677
+ promises.push(moneyAmountRepo.update({ id: data.id }, rest));
678
+ });
679
+ }
680
+ return [4 /*yield*/, Promise.all(promises)];
681
+ case 2:
682
+ _a.sent();
683
+ return [2 /*return*/];
493
684
  }
494
685
  });
495
686
  }); })];
@@ -541,6 +732,7 @@ var ProductVariantService = /** @class */ (function (_super) {
541
732
  });
542
733
  };
543
734
  /**
735
+ * @deprecated use addOrUpdateRegionPrices instead
544
736
  * Sets the default price of a specific region
545
737
  * @param variantId - the id of the variant to update
546
738
  * @param price - the price for the variant.
@@ -583,6 +775,7 @@ var ProductVariantService = /** @class */ (function (_super) {
583
775
  });
584
776
  };
585
777
  /**
778
+ * @deprecated use addOrUpdateCurrencyPrices instead
586
779
  * Sets the default price for the given currency.
587
780
  * @param variantId - the id of the variant to set prices for
588
781
  * @param price - the price for the variant
@@ -815,48 +1008,55 @@ var ProductVariantService = /** @class */ (function (_super) {
815
1008
  });
816
1009
  };
817
1010
  /**
818
- * Deletes variant.
1011
+ * Deletes variant or variants.
819
1012
  * Will never fail due to delete being idempotent.
820
- * @param variantId - the id of the variant to delete. Must be
1013
+ * @param variantIds - the id of the variant to delete. Must be
821
1014
  * castable as an ObjectId
822
1015
  * @return empty promise
823
1016
  */
824
- ProductVariantService.prototype.delete = function (variantId) {
1017
+ ProductVariantService.prototype.delete = function (variantIds) {
825
1018
  return __awaiter(this, void 0, void 0, function () {
1019
+ var variantIds_;
826
1020
  var _this = this;
827
1021
  return __generator(this, function (_a) {
828
1022
  switch (_a.label) {
829
- case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
830
- var variantRepo, variant;
831
- return __generator(this, function (_a) {
832
- switch (_a.label) {
833
- case 0:
834
- variantRepo = manager.getCustomRepository(this.productVariantRepository_);
835
- return [4 /*yield*/, variantRepo.findOne({
836
- where: { id: variantId },
837
- relations: ["prices", "options", "inventory_items"],
838
- })];
839
- case 1:
840
- variant = _a.sent();
841
- if (!variant) {
842
- return [2 /*return*/, Promise.resolve()];
843
- }
844
- return [4 /*yield*/, variantRepo.softRemove(variant)];
845
- case 2:
846
- _a.sent();
847
- return [4 /*yield*/, this.eventBus_
848
- .withTransaction(manager)
849
- .emit(ProductVariantService.Events.DELETED, {
850
- id: variant.id,
851
- product_id: variant.product_id,
852
- metadata: variant.metadata,
853
- })];
854
- case 3:
855
- _a.sent();
856
- return [2 /*return*/];
857
- }
858
- });
859
- }); })];
1023
+ case 0:
1024
+ variantIds_ = (0, utils_1.isString)(variantIds) ? [variantIds] : variantIds;
1025
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1026
+ var variantRepo, variants, events;
1027
+ return __generator(this, function (_a) {
1028
+ switch (_a.label) {
1029
+ case 0:
1030
+ variantRepo = manager.getCustomRepository(this.productVariantRepository_);
1031
+ return [4 /*yield*/, variantRepo.find({
1032
+ where: { id: (0, typeorm_1.In)(variantIds_) },
1033
+ relations: ["prices", "options", "inventory_items"],
1034
+ })];
1035
+ case 1:
1036
+ variants = _a.sent();
1037
+ if (!variants.length) {
1038
+ return [2 /*return*/, Promise.resolve()];
1039
+ }
1040
+ return [4 /*yield*/, variantRepo.softRemove(variants)];
1041
+ case 2:
1042
+ _a.sent();
1043
+ events = variants.map(function (variant) {
1044
+ return {
1045
+ eventName: ProductVariantService.Events.DELETED,
1046
+ data: {
1047
+ id: variant.id,
1048
+ product_id: variant.product_id,
1049
+ metadata: variant.metadata,
1050
+ },
1051
+ };
1052
+ });
1053
+ return [4 /*yield*/, this.eventBus_.withTransaction(manager).emit(events)];
1054
+ case 3:
1055
+ _a.sent();
1056
+ return [2 /*return*/];
1057
+ }
1058
+ });
1059
+ }); })];
860
1060
  case 1: return [2 /*return*/, _a.sent()];
861
1061
  }
862
1062
  });