1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 |
|
13 |
|
14 |
|
15 | const moment = require("moment-timezone");
|
16 | const pug = require("pug");
|
17 | const util = require("util");
|
18 | const factory = require("./factory");
|
19 | const templateDirectory = `${__dirname}/../emails`;
|
20 | const DEFAULT_SENDER_EMAIL = 'noreply@example.com';
|
21 | function createEmailMessageText(params) {
|
22 | return __awaiter(this, void 0, void 0, function* () {
|
23 | const emailTemplate = (params.email !== undefined) ? params.email.template : undefined;
|
24 | const emailText = (params.email !== undefined) ? params.email.text : undefined;
|
25 | let emailMessageText;
|
26 | if (typeof emailText === 'string') {
|
27 | emailMessageText = emailText;
|
28 | }
|
29 | else if (typeof emailTemplate === 'string') {
|
30 | emailMessageText = yield new Promise((resolve, reject) => {
|
31 | pug.render(emailTemplate, {
|
32 | order: params.order
|
33 | }, (err, message) => {
|
34 | if (err instanceof Error) {
|
35 | reject(new factory.errors.Argument('emailTemplate', err.message));
|
36 | return;
|
37 | }
|
38 | resolve(message);
|
39 | });
|
40 | });
|
41 | }
|
42 | else {
|
43 | emailMessageText = yield new Promise((resolve, reject) => {
|
44 | pug.renderFile(params.renderFilePath, params.renderFileOptions, (err, message) => {
|
45 | if (err instanceof Error) {
|
46 | reject(err);
|
47 | return;
|
48 | }
|
49 | resolve(message);
|
50 | });
|
51 | });
|
52 | }
|
53 | return emailMessageText;
|
54 | });
|
55 | }
|
56 | function createEmailMessageAbout(params) {
|
57 | return __awaiter(this, void 0, void 0, function* () {
|
58 | let about;
|
59 | if (params.email !== undefined && typeof params.email.about === 'string') {
|
60 | about = params.email.about;
|
61 | }
|
62 | else {
|
63 | about = yield new Promise((resolve, reject) => {
|
64 | pug.renderFile(params.renderFilePath, params.renderFileOptions, (err, defaultSubject) => {
|
65 | if (err instanceof Error) {
|
66 | reject(err);
|
67 | return;
|
68 | }
|
69 | resolve(defaultSubject);
|
70 | });
|
71 | });
|
72 | }
|
73 | return about;
|
74 | });
|
75 | }
|
76 | function createEmailMessageSender(params) {
|
77 | return {
|
78 | typeOf: params.order.seller.typeOf,
|
79 | name: (params.email !== undefined
|
80 | && params.email.sender !== undefined
|
81 | && typeof params.email.sender.name === 'string')
|
82 | ? params.email.sender.name
|
83 | : params.order.seller.name,
|
84 | email: (params.email !== undefined
|
85 | && params.email.sender !== undefined
|
86 | && typeof params.email.sender.email === 'string')
|
87 | ? params.email.sender.email
|
88 | : DEFAULT_SENDER_EMAIL
|
89 | };
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 | function createSendOrderMessage(params) {
|
95 | return __awaiter(this, void 0, void 0, function* () {
|
96 | const emailMessageText = yield createEmailMessageText({
|
97 | order: params.order,
|
98 | email: params.email,
|
99 | renderFilePath: `${templateDirectory}/sendOrder/text.pug`,
|
100 | renderFileOptions: {
|
101 | order: params.order,
|
102 | orderItems: createOrderItems({ order: params.order })
|
103 | .join('\n')
|
104 | }
|
105 | });
|
106 | const about = yield createEmailMessageAbout({
|
107 | email: params.email,
|
108 | renderFilePath: `${templateDirectory}/sendOrder/subject.pug`,
|
109 | renderFileOptions: {
|
110 | sellerName: params.order.seller.name
|
111 | }
|
112 | });
|
113 | const defaultToRecipientEmail = params.order.customer.email;
|
114 | if (defaultToRecipientEmail === undefined) {
|
115 | throw new factory.errors.Argument('order', 'order.customer.email undefined');
|
116 | }
|
117 | const sender = createEmailMessageSender(params);
|
118 | const toRecipient = {
|
119 | typeOf: params.order.customer.typeOf,
|
120 | name: (params.email !== undefined
|
121 | && params.email.toRecipient !== undefined
|
122 | && typeof params.email.toRecipient.name === 'string')
|
123 | ? params.email.toRecipient.name
|
124 | : `${params.order.customer.familyName} ${params.order.customer.givenName}`,
|
125 | email: (params.email !== undefined
|
126 | && params.email.toRecipient !== undefined
|
127 | && typeof params.email.toRecipient.email === 'string')
|
128 | ? params.email.toRecipient.email
|
129 | : defaultToRecipientEmail
|
130 | };
|
131 | return {
|
132 | typeOf: factory.creativeWorkType.EmailMessage,
|
133 | identifier: `SendOrder-${params.order.orderNumber}`,
|
134 | name: `SendOrder-${params.order.orderNumber}`,
|
135 | sender: sender,
|
136 | toRecipient: toRecipient,
|
137 | about: about,
|
138 | text: emailMessageText
|
139 | };
|
140 | });
|
141 | }
|
142 | exports.createSendOrderMessage = createSendOrderMessage;
|
143 |
|
144 |
|
145 |
|
146 | function createOrderItems(params) {
|
147 | return params.order.acceptedOffers.map((o) => {
|
148 | var _a, _b, _c;
|
149 | if (o.itemOffered.typeOf === factory.chevre.reservationType.EventReservation) {
|
150 | const reservation = o.itemOffered;
|
151 | const event = reservation.reservationFor;
|
152 | const eventStartDate = util.format('%s - %s', moment(event.startDate)
|
153 | .toISOString(), moment(event.endDate)
|
154 | .toISOString());
|
155 | const locationName = util.format('%s %s%s', (_a = event.superEvent.location.name) === null || _a === void 0 ? void 0 : _a.ja, (_b = event.location.name) === null || _b === void 0 ? void 0 : _b.ja, (event.location.address !== undefined) ? `(${event.location.address.ja})` : '');
|
156 | let option = '';
|
157 | if (Array.isArray(reservation.reservationFor.superEvent.videoFormat)) {
|
158 | option += reservation.reservationFor.superEvent.videoFormat.map((format) => format.typeOf)
|
159 | .join(',');
|
160 | }
|
161 | let priceStr = '';
|
162 | let reservationPriceSpec;
|
163 | if (o.priceSpecification !== undefined && o.priceSpecification !== null) {
|
164 | if (typeof o.priceSpecification === 'number') {
|
165 |
|
166 | reservationPriceSpec = {
|
167 | project: { typeOf: params.order.project.typeOf, id: params.order.project.id },
|
168 | typeOf: factory.chevre.priceSpecificationType.CompoundPriceSpecification,
|
169 | priceCurrency: factory.chevre.priceCurrency.JPY,
|
170 | valueAddedTaxIncluded: true,
|
171 | priceComponent: [
|
172 | {
|
173 | project: { typeOf: params.order.project.typeOf, id: params.order.project.id },
|
174 | typeOf: factory.chevre.priceSpecificationType.UnitPriceSpecification,
|
175 | price: o.priceSpecification,
|
176 | priceCurrency: o.priceCurrency,
|
177 | valueAddedTaxIncluded: true
|
178 | }
|
179 | ]
|
180 | };
|
181 | }
|
182 | else {
|
183 | reservationPriceSpec = o.priceSpecification;
|
184 | }
|
185 | }
|
186 |
|
187 | if (reservationPriceSpec !== undefined) {
|
188 | const unitPriceSpec = reservationPriceSpec.priceComponent.find((spec) => spec.typeOf === factory.chevre.priceSpecificationType.UnitPriceSpecification);
|
189 | if (unitPriceSpec !== undefined) {
|
190 | priceStr = `${unitPriceSpec.price}/${unitPriceSpec.referenceQuantity.value}`;
|
191 | }
|
192 | }
|
193 | return util.format('%s %s @%s %s %s %s %s (%s)', event.name.ja, eventStartDate, locationName, (reservation.reservedTicket.ticketedSeat !== undefined)
|
194 | ? reservation.reservedTicket.ticketedSeat.seatNumber
|
195 | : 'Non-reserved Seat', (typeof reservation.reservedTicket.ticketType.name === 'string')
|
196 | ? reservation.reservedTicket.ticketType.name
|
197 | : (_c = reservation.reservedTicket.ticketType.name) === null || _c === void 0 ? void 0 : _c.ja, priceStr, o.priceCurrency, option);
|
198 | }
|
199 | else {
|
200 | return util.format('%s %s %s %s', o.itemOffered.typeOf, (typeof o.itemOffered.name === 'string') ? o.itemOffered.name : '', (typeof o.price === 'number') ? String(o.price) : '', o.priceCurrency);
|
201 | }
|
202 | });
|
203 | }
|
204 | exports.createOrderItems = createOrderItems;
|
205 |
|
206 |
|
207 |
|
208 | function createReturnOrderMessage(params) {
|
209 | return __awaiter(this, void 0, void 0, function* () {
|
210 | const emailMessageText = yield createEmailMessageText({
|
211 | order: params.order,
|
212 | email: params.email,
|
213 | renderFilePath: `${templateDirectory}/returnOrder/text.pug`,
|
214 | renderFileOptions: {
|
215 | order: params.order
|
216 | }
|
217 | });
|
218 | const about = yield createEmailMessageAbout({
|
219 | email: params.email,
|
220 | renderFilePath: `${templateDirectory}/returnOrder/subject.pug`,
|
221 | renderFileOptions: {
|
222 | sellerName: params.order.seller.name
|
223 | }
|
224 | });
|
225 | const defaultToRecipientEmail = params.order.customer.email;
|
226 | if (defaultToRecipientEmail === undefined) {
|
227 | throw new factory.errors.Argument('order', 'order.customer.email undefined');
|
228 | }
|
229 | const sender = createEmailMessageSender(params);
|
230 | const toRecipient = {
|
231 | typeOf: params.order.customer.typeOf,
|
232 | name: (params.email !== undefined
|
233 | && params.email.toRecipient !== undefined
|
234 | && typeof params.email.toRecipient.name === 'string')
|
235 | ? params.email.toRecipient.name
|
236 | : `${params.order.customer.familyName} ${params.order.customer.givenName}`,
|
237 | email: (params.email !== undefined
|
238 | && params.email.toRecipient !== undefined
|
239 | && typeof params.email.toRecipient.email === 'string')
|
240 | ? params.email.toRecipient.email
|
241 | : defaultToRecipientEmail
|
242 | };
|
243 | return {
|
244 | typeOf: factory.creativeWorkType.EmailMessage,
|
245 | identifier: `ReturnOrder-${params.order.orderNumber}`,
|
246 | name: `ReturnOrder-${params.order.orderNumber}`,
|
247 | sender: sender,
|
248 | toRecipient: toRecipient,
|
249 | about: about,
|
250 | text: emailMessageText
|
251 | };
|
252 | });
|
253 | }
|
254 | exports.createReturnOrderMessage = createReturnOrderMessage;
|
255 |
|
256 |
|
257 |
|
258 | function createRefundMessage(params) {
|
259 | return __awaiter(this, void 0, void 0, function* () {
|
260 | const emailMessageText = yield createEmailMessageText({
|
261 | order: params.order,
|
262 | email: params.email,
|
263 | renderFilePath: `${templateDirectory}/refundOrder/text.pug`,
|
264 | renderFileOptions: {
|
265 | order: params.order,
|
266 | paymentMethods: params.paymentMethods.map((p) => {
|
267 | return util.format('%s\n%s\n%s\n', p.typeOf, (p.accountId !== undefined) ? p.accountId : '', (p.totalPaymentDue !== undefined) ? `${p.totalPaymentDue.value} ${p.totalPaymentDue.currency}` : '');
|
268 | })
|
269 | .join('\n')
|
270 | }
|
271 | });
|
272 | const about = yield createEmailMessageAbout({
|
273 | email: params.email,
|
274 | renderFilePath: `${templateDirectory}/refundOrder/subject.pug`,
|
275 | renderFileOptions: {
|
276 | sellerName: params.order.seller.name
|
277 | }
|
278 | });
|
279 | const defaultToRecipientEmail = params.order.customer.email;
|
280 | if (defaultToRecipientEmail === undefined) {
|
281 | throw new factory.errors.Argument('order', 'order.customer.email undefined');
|
282 | }
|
283 | const sender = createEmailMessageSender(params);
|
284 | const toRecipient = {
|
285 | typeOf: params.order.customer.typeOf,
|
286 | name: (params.email !== undefined
|
287 | && params.email.toRecipient !== undefined
|
288 | && typeof params.email.toRecipient.name === 'string')
|
289 | ? params.email.toRecipient.name
|
290 | : `${params.order.customer.familyName} ${params.order.customer.givenName}`,
|
291 | email: (params.email !== undefined
|
292 | && params.email.toRecipient !== undefined
|
293 | && typeof params.email.toRecipient.email === 'string')
|
294 | ? params.email.toRecipient.email
|
295 | : defaultToRecipientEmail
|
296 | };
|
297 | return {
|
298 | typeOf: factory.creativeWorkType.EmailMessage,
|
299 | identifier: `RefundOrder-${params.order.orderNumber}`,
|
300 | name: `RefundOrder-${params.order.orderNumber}`,
|
301 | sender: sender,
|
302 | toRecipient: toRecipient,
|
303 | about: about,
|
304 | text: emailMessageText
|
305 | };
|
306 | });
|
307 | }
|
308 | exports.createRefundMessage = createRefundMessage;
|