medusa-plugin-ses 1.0.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.
@@ -0,0 +1,1453 @@
1
+ "use strict";
2
+
3
+ function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports["default"] = void 0;
8
+ var _clientSes = require("@aws-sdk/client-ses");
9
+ var _medusaCoreUtils = require("medusa-core-utils");
10
+ var _medusaInterfaces = require("medusa-interfaces");
11
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
14
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && _instanceof(outerFn.prototype, Generator) ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
15
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
16
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
17
+ function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
19
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
20
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
21
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
22
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
23
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
24
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
25
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
26
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
27
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
28
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
29
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
30
+ var SESService = /*#__PURE__*/function (_NotificationService) {
31
+ _inherits(SESService, _NotificationService);
32
+ var _super = _createSuper(SESService);
33
+ /**
34
+ * @param {Object} options - options defined in `medusa-config.js`
35
+ * e.g.
36
+ * {
37
+ * access_key_id: process.env.SES_ACCESS_KEY_ID,
38
+ * secret_access_key: process.env.SES_SECRET_ACCESS_KEY,
39
+ * region: process.env.SES_REGION,
40
+ * from: "Medusa <hello@medusa.example>",
41
+ * order_placed_template: 01234,
42
+ * order_updated_template: 56789,
43
+ * order_canceled_template: 4242,
44
+ * user_password_reset_template: 0000,
45
+ * customer_password_reset_template: 1111,
46
+ * }
47
+ */
48
+ function SESService(_ref, options) {
49
+ var _this;
50
+ var storeService = _ref.storeService,
51
+ orderService = _ref.orderService,
52
+ returnService = _ref.returnService,
53
+ swapService = _ref.swapService,
54
+ cartService = _ref.cartService,
55
+ lineItemService = _ref.lineItemService,
56
+ claimService = _ref.claimService,
57
+ fulfillmentService = _ref.fulfillmentService,
58
+ fulfillmentProviderService = _ref.fulfillmentProviderService,
59
+ totalsService = _ref.totalsService,
60
+ productVariantService = _ref.productVariantService;
61
+ _classCallCheck(this, SESService);
62
+ _this = _super.call(this);
63
+ _this.options_ = options;
64
+ _this.fulfillmentProviderService_ = fulfillmentProviderService;
65
+ _this.storeService_ = storeService;
66
+ _this.lineItemService_ = lineItemService;
67
+ _this.orderService_ = orderService;
68
+ _this.cartService_ = cartService;
69
+ _this.claimService_ = claimService;
70
+ _this.returnService_ = returnService;
71
+ _this.swapService_ = swapService;
72
+ _this.fulfillmentService_ = fulfillmentService;
73
+ _this.totalsService_ = totalsService;
74
+ _this.productVariantService_ = productVariantService;
75
+ _this.client_ = new _clientSes.SESClient({
76
+ region: options.region,
77
+ credentials: {
78
+ accessKeyId: options.access_key_id,
79
+ secretAccessKey: options.secret_access_key
80
+ }
81
+ });
82
+ return _this;
83
+ }
84
+ _createClass(SESService, [{
85
+ key: "fetchAttachments",
86
+ value: function () {
87
+ var _fetchAttachments = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(event, data, attachmentGenerator) {
88
+ var attachments, _data$return_request, shipping_method, shipping_data, provider, lbl, base64;
89
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
90
+ while (1) switch (_context.prev = _context.next) {
91
+ case 0:
92
+ _context.t0 = event;
93
+ _context.next = _context.t0 === "swap.created" ? 3 : _context.t0 === "order.return_requested" ? 3 : 17;
94
+ break;
95
+ case 3:
96
+ attachments = [];
97
+ _data$return_request = data.return_request, shipping_method = _data$return_request.shipping_method, shipping_data = _data$return_request.shipping_data;
98
+ if (!shipping_method) {
99
+ _context.next = 11;
100
+ break;
101
+ }
102
+ provider = shipping_method.shipping_option.provider_id;
103
+ _context.next = 9;
104
+ return this.fulfillmentProviderService_.retrieveDocuments(provider, shipping_data, "label");
105
+ case 9:
106
+ lbl = _context.sent;
107
+ attachments = attachments.concat(lbl.map(function (d) {
108
+ return {
109
+ name: "return-label",
110
+ base64: d.base_64,
111
+ type: d.type
112
+ };
113
+ }));
114
+ case 11:
115
+ if (!(attachmentGenerator && attachmentGenerator.createReturnInvoice)) {
116
+ _context.next = 16;
117
+ break;
118
+ }
119
+ _context.next = 14;
120
+ return attachmentGenerator.createReturnInvoice(data.order, data.return_request.items);
121
+ case 14:
122
+ base64 = _context.sent;
123
+ attachments.push({
124
+ name: "invoice",
125
+ base64: base64,
126
+ type: "application/pdf"
127
+ });
128
+ case 16:
129
+ return _context.abrupt("return", attachments);
130
+ case 17:
131
+ return _context.abrupt("return", []);
132
+ case 18:
133
+ case "end":
134
+ return _context.stop();
135
+ }
136
+ }, _callee, this);
137
+ }));
138
+ function fetchAttachments(_x, _x2, _x3) {
139
+ return _fetchAttachments.apply(this, arguments);
140
+ }
141
+ return fetchAttachments;
142
+ }()
143
+ }, {
144
+ key: "fetchData",
145
+ value: function () {
146
+ var _fetchData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(event, eventData, attachmentGenerator) {
147
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
148
+ while (1) switch (_context2.prev = _context2.next) {
149
+ case 0:
150
+ _context2.t0 = event;
151
+ _context2.next = _context2.t0 === "order.return_requested" ? 3 : _context2.t0 === "swap.shipment_created" ? 4 : _context2.t0 === "claim.shipment_created" ? 5 : _context2.t0 === "order.items_returned" ? 6 : _context2.t0 === "swap.received" ? 7 : _context2.t0 === "swap.created" ? 8 : _context2.t0 === "gift_card.created" ? 9 : _context2.t0 === "order.gift_card_created" ? 10 : _context2.t0 === "order.placed" ? 11 : _context2.t0 === "order.shipment_created" ? 12 : _context2.t0 === "order.canceled" ? 13 : _context2.t0 === "user.password_reset" ? 14 : _context2.t0 === "customer.password_reset" ? 15 : _context2.t0 === "restock-notification.restocked" ? 16 : _context2.t0 === "order.refund_created" ? 19 : 20;
152
+ break;
153
+ case 3:
154
+ return _context2.abrupt("return", this.returnRequestedData(eventData, attachmentGenerator));
155
+ case 4:
156
+ return _context2.abrupt("return", this.swapShipmentCreatedData(eventData, attachmentGenerator));
157
+ case 5:
158
+ return _context2.abrupt("return", this.claimShipmentCreatedData(eventData, attachmentGenerator));
159
+ case 6:
160
+ return _context2.abrupt("return", this.itemsReturnedData(eventData, attachmentGenerator));
161
+ case 7:
162
+ return _context2.abrupt("return", this.swapReceivedData(eventData, attachmentGenerator));
163
+ case 8:
164
+ return _context2.abrupt("return", this.swapCreatedData(eventData, attachmentGenerator));
165
+ case 9:
166
+ return _context2.abrupt("return", this.gcCreatedData(eventData, attachmentGenerator));
167
+ case 10:
168
+ return _context2.abrupt("return", this.gcCreatedData(eventData, attachmentGenerator));
169
+ case 11:
170
+ return _context2.abrupt("return", this.orderPlacedData(eventData, attachmentGenerator));
171
+ case 12:
172
+ return _context2.abrupt("return", this.orderShipmentCreatedData(eventData, attachmentGenerator));
173
+ case 13:
174
+ return _context2.abrupt("return", this.orderCanceledData(eventData, attachmentGenerator));
175
+ case 14:
176
+ return _context2.abrupt("return", this.userPasswordResetData(eventData, attachmentGenerator));
177
+ case 15:
178
+ return _context2.abrupt("return", this.customerPasswordResetData(eventData, attachmentGenerator));
179
+ case 16:
180
+ _context2.next = 18;
181
+ return this.restockNotificationData(eventData, attachmentGenerator);
182
+ case 18:
183
+ return _context2.abrupt("return", _context2.sent);
184
+ case 19:
185
+ return _context2.abrupt("return", this.orderRefundCreatedData(eventData, attachmentGenerator));
186
+ case 20:
187
+ return _context2.abrupt("return", {});
188
+ case 21:
189
+ case "end":
190
+ return _context2.stop();
191
+ }
192
+ }, _callee2, this);
193
+ }));
194
+ function fetchData(_x4, _x5, _x6) {
195
+ return _fetchData.apply(this, arguments);
196
+ }
197
+ return fetchData;
198
+ }()
199
+ }, {
200
+ key: "getLocalizedTemplateId",
201
+ value: function getLocalizedTemplateId(event, locale) {
202
+ if (this.options_.localization && this.options_.localization[locale]) {
203
+ var map = this.options_.localization[locale];
204
+ switch (event) {
205
+ case "order.return_requested":
206
+ return map.order_return_requested_template;
207
+ case "swap.shipment_created":
208
+ return map.swap_shipment_created_template;
209
+ case "claim.shipment_created":
210
+ return map.claim_shipment_created_template;
211
+ case "order.items_returned":
212
+ return map.order_items_returned_template;
213
+ case "swap.received":
214
+ return map.swap_received_template;
215
+ case "swap.created":
216
+ return map.swap_created_template;
217
+ case "gift_card.created":
218
+ return map.gift_card_created_template;
219
+ case "order.gift_card_created":
220
+ return map.gift_card_created_template;
221
+ case "order.placed":
222
+ return map.order_placed_template;
223
+ case "order.shipment_created":
224
+ return map.order_shipped_template;
225
+ case "order.canceled":
226
+ return map.order_canceled_template;
227
+ case "user.password_reset":
228
+ return map.user_password_reset_template;
229
+ case "customer.password_reset":
230
+ return map.customer_password_reset_template;
231
+ case "restock-notification.restocked":
232
+ return map.medusa_restock_template;
233
+ case "order.refund_created":
234
+ return map.order_refund_created_template;
235
+ default:
236
+ return null;
237
+ }
238
+ }
239
+ return null;
240
+ }
241
+ }, {
242
+ key: "getTemplateId",
243
+ value: function getTemplateId(event) {
244
+ switch (event) {
245
+ case "order.return_requested":
246
+ return this.options_.order_return_requested_template;
247
+ case "swap.shipment_created":
248
+ return this.options_.swap_shipment_created_template;
249
+ case "claim.shipment_created":
250
+ return this.options_.claim_shipment_created_template;
251
+ case "order.items_returned":
252
+ return this.options_.order_items_returned_template;
253
+ case "swap.received":
254
+ return this.options_.swap_received_template;
255
+ case "swap.created":
256
+ return this.options_.swap_created_template;
257
+ case "gift_card.created":
258
+ return this.options_.gift_card_created_template;
259
+ case "order.gift_card_created":
260
+ return this.options_.gift_card_created_template;
261
+ case "order.placed":
262
+ return this.options_.order_placed_template;
263
+ case "order.shipment_created":
264
+ return this.options_.order_shipped_template;
265
+ case "order.canceled":
266
+ return this.options_.order_canceled_template;
267
+ case "user.password_reset":
268
+ return this.options_.user_password_reset_template;
269
+ case "customer.password_reset":
270
+ return this.options_.customer_password_reset_template;
271
+ case "restock-notification.restocked":
272
+ return this.options_.medusa_restock_template;
273
+ case "order.refund_created":
274
+ return this.options_.order_refund_created_template;
275
+ default:
276
+ return null;
277
+ }
278
+ }
279
+ }, {
280
+ key: "sendNotification",
281
+ value: function () {
282
+ var _sendNotification = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(event, eventData, attachmentGenerator) {
283
+ var templateId, data, attachments, sendOptions, status;
284
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
285
+ while (1) switch (_context3.prev = _context3.next) {
286
+ case 0:
287
+ templateId = this.getTemplateId(event);
288
+ if (templateId) {
289
+ _context3.next = 3;
290
+ break;
291
+ }
292
+ return _context3.abrupt("return", false);
293
+ case 3:
294
+ _context3.next = 5;
295
+ return this.fetchData(event, eventData, attachmentGenerator);
296
+ case 5:
297
+ data = _context3.sent;
298
+ _context3.next = 8;
299
+ return this.fetchAttachments(event, data, attachmentGenerator);
300
+ case 8:
301
+ attachments = _context3.sent;
302
+ if (data.locale) {
303
+ templateId = this.getLocalizedTemplateId(event, data.locale) || templateId;
304
+ }
305
+ sendOptions = {
306
+ Destination: {
307
+ ToAddresses: [data.email]
308
+ },
309
+ Source: this.options_.from,
310
+ Template: templateId,
311
+ TemplateData: JSON.stringify(data)
312
+ }; // if (attachments?.length) {
313
+ // sendOptions.has_attachments = true
314
+ // sendOptions.attachments = attachments.map((a) => {
315
+ // return {
316
+ // content: a.base64,
317
+ // filename: a.name,
318
+ // type: a.type,
319
+ // disposition: "attachment",
320
+ // contentId: a.name,
321
+ // }
322
+ // })
323
+ // }
324
+ console.log(sendOptions);
325
+ _context3.next = 14;
326
+ return this.client_.send(new _clientSes.SendTemplatedEmailCommand(sendOptions)).then(function () {
327
+ status = "sent";
328
+ })["catch"](function (error) {
329
+ status = "failed";
330
+ console.log(error);
331
+ });
332
+ case 14:
333
+ // We don't want heavy docs stored in DB
334
+ delete sendOptions.attachments;
335
+ return _context3.abrupt("return", {
336
+ to: data.email,
337
+ status: status,
338
+ data: sendOptions
339
+ });
340
+ case 16:
341
+ case "end":
342
+ return _context3.stop();
343
+ }
344
+ }, _callee3, this);
345
+ }));
346
+ function sendNotification(_x7, _x8, _x9) {
347
+ return _sendNotification.apply(this, arguments);
348
+ }
349
+ return sendNotification;
350
+ }()
351
+ }, {
352
+ key: "resendNotification",
353
+ value: function () {
354
+ var _resendNotification = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(notification, config, attachmentGenerator) {
355
+ var sendOptions, attachs;
356
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
357
+ while (1) switch (_context4.prev = _context4.next) {
358
+ case 0:
359
+ sendOptions = _objectSpread(_objectSpread({}, notification.data), {}, {
360
+ to: config.to || notification.to
361
+ });
362
+ _context4.next = 3;
363
+ return this.fetchAttachments(notification.event_name, notification.data.dynamic_template_data, attachmentGenerator);
364
+ case 3:
365
+ attachs = _context4.sent;
366
+ sendOptions.attachments = attachs.map(function (a) {
367
+ return {
368
+ content: a.base64,
369
+ filename: a.name,
370
+ type: a.type,
371
+ disposition: "attachment",
372
+ contentId: a.name
373
+ };
374
+ });
375
+
376
+ // const status = await SendGrid.send(sendOptions)
377
+ // .then(() => "sent")
378
+ // .catch(() => "failed")
379
+
380
+ // return { to: sendOptions.to, status, data: sendOptions }
381
+ return _context4.abrupt("return", {});
382
+ case 6:
383
+ case "end":
384
+ return _context4.stop();
385
+ }
386
+ }, _callee4, this);
387
+ }));
388
+ function resendNotification(_x10, _x11, _x12) {
389
+ return _resendNotification.apply(this, arguments);
390
+ }
391
+ return resendNotification;
392
+ }()
393
+ /**
394
+ * Sends an email using SendGrid.
395
+ * @param {string} templateId - id of template in SendGrid
396
+ * @param {string} from - sender of email
397
+ * @param {string} to - receiver of email
398
+ * @param {Object} data - data to send in mail (match with template)
399
+ * @return {Promise} result of the send operation
400
+ */
401
+ // async sendEmail(options) {
402
+ // try {
403
+ // return SendGrid.send(options)
404
+ // } catch (error) {
405
+ // throw error
406
+ // }
407
+ // }
408
+ }, {
409
+ key: "orderShipmentCreatedData",
410
+ value: function () {
411
+ var _orderShipmentCreatedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(_ref2, attachmentGenerator) {
412
+ var id, fulfillment_id, order, shipment, locale;
413
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
414
+ while (1) switch (_context5.prev = _context5.next) {
415
+ case 0:
416
+ id = _ref2.id, fulfillment_id = _ref2.fulfillment_id;
417
+ _context5.next = 3;
418
+ return this.orderService_.retrieve(id, {
419
+ select: ["shipping_total", "discount_total", "tax_total", "refunded_total", "gift_card_total", "subtotal", "total", "refundable_amount"],
420
+ relations: ["customer", "billing_address", "shipping_address", "discounts", "discounts.rule", "shipping_methods", "shipping_methods.shipping_option", "payments", "fulfillments", "returns", "gift_cards", "gift_card_transactions"]
421
+ });
422
+ case 3:
423
+ order = _context5.sent;
424
+ _context5.next = 6;
425
+ return this.fulfillmentService_.retrieve(fulfillment_id, {
426
+ relations: ["items", "tracking_links"]
427
+ });
428
+ case 6:
429
+ shipment = _context5.sent;
430
+ _context5.next = 9;
431
+ return this.extractLocale(order);
432
+ case 9:
433
+ locale = _context5.sent;
434
+ return _context5.abrupt("return", {
435
+ locale: locale,
436
+ order: order,
437
+ date: shipment.shipped_at.toDateString(),
438
+ email: order.email,
439
+ fulfillment: shipment,
440
+ tracking_links: shipment.tracking_links,
441
+ tracking_number: shipment.tracking_numbers.join(", ")
442
+ });
443
+ case 11:
444
+ case "end":
445
+ return _context5.stop();
446
+ }
447
+ }, _callee5, this);
448
+ }));
449
+ function orderShipmentCreatedData(_x13, _x14) {
450
+ return _orderShipmentCreatedData.apply(this, arguments);
451
+ }
452
+ return orderShipmentCreatedData;
453
+ }()
454
+ }, {
455
+ key: "orderCanceledData",
456
+ value: function () {
457
+ var _orderCanceledData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref3) {
458
+ var id, order, subtotal, tax_total, discount_total, shipping_total, gift_card_total, total, taxRate, currencyCode, items, discounts, giftCards, locale;
459
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
460
+ while (1) switch (_context6.prev = _context6.next) {
461
+ case 0:
462
+ id = _ref3.id;
463
+ _context6.next = 3;
464
+ return this.orderService_.retrieve(id, {
465
+ select: ["shipping_total", "discount_total", "tax_total", "refunded_total", "gift_card_total", "subtotal", "total"],
466
+ relations: ["customer", "billing_address", "shipping_address", "discounts", "discounts.rule", "shipping_methods", "shipping_methods.shipping_option", "payments", "fulfillments", "returns", "gift_cards", "gift_card_transactions"]
467
+ });
468
+ case 3:
469
+ order = _context6.sent;
470
+ subtotal = order.subtotal, tax_total = order.tax_total, discount_total = order.discount_total, shipping_total = order.shipping_total, gift_card_total = order.gift_card_total, total = order.total;
471
+ taxRate = order.tax_rate / 100;
472
+ currencyCode = order.currency_code.toUpperCase();
473
+ items = this.processItems_(order.items, taxRate, currencyCode);
474
+ discounts = [];
475
+ if (order.discounts) {
476
+ discounts = order.discounts.map(function (discount) {
477
+ return {
478
+ is_giftcard: false,
479
+ code: discount.code,
480
+ descriptor: "".concat(discount.rule.value).concat(discount.rule.type === "percentage" ? "%" : " ".concat(currencyCode))
481
+ };
482
+ });
483
+ }
484
+ giftCards = [];
485
+ if (order.gift_cards) {
486
+ giftCards = order.gift_cards.map(function (gc) {
487
+ return {
488
+ is_giftcard: true,
489
+ code: gc.code,
490
+ descriptor: "".concat(gc.value, " ").concat(currencyCode)
491
+ };
492
+ });
493
+ discounts.concat(giftCards);
494
+ }
495
+ _context6.next = 14;
496
+ return this.extractLocale(order);
497
+ case 14:
498
+ locale = _context6.sent;
499
+ return _context6.abrupt("return", _objectSpread(_objectSpread({}, order), {}, {
500
+ locale: locale,
501
+ has_discounts: order.discounts.length,
502
+ has_gift_cards: order.gift_cards.length,
503
+ date: order.created_at.toDateString(),
504
+ items: items,
505
+ discounts: discounts,
506
+ subtotal: "".concat(this.humanPrice_(subtotal * (1 + taxRate), currencyCode), " ").concat(currencyCode),
507
+ gift_card_total: "".concat(this.humanPrice_(gift_card_total * (1 + taxRate), currencyCode), " ").concat(currencyCode),
508
+ tax_total: "".concat(this.humanPrice_(tax_total, currencyCode), " ").concat(currencyCode),
509
+ discount_total: "".concat(this.humanPrice_(discount_total * (1 + taxRate), currencyCode), " ").concat(currencyCode),
510
+ shipping_total: "".concat(this.humanPrice_(shipping_total * (1 + taxRate), currencyCode), " ").concat(currencyCode),
511
+ total: "".concat(this.humanPrice_(total, currencyCode), " ").concat(currencyCode)
512
+ }));
513
+ case 16:
514
+ case "end":
515
+ return _context6.stop();
516
+ }
517
+ }, _callee6, this);
518
+ }));
519
+ function orderCanceledData(_x15) {
520
+ return _orderCanceledData.apply(this, arguments);
521
+ }
522
+ return orderCanceledData;
523
+ }()
524
+ }, {
525
+ key: "orderPlacedData",
526
+ value: function () {
527
+ var _orderPlacedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref4) {
528
+ var _this2 = this;
529
+ var id, order, tax_total, shipping_total, gift_card_total, total, currencyCode, items, discounts, giftCards, locale, discountTotal, discounted_subtotal, subtotal, subtotal_ex_tax;
530
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
531
+ while (1) switch (_context8.prev = _context8.next) {
532
+ case 0:
533
+ id = _ref4.id;
534
+ _context8.next = 3;
535
+ return this.orderService_.retrieve(id, {
536
+ select: ["shipping_total", "discount_total", "tax_total", "refunded_total", "gift_card_total", "subtotal", "total"],
537
+ relations: ["customer", "billing_address", "shipping_address", "discounts", "discounts.rule", "shipping_methods", "shipping_methods.shipping_option", "payments", "fulfillments", "returns", "gift_cards", "gift_card_transactions"]
538
+ });
539
+ case 3:
540
+ order = _context8.sent;
541
+ tax_total = order.tax_total, shipping_total = order.shipping_total, gift_card_total = order.gift_card_total, total = order.total;
542
+ currencyCode = order.currency_code.toUpperCase();
543
+ _context8.next = 8;
544
+ return Promise.all(order.items.map( /*#__PURE__*/function () {
545
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(i) {
546
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
547
+ while (1) switch (_context7.prev = _context7.next) {
548
+ case 0:
549
+ _context7.next = 2;
550
+ return _this2.totalsService_.getLineItemTotals(i, order, {
551
+ include_tax: true,
552
+ use_tax_lines: true
553
+ });
554
+ case 2:
555
+ i.totals = _context7.sent;
556
+ i.thumbnail = _this2.normalizeThumbUrl_(i.thumbnail);
557
+ i.discounted_price = "".concat(_this2.humanPrice_(i.totals.total / i.quantity, currencyCode), " ").concat(currencyCode);
558
+ i.price = "".concat(_this2.humanPrice_(i.totals.original_total / i.quantity, currencyCode), " ").concat(currencyCode);
559
+ return _context7.abrupt("return", i);
560
+ case 7:
561
+ case "end":
562
+ return _context7.stop();
563
+ }
564
+ }, _callee7);
565
+ }));
566
+ return function (_x17) {
567
+ return _ref5.apply(this, arguments);
568
+ };
569
+ }()));
570
+ case 8:
571
+ items = _context8.sent;
572
+ discounts = [];
573
+ if (order.discounts) {
574
+ discounts = order.discounts.map(function (discount) {
575
+ return {
576
+ is_giftcard: false,
577
+ code: discount.code,
578
+ descriptor: "".concat(discount.rule.value).concat(discount.rule.type === "percentage" ? "%" : " ".concat(currencyCode))
579
+ };
580
+ });
581
+ }
582
+ giftCards = [];
583
+ if (order.gift_cards) {
584
+ giftCards = order.gift_cards.map(function (gc) {
585
+ return {
586
+ is_giftcard: true,
587
+ code: gc.code,
588
+ descriptor: "".concat(gc.value, " ").concat(currencyCode)
589
+ };
590
+ });
591
+ discounts.concat(giftCards);
592
+ }
593
+ _context8.next = 15;
594
+ return this.extractLocale(order);
595
+ case 15:
596
+ locale = _context8.sent;
597
+ // Includes taxes in discount amount
598
+ discountTotal = items.reduce(function (acc, i) {
599
+ return acc + i.totals.original_total - i.totals.total;
600
+ }, 0);
601
+ discounted_subtotal = items.reduce(function (acc, i) {
602
+ return acc + i.totals.total;
603
+ }, 0);
604
+ subtotal = items.reduce(function (acc, i) {
605
+ return acc + i.totals.original_total;
606
+ }, 0);
607
+ subtotal_ex_tax = items.reduce(function (total, i) {
608
+ return total + i.totals.subtotal;
609
+ }, 0);
610
+ return _context8.abrupt("return", _objectSpread(_objectSpread({}, order), {}, {
611
+ locale: locale,
612
+ has_discounts: order.discounts.length,
613
+ has_gift_cards: order.gift_cards.length,
614
+ date: order.created_at.toDateString(),
615
+ items: items,
616
+ discounts: discounts,
617
+ subtotal_ex_tax: "".concat(this.humanPrice_(subtotal_ex_tax, currencyCode), " ").concat(currencyCode),
618
+ subtotal: "".concat(this.humanPrice_(subtotal, currencyCode), " ").concat(currencyCode),
619
+ gift_card_total: "".concat(this.humanPrice_(gift_card_total, currencyCode), " ").concat(currencyCode),
620
+ tax_total: "".concat(this.humanPrice_(tax_total, currencyCode), " ").concat(currencyCode),
621
+ discount_total: "".concat(this.humanPrice_(discountTotal, currencyCode), " ").concat(currencyCode),
622
+ shipping_total: "".concat(this.humanPrice_(shipping_total, currencyCode), " ").concat(currencyCode),
623
+ total: "".concat(this.humanPrice_(total, currencyCode), " ").concat(currencyCode)
624
+ }));
625
+ case 21:
626
+ case "end":
627
+ return _context8.stop();
628
+ }
629
+ }, _callee8, this);
630
+ }));
631
+ function orderPlacedData(_x16) {
632
+ return _orderPlacedData.apply(this, arguments);
633
+ }
634
+ return orderPlacedData;
635
+ }()
636
+ }, {
637
+ key: "gcCreatedData",
638
+ value: function () {
639
+ var _gcCreatedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref6) {
640
+ var id, giftCard, taxRate, locale;
641
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
642
+ while (1) switch (_context9.prev = _context9.next) {
643
+ case 0:
644
+ id = _ref6.id;
645
+ _context9.next = 3;
646
+ return this.giftCardService_.retrieve(id, {
647
+ relations: ["region", "order"]
648
+ });
649
+ case 3:
650
+ giftCard = _context9.sent;
651
+ if (giftCard.order) {
652
+ _context9.next = 6;
653
+ break;
654
+ }
655
+ return _context9.abrupt("return");
656
+ case 6:
657
+ taxRate = giftCard.region.tax_rate / 100;
658
+ _context9.next = 9;
659
+ return this.extractLocale(order);
660
+ case 9:
661
+ locale = _context9.sent;
662
+ return _context9.abrupt("return", _objectSpread(_objectSpread({}, giftCard), {}, {
663
+ locale: locale,
664
+ email: giftCard.order.email,
665
+ display_value: giftCard.value * (1 + taxRate)
666
+ }));
667
+ case 11:
668
+ case "end":
669
+ return _context9.stop();
670
+ }
671
+ }, _callee9, this);
672
+ }));
673
+ function gcCreatedData(_x18) {
674
+ return _gcCreatedData.apply(this, arguments);
675
+ }
676
+ return gcCreatedData;
677
+ }()
678
+ }, {
679
+ key: "returnRequestedData",
680
+ value: function () {
681
+ var _returnRequestedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(_ref7) {
682
+ var _this3 = this;
683
+ var id, return_id, returnRequest, items, order, currencyCode, returnItems, item_subtotal, shippingTotal, base, locale;
684
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
685
+ while (1) switch (_context11.prev = _context11.next) {
686
+ case 0:
687
+ id = _ref7.id, return_id = _ref7.return_id;
688
+ _context11.next = 3;
689
+ return this.returnService_.retrieve(return_id, {
690
+ relations: ["items", "items.item", "items.item.tax_lines", "items.item.variant", "items.item.variant.product", "shipping_method", "shipping_method.tax_lines", "shipping_method.shipping_option"]
691
+ });
692
+ case 3:
693
+ returnRequest = _context11.sent;
694
+ _context11.next = 6;
695
+ return this.lineItemService_.list({
696
+ id: returnRequest.items.map(function (_ref8) {
697
+ var item_id = _ref8.item_id;
698
+ return item_id;
699
+ })
700
+ }, {
701
+ relations: ["tax_lines"]
702
+ });
703
+ case 6:
704
+ items = _context11.sent;
705
+ _context11.next = 9;
706
+ return this.orderService_.retrieve(id, {
707
+ select: ["total"],
708
+ relations: ["items", "items.tax_lines", "discounts", "discounts.rule", "shipping_address", "returns"]
709
+ });
710
+ case 9:
711
+ order = _context11.sent;
712
+ currencyCode = order.currency_code.toUpperCase(); // Calculate which items are in the return
713
+ _context11.next = 13;
714
+ return Promise.all(returnRequest.items.map( /*#__PURE__*/function () {
715
+ var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(i) {
716
+ var found;
717
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
718
+ while (1) switch (_context10.prev = _context10.next) {
719
+ case 0:
720
+ found = items.find(function (oi) {
721
+ return oi.id === i.item_id;
722
+ });
723
+ found.quantity = i.quantity;
724
+ found.thumbnail = _this3.normalizeThumbUrl_(found.thumbnail);
725
+ _context10.next = 5;
726
+ return _this3.totalsService_.getLineItemTotals(found, order, {
727
+ include_tax: true,
728
+ use_tax_lines: true
729
+ });
730
+ case 5:
731
+ found.totals = _context10.sent;
732
+ found.price = "".concat(_this3.humanPrice_(found.totals.total, currencyCode), " ").concat(currencyCode);
733
+ found.tax_lines = found.totals.tax_lines;
734
+ return _context10.abrupt("return", found);
735
+ case 9:
736
+ case "end":
737
+ return _context10.stop();
738
+ }
739
+ }, _callee10);
740
+ }));
741
+ return function (_x20) {
742
+ return _ref9.apply(this, arguments);
743
+ };
744
+ }()));
745
+ case 13:
746
+ returnItems = _context11.sent;
747
+ // Get total of the returned products
748
+ item_subtotal = returnItems.reduce(function (acc, next) {
749
+ return acc + next.totals.total;
750
+ }, 0); // If the return has a shipping method get the price and any attachments
751
+ shippingTotal = 0;
752
+ if (returnRequest.shipping_method) {
753
+ base = returnRequest.shipping_method.price;
754
+ shippingTotal = base + returnRequest.shipping_method.tax_lines.reduce(function (acc, next) {
755
+ return Math.round(acc + base * (next.rate / 100));
756
+ }, 0);
757
+ }
758
+ _context11.next = 19;
759
+ return this.extractLocale(order);
760
+ case 19:
761
+ locale = _context11.sent;
762
+ return _context11.abrupt("return", {
763
+ locale: locale,
764
+ has_shipping: !!returnRequest.shipping_method,
765
+ email: order.email,
766
+ items: returnItems,
767
+ subtotal: "".concat(this.humanPrice_(item_subtotal, currencyCode), " ").concat(currencyCode),
768
+ shipping_total: "".concat(this.humanPrice_(shippingTotal, currencyCode), " ").concat(currencyCode),
769
+ refund_amount: "".concat(this.humanPrice_(returnRequest.refund_amount, currencyCode), " ").concat(currencyCode),
770
+ return_request: _objectSpread(_objectSpread({}, returnRequest), {}, {
771
+ refund_amount: "".concat(this.humanPrice_(returnRequest.refund_amount, currencyCode), " ").concat(currencyCode)
772
+ }),
773
+ order: order,
774
+ date: returnRequest.updated_at.toDateString()
775
+ });
776
+ case 21:
777
+ case "end":
778
+ return _context11.stop();
779
+ }
780
+ }, _callee11, this);
781
+ }));
782
+ function returnRequestedData(_x19) {
783
+ return _returnRequestedData.apply(this, arguments);
784
+ }
785
+ return returnRequestedData;
786
+ }()
787
+ }, {
788
+ key: "swapReceivedData",
789
+ value: function () {
790
+ var _swapReceivedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(_ref10) {
791
+ var _this4 = this;
792
+ var id, store, swap, returnRequest, items, swapLink, order, cart, currencyCode, decoratedItems, returnTotal, additionalTotal, refundAmount, locale;
793
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
794
+ while (1) switch (_context13.prev = _context13.next) {
795
+ case 0:
796
+ id = _ref10.id;
797
+ _context13.next = 3;
798
+ return this.storeService_.retrieve();
799
+ case 3:
800
+ store = _context13.sent;
801
+ _context13.next = 6;
802
+ return this.swapService_.retrieve(id, {
803
+ relations: ["additional_items", "additional_items.tax_lines", "return_order", "return_order.items", "return_order.items.item", "return_order.shipping_method", "return_order.shipping_method.shipping_option"]
804
+ });
805
+ case 6:
806
+ swap = _context13.sent;
807
+ returnRequest = swap.return_order;
808
+ _context13.next = 10;
809
+ return this.lineItemService_.list({
810
+ id: returnRequest.items.map(function (_ref11) {
811
+ var item_id = _ref11.item_id;
812
+ return item_id;
813
+ })
814
+ }, {
815
+ relations: ["tax_lines"]
816
+ });
817
+ case 10:
818
+ items = _context13.sent;
819
+ returnRequest.items = returnRequest.items.map(function (item) {
820
+ var found = items.find(function (i) {
821
+ return i.id === item.item_id;
822
+ });
823
+ return _objectSpread(_objectSpread({}, item), {}, {
824
+ item: found
825
+ });
826
+ });
827
+ swapLink = store.swap_link_template.replace(/\{cart_id\}/, swap.cart_id);
828
+ _context13.next = 15;
829
+ return this.orderService_.retrieve(swap.order_id, {
830
+ select: ["total"],
831
+ relations: ["items", "discounts", "discounts.rule", "shipping_address", "swaps", "swaps.additional_items", "swaps.additional_items.tax_lines"]
832
+ });
833
+ case 15:
834
+ order = _context13.sent;
835
+ _context13.next = 18;
836
+ return this.cartService_.retrieve(swap.cart_id, {
837
+ select: ["total", "tax_total", "discount_total", "shipping_total", "subtotal"]
838
+ });
839
+ case 18:
840
+ cart = _context13.sent;
841
+ currencyCode = order.currency_code.toUpperCase();
842
+ _context13.next = 22;
843
+ return Promise.all(cart.items.map( /*#__PURE__*/function () {
844
+ var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(i) {
845
+ var totals;
846
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
847
+ while (1) switch (_context12.prev = _context12.next) {
848
+ case 0:
849
+ _context12.next = 2;
850
+ return _this4.totalsService_.getLineItemTotals(i, cart, {
851
+ include_tax: true
852
+ });
853
+ case 2:
854
+ totals = _context12.sent;
855
+ return _context12.abrupt("return", _objectSpread(_objectSpread({}, i), {}, {
856
+ totals: totals,
857
+ price: _this4.humanPrice_(totals.subtotal + totals.tax_total, currencyCode)
858
+ }));
859
+ case 4:
860
+ case "end":
861
+ return _context12.stop();
862
+ }
863
+ }, _callee12);
864
+ }));
865
+ return function (_x22) {
866
+ return _ref12.apply(this, arguments);
867
+ };
868
+ }()));
869
+ case 22:
870
+ decoratedItems = _context13.sent;
871
+ returnTotal = decoratedItems.reduce(function (acc, next) {
872
+ if (next.is_return) {
873
+ return acc + -1 * (next.totals.subtotal + next.totals.tax_total);
874
+ }
875
+ return acc;
876
+ }, 0);
877
+ additionalTotal = decoratedItems.reduce(function (acc, next) {
878
+ if (!next.is_return) {
879
+ return acc + next.totals.subtotal + next.totals.tax_total;
880
+ }
881
+ return acc;
882
+ }, 0);
883
+ refundAmount = swap.return_order.refund_amount;
884
+ _context13.next = 28;
885
+ return this.extractLocale(order);
886
+ case 28:
887
+ locale = _context13.sent;
888
+ return _context13.abrupt("return", {
889
+ locale: locale,
890
+ swap: swap,
891
+ order: order,
892
+ return_request: returnRequest,
893
+ date: swap.updated_at.toDateString(),
894
+ swap_link: swapLink,
895
+ email: order.email,
896
+ items: decoratedItems.filter(function (di) {
897
+ return !di.is_return;
898
+ }),
899
+ return_items: decoratedItems.filter(function (di) {
900
+ return di.is_return;
901
+ }),
902
+ return_total: "".concat(this.humanPrice_(returnTotal, currencyCode), " ").concat(currencyCode),
903
+ tax_total: "".concat(this.humanPrice_(cart.total, currencyCode), " ").concat(currencyCode),
904
+ refund_amount: "".concat(this.humanPrice_(refundAmount, currencyCode), " ").concat(currencyCode),
905
+ additional_total: "".concat(this.humanPrice_(additionalTotal, currencyCode), " ").concat(currencyCode)
906
+ });
907
+ case 30:
908
+ case "end":
909
+ return _context13.stop();
910
+ }
911
+ }, _callee13, this);
912
+ }));
913
+ function swapReceivedData(_x21) {
914
+ return _swapReceivedData.apply(this, arguments);
915
+ }
916
+ return swapReceivedData;
917
+ }()
918
+ }, {
919
+ key: "swapCreatedData",
920
+ value: function () {
921
+ var _swapCreatedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(_ref13) {
922
+ var _this5 = this;
923
+ var id, store, swap, returnRequest, items, swapLink, order, cart, currencyCode, decoratedItems, returnTotal, additionalTotal, refundAmount, locale;
924
+ return _regeneratorRuntime().wrap(function _callee15$(_context15) {
925
+ while (1) switch (_context15.prev = _context15.next) {
926
+ case 0:
927
+ id = _ref13.id;
928
+ _context15.next = 3;
929
+ return this.storeService_.retrieve();
930
+ case 3:
931
+ store = _context15.sent;
932
+ _context15.next = 6;
933
+ return this.swapService_.retrieve(id, {
934
+ relations: ["additional_items", "additional_items.tax_lines", "return_order", "return_order.items", "return_order.items.item", "return_order.shipping_method", "return_order.shipping_method.shipping_option"]
935
+ });
936
+ case 6:
937
+ swap = _context15.sent;
938
+ returnRequest = swap.return_order;
939
+ _context15.next = 10;
940
+ return this.lineItemService_.list({
941
+ id: returnRequest.items.map(function (_ref14) {
942
+ var item_id = _ref14.item_id;
943
+ return item_id;
944
+ })
945
+ }, {
946
+ relations: ["tax_lines"]
947
+ });
948
+ case 10:
949
+ items = _context15.sent;
950
+ returnRequest.items = returnRequest.items.map(function (item) {
951
+ var found = items.find(function (i) {
952
+ return i.id === item.item_id;
953
+ });
954
+ return _objectSpread(_objectSpread({}, item), {}, {
955
+ item: found
956
+ });
957
+ });
958
+ swapLink = store.swap_link_template.replace(/\{cart_id\}/, swap.cart_id);
959
+ _context15.next = 15;
960
+ return this.orderService_.retrieve(swap.order_id, {
961
+ select: ["total"],
962
+ relations: ["items", "items.tax_lines", "discounts", "discounts.rule", "shipping_address", "swaps", "swaps.additional_items", "swaps.additional_items.tax_lines"]
963
+ });
964
+ case 15:
965
+ order = _context15.sent;
966
+ _context15.next = 18;
967
+ return this.cartService_.retrieve(swap.cart_id, {
968
+ select: ["total", "tax_total", "discount_total", "shipping_total", "subtotal"]
969
+ });
970
+ case 18:
971
+ cart = _context15.sent;
972
+ currencyCode = order.currency_code.toUpperCase();
973
+ _context15.next = 22;
974
+ return Promise.all(cart.items.map( /*#__PURE__*/function () {
975
+ var _ref15 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(i) {
976
+ var totals;
977
+ return _regeneratorRuntime().wrap(function _callee14$(_context14) {
978
+ while (1) switch (_context14.prev = _context14.next) {
979
+ case 0:
980
+ _context14.next = 2;
981
+ return _this5.totalsService_.getLineItemTotals(i, cart, {
982
+ include_tax: true
983
+ });
984
+ case 2:
985
+ totals = _context14.sent;
986
+ return _context14.abrupt("return", _objectSpread(_objectSpread({}, i), {}, {
987
+ totals: totals,
988
+ tax_lines: totals.tax_lines,
989
+ price: "".concat(_this5.humanPrice_(totals.original_total / i.quantity, currencyCode), " ").concat(currencyCode),
990
+ discounted_price: "".concat(_this5.humanPrice_(totals.total / i.quantity, currencyCode), " ").concat(currencyCode)
991
+ }));
992
+ case 4:
993
+ case "end":
994
+ return _context14.stop();
995
+ }
996
+ }, _callee14);
997
+ }));
998
+ return function (_x24) {
999
+ return _ref15.apply(this, arguments);
1000
+ };
1001
+ }()));
1002
+ case 22:
1003
+ decoratedItems = _context15.sent;
1004
+ returnTotal = decoratedItems.reduce(function (acc, next) {
1005
+ var total = next.totals.total;
1006
+ if (next.is_return && next.variant_id) {
1007
+ return acc + -1 * total;
1008
+ }
1009
+ return acc;
1010
+ }, 0);
1011
+ additionalTotal = decoratedItems.reduce(function (acc, next) {
1012
+ var total = next.totals.total;
1013
+ if (!next.is_return) {
1014
+ return acc + total;
1015
+ }
1016
+ return acc;
1017
+ }, 0);
1018
+ refundAmount = swap.return_order.refund_amount;
1019
+ _context15.next = 28;
1020
+ return this.extractLocale(order);
1021
+ case 28:
1022
+ locale = _context15.sent;
1023
+ return _context15.abrupt("return", {
1024
+ locale: locale,
1025
+ swap: swap,
1026
+ order: order,
1027
+ return_request: returnRequest,
1028
+ date: swap.updated_at.toDateString(),
1029
+ swap_link: swapLink,
1030
+ email: order.email,
1031
+ items: decoratedItems.filter(function (di) {
1032
+ return !di.is_return;
1033
+ }),
1034
+ return_items: decoratedItems.filter(function (di) {
1035
+ return di.is_return;
1036
+ }),
1037
+ return_total: "".concat(this.humanPrice_(returnTotal, currencyCode), " ").concat(currencyCode),
1038
+ refund_amount: "".concat(this.humanPrice_(refundAmount, currencyCode), " ").concat(currencyCode),
1039
+ additional_total: "".concat(this.humanPrice_(additionalTotal, currencyCode), " ").concat(currencyCode)
1040
+ });
1041
+ case 30:
1042
+ case "end":
1043
+ return _context15.stop();
1044
+ }
1045
+ }, _callee15, this);
1046
+ }));
1047
+ function swapCreatedData(_x23) {
1048
+ return _swapCreatedData.apply(this, arguments);
1049
+ }
1050
+ return swapCreatedData;
1051
+ }()
1052
+ }, {
1053
+ key: "itemsReturnedData",
1054
+ value: function () {
1055
+ var _itemsReturnedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(data) {
1056
+ return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1057
+ while (1) switch (_context16.prev = _context16.next) {
1058
+ case 0:
1059
+ return _context16.abrupt("return", this.returnRequestedData(data));
1060
+ case 1:
1061
+ case "end":
1062
+ return _context16.stop();
1063
+ }
1064
+ }, _callee16, this);
1065
+ }));
1066
+ function itemsReturnedData(_x25) {
1067
+ return _itemsReturnedData.apply(this, arguments);
1068
+ }
1069
+ return itemsReturnedData;
1070
+ }()
1071
+ }, {
1072
+ key: "swapShipmentCreatedData",
1073
+ value: function () {
1074
+ var _swapShipmentCreatedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19(_ref16) {
1075
+ var _this6 = this;
1076
+ var id, fulfillment_id, swap, order, cart, returnRequest, items, taxRate, currencyCode, returnItems, returnTotal, constructedOrder, additionalTotal, refundAmount, shipment, locale;
1077
+ return _regeneratorRuntime().wrap(function _callee19$(_context19) {
1078
+ while (1) switch (_context19.prev = _context19.next) {
1079
+ case 0:
1080
+ id = _ref16.id, fulfillment_id = _ref16.fulfillment_id;
1081
+ _context19.next = 3;
1082
+ return this.swapService_.retrieve(id, {
1083
+ relations: ["shipping_address", "shipping_methods", "shipping_methods.tax_lines", "additional_items", "additional_items.tax_lines", "return_order", "return_order.items"]
1084
+ });
1085
+ case 3:
1086
+ swap = _context19.sent;
1087
+ _context19.next = 6;
1088
+ return this.orderService_.retrieve(swap.order_id, {
1089
+ relations: ["region", "items", "items.tax_lines", "discounts", "discounts.rule", "swaps", "swaps.additional_items", "swaps.additional_items.tax_lines"]
1090
+ });
1091
+ case 6:
1092
+ order = _context19.sent;
1093
+ _context19.next = 9;
1094
+ return this.cartService_.retrieve(swap.cart_id, {
1095
+ select: ["total", "tax_total", "discount_total", "shipping_total", "subtotal"]
1096
+ });
1097
+ case 9:
1098
+ cart = _context19.sent;
1099
+ returnRequest = swap.return_order;
1100
+ _context19.next = 13;
1101
+ return this.lineItemService_.list({
1102
+ id: returnRequest.items.map(function (_ref17) {
1103
+ var item_id = _ref17.item_id;
1104
+ return item_id;
1105
+ })
1106
+ }, {
1107
+ relations: ["tax_lines"]
1108
+ });
1109
+ case 13:
1110
+ items = _context19.sent;
1111
+ taxRate = order.tax_rate / 100;
1112
+ currencyCode = order.currency_code.toUpperCase();
1113
+ _context19.next = 18;
1114
+ return Promise.all(swap.return_order.items.map( /*#__PURE__*/function () {
1115
+ var _ref18 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(i) {
1116
+ var found, totals;
1117
+ return _regeneratorRuntime().wrap(function _callee17$(_context17) {
1118
+ while (1) switch (_context17.prev = _context17.next) {
1119
+ case 0:
1120
+ found = items.find(function (oi) {
1121
+ return oi.id === i.item_id;
1122
+ });
1123
+ _context17.next = 3;
1124
+ return _this6.totalsService_.getLineItemTotals(i, cart, {
1125
+ include_tax: true
1126
+ });
1127
+ case 3:
1128
+ totals = _context17.sent;
1129
+ return _context17.abrupt("return", _objectSpread(_objectSpread({}, found), {}, {
1130
+ thumbnail: _this6.normalizeThumbUrl_(found.thumbnail),
1131
+ price: "".concat(_this6.humanPrice_(totals.original_total / i.quantity, currencyCode), " ").concat(currencyCode),
1132
+ discounted_price: "".concat(_this6.humanPrice_(totals.total / i.quantity, currencyCode), " ").concat(currencyCode),
1133
+ quantity: i.quantity
1134
+ }));
1135
+ case 5:
1136
+ case "end":
1137
+ return _context17.stop();
1138
+ }
1139
+ }, _callee17);
1140
+ }));
1141
+ return function (_x27) {
1142
+ return _ref18.apply(this, arguments);
1143
+ };
1144
+ }()));
1145
+ case 18:
1146
+ returnItems = _context19.sent;
1147
+ _context19.next = 21;
1148
+ return this.totalsService_.getRefundTotal(order, returnItems);
1149
+ case 21:
1150
+ returnTotal = _context19.sent;
1151
+ constructedOrder = _objectSpread(_objectSpread({}, order), {}, {
1152
+ shipping_methods: swap.shipping_methods,
1153
+ items: swap.additional_items
1154
+ });
1155
+ _context19.next = 25;
1156
+ return this.totalsService_.getTotal(constructedOrder);
1157
+ case 25:
1158
+ additionalTotal = _context19.sent;
1159
+ refundAmount = swap.return_order.refund_amount;
1160
+ _context19.next = 29;
1161
+ return this.fulfillmentService_.retrieve(fulfillment_id, {
1162
+ relations: ["tracking_links"]
1163
+ });
1164
+ case 29:
1165
+ shipment = _context19.sent;
1166
+ _context19.next = 32;
1167
+ return this.extractLocale(order);
1168
+ case 32:
1169
+ locale = _context19.sent;
1170
+ _context19.t0 = locale;
1171
+ _context19.t1 = swap;
1172
+ _context19.t2 = order;
1173
+ _context19.next = 38;
1174
+ return Promise.all(swap.additional_items.map( /*#__PURE__*/function () {
1175
+ var _ref19 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(i) {
1176
+ var totals;
1177
+ return _regeneratorRuntime().wrap(function _callee18$(_context18) {
1178
+ while (1) switch (_context18.prev = _context18.next) {
1179
+ case 0:
1180
+ _context18.next = 2;
1181
+ return _this6.totalsService_.getLineItemTotals(i, cart, {
1182
+ include_tax: true
1183
+ });
1184
+ case 2:
1185
+ totals = _context18.sent;
1186
+ return _context18.abrupt("return", _objectSpread(_objectSpread({}, i), {}, {
1187
+ thumbnail: _this6.normalizeThumbUrl_(i.thumbnail),
1188
+ price: "".concat(_this6.humanPrice_(totals.original_total / i.quantity, currencyCode), " ").concat(currencyCode),
1189
+ discounted_price: "".concat(_this6.humanPrice_(totals.total / i.quantity, currencyCode), " ").concat(currencyCode),
1190
+ quantity: i.quantity
1191
+ }));
1192
+ case 4:
1193
+ case "end":
1194
+ return _context18.stop();
1195
+ }
1196
+ }, _callee18);
1197
+ }));
1198
+ return function (_x28) {
1199
+ return _ref19.apply(this, arguments);
1200
+ };
1201
+ }()));
1202
+ case 38:
1203
+ _context19.t3 = _context19.sent;
1204
+ _context19.t4 = swap.updated_at.toDateString();
1205
+ _context19.t5 = order.email;
1206
+ _context19.t6 = "".concat(this.humanPrice_(cart.tax_total, currencyCode), " ").concat(currencyCode);
1207
+ _context19.t7 = "".concat(this.humanPrice_(swap.difference_due, currencyCode), " ").concat(currencyCode);
1208
+ _context19.t8 = "".concat(this.humanPrice_(returnTotal, currencyCode), " ").concat(currencyCode);
1209
+ _context19.t9 = "".concat(this.humanPrice_(refundAmount, currencyCode), " ").concat(currencyCode);
1210
+ _context19.t10 = "".concat(this.humanPrice_(additionalTotal, currencyCode), " ").concat(currencyCode);
1211
+ _context19.t11 = shipment;
1212
+ _context19.t12 = shipment.tracking_links;
1213
+ _context19.t13 = shipment.tracking_numbers.join(", ");
1214
+ return _context19.abrupt("return", {
1215
+ locale: _context19.t0,
1216
+ swap: _context19.t1,
1217
+ order: _context19.t2,
1218
+ items: _context19.t3,
1219
+ date: _context19.t4,
1220
+ email: _context19.t5,
1221
+ tax_amount: _context19.t6,
1222
+ paid_total: _context19.t7,
1223
+ return_total: _context19.t8,
1224
+ refund_amount: _context19.t9,
1225
+ additional_total: _context19.t10,
1226
+ fulfillment: _context19.t11,
1227
+ tracking_links: _context19.t12,
1228
+ tracking_number: _context19.t13
1229
+ });
1230
+ case 50:
1231
+ case "end":
1232
+ return _context19.stop();
1233
+ }
1234
+ }, _callee19, this);
1235
+ }));
1236
+ function swapShipmentCreatedData(_x26) {
1237
+ return _swapShipmentCreatedData.apply(this, arguments);
1238
+ }
1239
+ return swapShipmentCreatedData;
1240
+ }()
1241
+ }, {
1242
+ key: "claimShipmentCreatedData",
1243
+ value: function () {
1244
+ var _claimShipmentCreatedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20(_ref20) {
1245
+ var id, fulfillment_id, claim, shipment, locale;
1246
+ return _regeneratorRuntime().wrap(function _callee20$(_context20) {
1247
+ while (1) switch (_context20.prev = _context20.next) {
1248
+ case 0:
1249
+ id = _ref20.id, fulfillment_id = _ref20.fulfillment_id;
1250
+ _context20.next = 3;
1251
+ return this.claimService_.retrieve(id, {
1252
+ relations: ["order", "order.items", "order.shipping_address"]
1253
+ });
1254
+ case 3:
1255
+ claim = _context20.sent;
1256
+ _context20.next = 6;
1257
+ return this.fulfillmentService_.retrieve(fulfillment_id, {
1258
+ relations: ["tracking_links"]
1259
+ });
1260
+ case 6:
1261
+ shipment = _context20.sent;
1262
+ _context20.next = 9;
1263
+ return this.extractLocale(claim.order);
1264
+ case 9:
1265
+ locale = _context20.sent;
1266
+ return _context20.abrupt("return", {
1267
+ locale: locale,
1268
+ email: claim.order.email,
1269
+ claim: claim,
1270
+ order: claim.order,
1271
+ fulfillment: shipment,
1272
+ tracking_links: shipment.tracking_links,
1273
+ tracking_number: shipment.tracking_numbers.join(", ")
1274
+ });
1275
+ case 11:
1276
+ case "end":
1277
+ return _context20.stop();
1278
+ }
1279
+ }, _callee20, this);
1280
+ }));
1281
+ function claimShipmentCreatedData(_x29) {
1282
+ return _claimShipmentCreatedData.apply(this, arguments);
1283
+ }
1284
+ return claimShipmentCreatedData;
1285
+ }()
1286
+ }, {
1287
+ key: "restockNotificationData",
1288
+ value: function () {
1289
+ var _restockNotificationData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21(_ref21) {
1290
+ var variant_id, emails, variant, thumb;
1291
+ return _regeneratorRuntime().wrap(function _callee21$(_context21) {
1292
+ while (1) switch (_context21.prev = _context21.next) {
1293
+ case 0:
1294
+ variant_id = _ref21.variant_id, emails = _ref21.emails;
1295
+ _context21.next = 3;
1296
+ return this.productVariantService_.retrieve(variant_id, {
1297
+ relations: ["product"]
1298
+ });
1299
+ case 3:
1300
+ variant = _context21.sent;
1301
+ if (variant.product.thumbnail) {
1302
+ thumb = this.normalizeThumbUrl_(variant.product.thumbnail);
1303
+ }
1304
+ return _context21.abrupt("return", {
1305
+ product: _objectSpread(_objectSpread({}, variant.product), {}, {
1306
+ thumbnail: thumb
1307
+ }),
1308
+ variant: variant,
1309
+ variant_id: variant_id,
1310
+ emails: emails
1311
+ });
1312
+ case 6:
1313
+ case "end":
1314
+ return _context21.stop();
1315
+ }
1316
+ }, _callee21, this);
1317
+ }));
1318
+ function restockNotificationData(_x30) {
1319
+ return _restockNotificationData.apply(this, arguments);
1320
+ }
1321
+ return restockNotificationData;
1322
+ }()
1323
+ }, {
1324
+ key: "userPasswordResetData",
1325
+ value: function userPasswordResetData(data) {
1326
+ return data;
1327
+ }
1328
+ }, {
1329
+ key: "customerPasswordResetData",
1330
+ value: function customerPasswordResetData(data) {
1331
+ return data;
1332
+ }
1333
+ }, {
1334
+ key: "orderRefundCreatedData",
1335
+ value: function () {
1336
+ var _orderRefundCreatedData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22(_ref22) {
1337
+ var id, refund_id, order, refund;
1338
+ return _regeneratorRuntime().wrap(function _callee22$(_context22) {
1339
+ while (1) switch (_context22.prev = _context22.next) {
1340
+ case 0:
1341
+ id = _ref22.id, refund_id = _ref22.refund_id;
1342
+ _context22.next = 3;
1343
+ return this.orderService_.retrieveWithTotals(id, {
1344
+ select: ["total"],
1345
+ relations: ["refunds", "items"]
1346
+ });
1347
+ case 3:
1348
+ order = _context22.sent;
1349
+ refund = order.refunds.find(function (refund) {
1350
+ return refund.id === refund_id;
1351
+ });
1352
+ return _context22.abrupt("return", {
1353
+ order: order,
1354
+ refund: refund,
1355
+ refund_amount: "".concat(this.humanPrice_(refund.amount, order.currency_code), " ").concat(order.currency_code),
1356
+ email: order.email
1357
+ });
1358
+ case 6:
1359
+ case "end":
1360
+ return _context22.stop();
1361
+ }
1362
+ }, _callee22, this);
1363
+ }));
1364
+ function orderRefundCreatedData(_x31) {
1365
+ return _orderRefundCreatedData.apply(this, arguments);
1366
+ }
1367
+ return orderRefundCreatedData;
1368
+ }()
1369
+ }, {
1370
+ key: "processItems_",
1371
+ value: function processItems_(items, taxRate, currencyCode) {
1372
+ var _this7 = this;
1373
+ return items.map(function (i) {
1374
+ return _objectSpread(_objectSpread({}, i), {}, {
1375
+ thumbnail: _this7.normalizeThumbUrl_(i.thumbnail),
1376
+ price: "".concat(_this7.humanPrice_(i.unit_price * (1 + taxRate), currencyCode), " ").concat(currencyCode)
1377
+ });
1378
+ });
1379
+ }
1380
+ }, {
1381
+ key: "humanPrice_",
1382
+ value: function humanPrice_(amount, currency) {
1383
+ if (!amount) {
1384
+ return "0.00";
1385
+ }
1386
+ var normalized = (0, _medusaCoreUtils.humanizeAmount)(amount, currency);
1387
+ return normalized.toFixed(_medusaCoreUtils.zeroDecimalCurrencies.includes(currency.toLowerCase()) ? 0 : 2);
1388
+ }
1389
+ }, {
1390
+ key: "normalizeThumbUrl_",
1391
+ value: function normalizeThumbUrl_(url) {
1392
+ if (!url) {
1393
+ return null;
1394
+ }
1395
+ if (url.startsWith("http")) {
1396
+ return url;
1397
+ } else if (url.startsWith("//")) {
1398
+ return "https:".concat(url);
1399
+ }
1400
+ return url;
1401
+ }
1402
+ }, {
1403
+ key: "extractLocale",
1404
+ value: function () {
1405
+ var _extractLocale = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee23(fromOrder) {
1406
+ var cart;
1407
+ return _regeneratorRuntime().wrap(function _callee23$(_context23) {
1408
+ while (1) switch (_context23.prev = _context23.next) {
1409
+ case 0:
1410
+ if (!fromOrder.cart_id) {
1411
+ _context23.next = 14;
1412
+ break;
1413
+ }
1414
+ _context23.prev = 1;
1415
+ _context23.next = 4;
1416
+ return this.cartService_.retrieve(fromOrder.cart_id, {
1417
+ select: ["id", "context"]
1418
+ });
1419
+ case 4:
1420
+ cart = _context23.sent;
1421
+ if (!(cart.context && cart.context.locale)) {
1422
+ _context23.next = 7;
1423
+ break;
1424
+ }
1425
+ return _context23.abrupt("return", cart.context.locale);
1426
+ case 7:
1427
+ _context23.next = 14;
1428
+ break;
1429
+ case 9:
1430
+ _context23.prev = 9;
1431
+ _context23.t0 = _context23["catch"](1);
1432
+ console.log(_context23.t0);
1433
+ console.warn("Failed to gather context for order");
1434
+ return _context23.abrupt("return", null);
1435
+ case 14:
1436
+ return _context23.abrupt("return", null);
1437
+ case 15:
1438
+ case "end":
1439
+ return _context23.stop();
1440
+ }
1441
+ }, _callee23, this, [[1, 9]]);
1442
+ }));
1443
+ function extractLocale(_x32) {
1444
+ return _extractLocale.apply(this, arguments);
1445
+ }
1446
+ return extractLocale;
1447
+ }()
1448
+ }]);
1449
+ return SESService;
1450
+ }(_medusaInterfaces.NotificationService);
1451
+ _defineProperty(SESService, "identifier", "ses");
1452
+ var _default = SESService;
1453
+ exports["default"] = _default;