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