-
Notifications
You must be signed in to change notification settings - Fork 225
/
users.js
151 lines (137 loc) · 3.64 KB
/
users.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
/**
* @file Defines all routes for the Users route.
*/
const express = require('express');
const Boom = require('@hapi/boom');
const {
retrieveUsers,
retrieveUserByUsername,
retrieveAccountsByUserId,
createUser,
deleteUsers,
retrieveItemsByUser,
retrieveTransactionsByUserId,
retrieveUserById,
} = require('../db/queries');
const { asyncWrapper } = require('../middleware');
const {
sanitizeAccounts,
sanitizeItems,
sanitizeUsers,
sanitizeTransactions,
} = require('../util');
const router = express.Router();
const plaid = require('../plaid');
/**
* Retrieves all users.
*
* @returns {Object[]} an array of users.
*/
router.get(
'/',
asyncWrapper(async (req, res) => {
const users = await retrieveUsers();
res.json(sanitizeUsers(users));
})
);
/**
* Creates a new user (unless the username is already taken).
*
* @TODO make this return an array for consistency.
*
* @param {string} username the username of the new user.
* @returns {Object[]} an array containing the new user.
*/
router.post(
'/',
asyncWrapper(async (req, res) => {
const { username } = req.body;
const usernameExists = await retrieveUserByUsername(username);
// prevent duplicates
if (usernameExists)
throw new Boom('Username already exists', { statusCode: 409 });
const newUser = await createUser(username);
res.json(sanitizeUsers(newUser));
})
);
/**
* Retrieves user information for a single user.
*
* @param {string} userId the ID of the user.
* @returns {Object[]} an array containing a single user.
*/
router.get(
'/:userId',
asyncWrapper(async (req, res) => {
const { userId } = req.params;
const user = await retrieveUserById(userId);
res.json(sanitizeUsers(user));
})
);
/**
* Retrieves all items associated with a single user.
*
* @param {string} userId the ID of the user.
* @returns {Object[]} an array of items.
*/
router.get(
'/:userId/items',
asyncWrapper(async (req, res) => {
const { userId } = req.params;
const items = await retrieveItemsByUser(userId);
res.json(sanitizeItems(items));
})
);
/**
* Retrieves all accounts associated with a single user.
*
* @param {string} userId the ID of the user.
* @returns {Object[]} an array of accounts.
*/
router.get(
'/:userId/accounts',
asyncWrapper(async (req, res) => {
const { userId } = req.params;
const accounts = await retrieveAccountsByUserId(userId);
res.json(sanitizeAccounts(accounts));
})
);
/**
* Retrieves all transactions associated with a single user.
*
* @param {string} userId the ID of the user.
* @returns {Object[]} an array of transactions
*/
router.get(
'/:userId/transactions',
asyncWrapper(async (req, res) => {
const { userId } = req.params;
const transactions = await retrieveTransactionsByUserId(userId);
res.json(sanitizeTransactions(transactions));
})
);
/**
* Deletes a user and its related items
*
* @param {string} userId the ID of the user.
*/
router.delete(
'/:userId',
asyncWrapper(async (req, res) => {
const { userId } = req.params;
// removes all items from Plaid services associated with the user. Once removed, the access_token
// associated with an Item is no longer valid and cannot be used to
// access any data that was associated with the Item.
// @TODO wrap promise in a try catch block once proper error handling introduced
const items = await retrieveItemsByUser(userId);
await Promise.all(
items.map(({ plaid_access_token: token }) =>
plaid.itemRemove({ access_token: token })
)
);
// delete from the db
await deleteUsers(userId);
res.sendStatus(204);
})
);
module.exports = router;