@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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +19 -0
- package/dist/api/routes/admin/discounts/create-condition.d.ts +2 -2
- package/dist/api/routes/admin/discounts/create-condition.js +2 -2
- package/dist/api/routes/admin/discounts/update-condition.d.ts +2 -2
- package/dist/api/routes/admin/discounts/update-condition.js +2 -2
- package/dist/api/routes/admin/gift-cards/update-gift-card.d.ts +2 -2
- package/dist/api/routes/admin/gift-cards/update-gift-card.js +2 -2
- package/dist/api/routes/admin/orders/create-swap-shipment.d.ts +1 -1
- package/dist/api/routes/admin/orders/create-swap-shipment.js +1 -1
- package/dist/api/routes/admin/orders/fulfill-swap.d.ts +1 -1
- package/dist/api/routes/admin/orders/fulfill-swap.js.map +1 -1
- package/dist/api/routes/admin/products/create-product.d.ts +1 -1
- package/dist/api/routes/admin/products/create-product.js +1 -1
- package/dist/api/routes/admin/products/update-variant.d.ts +1 -1
- package/dist/api/routes/admin/products/update-variant.js +1 -1
- package/dist/api/routes/admin/swaps/list-swaps.js +1 -1
- package/dist/api/routes/admin/swaps/list-swaps.js.map +1 -1
- package/dist/api/routes/store/customers/list-orders.d.ts +94 -7
- package/dist/api/routes/store/customers/list-orders.js +96 -9
- package/dist/api/routes/store/customers/list-orders.js.map +1 -1
- package/dist/api/routes/store/swaps/index.d.ts +2 -1
- package/dist/api/routes/store/swaps/index.js.map +1 -1
- package/dist/commands/develop.js +3 -0
- package/dist/commands/develop.js.map +1 -1
- package/dist/loaders/strategies.js +9 -1
- package/dist/loaders/strategies.js.map +1 -1
- package/dist/migrations/1661863940645-swap_fulfillment_status_requires_action.d.ts +6 -0
- package/dist/migrations/1661863940645-swap_fulfillment_status_requires_action.js +89 -0
- package/dist/migrations/1661863940645-swap_fulfillment_status_requires_action.js.map +1 -0
- package/dist/models/shipping-method-tax-line.d.ts +4 -4
- package/dist/models/shipping-method-tax-line.js +4 -4
- package/dist/models/shipping-method.d.ts +1 -1
- package/dist/models/shipping-method.js +1 -1
- package/dist/models/swap.d.ts +2 -1
- package/dist/models/swap.js +2 -1
- package/dist/models/swap.js.map +1 -1
- package/dist/repositories/sales-channel.d.ts +3 -1
- package/dist/repositories/sales-channel.js +123 -0
- package/dist/repositories/sales-channel.js.map +1 -1
- package/dist/services/batch-job.js.map +1 -1
- package/dist/services/cart.d.ts +0 -7
- package/dist/services/cart.js +10 -58
- package/dist/services/cart.js.map +1 -1
- package/dist/services/csv-parser.d.ts +1 -3
- package/dist/services/csv-parser.js +4 -4
- package/dist/services/csv-parser.js.map +1 -1
- package/dist/services/event-bus.d.ts +4 -4
- package/dist/services/line-item.d.ts +8 -0
- package/dist/services/line-item.js +9 -0
- package/dist/services/line-item.js.map +1 -1
- package/dist/services/product-variant.d.ts +2 -2
- package/dist/services/product-variant.js +43 -35
- package/dist/services/product-variant.js.map +1 -1
- package/dist/services/product.d.ts +9 -1
- package/dist/services/product.js +36 -15
- package/dist/services/product.js.map +1 -1
- package/dist/services/region.d.ts +7 -0
- package/dist/services/region.js +22 -0
- package/dist/services/region.js.map +1 -1
- package/dist/services/sales-channel.d.ts +19 -1
- package/dist/services/sales-channel.js +64 -14
- package/dist/services/sales-channel.js.map +1 -1
- package/dist/services/swap.d.ts +152 -168
- package/dist/services/swap.js +898 -878
- package/dist/services/swap.js.map +1 -1
- package/dist/services/tax-provider.d.ts +1 -1
- package/dist/services/user.d.ts +1 -7
- package/dist/services/user.js +3 -15
- package/dist/services/user.js.map +1 -1
- package/dist/strategies/batch-jobs/product/import.d.ts +140 -0
- package/dist/strategies/batch-jobs/product/import.js +1162 -0
- package/dist/strategies/batch-jobs/product/import.js.map +1 -0
- package/dist/strategies/batch-jobs/product/types.d.ts +47 -0
- package/dist/strategies/batch-jobs/product/types.js +14 -0
- package/dist/strategies/batch-jobs/product/types.js.map +1 -0
- package/dist/strategies/batch-jobs/product/utils.d.ts +15 -0
- package/dist/strategies/batch-jobs/product/utils.js +44 -0
- package/dist/strategies/batch-jobs/product/utils.js.map +1 -0
- package/dist/subscribers/batch-job.js +28 -8
- package/dist/subscribers/batch-job.js.map +1 -1
- package/dist/types/fulfillment.d.ts +4 -2
- package/dist/utils/is-email.d.ts +6 -0
- package/dist/utils/is-email.js +19 -0
- package/dist/utils/is-email.js.map +1 -0
- package/package.json +3 -5
package/dist/services/swap.js
CHANGED
|
@@ -108,79 +108,51 @@ var __values = (this && this.__values) || function(o) {
|
|
|
108
108
|
};
|
|
109
109
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
110
110
|
};
|
|
111
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
112
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
113
|
+
};
|
|
111
114
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
112
115
|
var medusa_core_utils_1 = require("medusa-core-utils");
|
|
113
|
-
var medusa_interfaces_1 = require("medusa-interfaces");
|
|
114
116
|
var utils_1 = require("../utils");
|
|
117
|
+
var interfaces_1 = require("../interfaces");
|
|
118
|
+
var cart_1 = __importDefault(require("./cart"));
|
|
119
|
+
var models_1 = require("../models");
|
|
115
120
|
/**
|
|
116
121
|
* Handles swaps
|
|
117
|
-
* @extends BaseService
|
|
118
122
|
*/
|
|
119
123
|
var SwapService = /** @class */ (function (_super) {
|
|
120
124
|
__extends(SwapService, _super);
|
|
121
125
|
function SwapService(_a) {
|
|
122
|
-
var manager = _a.manager, swapRepository = _a.swapRepository, eventBusService = _a.eventBusService, cartService = _a.cartService, totalsService = _a.totalsService, returnService = _a.returnService, lineItemService = _a.lineItemService, paymentProviderService = _a.paymentProviderService,
|
|
123
|
-
var _this =
|
|
124
|
-
|
|
126
|
+
var manager = _a.manager, swapRepository = _a.swapRepository, eventBusService = _a.eventBusService, cartService = _a.cartService, totalsService = _a.totalsService, returnService = _a.returnService, lineItemService = _a.lineItemService, paymentProviderService = _a.paymentProviderService, shippingOptionService = _a.shippingOptionService, fulfillmentService = _a.fulfillmentService, orderService = _a.orderService, inventoryService = _a.inventoryService, customShippingOptionService = _a.customShippingOptionService, lineItemAdjustmentService = _a.lineItemAdjustmentService;
|
|
127
|
+
var _this =
|
|
128
|
+
// eslint-disable-next-line prefer-rest-params
|
|
129
|
+
_super.call(this, arguments[0]) || this;
|
|
125
130
|
_this.manager_ = manager;
|
|
126
|
-
/** @private @const {SwapModel} */
|
|
127
131
|
_this.swapRepository_ = swapRepository;
|
|
128
|
-
/** @private @const {TotalsService} */
|
|
129
132
|
_this.totalsService_ = totalsService;
|
|
130
|
-
/** @private @const {LineItemService} */
|
|
131
133
|
_this.lineItemService_ = lineItemService;
|
|
132
|
-
/** @private @const {ReturnService} */
|
|
133
134
|
_this.returnService_ = returnService;
|
|
134
|
-
/** @private @const {PaymentProviderService} */
|
|
135
135
|
_this.paymentProviderService_ = paymentProviderService;
|
|
136
|
-
/** @private @const {CartService} */
|
|
137
136
|
_this.cartService_ = cartService;
|
|
138
|
-
/** @private @const {FulfillmentService} */
|
|
139
137
|
_this.fulfillmentService_ = fulfillmentService;
|
|
140
|
-
/** @private @const {OrderService} */
|
|
141
138
|
_this.orderService_ = orderService;
|
|
142
|
-
/** @private @const {ShippingOptionService} */
|
|
143
139
|
_this.shippingOptionService_ = shippingOptionService;
|
|
144
|
-
/** @private @const {InventoryService} */
|
|
145
140
|
_this.inventoryService_ = inventoryService;
|
|
146
|
-
/** @private @const {EventBusService} */
|
|
147
141
|
_this.eventBus_ = eventBusService;
|
|
148
|
-
/** @private @const {typeof ShippingMethodTaxLineRepository} */
|
|
149
|
-
_this.shippingTaxLineRepo_ = shippingMethodTaxLineRepository;
|
|
150
|
-
/** @private @const {CustomShippingOptionService} */
|
|
151
142
|
_this.customShippingOptionService_ = customShippingOptionService;
|
|
152
|
-
/** @private @const {LineItemAdjustmentService} */
|
|
153
143
|
_this.lineItemAdjustmentService_ = lineItemAdjustmentService;
|
|
154
144
|
return _this;
|
|
155
145
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
eventBusService: this.eventBus_,
|
|
164
|
-
cartService: this.cartService_,
|
|
165
|
-
totalsService: this.totalsService_,
|
|
166
|
-
returnService: this.returnService_,
|
|
167
|
-
lineItemService: this.lineItemService_,
|
|
168
|
-
shippingMethodTaxLineRepository: this.shippingTaxLineRepo_,
|
|
169
|
-
paymentProviderService: this.paymentProviderService_,
|
|
170
|
-
shippingOptionService: this.shippingOptionService_,
|
|
171
|
-
orderService: this.orderService_,
|
|
172
|
-
inventoryService: this.inventoryService_,
|
|
173
|
-
fulfillmentService: this.fulfillmentService_,
|
|
174
|
-
customShippingOptionService: this.customShippingOptionService_,
|
|
175
|
-
lineItemAdjustmentService: this.lineItemAdjustmentService_,
|
|
176
|
-
});
|
|
177
|
-
cloned.transactionManager_ = transactionManager;
|
|
178
|
-
return cloned;
|
|
179
|
-
};
|
|
180
|
-
SwapService.prototype.transformQueryForCart_ = function (config) {
|
|
146
|
+
/**
|
|
147
|
+
* Transform find config object for retrieval.
|
|
148
|
+
*
|
|
149
|
+
* @param config parsed swap find config
|
|
150
|
+
* @return transformed find swap config
|
|
151
|
+
*/
|
|
152
|
+
SwapService.prototype.transformQueryForCart = function (config) {
|
|
181
153
|
var select = config.select, relations = config.relations;
|
|
182
|
-
var cartSelects
|
|
183
|
-
var cartRelations
|
|
154
|
+
var cartSelects;
|
|
155
|
+
var cartRelations;
|
|
184
156
|
if ((0, utils_1.isDefined)(relations) && relations.includes("cart")) {
|
|
185
157
|
var _a = __read(relations.reduce(function (acc, next) {
|
|
186
158
|
if (next === "cart") {
|
|
@@ -220,30 +192,31 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
220
192
|
};
|
|
221
193
|
/**
|
|
222
194
|
* Retrieves a swap with the given id.
|
|
223
|
-
*
|
|
224
|
-
* @param
|
|
225
|
-
* @
|
|
195
|
+
*
|
|
196
|
+
* @param id - the id of the swap to retrieve
|
|
197
|
+
* @param config - the configuration to retrieve the swap
|
|
198
|
+
* @return the swap
|
|
226
199
|
*/
|
|
227
200
|
SwapService.prototype.retrieve = function (id, config) {
|
|
228
201
|
if (config === void 0) { config = {}; }
|
|
229
202
|
return __awaiter(this, void 0, void 0, function () {
|
|
230
|
-
var swapRepo,
|
|
231
|
-
return __generator(this, function (
|
|
232
|
-
switch (
|
|
203
|
+
var swapRepo, _a, cartSelects, cartRelations, newConfig, query, relations, swap, _b;
|
|
204
|
+
return __generator(this, function (_c) {
|
|
205
|
+
switch (_c.label) {
|
|
233
206
|
case 0:
|
|
234
207
|
swapRepo = this.manager_.getCustomRepository(this.swapRepository_);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
rels = query.relations;
|
|
208
|
+
_a = this.transformQueryForCart(config), cartSelects = _a.cartSelects, cartRelations = _a.cartRelations, newConfig = __rest(_a, ["cartSelects", "cartRelations"]);
|
|
209
|
+
query = (0, utils_1.buildQuery)({ id: id }, newConfig);
|
|
210
|
+
relations = query.relations;
|
|
239
211
|
delete query.relations;
|
|
240
|
-
return [4 /*yield*/, swapRepo.findOneWithRelations(
|
|
212
|
+
return [4 /*yield*/, swapRepo.findOneWithRelations(relations, query)];
|
|
241
213
|
case 1:
|
|
242
|
-
swap =
|
|
214
|
+
swap = _c.sent();
|
|
243
215
|
if (!swap) {
|
|
244
216
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Swap was not found");
|
|
245
217
|
}
|
|
246
218
|
if (!(cartRelations || cartSelects)) return [3 /*break*/, 3];
|
|
219
|
+
_b = swap;
|
|
247
220
|
return [4 /*yield*/, this.cartService_
|
|
248
221
|
.withTransaction(this.manager_)
|
|
249
222
|
.retrieve(swap.cart_id, {
|
|
@@ -251,9 +224,8 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
251
224
|
relations: cartRelations,
|
|
252
225
|
})];
|
|
253
226
|
case 2:
|
|
254
|
-
cart =
|
|
255
|
-
|
|
256
|
-
_b.label = 3;
|
|
227
|
+
_b.cart = _c.sent();
|
|
228
|
+
_c.label = 3;
|
|
257
229
|
case 3: return [2 /*return*/, swap];
|
|
258
230
|
}
|
|
259
231
|
});
|
|
@@ -261,9 +233,10 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
261
233
|
};
|
|
262
234
|
/**
|
|
263
235
|
* Retrieves a swap based on its associated cart id
|
|
264
|
-
*
|
|
265
|
-
* @param
|
|
266
|
-
* @
|
|
236
|
+
*
|
|
237
|
+
* @param cartId - the cart id that the swap's cart has
|
|
238
|
+
* @param relations - the relations to retrieve swap
|
|
239
|
+
* @return the swap
|
|
267
240
|
*/
|
|
268
241
|
SwapService.prototype.retrieveByCartId = function (cartId, relations) {
|
|
269
242
|
if (relations === void 0) { relations = []; }
|
|
@@ -290,497 +263,505 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
290
263
|
});
|
|
291
264
|
};
|
|
292
265
|
/**
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
* @
|
|
266
|
+
* List swaps.
|
|
267
|
+
*
|
|
268
|
+
* @param selector - the query object for find
|
|
269
|
+
* @param config - the configuration used to find the objects. contains relations, skip, and take.
|
|
270
|
+
* @return the result of the find operation
|
|
296
271
|
*/
|
|
297
272
|
SwapService.prototype.list = function (selector, config) {
|
|
298
|
-
if (config === void 0) { config = {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
* original order. If the item exists it is verified that the quantity to
|
|
317
|
-
* return is not higher than the original quantity ordered.
|
|
318
|
-
* @param {OrderLike} order - the order to return from
|
|
319
|
-
* @param {Array<ReturnItem>} returnItems - the items to return
|
|
320
|
-
* @return {Array<ReturnItems>} the validated returnItems
|
|
321
|
-
*/
|
|
322
|
-
SwapService.prototype.validateReturnItems_ = function (order, returnItems) {
|
|
323
|
-
return returnItems.map(function (_a) {
|
|
324
|
-
var item_id = _a.item_id, quantity = _a.quantity;
|
|
325
|
-
var item = order.items.find(function (i) { return i.id === item_id; });
|
|
326
|
-
// The item must exist in the order
|
|
327
|
-
if (!item) {
|
|
328
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Item does not exist on order");
|
|
329
|
-
}
|
|
330
|
-
// Item's cannot be returned multiple times
|
|
331
|
-
if (item.quantity < item.returned_quantity + quantity) {
|
|
332
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Cannot return more items than have been ordered");
|
|
333
|
-
}
|
|
334
|
-
return { item_id: item_id, quantity: quantity };
|
|
273
|
+
if (config === void 0) { config = {
|
|
274
|
+
skip: 0,
|
|
275
|
+
take: 50,
|
|
276
|
+
order: { created_at: "DESC" },
|
|
277
|
+
}; }
|
|
278
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
279
|
+
var swapRepo, query, relations;
|
|
280
|
+
return __generator(this, function (_a) {
|
|
281
|
+
switch (_a.label) {
|
|
282
|
+
case 0:
|
|
283
|
+
swapRepo = this.manager_.getCustomRepository(this.swapRepository_);
|
|
284
|
+
query = (0, utils_1.buildQuery)(selector, config);
|
|
285
|
+
relations = query.relations;
|
|
286
|
+
delete query.relations;
|
|
287
|
+
return [4 /*yield*/, swapRepo.findWithRelations(relations, query)];
|
|
288
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
289
|
+
}
|
|
290
|
+
});
|
|
335
291
|
});
|
|
336
292
|
};
|
|
337
|
-
/**
|
|
338
|
-
* @typedef PreliminaryLineItem
|
|
339
|
-
* @property {string} variant_id - the id of the variant to create an item from
|
|
340
|
-
* @property {number} quantity - the amount of the variant to add to the line item
|
|
341
|
-
*/
|
|
342
293
|
/**
|
|
343
294
|
* Creates a swap from an order, with given return items, additional items
|
|
344
295
|
* and an optional return shipping method.
|
|
345
|
-
*
|
|
346
|
-
* @param
|
|
347
|
-
* @param
|
|
348
|
-
*
|
|
349
|
-
* @param
|
|
350
|
-
*
|
|
351
|
-
* @param {Object} custom - contains relevant custom information. This object may
|
|
296
|
+
*
|
|
297
|
+
* @param order - the order to base the swap off
|
|
298
|
+
* @param returnItems - the items to return in the swap
|
|
299
|
+
* @param additionalItems - the items to send to the customer
|
|
300
|
+
* @param returnShipping - an optional shipping method for returning the returnItems
|
|
301
|
+
* @param custom - contains relevant custom information. This object may
|
|
352
302
|
* include no_notification which will disable sending notification when creating
|
|
353
|
-
* swap. If set, it overrules the attribute inherited from the order
|
|
354
|
-
* @return
|
|
303
|
+
* swap. If set, it overrules the attribute inherited from the order
|
|
304
|
+
* @return the newly created swap
|
|
355
305
|
*/
|
|
356
306
|
SwapService.prototype.create = function (order, returnItems, additionalItems, returnShipping, custom) {
|
|
357
|
-
if (custom === void 0) { custom = {
|
|
358
|
-
no_notification: undefined,
|
|
359
|
-
}; }
|
|
307
|
+
if (custom === void 0) { custom = { no_notification: undefined }; }
|
|
360
308
|
return __awaiter(this, void 0, void 0, function () {
|
|
361
309
|
var no_notification, rest;
|
|
362
310
|
var _this = this;
|
|
363
311
|
return __generator(this, function (_a) {
|
|
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
|
-
|
|
312
|
+
switch (_a.label) {
|
|
313
|
+
case 0:
|
|
314
|
+
no_notification = custom.no_notification, rest = __rest(custom, ["no_notification"]);
|
|
315
|
+
return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
316
|
+
var lineItemServiceTx, returnItems_1, returnItems_1_1, item, line, e_1_1, newItems, evaluatedNoNotification, swapRepo, created, result;
|
|
317
|
+
var e_1, _a;
|
|
318
|
+
var _this = this;
|
|
319
|
+
var _b, _c, _d;
|
|
320
|
+
return __generator(this, function (_e) {
|
|
321
|
+
switch (_e.label) {
|
|
322
|
+
case 0:
|
|
323
|
+
if (order.fulfillment_status === "not_fulfilled" ||
|
|
324
|
+
order.payment_status !== "captured") {
|
|
325
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Order cannot be swapped");
|
|
326
|
+
}
|
|
327
|
+
lineItemServiceTx = this.lineItemService_.withTransaction(manager);
|
|
328
|
+
_e.label = 1;
|
|
329
|
+
case 1:
|
|
330
|
+
_e.trys.push([1, 6, 7, 8]);
|
|
331
|
+
returnItems_1 = __values(returnItems), returnItems_1_1 = returnItems_1.next();
|
|
332
|
+
_e.label = 2;
|
|
333
|
+
case 2:
|
|
334
|
+
if (!!returnItems_1_1.done) return [3 /*break*/, 5];
|
|
335
|
+
item = returnItems_1_1.value;
|
|
336
|
+
return [4 /*yield*/, lineItemServiceTx.retrieve(item.item_id, {
|
|
337
|
+
relations: ["order", "swap", "claim_order"],
|
|
338
|
+
})];
|
|
339
|
+
case 3:
|
|
340
|
+
line = _e.sent();
|
|
341
|
+
if (((_b = line.order) === null || _b === void 0 ? void 0 : _b.canceled_at) ||
|
|
342
|
+
((_c = line.swap) === null || _c === void 0 ? void 0 : _c.canceled_at) ||
|
|
343
|
+
((_d = line.claim_order) === null || _d === void 0 ? void 0 : _d.canceled_at)) {
|
|
344
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Cannot create a swap on a canceled item.");
|
|
345
|
+
}
|
|
346
|
+
_e.label = 4;
|
|
347
|
+
case 4:
|
|
348
|
+
returnItems_1_1 = returnItems_1.next();
|
|
349
|
+
return [3 /*break*/, 2];
|
|
350
|
+
case 5: return [3 /*break*/, 8];
|
|
351
|
+
case 6:
|
|
352
|
+
e_1_1 = _e.sent();
|
|
353
|
+
e_1 = { error: e_1_1 };
|
|
354
|
+
return [3 /*break*/, 8];
|
|
355
|
+
case 7:
|
|
356
|
+
try {
|
|
357
|
+
if (returnItems_1_1 && !returnItems_1_1.done && (_a = returnItems_1.return)) _a.call(returnItems_1);
|
|
358
|
+
}
|
|
359
|
+
finally { if (e_1) throw e_1.error; }
|
|
360
|
+
return [7 /*endfinally*/];
|
|
361
|
+
case 8:
|
|
362
|
+
newItems = [];
|
|
363
|
+
if (!additionalItems) return [3 /*break*/, 10];
|
|
364
|
+
return [4 /*yield*/, Promise.all(additionalItems.map(function (_a) {
|
|
365
|
+
var variant_id = _a.variant_id, quantity = _a.quantity;
|
|
366
|
+
return _this.lineItemService_
|
|
367
|
+
.withTransaction(manager)
|
|
368
|
+
.generate(variant_id, order.region_id, quantity);
|
|
369
|
+
}))];
|
|
370
|
+
case 9:
|
|
371
|
+
newItems = _e.sent();
|
|
372
|
+
_e.label = 10;
|
|
373
|
+
case 10:
|
|
374
|
+
evaluatedNoNotification = no_notification !== undefined ? no_notification : order.no_notification;
|
|
375
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
376
|
+
created = swapRepo.create(__assign(__assign({}, rest), { fulfillment_status: models_1.SwapFulfillmentStatus.NOT_FULFILLED, payment_status: models_1.SwapPaymentStatus.NOT_PAID, order_id: order.id, additional_items: newItems, no_notification: evaluatedNoNotification }));
|
|
377
|
+
return [4 /*yield*/, swapRepo.save(created)];
|
|
378
|
+
case 11:
|
|
379
|
+
result = _e.sent();
|
|
380
|
+
return [4 /*yield*/, this.returnService_.withTransaction(manager).create({
|
|
381
|
+
swap_id: result.id,
|
|
382
|
+
order_id: order.id,
|
|
383
|
+
items: returnItems,
|
|
384
|
+
shipping_method: returnShipping,
|
|
385
|
+
no_notification: evaluatedNoNotification,
|
|
386
|
+
})];
|
|
387
|
+
case 12:
|
|
388
|
+
_e.sent();
|
|
389
|
+
return [4 /*yield*/, this.eventBus_
|
|
390
|
+
.withTransaction(manager)
|
|
391
|
+
.emit(SwapService.Events.CREATED, {
|
|
392
|
+
id: result.id,
|
|
393
|
+
no_notification: evaluatedNoNotification,
|
|
394
|
+
})];
|
|
395
|
+
case 13:
|
|
396
|
+
_e.sent();
|
|
397
|
+
return [2 /*return*/, result];
|
|
407
398
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
return _this.lineItemService_.generate(variant_id, order.region_id, quantity);
|
|
413
|
-
}))];
|
|
414
|
-
case 9:
|
|
415
|
-
newItems = _e.sent();
|
|
416
|
-
evaluatedNoNotification = no_notification !== undefined ? no_notification : order.no_notification;
|
|
417
|
-
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
418
|
-
created = swapRepo.create(__assign(__assign({}, rest), { fulfillment_status: "not_fulfilled", payment_status: "not_paid", order_id: order.id, additional_items: newItems, no_notification: evaluatedNoNotification }));
|
|
419
|
-
return [4 /*yield*/, swapRepo.save(created)];
|
|
420
|
-
case 10:
|
|
421
|
-
result = _e.sent();
|
|
422
|
-
return [4 /*yield*/, this.returnService_.withTransaction(manager).create({
|
|
423
|
-
swap_id: result.id,
|
|
424
|
-
order_id: order.id,
|
|
425
|
-
items: returnItems,
|
|
426
|
-
shipping_method: returnShipping,
|
|
427
|
-
no_notification: evaluatedNoNotification,
|
|
428
|
-
})];
|
|
429
|
-
case 11:
|
|
430
|
-
_e.sent();
|
|
431
|
-
return [4 /*yield*/, this.eventBus_
|
|
432
|
-
.withTransaction(manager)
|
|
433
|
-
.emit(SwapService.Events.CREATED, {
|
|
434
|
-
id: result.id,
|
|
435
|
-
no_notification: evaluatedNoNotification,
|
|
436
|
-
})];
|
|
437
|
-
case 12:
|
|
438
|
-
_e.sent();
|
|
439
|
-
return [2 /*return*/, result];
|
|
440
|
-
}
|
|
441
|
-
});
|
|
442
|
-
}); })];
|
|
399
|
+
});
|
|
400
|
+
}); })];
|
|
401
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
402
|
+
}
|
|
443
403
|
});
|
|
444
404
|
});
|
|
445
405
|
};
|
|
406
|
+
/**
|
|
407
|
+
* Process difference for the requested swap.
|
|
408
|
+
*
|
|
409
|
+
* @param swapId id of a swap being processed
|
|
410
|
+
* @return processed swap
|
|
411
|
+
*/
|
|
446
412
|
SwapService.prototype.processDifference = function (swapId) {
|
|
447
413
|
return __awaiter(this, void 0, void 0, function () {
|
|
448
414
|
var _this = this;
|
|
449
415
|
return __generator(this, function (_a) {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
case 1:
|
|
458
|
-
swap = _a.sent();
|
|
459
|
-
if (swap.canceled_at) {
|
|
460
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be processed");
|
|
461
|
-
}
|
|
462
|
-
if (!swap.confirmed_at) {
|
|
463
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot process a swap that hasn't been confirmed by the customer");
|
|
464
|
-
}
|
|
465
|
-
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
466
|
-
if (!(swap.difference_due < 0)) return [3 /*break*/, 10];
|
|
467
|
-
if (swap.payment_status === "difference_refunded") {
|
|
468
|
-
return [2 /*return*/, swap];
|
|
469
|
-
}
|
|
470
|
-
_a.label = 2;
|
|
471
|
-
case 2:
|
|
472
|
-
_a.trys.push([2, 4, , 7]);
|
|
473
|
-
return [4 /*yield*/, this.paymentProviderService_
|
|
474
|
-
.withTransaction(manager)
|
|
475
|
-
.refundPayment(swap.order.payments, -1 * swap.difference_due, "swap")];
|
|
476
|
-
case 3:
|
|
477
|
-
_a.sent();
|
|
478
|
-
return [3 /*break*/, 7];
|
|
479
|
-
case 4:
|
|
480
|
-
err_1 = _a.sent();
|
|
481
|
-
swap.payment_status = "requires_action";
|
|
482
|
-
return [4 /*yield*/, swapRepo.save(swap)];
|
|
483
|
-
case 5:
|
|
484
|
-
result_1 = _a.sent();
|
|
485
|
-
return [4 /*yield*/, this.eventBus_
|
|
486
|
-
.withTransaction(manager)
|
|
487
|
-
.emit(SwapService.Events.PROCESS_REFUND_FAILED, {
|
|
488
|
-
id: result_1.id,
|
|
489
|
-
no_notification: swap.no_notification,
|
|
490
|
-
})];
|
|
491
|
-
case 6:
|
|
492
|
-
_a.sent();
|
|
493
|
-
return [2 /*return*/, result_1];
|
|
494
|
-
case 7:
|
|
495
|
-
swap.payment_status = "difference_refunded";
|
|
496
|
-
return [4 /*yield*/, swapRepo.save(swap)];
|
|
497
|
-
case 8:
|
|
498
|
-
result_2 = _a.sent();
|
|
499
|
-
return [4 /*yield*/, this.eventBus_
|
|
500
|
-
.withTransaction(manager)
|
|
501
|
-
.emit(SwapService.Events.REFUND_PROCESSED, {
|
|
502
|
-
id: result_2.id,
|
|
503
|
-
no_notification: swap.no_notification,
|
|
504
|
-
})];
|
|
505
|
-
case 9:
|
|
506
|
-
_a.sent();
|
|
507
|
-
return [2 /*return*/, result_2];
|
|
508
|
-
case 10:
|
|
509
|
-
if (!(swap.difference_due === 0)) return [3 /*break*/, 13];
|
|
510
|
-
if (swap.payment_status === "difference_refunded") {
|
|
511
|
-
return [2 /*return*/, swap];
|
|
512
|
-
}
|
|
513
|
-
swap.payment_status = "difference_refunded";
|
|
514
|
-
return [4 /*yield*/, swapRepo.save(swap)];
|
|
515
|
-
case 11:
|
|
516
|
-
result_3 = _a.sent();
|
|
517
|
-
return [4 /*yield*/, this.eventBus_
|
|
518
|
-
.withTransaction(manager)
|
|
519
|
-
.emit(SwapService.Events.REFUND_PROCESSED, {
|
|
520
|
-
id: result_3.id,
|
|
521
|
-
no_notification: swap.no_notification,
|
|
522
|
-
})];
|
|
523
|
-
case 12:
|
|
524
|
-
_a.sent();
|
|
525
|
-
return [2 /*return*/, result_3];
|
|
526
|
-
case 13:
|
|
527
|
-
_a.trys.push([13, 15, , 18]);
|
|
528
|
-
if (swap.payment_status === "captured") {
|
|
529
|
-
return [2 /*return*/, swap];
|
|
530
|
-
}
|
|
531
|
-
return [4 /*yield*/, this.paymentProviderService_
|
|
532
|
-
.withTransaction(manager)
|
|
533
|
-
.capturePayment(swap.payment)];
|
|
534
|
-
case 14:
|
|
535
|
-
_a.sent();
|
|
536
|
-
return [3 /*break*/, 18];
|
|
537
|
-
case 15:
|
|
538
|
-
err_2 = _a.sent();
|
|
539
|
-
swap.payment_status = "requires_action";
|
|
540
|
-
return [4 /*yield*/, swapRepo.save(swap)];
|
|
541
|
-
case 16:
|
|
542
|
-
result_4 = _a.sent();
|
|
543
|
-
return [4 /*yield*/, this.eventBus_
|
|
544
|
-
.withTransaction(manager)
|
|
545
|
-
.emit(SwapService.Events.PAYMENT_CAPTURE_FAILED, {
|
|
546
|
-
id: swap.id,
|
|
547
|
-
no_notification: swap.no_notification,
|
|
548
|
-
})];
|
|
549
|
-
case 17:
|
|
550
|
-
_a.sent();
|
|
551
|
-
return [2 /*return*/, result_4];
|
|
552
|
-
case 18:
|
|
553
|
-
swap.payment_status = "captured";
|
|
554
|
-
return [4 /*yield*/, swapRepo.save(swap)];
|
|
555
|
-
case 19:
|
|
556
|
-
result = _a.sent();
|
|
557
|
-
return [4 /*yield*/, this.eventBus_
|
|
558
|
-
.withTransaction(manager)
|
|
559
|
-
.emit(SwapService.Events.PAYMENT_CAPTURED, {
|
|
560
|
-
id: result.id,
|
|
561
|
-
no_notification: swap.no_notification,
|
|
416
|
+
switch (_a.label) {
|
|
417
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
418
|
+
var swap, swapRepo, err_1, result_1, result_2, result_3, err_2, result_4, result;
|
|
419
|
+
return __generator(this, function (_a) {
|
|
420
|
+
switch (_a.label) {
|
|
421
|
+
case 0: return [4 /*yield*/, this.retrieve(swapId, {
|
|
422
|
+
relations: ["payment", "order", "order.payments"],
|
|
562
423
|
})];
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
424
|
+
case 1:
|
|
425
|
+
swap = _a.sent();
|
|
426
|
+
if (swap.canceled_at) {
|
|
427
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be processed");
|
|
428
|
+
}
|
|
429
|
+
if (!swap.confirmed_at) {
|
|
430
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot process a swap that hasn't been confirmed by the customer");
|
|
431
|
+
}
|
|
432
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
433
|
+
if (!(swap.difference_due < 0)) return [3 /*break*/, 10];
|
|
434
|
+
if (swap.payment_status === "difference_refunded") {
|
|
435
|
+
return [2 /*return*/, swap];
|
|
436
|
+
}
|
|
437
|
+
_a.label = 2;
|
|
438
|
+
case 2:
|
|
439
|
+
_a.trys.push([2, 4, , 7]);
|
|
440
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
441
|
+
.withTransaction(manager)
|
|
442
|
+
.refundPayment(swap.order.payments, -1 * swap.difference_due, "swap")];
|
|
443
|
+
case 3:
|
|
444
|
+
_a.sent();
|
|
445
|
+
return [3 /*break*/, 7];
|
|
446
|
+
case 4:
|
|
447
|
+
err_1 = _a.sent();
|
|
448
|
+
swap.payment_status = models_1.SwapPaymentStatus.REQUIRES_ACTION;
|
|
449
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
450
|
+
case 5:
|
|
451
|
+
result_1 = _a.sent();
|
|
452
|
+
return [4 /*yield*/, this.eventBus_
|
|
453
|
+
.withTransaction(manager)
|
|
454
|
+
.emit(SwapService.Events.PROCESS_REFUND_FAILED, {
|
|
455
|
+
id: result_1.id,
|
|
456
|
+
no_notification: swap.no_notification,
|
|
457
|
+
})];
|
|
458
|
+
case 6:
|
|
459
|
+
_a.sent();
|
|
460
|
+
return [2 /*return*/, result_1];
|
|
461
|
+
case 7:
|
|
462
|
+
swap.payment_status = models_1.SwapPaymentStatus.DIFFERENCE_REFUNDED;
|
|
463
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
464
|
+
case 8:
|
|
465
|
+
result_2 = _a.sent();
|
|
466
|
+
return [4 /*yield*/, this.eventBus_
|
|
467
|
+
.withTransaction(manager)
|
|
468
|
+
.emit(SwapService.Events.REFUND_PROCESSED, {
|
|
469
|
+
id: result_2.id,
|
|
470
|
+
no_notification: swap.no_notification,
|
|
471
|
+
})];
|
|
472
|
+
case 9:
|
|
473
|
+
_a.sent();
|
|
474
|
+
return [2 /*return*/, result_2];
|
|
475
|
+
case 10:
|
|
476
|
+
if (!(swap.difference_due === 0)) return [3 /*break*/, 13];
|
|
477
|
+
if (swap.payment_status === models_1.SwapPaymentStatus.DIFFERENCE_REFUNDED) {
|
|
478
|
+
return [2 /*return*/, swap];
|
|
479
|
+
}
|
|
480
|
+
swap.payment_status = models_1.SwapPaymentStatus.DIFFERENCE_REFUNDED;
|
|
481
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
482
|
+
case 11:
|
|
483
|
+
result_3 = _a.sent();
|
|
484
|
+
return [4 /*yield*/, this.eventBus_
|
|
485
|
+
.withTransaction(manager)
|
|
486
|
+
.emit(SwapService.Events.REFUND_PROCESSED, {
|
|
487
|
+
id: result_3.id,
|
|
488
|
+
no_notification: swap.no_notification,
|
|
489
|
+
})];
|
|
490
|
+
case 12:
|
|
491
|
+
_a.sent();
|
|
492
|
+
return [2 /*return*/, result_3];
|
|
493
|
+
case 13:
|
|
494
|
+
_a.trys.push([13, 15, , 18]);
|
|
495
|
+
if (swap.payment_status === models_1.SwapPaymentStatus.CAPTURED) {
|
|
496
|
+
return [2 /*return*/, swap];
|
|
497
|
+
}
|
|
498
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
499
|
+
.withTransaction(manager)
|
|
500
|
+
.capturePayment(swap.payment)];
|
|
501
|
+
case 14:
|
|
502
|
+
_a.sent();
|
|
503
|
+
return [3 /*break*/, 18];
|
|
504
|
+
case 15:
|
|
505
|
+
err_2 = _a.sent();
|
|
506
|
+
swap.payment_status = models_1.SwapPaymentStatus.REQUIRES_ACTION;
|
|
507
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
508
|
+
case 16:
|
|
509
|
+
result_4 = _a.sent();
|
|
510
|
+
return [4 /*yield*/, this.eventBus_
|
|
511
|
+
.withTransaction(manager)
|
|
512
|
+
.emit(SwapService.Events.PAYMENT_CAPTURE_FAILED, {
|
|
513
|
+
id: swap.id,
|
|
514
|
+
no_notification: swap.no_notification,
|
|
515
|
+
})];
|
|
516
|
+
case 17:
|
|
517
|
+
_a.sent();
|
|
518
|
+
return [2 /*return*/, result_4];
|
|
519
|
+
case 18:
|
|
520
|
+
swap.payment_status = models_1.SwapPaymentStatus.CAPTURED;
|
|
521
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
522
|
+
case 19:
|
|
523
|
+
result = _a.sent();
|
|
524
|
+
return [4 /*yield*/, this.eventBus_
|
|
525
|
+
.withTransaction(manager)
|
|
526
|
+
.emit(SwapService.Events.PAYMENT_CAPTURED, {
|
|
527
|
+
id: result.id,
|
|
528
|
+
no_notification: swap.no_notification,
|
|
529
|
+
})];
|
|
530
|
+
case 20:
|
|
531
|
+
_a.sent();
|
|
532
|
+
return [2 /*return*/, result];
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
}); })];
|
|
536
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
537
|
+
}
|
|
569
538
|
});
|
|
570
539
|
});
|
|
571
540
|
};
|
|
541
|
+
/**
|
|
542
|
+
* Update the swap record.
|
|
543
|
+
*
|
|
544
|
+
* @param swapId id of a swap to update
|
|
545
|
+
* @param update new data
|
|
546
|
+
* @return updated swap record
|
|
547
|
+
*/
|
|
572
548
|
SwapService.prototype.update = function (swapId, update) {
|
|
573
549
|
return __awaiter(this, void 0, void 0, function () {
|
|
574
550
|
var _this = this;
|
|
575
551
|
return __generator(this, function (_a) {
|
|
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
|
-
|
|
552
|
+
switch (_a.label) {
|
|
553
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
554
|
+
var swap, swapRepo;
|
|
555
|
+
return __generator(this, function (_a) {
|
|
556
|
+
switch (_a.label) {
|
|
557
|
+
case 0: return [4 /*yield*/, this.retrieve(swapId)];
|
|
558
|
+
case 1:
|
|
559
|
+
swap = _a.sent();
|
|
560
|
+
if ("metadata" in update) {
|
|
561
|
+
swap.metadata = (0, utils_1.setMetadata)(swap, update.metadata);
|
|
562
|
+
}
|
|
563
|
+
if ("no_notification" in update) {
|
|
564
|
+
swap.no_notification = update.no_notification;
|
|
565
|
+
}
|
|
566
|
+
if ("shipping_address" in update) {
|
|
567
|
+
// TODO: Check this - calling method that doesn't exist
|
|
568
|
+
// also it seems that update swap isn't call anywhere
|
|
569
|
+
// await this.updateShippingAddress_(swap, update.shipping_address)
|
|
570
|
+
}
|
|
571
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
572
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
573
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}); })];
|
|
577
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
578
|
+
}
|
|
603
579
|
});
|
|
604
580
|
});
|
|
605
581
|
};
|
|
606
582
|
/**
|
|
607
|
-
* Creates a cart from the given swap
|
|
583
|
+
* Creates a cart from the given swap. The cart can be used to pay
|
|
608
584
|
* for differences associated with the swap. The swap represented by the
|
|
609
585
|
* swapId must belong to the order. Fails if there is already a cart on the
|
|
610
586
|
* swap.
|
|
611
|
-
*
|
|
612
|
-
* @param
|
|
613
|
-
* @param
|
|
614
|
-
* @return
|
|
615
|
-
* the new cart.
|
|
587
|
+
*
|
|
588
|
+
* @param swapId - the id of the swap to create the cart from
|
|
589
|
+
* @param customShippingOptions - the shipping options
|
|
590
|
+
* @return the swap with its cart_id prop set to the id of the new cart.
|
|
616
591
|
*/
|
|
617
592
|
SwapService.prototype.createCart = function (swapId, customShippingOptions) {
|
|
618
593
|
if (customShippingOptions === void 0) { customShippingOptions = []; }
|
|
619
594
|
return __awaiter(this, void 0, void 0, function () {
|
|
620
595
|
var _this = this;
|
|
621
596
|
return __generator(this, function (_a) {
|
|
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
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
597
|
+
switch (_a.label) {
|
|
598
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
599
|
+
var swapRepo, swap, order, discounts, cart, customShippingOptionServiceTx, customShippingOptions_1, customShippingOptions_1_1, customShippingOption, e_2_1, lineItemServiceTx, lineItemAdjustmentServiceTx, _a, _b, item, e_3_1;
|
|
600
|
+
var e_2, _c, e_3, _d;
|
|
601
|
+
var _e;
|
|
602
|
+
return __generator(this, function (_f) {
|
|
603
|
+
switch (_f.label) {
|
|
604
|
+
case 0:
|
|
605
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
606
|
+
return [4 /*yield*/, this.retrieve(swapId, {
|
|
607
|
+
relations: [
|
|
608
|
+
"order",
|
|
609
|
+
"order.items",
|
|
610
|
+
"order.swaps",
|
|
611
|
+
"order.swaps.additional_items",
|
|
612
|
+
"order.discounts",
|
|
613
|
+
"order.discounts.rule",
|
|
614
|
+
"order.claims",
|
|
615
|
+
"order.claims.additional_items",
|
|
616
|
+
"additional_items",
|
|
617
|
+
"additional_items.variant",
|
|
618
|
+
"return_order",
|
|
619
|
+
"return_order.items",
|
|
620
|
+
"return_order.shipping_method",
|
|
621
|
+
"return_order.shipping_method.tax_lines",
|
|
622
|
+
],
|
|
623
|
+
})];
|
|
624
|
+
case 1:
|
|
625
|
+
swap = _f.sent();
|
|
626
|
+
if (swap.canceled_at) {
|
|
627
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be used to create a cart");
|
|
628
|
+
}
|
|
629
|
+
if (swap.cart_id) {
|
|
630
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DUPLICATE_ERROR, "A cart has already been created for the swap");
|
|
631
|
+
}
|
|
632
|
+
order = swap.order;
|
|
633
|
+
discounts = ((_e = order === null || order === void 0 ? void 0 : order.discounts) === null || _e === void 0 ? void 0 : _e.filter(function (_a) {
|
|
634
|
+
var rule = _a.rule;
|
|
635
|
+
return rule.type !== "free_shipping";
|
|
636
|
+
})) ||
|
|
637
|
+
undefined;
|
|
638
|
+
return [4 /*yield*/, this.cartService_.withTransaction(manager).create({
|
|
639
|
+
discounts: discounts,
|
|
640
|
+
email: order.email,
|
|
641
|
+
billing_address_id: order.billing_address_id,
|
|
642
|
+
shipping_address_id: order.shipping_address_id,
|
|
643
|
+
region_id: order.region_id,
|
|
644
|
+
customer_id: order.customer_id,
|
|
645
|
+
type: models_1.CartType.SWAP,
|
|
646
|
+
metadata: {
|
|
647
|
+
swap_id: swap.id,
|
|
648
|
+
parent_order_id: order.id,
|
|
649
|
+
},
|
|
650
|
+
})];
|
|
651
|
+
case 2:
|
|
652
|
+
cart = _f.sent();
|
|
653
|
+
customShippingOptionServiceTx = this.customShippingOptionService_.withTransaction(manager);
|
|
654
|
+
_f.label = 3;
|
|
655
|
+
case 3:
|
|
656
|
+
_f.trys.push([3, 8, 9, 10]);
|
|
657
|
+
customShippingOptions_1 = __values(customShippingOptions), customShippingOptions_1_1 = customShippingOptions_1.next();
|
|
658
|
+
_f.label = 4;
|
|
659
|
+
case 4:
|
|
660
|
+
if (!!customShippingOptions_1_1.done) return [3 /*break*/, 7];
|
|
661
|
+
customShippingOption = customShippingOptions_1_1.value;
|
|
662
|
+
return [4 /*yield*/, customShippingOptionServiceTx.create({
|
|
663
|
+
cart_id: cart.id,
|
|
664
|
+
shipping_option_id: customShippingOption.option_id,
|
|
665
|
+
price: customShippingOption.price,
|
|
666
|
+
})];
|
|
667
|
+
case 5:
|
|
668
|
+
_f.sent();
|
|
669
|
+
_f.label = 6;
|
|
670
|
+
case 6:
|
|
671
|
+
customShippingOptions_1_1 = customShippingOptions_1.next();
|
|
672
|
+
return [3 /*break*/, 4];
|
|
673
|
+
case 7: return [3 /*break*/, 10];
|
|
674
|
+
case 8:
|
|
675
|
+
e_2_1 = _f.sent();
|
|
676
|
+
e_2 = { error: e_2_1 };
|
|
677
|
+
return [3 /*break*/, 10];
|
|
678
|
+
case 9:
|
|
679
|
+
try {
|
|
680
|
+
if (customShippingOptions_1_1 && !customShippingOptions_1_1.done && (_c = customShippingOptions_1.return)) _c.call(customShippingOptions_1);
|
|
681
|
+
}
|
|
682
|
+
finally { if (e_2) throw e_2.error; }
|
|
683
|
+
return [7 /*endfinally*/];
|
|
684
|
+
case 10:
|
|
685
|
+
lineItemServiceTx = this.lineItemService_.withTransaction(manager);
|
|
686
|
+
lineItemAdjustmentServiceTx = this.lineItemAdjustmentService_.withTransaction(manager);
|
|
687
|
+
_f.label = 11;
|
|
688
|
+
case 11:
|
|
689
|
+
_f.trys.push([11, 17, 18, 19]);
|
|
690
|
+
_a = __values(swap.additional_items), _b = _a.next();
|
|
691
|
+
_f.label = 12;
|
|
692
|
+
case 12:
|
|
693
|
+
if (!!_b.done) return [3 /*break*/, 16];
|
|
694
|
+
item = _b.value;
|
|
695
|
+
return [4 /*yield*/, lineItemServiceTx.update(item.id, {
|
|
696
|
+
cart_id: cart.id,
|
|
697
|
+
})
|
|
698
|
+
// we generate adjustments in case the cart has any discounts that should be applied to the additional items
|
|
699
|
+
];
|
|
700
|
+
case 13:
|
|
701
|
+
_f.sent();
|
|
720
702
|
// we generate adjustments in case the cart has any discounts that should be applied to the additional items
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
.
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
case 23: return [2 /*return*/, _f.sent()];
|
|
776
|
-
}
|
|
777
|
-
});
|
|
778
|
-
}); })];
|
|
703
|
+
return [4 /*yield*/, lineItemAdjustmentServiceTx.createAdjustmentForLineItem(cart, item)];
|
|
704
|
+
case 14:
|
|
705
|
+
// we generate adjustments in case the cart has any discounts that should be applied to the additional items
|
|
706
|
+
_f.sent();
|
|
707
|
+
_f.label = 15;
|
|
708
|
+
case 15:
|
|
709
|
+
_b = _a.next();
|
|
710
|
+
return [3 /*break*/, 12];
|
|
711
|
+
case 16: return [3 /*break*/, 19];
|
|
712
|
+
case 17:
|
|
713
|
+
e_3_1 = _f.sent();
|
|
714
|
+
e_3 = { error: e_3_1 };
|
|
715
|
+
return [3 /*break*/, 19];
|
|
716
|
+
case 18:
|
|
717
|
+
try {
|
|
718
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
719
|
+
}
|
|
720
|
+
finally { if (e_3) throw e_3.error; }
|
|
721
|
+
return [7 /*endfinally*/];
|
|
722
|
+
case 19:
|
|
723
|
+
if (!(swap.return_order && swap.return_order.shipping_method)) return [3 /*break*/, 21];
|
|
724
|
+
return [4 /*yield*/, this.lineItemService_.withTransaction(manager).create({
|
|
725
|
+
cart_id: cart.id,
|
|
726
|
+
title: "Return shipping",
|
|
727
|
+
quantity: 1,
|
|
728
|
+
has_shipping: true,
|
|
729
|
+
allow_discounts: false,
|
|
730
|
+
unit_price: swap.return_order.shipping_method.price,
|
|
731
|
+
is_return: true,
|
|
732
|
+
tax_lines: swap.return_order.shipping_method.tax_lines.map(function (tl) {
|
|
733
|
+
return lineItemServiceTx.createTaxLine({
|
|
734
|
+
name: tl.name,
|
|
735
|
+
code: tl.code,
|
|
736
|
+
rate: tl.rate,
|
|
737
|
+
metadata: tl.metadata,
|
|
738
|
+
});
|
|
739
|
+
}),
|
|
740
|
+
})];
|
|
741
|
+
case 20:
|
|
742
|
+
_f.sent();
|
|
743
|
+
_f.label = 21;
|
|
744
|
+
case 21: return [4 /*yield*/, this.lineItemService_
|
|
745
|
+
.withTransaction(manager)
|
|
746
|
+
.createReturnLines(swap.return_order.id, cart.id)];
|
|
747
|
+
case 22:
|
|
748
|
+
_f.sent();
|
|
749
|
+
swap.cart_id = cart.id;
|
|
750
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
751
|
+
case 23: return [2 /*return*/, _f.sent()];
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
}); })];
|
|
755
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
756
|
+
}
|
|
779
757
|
});
|
|
780
758
|
});
|
|
781
759
|
};
|
|
782
760
|
/**
|
|
783
|
-
|
|
761
|
+
* Register a cart completion
|
|
762
|
+
*
|
|
763
|
+
* @param swapId - The id of the swap
|
|
764
|
+
* @return swap related to the cart
|
|
784
765
|
*/
|
|
785
766
|
SwapService.prototype.registerCartCompletion = function (swapId) {
|
|
786
767
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -788,7 +769,7 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
788
769
|
return __generator(this, function (_a) {
|
|
789
770
|
switch (_a.label) {
|
|
790
771
|
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
791
|
-
var swap, cart, payment, items, inventoryServiceTx, paymentProviderServiceTx, cartServiceTx, items_1, items_1_1, item, err_3, e_4_1, total, paymentStatus, inventoryServiceTx, items_2, items_2_1, item, e_5_1,
|
|
772
|
+
var swap, cart, payment, items, inventoryServiceTx, paymentProviderServiceTx, cartServiceTx, items_1, items_1_1, item, err_3, e_4_1, total, paymentStatus, inventoryServiceTx, items_2, items_2_1, item, e_5_1, swapRepo, result, shippingOptionServiceTx, _a, _b, method, e_6_1;
|
|
792
773
|
var e_4, _c, e_5, _d, e_6, _e;
|
|
793
774
|
return __generator(this, function (_f) {
|
|
794
775
|
switch (_f.label) {
|
|
@@ -814,7 +795,9 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
814
795
|
if (swap.canceled_at) {
|
|
815
796
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cart related to canceled swap cannot be completed");
|
|
816
797
|
}
|
|
817
|
-
return [4 /*yield*/, this.cartService_
|
|
798
|
+
return [4 /*yield*/, this.cartService_
|
|
799
|
+
.withTransaction(manager)
|
|
800
|
+
.retrieve(swap.cart_id, {
|
|
818
801
|
select: ["total"],
|
|
819
802
|
relations: [
|
|
820
803
|
"payment",
|
|
@@ -885,7 +868,9 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
885
868
|
case 16:
|
|
886
869
|
paymentStatus = _f.sent();
|
|
887
870
|
// If payment status is not authorized, we throw
|
|
888
|
-
if (paymentStatus !==
|
|
871
|
+
if (paymentStatus !== models_1.PaymentSessionStatus.AUTHORIZED &&
|
|
872
|
+
// @ts-ignore TODO: check why this is not in the enum
|
|
873
|
+
paymentStatus !== "succeeded") {
|
|
889
874
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Payment method is not authorized");
|
|
890
875
|
}
|
|
891
876
|
return [4 /*yield*/, this.paymentProviderService_
|
|
@@ -924,12 +909,12 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
924
909
|
finally { if (e_5) throw e_5.error; }
|
|
925
910
|
return [7 /*endfinally*/];
|
|
926
911
|
case 25:
|
|
927
|
-
now = new Date();
|
|
928
912
|
swap.difference_due = total;
|
|
929
913
|
swap.shipping_address_id = cart.shipping_address_id;
|
|
930
914
|
swap.shipping_methods = cart.shipping_methods;
|
|
931
|
-
swap.confirmed_at =
|
|
932
|
-
swap.payment_status =
|
|
915
|
+
swap.confirmed_at = new Date();
|
|
916
|
+
swap.payment_status =
|
|
917
|
+
total === 0 ? models_1.SwapPaymentStatus.CONFIRMED : models_1.SwapPaymentStatus.AWAITING;
|
|
933
918
|
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
934
919
|
return [4 /*yield*/, swapRepo.save(swap)];
|
|
935
920
|
case 26:
|
|
@@ -963,17 +948,18 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
963
948
|
}
|
|
964
949
|
finally { if (e_6) throw e_6.error; }
|
|
965
950
|
return [7 /*endfinally*/];
|
|
966
|
-
case 34:
|
|
967
|
-
this.eventBus_
|
|
951
|
+
case 34: return [4 /*yield*/, this.eventBus_
|
|
968
952
|
.withTransaction(manager)
|
|
969
953
|
.emit(SwapService.Events.PAYMENT_COMPLETED, {
|
|
970
954
|
id: swap.id,
|
|
971
955
|
no_notification: swap.no_notification,
|
|
972
|
-
});
|
|
956
|
+
})];
|
|
957
|
+
case 35:
|
|
958
|
+
_f.sent();
|
|
973
959
|
return [4 /*yield*/, this.cartService_
|
|
974
960
|
.withTransaction(manager)
|
|
975
961
|
.update(cart.id, { completed_at: new Date() })];
|
|
976
|
-
case
|
|
962
|
+
case 36:
|
|
977
963
|
_f.sent();
|
|
978
964
|
return [2 /*return*/, result];
|
|
979
965
|
}
|
|
@@ -988,73 +974,77 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
988
974
|
* Cancels a given swap if possible. A swap can only be canceled if all
|
|
989
975
|
* related returns, fulfillments, and payments have been canceled. If a swap
|
|
990
976
|
* is associated with a refund, it cannot be canceled.
|
|
991
|
-
*
|
|
992
|
-
* @
|
|
977
|
+
*
|
|
978
|
+
* @param swapId - the id of the swap to cancel.
|
|
979
|
+
* @return the canceled swap.
|
|
993
980
|
*/
|
|
994
981
|
SwapService.prototype.cancel = function (swapId) {
|
|
995
982
|
return __awaiter(this, void 0, void 0, function () {
|
|
996
983
|
var _this = this;
|
|
997
984
|
return __generator(this, function (_a) {
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
swap
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
for (_a = __values(swap.fulfillments), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1016
|
-
f = _b.value;
|
|
1017
|
-
if (!f.canceled_at) {
|
|
1018
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "All fulfillments must be canceled before the swap can be canceled");
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
985
|
+
switch (_a.label) {
|
|
986
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
987
|
+
var swapRepo, swap, _a, _b, f;
|
|
988
|
+
var e_7, _c;
|
|
989
|
+
return __generator(this, function (_d) {
|
|
990
|
+
switch (_d.label) {
|
|
991
|
+
case 0:
|
|
992
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
993
|
+
return [4 /*yield*/, this.retrieve(swapId, {
|
|
994
|
+
relations: ["payment", "fulfillments", "return_order"],
|
|
995
|
+
})];
|
|
996
|
+
case 1:
|
|
997
|
+
swap = _d.sent();
|
|
998
|
+
if (swap.payment_status === models_1.SwapPaymentStatus.DIFFERENCE_REFUNDED ||
|
|
999
|
+
swap.payment_status === models_1.SwapPaymentStatus.PARTIALLY_REFUNDED ||
|
|
1000
|
+
swap.payment_status === models_1.SwapPaymentStatus.REFUNDED) {
|
|
1001
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Swap with a refund cannot be canceled");
|
|
1021
1002
|
}
|
|
1022
|
-
|
|
1023
|
-
finally {
|
|
1003
|
+
if (swap.fulfillments) {
|
|
1024
1004
|
try {
|
|
1025
|
-
|
|
1005
|
+
for (_a = __values(swap.fulfillments), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
1006
|
+
f = _b.value;
|
|
1007
|
+
if (!f.canceled_at) {
|
|
1008
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "All fulfillments must be canceled before the swap can be canceled");
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
1013
|
+
finally {
|
|
1014
|
+
try {
|
|
1015
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1016
|
+
}
|
|
1017
|
+
finally { if (e_7) throw e_7.error; }
|
|
1026
1018
|
}
|
|
1027
|
-
finally { if (e_7) throw e_7.error; }
|
|
1028
1019
|
}
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
});
|
|
1051
|
-
}); })];
|
|
1020
|
+
if (swap.return_order &&
|
|
1021
|
+
swap.return_order.status !== models_1.ReturnStatus.CANCELED) {
|
|
1022
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Return must be canceled before the swap can be canceled");
|
|
1023
|
+
}
|
|
1024
|
+
swap.payment_status = models_1.SwapPaymentStatus.CANCELED;
|
|
1025
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.CANCELED;
|
|
1026
|
+
swap.canceled_at = new Date();
|
|
1027
|
+
if (!swap.payment) return [3 /*break*/, 3];
|
|
1028
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1029
|
+
.withTransaction(manager)
|
|
1030
|
+
.cancelPayment(swap.payment)];
|
|
1031
|
+
case 2:
|
|
1032
|
+
_d.sent();
|
|
1033
|
+
_d.label = 3;
|
|
1034
|
+
case 3: return [4 /*yield*/, swapRepo.save(swap)];
|
|
1035
|
+
case 4: return [2 /*return*/, _d.sent()];
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
}); })];
|
|
1039
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1040
|
+
}
|
|
1052
1041
|
});
|
|
1053
1042
|
});
|
|
1054
1043
|
};
|
|
1055
1044
|
/**
|
|
1056
|
-
* Fulfills the
|
|
1045
|
+
* Fulfills the additional items associated with the swap. Will call the
|
|
1057
1046
|
* fulfillment providers associated with the shipping methods.
|
|
1047
|
+
*
|
|
1058
1048
|
* @param {string} swapId - the id of the swap to fulfill,
|
|
1059
1049
|
* @param {object} config - optional configurations, includes optional metadata to attach to the shipment, and a no_notification flag.
|
|
1060
1050
|
* @return {Promise<Swap>} the updated swap with new status and fulfillments.
|
|
@@ -1065,189 +1055,193 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
1065
1055
|
no_notification: undefined,
|
|
1066
1056
|
}; }
|
|
1067
1057
|
return __awaiter(this, void 0, void 0, function () {
|
|
1068
|
-
var metadata, no_notification;
|
|
1069
1058
|
var _this = this;
|
|
1070
1059
|
return __generator(this, function (_a) {
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The swap was already fulfilled");
|
|
1103
|
-
}
|
|
1104
|
-
if (!((_h = swap.shipping_methods) === null || _h === void 0 ? void 0 : _h.length)) {
|
|
1105
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot fulfill an swap that doesn't have shipping methods");
|
|
1106
|
-
}
|
|
1107
|
-
evaluatedNoNotification = no_notification !== undefined ? no_notification : swap.no_notification;
|
|
1108
|
-
_a = swap;
|
|
1109
|
-
return [4 /*yield*/, this.fulfillmentService_
|
|
1110
|
-
.withTransaction(manager)
|
|
1111
|
-
.createFulfillment(__assign(__assign({}, swap), { payments: swap.payment ? [swap.payment] : order.payments, email: order.email, discounts: order.discounts, currency_code: order.currency_code, tax_rate: order.tax_rate, region_id: order.region_id, region: order.region, display_id: order.display_id, billing_address: order.billing_address, items: swap.additional_items, shipping_methods: swap.shipping_methods, is_swap: true, no_notification: evaluatedNoNotification }), swap.additional_items.map(function (i) { return ({
|
|
1112
|
-
item_id: i.id,
|
|
1113
|
-
quantity: i.quantity,
|
|
1114
|
-
}); }), { swap_id: swapId, metadata: metadata })];
|
|
1115
|
-
case 2:
|
|
1116
|
-
_a.fulfillments = _j.sent();
|
|
1117
|
-
successfullyFulfilled = [];
|
|
1118
|
-
try {
|
|
1119
|
-
for (_b = __values(swap.fulfillments), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1120
|
-
f = _c.value;
|
|
1121
|
-
successfullyFulfilled = successfullyFulfilled.concat(f.items);
|
|
1060
|
+
switch (_a.label) {
|
|
1061
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1062
|
+
var metadata, no_notification, swapRepo, swap, order, evaluatedNoNotification, _a, successfullyFulfilled, _b, _c, f, lineItemServiceTx, _loop_1, _d, _e, item, e_8_1, result;
|
|
1063
|
+
var e_9, _f, e_8, _g;
|
|
1064
|
+
var _h;
|
|
1065
|
+
return __generator(this, function (_j) {
|
|
1066
|
+
switch (_j.label) {
|
|
1067
|
+
case 0:
|
|
1068
|
+
metadata = config.metadata, no_notification = config.no_notification;
|
|
1069
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
1070
|
+
return [4 /*yield*/, this.retrieve(swapId, {
|
|
1071
|
+
relations: [
|
|
1072
|
+
"payment",
|
|
1073
|
+
"shipping_address",
|
|
1074
|
+
"additional_items",
|
|
1075
|
+
"additional_items.tax_lines",
|
|
1076
|
+
"shipping_methods",
|
|
1077
|
+
"shipping_methods.tax_lines",
|
|
1078
|
+
"order",
|
|
1079
|
+
"order.region",
|
|
1080
|
+
"order.billing_address",
|
|
1081
|
+
"order.discounts",
|
|
1082
|
+
"order.discounts.rule",
|
|
1083
|
+
"order.payments",
|
|
1084
|
+
],
|
|
1085
|
+
})];
|
|
1086
|
+
case 1:
|
|
1087
|
+
swap = _j.sent();
|
|
1088
|
+
order = swap.order;
|
|
1089
|
+
if (swap.canceled_at) {
|
|
1090
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be fulfilled");
|
|
1122
1091
|
}
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1092
|
+
if (swap.fulfillment_status !== "not_fulfilled" &&
|
|
1093
|
+
swap.fulfillment_status !== "canceled") {
|
|
1094
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The swap was already fulfilled");
|
|
1095
|
+
}
|
|
1096
|
+
if (!((_h = swap.shipping_methods) === null || _h === void 0 ? void 0 : _h.length)) {
|
|
1097
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot fulfill an swap that doesn't have shipping methods");
|
|
1098
|
+
}
|
|
1099
|
+
evaluatedNoNotification = no_notification !== undefined ? no_notification : swap.no_notification;
|
|
1100
|
+
_a = swap;
|
|
1101
|
+
return [4 /*yield*/, this.fulfillmentService_
|
|
1102
|
+
.withTransaction(manager)
|
|
1103
|
+
.createFulfillment(__assign(__assign({}, swap), { payments: swap.payment ? [swap.payment] : order.payments, email: order.email, discounts: order.discounts, currency_code: order.currency_code, tax_rate: order.tax_rate, region_id: order.region_id, region: order.region, display_id: order.display_id, billing_address: order.billing_address, items: swap.additional_items, shipping_methods: swap.shipping_methods, is_swap: true, no_notification: evaluatedNoNotification }), swap.additional_items.map(function (i) { return ({
|
|
1104
|
+
item_id: i.id,
|
|
1105
|
+
quantity: i.quantity,
|
|
1106
|
+
}); }), { swap_id: swapId, metadata: metadata })];
|
|
1107
|
+
case 2:
|
|
1108
|
+
_a.fulfillments = _j.sent();
|
|
1109
|
+
successfullyFulfilled = [];
|
|
1126
1110
|
try {
|
|
1127
|
-
|
|
1111
|
+
for (_b = __values(swap.fulfillments), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1112
|
+
f = _c.value;
|
|
1113
|
+
successfullyFulfilled = successfullyFulfilled.concat(f.items);
|
|
1114
|
+
}
|
|
1128
1115
|
}
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
_loop_1 = function (item) {
|
|
1134
|
-
var fulfillmentItem, fulfilledQuantity;
|
|
1135
|
-
return __generator(this, function (_k) {
|
|
1136
|
-
switch (_k.label) {
|
|
1137
|
-
case 0:
|
|
1138
|
-
fulfillmentItem = successfullyFulfilled.find(function (f) { return item.id === f.item_id; });
|
|
1139
|
-
if (!fulfillmentItem) return [3 /*break*/, 2];
|
|
1140
|
-
fulfilledQuantity = (item.fulfilled_quantity || 0) + fulfillmentItem.quantity;
|
|
1141
|
-
// Update the fulfilled quantity
|
|
1142
|
-
return [4 /*yield*/, lineItemServiceTx.update(item.id, {
|
|
1143
|
-
fulfilled_quantity: fulfilledQuantity,
|
|
1144
|
-
})];
|
|
1145
|
-
case 1:
|
|
1146
|
-
// Update the fulfilled quantity
|
|
1147
|
-
_k.sent();
|
|
1148
|
-
if (item.quantity !== fulfilledQuantity) {
|
|
1149
|
-
swap.fulfillment_status = "requires_action";
|
|
1150
|
-
}
|
|
1151
|
-
return [3 /*break*/, 3];
|
|
1152
|
-
case 2:
|
|
1153
|
-
if (item.quantity !== item.fulfilled_quantity) {
|
|
1154
|
-
swap.fulfillment_status = "requires_action";
|
|
1155
|
-
}
|
|
1156
|
-
_k.label = 3;
|
|
1157
|
-
case 3: return [2 /*return*/];
|
|
1116
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
1117
|
+
finally {
|
|
1118
|
+
try {
|
|
1119
|
+
if (_c && !_c.done && (_f = _b.return)) _f.call(_b);
|
|
1158
1120
|
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1121
|
+
finally { if (e_9) throw e_9.error; }
|
|
1122
|
+
}
|
|
1123
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.FULFILLED;
|
|
1124
|
+
lineItemServiceTx = this.lineItemService_.withTransaction(manager);
|
|
1125
|
+
_loop_1 = function (item) {
|
|
1126
|
+
var fulfillmentItem, fulfilledQuantity;
|
|
1127
|
+
return __generator(this, function (_k) {
|
|
1128
|
+
switch (_k.label) {
|
|
1129
|
+
case 0:
|
|
1130
|
+
fulfillmentItem = successfullyFulfilled.find(function (f) { return item.id === f.item_id; });
|
|
1131
|
+
if (!fulfillmentItem) return [3 /*break*/, 2];
|
|
1132
|
+
fulfilledQuantity = (item.fulfilled_quantity || 0) + fulfillmentItem.quantity;
|
|
1133
|
+
// Update the fulfilled quantity
|
|
1134
|
+
return [4 /*yield*/, lineItemServiceTx.update(item.id, {
|
|
1135
|
+
fulfilled_quantity: fulfilledQuantity,
|
|
1136
|
+
})];
|
|
1137
|
+
case 1:
|
|
1138
|
+
// Update the fulfilled quantity
|
|
1139
|
+
_k.sent();
|
|
1140
|
+
if (item.quantity !== fulfilledQuantity) {
|
|
1141
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.REQUIRES_ACTION;
|
|
1142
|
+
}
|
|
1143
|
+
return [3 /*break*/, 3];
|
|
1144
|
+
case 2:
|
|
1145
|
+
if (item.quantity !== item.fulfilled_quantity) {
|
|
1146
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.REQUIRES_ACTION;
|
|
1147
|
+
}
|
|
1148
|
+
_k.label = 3;
|
|
1149
|
+
case 3: return [2 /*return*/];
|
|
1150
|
+
}
|
|
1151
|
+
});
|
|
1152
|
+
};
|
|
1153
|
+
_j.label = 3;
|
|
1154
|
+
case 3:
|
|
1155
|
+
_j.trys.push([3, 8, 9, 10]);
|
|
1156
|
+
_d = __values(swap.additional_items), _e = _d.next();
|
|
1157
|
+
_j.label = 4;
|
|
1158
|
+
case 4:
|
|
1159
|
+
if (!!_e.done) return [3 /*break*/, 7];
|
|
1160
|
+
item = _e.value;
|
|
1161
|
+
return [5 /*yield**/, _loop_1(item)];
|
|
1162
|
+
case 5:
|
|
1163
|
+
_j.sent();
|
|
1164
|
+
_j.label = 6;
|
|
1165
|
+
case 6:
|
|
1166
|
+
_e = _d.next();
|
|
1167
|
+
return [3 /*break*/, 4];
|
|
1168
|
+
case 7: return [3 /*break*/, 10];
|
|
1169
|
+
case 8:
|
|
1170
|
+
e_8_1 = _j.sent();
|
|
1171
|
+
e_8 = { error: e_8_1 };
|
|
1172
|
+
return [3 /*break*/, 10];
|
|
1173
|
+
case 9:
|
|
1174
|
+
try {
|
|
1175
|
+
if (_e && !_e.done && (_g = _d.return)) _g.call(_d);
|
|
1176
|
+
}
|
|
1177
|
+
finally { if (e_8) throw e_8.error; }
|
|
1178
|
+
return [7 /*endfinally*/];
|
|
1179
|
+
case 10: return [4 /*yield*/, swapRepo.save(swap)];
|
|
1180
|
+
case 11:
|
|
1181
|
+
result = _j.sent();
|
|
1182
|
+
return [4 /*yield*/, this.eventBus_.withTransaction(manager).emit(SwapService.Events.FULFILLMENT_CREATED, {
|
|
1183
|
+
id: swapId,
|
|
1184
|
+
fulfillment_id: result.id,
|
|
1185
|
+
no_notification: evaluatedNoNotification,
|
|
1186
|
+
})];
|
|
1187
|
+
case 12:
|
|
1188
|
+
_j.sent();
|
|
1189
|
+
return [2 /*return*/, result];
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
}); })];
|
|
1193
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1194
|
+
}
|
|
1203
1195
|
});
|
|
1204
1196
|
});
|
|
1205
1197
|
};
|
|
1206
1198
|
/**
|
|
1207
1199
|
* Cancels a fulfillment (if related to a swap)
|
|
1208
|
-
*
|
|
1209
|
-
* @
|
|
1200
|
+
*
|
|
1201
|
+
* @param fulfillmentId - the ID of the fulfillment to cancel
|
|
1202
|
+
* @return updated swap
|
|
1210
1203
|
*/
|
|
1211
1204
|
SwapService.prototype.cancelFulfillment = function (fulfillmentId) {
|
|
1212
1205
|
return __awaiter(this, void 0, void 0, function () {
|
|
1213
1206
|
var _this = this;
|
|
1214
1207
|
return __generator(this, function (_a) {
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
.
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
return [2 /*return*/,
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1208
|
+
switch (_a.label) {
|
|
1209
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1210
|
+
var swapRepo, canceled, swap;
|
|
1211
|
+
return __generator(this, function (_a) {
|
|
1212
|
+
switch (_a.label) {
|
|
1213
|
+
case 0:
|
|
1214
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
1215
|
+
return [4 /*yield*/, this.fulfillmentService_
|
|
1216
|
+
.withTransaction(manager)
|
|
1217
|
+
.cancelFulfillment(fulfillmentId)];
|
|
1218
|
+
case 1:
|
|
1219
|
+
canceled = _a.sent();
|
|
1220
|
+
if (!canceled.swap_id) {
|
|
1221
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Fufillment not related to a swap");
|
|
1222
|
+
}
|
|
1223
|
+
return [4 /*yield*/, this.retrieve(canceled.swap_id)];
|
|
1224
|
+
case 2:
|
|
1225
|
+
swap = _a.sent();
|
|
1226
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.CANCELED;
|
|
1227
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
1228
|
+
case 3: return [2 /*return*/, _a.sent()];
|
|
1229
|
+
}
|
|
1230
|
+
});
|
|
1231
|
+
}); })];
|
|
1232
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1233
|
+
}
|
|
1239
1234
|
});
|
|
1240
1235
|
});
|
|
1241
1236
|
};
|
|
1242
1237
|
/**
|
|
1243
1238
|
* Marks a fulfillment as shipped and attaches tracking numbers.
|
|
1244
|
-
*
|
|
1245
|
-
* @param
|
|
1246
|
-
*
|
|
1247
|
-
* @param
|
|
1248
|
-
*
|
|
1249
|
-
* @
|
|
1250
|
-
* @return {Promise<Swap>} the updated swap with new fulfillments and status.
|
|
1239
|
+
*
|
|
1240
|
+
* @param swapId - the id of the swap that has been shipped.
|
|
1241
|
+
* @param fulfillmentId - the id of the specific fulfillment that has been shipped
|
|
1242
|
+
* @param trackingLinks - the tracking numbers associated with the shipment
|
|
1243
|
+
* @param config - optional configurations, includes optional metadata to attach to the shipment, and a noNotification flag.
|
|
1244
|
+
* @return the updated swap with new fulfillments and status.
|
|
1251
1245
|
*/
|
|
1252
1246
|
SwapService.prototype.createShipment = function (swapId, fulfillmentId, trackingLinks, config) {
|
|
1253
1247
|
if (config === void 0) { config = {
|
|
@@ -1255,173 +1249,199 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
1255
1249
|
no_notification: undefined,
|
|
1256
1250
|
}; }
|
|
1257
1251
|
return __awaiter(this, void 0, void 0, function () {
|
|
1258
|
-
var metadata, no_notification;
|
|
1259
1252
|
var _this = this;
|
|
1260
1253
|
return __generator(this, function (_a) {
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1254
|
+
switch (_a.label) {
|
|
1255
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1256
|
+
var metadata, no_notification, swapRepo, swap, evaluatedNoNotification, shipment, lineItemServiceTx, _loop_2, _a, _b, i, e_10_1, result;
|
|
1257
|
+
var e_10, _c;
|
|
1258
|
+
return __generator(this, function (_d) {
|
|
1259
|
+
switch (_d.label) {
|
|
1260
|
+
case 0:
|
|
1261
|
+
metadata = config.metadata, no_notification = config.no_notification;
|
|
1262
|
+
swapRepo = manager.getCustomRepository(this.swapRepository_);
|
|
1263
|
+
return [4 /*yield*/, this.retrieve(swapId, {
|
|
1264
|
+
relations: ["additional_items"],
|
|
1265
|
+
})];
|
|
1266
|
+
case 1:
|
|
1267
|
+
swap = _d.sent();
|
|
1268
|
+
if (swap.canceled_at) {
|
|
1269
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be fulfilled as shipped");
|
|
1270
|
+
}
|
|
1271
|
+
evaluatedNoNotification = no_notification !== undefined ? no_notification : swap.no_notification;
|
|
1272
|
+
return [4 /*yield*/, this.fulfillmentService_
|
|
1273
|
+
.withTransaction(manager)
|
|
1274
|
+
.createShipment(fulfillmentId, trackingLinks, {
|
|
1275
|
+
metadata: metadata,
|
|
1276
|
+
no_notification: evaluatedNoNotification,
|
|
1277
|
+
})];
|
|
1278
|
+
case 2:
|
|
1279
|
+
shipment = _d.sent();
|
|
1280
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.SHIPPED;
|
|
1281
|
+
lineItemServiceTx = this.lineItemService_.withTransaction(manager);
|
|
1282
|
+
_loop_2 = function (i) {
|
|
1283
|
+
var shipped, shippedQty;
|
|
1284
|
+
return __generator(this, function (_e) {
|
|
1285
|
+
switch (_e.label) {
|
|
1286
|
+
case 0:
|
|
1287
|
+
shipped = shipment.items.find(function (si) { return si.item_id === i.id; });
|
|
1288
|
+
if (!shipped) return [3 /*break*/, 2];
|
|
1289
|
+
shippedQty = (i.shipped_quantity || 0) + shipped.quantity;
|
|
1290
|
+
return [4 /*yield*/, lineItemServiceTx.update(i.id, {
|
|
1291
|
+
shipped_quantity: shippedQty,
|
|
1292
|
+
})];
|
|
1293
|
+
case 1:
|
|
1294
|
+
_e.sent();
|
|
1295
|
+
if (shippedQty !== i.quantity) {
|
|
1296
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.PARTIALLY_SHIPPED;
|
|
1297
|
+
}
|
|
1298
|
+
return [3 /*break*/, 3];
|
|
1299
|
+
case 2:
|
|
1300
|
+
if (i.shipped_quantity !== i.quantity) {
|
|
1301
|
+
swap.fulfillment_status = models_1.SwapFulfillmentStatus.PARTIALLY_SHIPPED;
|
|
1302
|
+
}
|
|
1303
|
+
_e.label = 3;
|
|
1304
|
+
case 3: return [2 /*return*/];
|
|
1305
|
+
}
|
|
1306
|
+
});
|
|
1307
|
+
};
|
|
1308
|
+
_d.label = 3;
|
|
1309
|
+
case 3:
|
|
1310
|
+
_d.trys.push([3, 8, 9, 10]);
|
|
1311
|
+
_a = __values(swap.additional_items), _b = _a.next();
|
|
1312
|
+
_d.label = 4;
|
|
1313
|
+
case 4:
|
|
1314
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
1315
|
+
i = _b.value;
|
|
1316
|
+
return [5 /*yield**/, _loop_2(i)];
|
|
1317
|
+
case 5:
|
|
1318
|
+
_d.sent();
|
|
1319
|
+
_d.label = 6;
|
|
1320
|
+
case 6:
|
|
1321
|
+
_b = _a.next();
|
|
1322
|
+
return [3 /*break*/, 4];
|
|
1323
|
+
case 7: return [3 /*break*/, 10];
|
|
1324
|
+
case 8:
|
|
1325
|
+
e_10_1 = _d.sent();
|
|
1326
|
+
e_10 = { error: e_10_1 };
|
|
1327
|
+
return [3 /*break*/, 10];
|
|
1328
|
+
case 9:
|
|
1329
|
+
try {
|
|
1330
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
1331
|
+
}
|
|
1332
|
+
finally { if (e_10) throw e_10.error; }
|
|
1333
|
+
return [7 /*endfinally*/];
|
|
1334
|
+
case 10: return [4 /*yield*/, swapRepo.save(swap)];
|
|
1335
|
+
case 11:
|
|
1336
|
+
result = _d.sent();
|
|
1337
|
+
return [4 /*yield*/, this.eventBus_
|
|
1338
|
+
.withTransaction(manager)
|
|
1339
|
+
.emit(SwapService.Events.SHIPMENT_CREATED, {
|
|
1340
|
+
id: swapId,
|
|
1341
|
+
fulfillment_id: shipment.id,
|
|
1342
|
+
no_notification: swap.no_notification,
|
|
1343
|
+
})];
|
|
1344
|
+
case 12:
|
|
1345
|
+
_d.sent();
|
|
1346
|
+
return [2 /*return*/, result];
|
|
1347
|
+
}
|
|
1348
|
+
});
|
|
1349
|
+
}); })];
|
|
1350
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1351
|
+
}
|
|
1356
1352
|
});
|
|
1357
1353
|
});
|
|
1358
1354
|
};
|
|
1359
1355
|
/**
|
|
1360
1356
|
* Dedicated method to delete metadata for a swap.
|
|
1361
|
-
*
|
|
1362
|
-
* @param
|
|
1363
|
-
* @
|
|
1357
|
+
*
|
|
1358
|
+
* @param swapId - the order to delete metadata from.
|
|
1359
|
+
* @param key - key for metadata field
|
|
1360
|
+
* @return resolves to the updated result.
|
|
1364
1361
|
*/
|
|
1365
1362
|
SwapService.prototype.deleteMetadata = function (swapId, key) {
|
|
1366
1363
|
return __awaiter(this, void 0, void 0, function () {
|
|
1367
|
-
var
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1364
|
+
var _this = this;
|
|
1365
|
+
return __generator(this, function (_a) {
|
|
1366
|
+
switch (_a.label) {
|
|
1367
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1368
|
+
var validatedId, swapRepo, swap, updated, updatedSwap;
|
|
1369
|
+
return __generator(this, function (_a) {
|
|
1370
|
+
switch (_a.label) {
|
|
1371
|
+
case 0:
|
|
1372
|
+
validatedId = (0, utils_1.validateId)(swapId);
|
|
1373
|
+
swapRepo = transactionManager.getCustomRepository(this.swapRepository_);
|
|
1374
|
+
return [4 /*yield*/, swapRepo.findOne(validatedId)];
|
|
1375
|
+
case 1:
|
|
1376
|
+
swap = _a.sent();
|
|
1377
|
+
if (!swap) {
|
|
1378
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Swap with id: ".concat(validatedId, " was not found"));
|
|
1379
|
+
}
|
|
1380
|
+
updated = swap.metadata || {};
|
|
1381
|
+
delete updated[key];
|
|
1382
|
+
swap.metadata = updated;
|
|
1383
|
+
return [4 /*yield*/, swapRepo.save(swap)];
|
|
1384
|
+
case 2:
|
|
1385
|
+
updatedSwap = _a.sent();
|
|
1386
|
+
return [4 /*yield*/, this.eventBus_
|
|
1387
|
+
.withTransaction(transactionManager)
|
|
1388
|
+
.emit(cart_1.default.Events.UPDATED, updatedSwap)];
|
|
1389
|
+
case 3:
|
|
1390
|
+
_a.sent();
|
|
1391
|
+
return [2 /*return*/, updatedSwap];
|
|
1392
|
+
}
|
|
1393
|
+
});
|
|
1394
|
+
}); })];
|
|
1395
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1373
1396
|
}
|
|
1374
|
-
keyPath = "metadata.".concat(key);
|
|
1375
|
-
return [2 /*return*/, this.swapModel_
|
|
1376
|
-
.updateOne({ _id: validatedId }, { $unset: (_a = {}, _a[keyPath] = "", _a) })
|
|
1377
|
-
.catch(function (err) {
|
|
1378
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DB_ERROR, err.message);
|
|
1379
|
-
})];
|
|
1380
1397
|
});
|
|
1381
1398
|
});
|
|
1382
1399
|
};
|
|
1383
1400
|
/**
|
|
1384
1401
|
* Registers the swap return items as received so that they cannot be used
|
|
1385
1402
|
* as a part of other swaps/returns.
|
|
1386
|
-
*
|
|
1387
|
-
* @param
|
|
1388
|
-
* @return
|
|
1403
|
+
*
|
|
1404
|
+
* @param id - the id of the order with the swap.
|
|
1405
|
+
* @return the resulting order
|
|
1389
1406
|
*/
|
|
1390
1407
|
SwapService.prototype.registerReceived = function (id) {
|
|
1391
1408
|
return __awaiter(this, void 0, void 0, function () {
|
|
1392
1409
|
var _this = this;
|
|
1393
1410
|
return __generator(this, function (_a) {
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
case 1:
|
|
1402
|
-
swap = _a.sent();
|
|
1403
|
-
if (swap.canceled_at) {
|
|
1404
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be registered as received");
|
|
1405
|
-
}
|
|
1406
|
-
if (swap.return_order.status !== "received") {
|
|
1407
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Swap is not received");
|
|
1408
|
-
}
|
|
1409
|
-
return [4 /*yield*/, this.retrieve(id)];
|
|
1410
|
-
case 2:
|
|
1411
|
-
result = _a.sent();
|
|
1412
|
-
return [4 /*yield*/, this.eventBus_
|
|
1413
|
-
.withTransaction(manager)
|
|
1414
|
-
.emit(SwapService.Events.RECEIVED, {
|
|
1415
|
-
id: id,
|
|
1416
|
-
order_id: result.order_id,
|
|
1417
|
-
no_notification: swap.no_notification,
|
|
1411
|
+
switch (_a.label) {
|
|
1412
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1413
|
+
var swap, result;
|
|
1414
|
+
return __generator(this, function (_a) {
|
|
1415
|
+
switch (_a.label) {
|
|
1416
|
+
case 0: return [4 /*yield*/, this.retrieve(id, {
|
|
1417
|
+
relations: ["return_order", "return_order.items"],
|
|
1418
1418
|
})];
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1419
|
+
case 1:
|
|
1420
|
+
swap = _a.sent();
|
|
1421
|
+
if (swap.canceled_at) {
|
|
1422
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Canceled swap cannot be registered as received");
|
|
1423
|
+
}
|
|
1424
|
+
if (swap.return_order.status !== "received") {
|
|
1425
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Swap is not received");
|
|
1426
|
+
}
|
|
1427
|
+
return [4 /*yield*/, this.retrieve(id)];
|
|
1428
|
+
case 2:
|
|
1429
|
+
result = _a.sent();
|
|
1430
|
+
return [4 /*yield*/, this.eventBus_
|
|
1431
|
+
.withTransaction(manager)
|
|
1432
|
+
.emit(SwapService.Events.RECEIVED, {
|
|
1433
|
+
id: id,
|
|
1434
|
+
order_id: result.order_id,
|
|
1435
|
+
no_notification: swap.no_notification,
|
|
1436
|
+
})];
|
|
1437
|
+
case 3:
|
|
1438
|
+
_a.sent();
|
|
1439
|
+
return [2 /*return*/, result];
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
}); })];
|
|
1443
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1444
|
+
}
|
|
1425
1445
|
});
|
|
1426
1446
|
});
|
|
1427
1447
|
};
|
|
@@ -1437,6 +1457,6 @@ var SwapService = /** @class */ (function (_super) {
|
|
|
1437
1457
|
FULFILLMENT_CREATED: "swap.fulfillment_created",
|
|
1438
1458
|
};
|
|
1439
1459
|
return SwapService;
|
|
1440
|
-
}(
|
|
1460
|
+
}(interfaces_1.TransactionBaseService));
|
|
1441
1461
|
exports.default = SwapService;
|
|
1442
1462
|
//# sourceMappingURL=swap.js.map
|