-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (175 loc) · 5.35 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const fs = require('fs')
const { chromium } = require('@playwright/test')
const bodyParser = require('body-parser')
const ejs = require('ejs')
const _ = require('lodash')
const moment = require('moment')
const axios = require('axios');
// Cache agreement template
const agreement = fs.readFileSync(`${__dirname}/views/agreement.ejs`, 'utf8')
// Cache email subjects and content
const emailContentInternal = ejs.compile(fs.readFileSync(`${__dirname}/emails/internal.ejs`, 'utf8'))
const emailContentSignee = ejs.compile(fs.readFileSync(`${__dirname}/emails/signee.ejs`, 'utf8'))
const signeeSubject = ejs.compile(process.env.SIGNEE_EMAIL_SUBJECT || '')
const internalSubject = ejs.compile(process.env.INTERNAL_EMAIL_SUBJECT || '')
const examples = JSON.parse(fs.readFileSync(`${__dirname}/examples.json`, 'utf8'))
validateConfig()
const postmark = require('postmark')
const client = new postmark.Client(process.env.POSTMARK_SERVER_TOKEN)
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
const viewData = {
title: process.env.TITLE || '',
exampleData: examples
}
/**
* Index express route
*/
app.get('/', (req, res) => {
res.render('index', viewData)
})
/**
* Sign document express route
*/
app.post('/sign', async (req, res) => {
const template = ejs.compile(agreement)
req.body.date = moment().format('MMMM Do, YYYY')
const pdfAgreement = await createDocument(template(req.body))
req.body.agreement = pdfAgreement
res.render('success', _.merge(viewData, req.body))
sendEmails(req.body)
const fmtBody = {
participant: {
name: req.body.name,
email: req.body.email,
phone: req.body.phone,
dob: req.body.dob,
initials: req.body.initials,
signature: req.body.signature
},
parent: {
name: req.body.parentName,
email: req.body.parentEmail,
phone: req.body.parentPhone,
signature: req.body.parentSignature,
},
address: {
street: req.body.address,
city: req.body.city,
state: req.body.state,
zip: req.body.zip
},
date: req.body.date,
agreement: req.body.agreement
}
await axios.post("https://high-weasel-42.deno.dev", fmtBody)
})
/**
* Generate example agreement
*/
app.get('/example.pdf', async (req, res) => {
const template = ejs.compile(agreement)
const data = viewData.exampleData
data.date = moment().format('MMMM Do, YYYY')
const pdf = await createDocument(template(data))
res.contentType("application/pdf");
res.end(pdf, 'base64');
})
app.get('/example.html', async (req, res) => {
const template = ejs.compile(agreement)
const data = viewData.exampleData
data.date = moment().format('MMMM Do, YYYY')
res.end(template(data))
})
/**
* Start express server
*/
app.listen(process.env.PORT || 3000, () => console.log('PactMaker is up and running!'))
/**
* Send emails to the signee and internal team
* @param {Object} data Request body data
*/
function sendEmails(data) {
const attachment = {
'Content': data.agreement,
'Name': `Kigali Hacks Liability Waiver_${data.date}.pdf`,
'ContentType': 'application/pdf'
}
// Send email to customer
client.sendEmail({
'From': process.env.POSTMARK_FROM_ADDRESS,
'To': `${data.email}, ${data.parentEmail}`,
'Subject': signeeSubject(data),
'HtmlBody': emailContentSignee(data),
'Attachments': [attachment]
}, (err, results) => {
if (err) {
console.error(err)
return
}
console.log('Email sent:')
console.log(results)
})
// Send email notification to internal team
if (process.env.INTERNAL_EMAIL_RECIPIENTS) {
const internalRecipients = process.env.INTERNAL_EMAIL_RECIPIENTS.split(',')
internalRecipients.forEach((email) => {
client.sendEmail({
'From': process.env.POSTMARK_FROM_ADDRESS,
'To': email,
'Subject': internalSubject(data),
'HtmlBody': emailContentInternal(data),
'Attachments': [attachment]
}, (err, results) => {
if (err) {
console.error(err)
return
}
console.log('Email sent:')
console.log(results)
})
})
}
}
/**
* Create PDF document
* @param {Object} content HTMl content content
* @param {Function} callback Callback containing the encoded PDF buffer
*/
async function createDocument(content, callback) {
let dataUrl = "data:text/html;base64," + btoa(unescape(encodeURIComponent(content)))
const browser = await chromium.launch({
headless: true
});
const context = await browser.newContext();
// Create a page.
const page = await context.newPage();
await page.goto(dataUrl)
const pdf = await page.pdf({
format: "letter",
landscape: false,
margin: {
top: "48px",
bottom: "48px",
left: "48px",
right: "48px"
}
})
return pdf.toString('base64');
}
/**
* Validate heroku config
*/
function validateConfig() {
if (!process.env.POSTMARK_FROM_ADDRESS) {
throw Error('No From address specified in config')
}
if (!process.env.POSTMARK_SERVER_TOKEN) {
throw Error('No Postmark server token specified in config')
}
}