-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
163 lines (133 loc) · 3.99 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
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const fs = require('fs')
const pdf = require('html-pdf')
const bodyParser = require('body-parser')
const ejs = require('ejs')
const _ = require('lodash')
const moment = require('moment')
// 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', (req, res) => {
const template = ejs.compile(agreement)
req.body.date = moment().format('MMMM Do, YYYY')
createDocument(template(req.body), (pdfAgreement) => {
req.body.agreement = pdfAgreement
res.render('success', _.merge(viewData, req.body))
sendEmails(req.body)
})
})
/**
* Generate example agreement
*/
app.get('/example.pdf', (req, res) => {
const template = ejs.compile(agreement)
const data = viewData.exampleData
data.date = moment().format('MMMM Do, YYYY')
createDocument(template(data), (pdf) => {
res.contentType("application/pdf");
res.end(pdf, 'base64');
})
})
/**
* 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': `${data.company}_${data.date}.pdf`,
'ContentType': 'application/pdf'
}
// Send email to customer
client.sendEmail({
'From': process.env.POSTMARK_FROM_ADDRESS,
'To': data.email,
'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
*/
function createDocument(content, callback) {
/* https://www.npmjs.com/package/html-pdf for more PDF options */
const options = {
border: '1in'
}
pdf.create(content, options).toBuffer((err, buffer) => {
callback(buffer.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')
}
}