-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
47 lines (40 loc) · 1.53 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const Joi = require('joi');
const app = express();
const validator = require('express-joi-validation').createValidator({});
const sesClient = require('./ses-client');
const Email = require('email-templates');
var dateFormat = require('dateformat');
var now = new Date();
const formattedDate = dateFormat(now, "d mmmm yyyy");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send("Email service is up.")
});
const customerOrderEmailSchema = Joi.object({
recipientEmail: Joi.string().email().required(),
customerName: Joi.string().required(),
orderCode: Joi.string().required(),
businessName: Joi.string().required(),
businessAddress: Joi.string().required(),
businessPhone: Joi.string().required(),
items: Joi.array().items({
name: Joi.string().required(),
quantity: Joi.number().required(),
price: Joi.number().required()
})
})
// order verification email to be sent to customers
app.post('/orders/customer', validator.body(customerOrderEmailSchema), (req, res) => {
const email = new Email();
email.render('orders/customer/html', {...req.body, formattedDate})
.then((html) => {
sesClient.sendEmail(req.body.recipientEmail, `Your order from ${req.body.businessName} (#${req.body.orderCode})`, html);
res.send('Email sent');
});
});
app.listen(3000, () => {
console.log('Email service is listening on port 3000');
});