forked from filiphric/trelloapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
254 lines (177 loc) · 6.5 KB
/
middleware.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const moment = require('moment');
const sendmail = require('sendmail')();
const path = require('path'); // used for file path
const fs = require('fs-extra');
const socket = require('socket.io-client')('http://localhost:3000');
function randomId() {
return Math.floor(100000 * Math.random() * 900000);
}
module.exports = (req, res, next) => {
const unauthorized = function () {
return res.status(403).jsonp({
error: 'User not authorized to access resource',
});
};
const userNotFound = function () {
return res.status(404).jsonp({
error: 'User not found',
});
};
const badRequest = function (param) {
return res.status(400).jsonp({
error: `Bad request. ${param} is required.`,
});
};
const parseJWT = function () {
if (req.headers.hasOwnProperty('authorization')) {
const base64Url = req.headers.authorization.split('.')[1];
const base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse(Buffer.from(base64, 'base64'));
}
return false;
};
const { db } = req.app;
const userData = parseJWT();
const userId = parseInt(userData.sub);
db.assign(require('require-uncached')('./public/data/data.json')).write();
// create board
if (req.method === 'POST' && req.path === '/boards') {
req.body.user = userId || 0;
req.body.id = randomId();
req.body.starred = false;
req.body.created = moment().format('YYYY-MM-DD');
socket.emit('boardCreated', req.body);
}
if (req.method === 'GET' && req.path === '/boards') {
const publicBoards = db.get('boards').filter({ user: 0 }).value();
const boards = db.get('boards').filter({ user: userId }).value();
const result = [ ...publicBoards, ...boards ];
const response = res.status(200).jsonp(result);
return response;
}
if (req.method === 'GET' && req.path.match(/\/boards\/\d*/g)) {
const id = parseInt(req.path.replace('/boards/', ''));
const board = db.get('boards').find({ id }).value();
const lists = db.get('lists').filter({ boardId: id }).sortBy('order').value();
const tasks = db.get('tasks').filter({ boardId: id }).sortBy('order').value();
const result = { ...board, lists, tasks };
const response = res.status(200).jsonp(result);
return response;
}
if (req.method === 'DELETE' && req.path.match(/\/boards\/\d*/g)) {
const id = parseInt(req.path.replace('/boards/', ''));
socket.emit('boardDeleted', id);
}
if (req.method === 'PATCH' && req.path.match(/\/boards\/\d*/g)) {
const id = parseInt(req.path.replace('/boards/', ''));
socket.emit('boardUpdate', id, req.body);
}
if (req.method === 'POST' && req.path === '/lists') {
// validation
if (req.body.boardId === undefined) return badRequest('boardId');
// data generation
req.body.id = randomId();
req.body.created = moment().format('YYYY-MM-DD');
// stream message
socket.emit('listCreated', req.body.boardId, req.body);
}
if (req.method === 'PATCH' && req.path.match(/\/lists\/\d*/g)) {
const id = parseInt(req.path.replace('/lists/', ''));
socket.emit('listUpdated', id, req.body);
}
if (req.method === 'DELETE' && req.path.match(/\/lists\/\d*/g)) {
const id = parseInt(req.path.replace('/lists/', ''));
socket.emit('listDeleted', id);
}
if (req.method === 'POST' && req.path === '/tasks') {
// validation
if (req.body.boardId === undefined) return badRequest('boardId');
if (req.body.listId === undefined) return badRequest('listId');
// data generation
req.body.id = randomId();
req.body.created = moment().format('YYYY-MM-DD');
req.body.deadline = moment().add(3, 'days').format('YYYY-MM-DD');
// stream message
socket.emit('taskCreated', req.body.listId, req.body);
}
if (req.method === 'PATCH' && req.path.match(/\/tasks\/\d*/g)) {
// stream message
const id = parseInt(req.path.replace('/tasks/', ''));
const task = db.get('tasks').find({ id }).value();
socket.emit('taskUpdated', id, { ...task, ...req.body });
}
if (req.method === 'DELETE' && req.path.match(/\/tasks\/\d*/g)) {
// stream message
const id = parseInt(req.path.replace('/tasks/', ''));
const task = db.get('tasks').find({ id }).value();
socket.emit('taskDeleted', id, { ...task, ...req.body });
}
if (req.method === 'POST' && req.path === '/upload') {
const name = req.headers.taskid;
let fstream;
req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => {
fstream = fs.createWriteStream(`${__dirname}/public/uploaded/${name}_${filename}`);
file.pipe(fstream);
fstream.on('close', () => {
res.status(201).jsonp({ path: `/public/uploaded/${name}_${filename}` });
});
});
return;
}
if (req.method === 'GET' && req.path === '/users') {
if (!userData) return unauthorized();
const user = db.get('users').find({ id: userId }).value();
const result = { user };
if (!user) return userNotFound();
const response = res.status(200).jsonp(result);
return response;
}
if (req.method === 'POST' && req.path === '/welcomeemail') {
// send welcome email if header is true
sendmail({
from: 'trelloapp@filiphric.sk',
to: req.body.email,
subject: 'Welcome to Trello app',
html: 'Your account was successfully created!\nIn the meantime, subscribe to my <a href="https://www.youtube.com/channel/UCDOCAVIhSh5VpJMEfdak1OA">YouTube channel for Cypress tips!</a>',
}, function(err, reply) {
console.log(err && err.stack);
console.dir(reply);
});
let response = res.status(201).jsonp(req.body);
return response;
}
// cleanup methods
if (req.method === 'POST' && req.path === '/reset') {
db
.setState({
boards: [],
tasks: [],
users: [],
lists: [],
})
.write();
socket.emit('boardsState', []);
return res.sendStatus(204);
}
if (req.method === 'DELETE' && req.path === '/boards') {
db.set('boards', []).write();
db.set('lists', []).write();
db.set('tasks', []).write();
return res.sendStatus(204);
}
if (req.method === 'DELETE' && req.path === '/lists') {
db.set('lists', []).write();
db.set('tasks', []).write();
return res.sendStatus(204);
}
if (req.method === 'DELETE' && req.path === '/tasks') {
db.set('tasks', []).write();
return res.sendStatus(204);
}
if (req.method === 'DELETE' && req.path === '/users') {
db.set('users', []).write();
return res.sendStatus(204);
}
next();
};