This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (137 loc) · 3.62 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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const awsServerlessExpress = require('aws-serverless-express');
const app = express();
const server = awsServerlessExpress.createServer(app)
app.get('/test', async (req, res, next) => {
res.status(200).send('Success!')
})
const port = process.env.PORT || 3000;
const cors = require('cors');
const jwt = require('jsonwebtoken');
const gateways = {
smsGateway: require('./gateways/ClickSendGateway'),
dbGateway: require('./gateways/PostgresGateway')
};
const {
sendMessage,
receiveMessage,
listMessages,
createContact,
listContacts,
getContact,
createUserAndSendMessage
} = require('./use-cases')(gateways);
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
// had to rewrite the path to get it playing nice with a not-root resource in api gateway
req.url = req.url.replace('/hn-collab-casework', '');
next();
});
// Middleware to check for a valid JWT token
app.use(function(req, res, next) {
try {
if (req.method !== 'OPTIONS') {
let token =
req.query.apikey || req.headers.authorization.replace('Bearer ', '');
jwt.verify(token, process.env.JWT_SECRET);
//need to put logging in here
// pass along the username and email
const decoded = jwt.decode(token);
res.locals.user = {
username: decoded.name,
email: decoded.email
};
}
next();
} catch (err) {
console.log(`full error is ${JSON.stringify(err)}`)
console.log('Invalid JWT Token');
res.sendStatus(403);
}
});
app.post('/contacts', async (req, res) => {
try {
let result;
result = await createContact(
req.body.name,
req.body.number,
req.body.jigsawId
);
res.redirect(`/contacts/${result.id}`);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.get('/contacts/:id', async (req, res) => {
try {
let contact = await getContact(req.params.id);
res.send(contact);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.get('/contacts', async (req, res) => {
//console.log('getting contacts');
try {
let contacts = await listContacts();
res.send(contacts);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.post('/contacts/:id/messages', async (req, res) => {
try {
let response = await sendMessage(
req.params.id,
req.body.message,
res.locals.user
);
res.send(response);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.post('/receive_message', async (req, res) => {
try {
await receiveMessage(req.body.from, req.body.message);
res.status(200).send();
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.post('/messages', async (req, res) => {
try {
console.log(`---- attempting to send message from user ${JSON.stringify(res.locals.user)}`)
await createUserAndSendMessage(
req.body.number,
req.body.name,
req.body.message,
res.locals.user
);
res.status(200).send();
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.get('/contacts/:id/messages', async (req, res) => {
try {
// Fetch the messages
const messages = await listMessages(req.params.id);
res.send(messages);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
//app.listen(port, () => console.log(`App listening on port ${port}!`));
module.exports.universal = (event, context) => awsServerlessExpress.proxy(server, event, context);