-
Notifications
You must be signed in to change notification settings - Fork 0
/
giftEmails.js
69 lines (66 loc) · 2.5 KB
/
giftEmails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
/* globals SSR */
import { i18n } from '/imports/utils/i18n';
import AdmZip from 'adm-zip';
import { APP_NAME } from '/imports/constants/appNames';
import invoicePdfI18n from '/imports/i18n/nedatlujPlus/invoicePdf';
import giftsVoucherI18n from '/imports/i18n/gifts/giftsVoucher.js';
import routesI18n from '/imports/constants/routes/routes';
import commonI18n from '/imports/i18n/common';
import commonBundleI18n from '/imports/i18n/commonBundle';
import transactionalEmailsI18n from '/imports/i18n/transactionalEmails';
import { createPdfBuffer } from '/imports/api/gopay/emails/emailUtils';
import { Gifts } from '/imports/api/gifts/gifts';
/**
* compiler and render gift voucher HTML
* exported for dev server route
*/
export function createGiftVoucherHtml(gift) {
const gopayVoucherTemplate = 'giftsVoucher';
SSR.compileTemplate(gopayVoucherTemplate, Assets.getText(`${gopayVoucherTemplate}.html`));
Template[gopayVoucherTemplate].onCreated(function () {
this.invoicePdfI18n = invoicePdfI18n;
this.giftsVoucherI18n = giftsVoucherI18n;
this.routesI18n = routesI18n;
this.commonI18n = commonI18n;
this.commonBundleI18n = commonBundleI18n;
});
return SSR.render(gopayVoucherTemplate, {
giftCode: gift.giftCode,
gopayId: gift.gopayId,
appName: APP_NAME.toLowerCase(),
});
}
/**
* render PDF of gift voucher from HTML template
*/
function createGiftVoucherPdf(gift) {
const giftVoucherHtml = createGiftVoucherHtml(gift);
return createPdfBuffer(giftVoucherHtml);
}
/**
* create voucher file from rendered html template
* create pdf file or zip archive in case of multiple vouchers
*
* @param selector {Object} find by property `paymentId` or `giftCode`
*/
export async function createVoucherFile(selector) {
const gifts = Gifts.find(selector).fetch();
let fileBuffer;
if (gifts.length > 1) {
const zip = new AdmZip();
const promises = [];
for (const gift of gifts) {
promises.push(createGiftVoucherPdf(gift));
}
const giftVouchers = await Promise.all(promises);
for (const gift of gifts) {
const t9n = i18n('transactionalEmails.paymentConfirmation.gift.filename', transactionalEmailsI18n);
const filename = `${t9n}-${APP_NAME}-${gift.giftCode}.pdf`;
zip.addFile(filename, giftVouchers.shift());
}
fileBuffer = zip.toBuffer();
} else {
fileBuffer = await createGiftVoucherPdf(gifts[0]);
}
return fileBuffer;
}