Skip to content

Commit ffd0b54

Browse files
author
dmlenton
committed
Adds support for new contact API
For handling DYW contact form submissions
1 parent dadbde4 commit ffd0b54

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const apiWorkspaceRoute = require('./routes/api/workspace');
9595

9696
// DYW only
9797
const apiCoachingRoute = require('./routes/api/coaching')(app);
98+
const apiContactRoute = require('./routes/api/contact')(app);
9899

99100
// View engine setup
100101
app.set('views', path.join(__dirname, 'views'));
@@ -853,6 +854,7 @@ app.use('/api/site', apiSiteRoute);
853854

854855
// Coaching
855856
app.use('/api/coaching', apiCoachingRoute);
857+
app.use('/api/contact', apiContactRoute);
856858

857859
// Ensure users are authenticated before making any of the following API calls
858860
app.use(isAuthenticated);

routes/api/contact.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const express = require('express');
2+
const Site = require('../../models/site');
3+
const { sendMail } = require('../../models/mail');
4+
const { JsonInfo } = require('../../utils');
5+
const router = express.Router();
6+
7+
module.exports = function () {
8+
router.post('/', (req, res, next) => {
9+
const body = req.body;
10+
try {
11+
const name = body['name'];
12+
const subject = body['subject'];
13+
const email = body['email'];
14+
const message = body['message'];
15+
const personal_template_id = 'd-ef38ad57230d4d5296f7e6b3d60ffc4c';
16+
const site = new Site();
17+
site.server.client_id = req.site.server.client_id;
18+
site.loadAddress((err, address) => {
19+
if (err || address === null) return next(new Error('Unable to find site details'));
20+
const msg = {
21+
to: req.site.content.email,
22+
from: req.site.content.email,
23+
templateId: personal_template_id,
24+
dynamic_template_data: {
25+
name: name,
26+
email: email,
27+
title: subject,
28+
message: message,
29+
Sender_Name: address.name,
30+
Sender_Address: address.address1,
31+
Sender_City: address.town,
32+
Sender_State: address.province,
33+
Sender_Zip: address.postcode
34+
}
35+
};
36+
sendMail(msg, req.hostname);
37+
return res.json(new JsonInfo('Done', 200));
38+
});
39+
}
40+
catch(err) {
41+
console.log(err);
42+
return next(err);
43+
}
44+
});
45+
return router;
46+
};

0 commit comments

Comments
 (0)