-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathitems.js
207 lines (192 loc) · 5.72 KB
/
items.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
/**
* @file Defines all routes for the Items route.
*/
const express = require('express');
const Boom = require('@hapi/boom');
const {
retrieveItemById,
retrieveItemByPlaidInstitutionId,
retrieveAccountsByItemId,
retrieveTransactionsByItemId,
createItem,
deleteItem,
updateItemStatus,
} = require('../db/queries');
const { asyncWrapper } = require('../middleware');
const plaid = require('../plaid');
const {
sanitizeAccounts,
sanitizeItems,
sanitizeTransactions,
isValidItemStatus,
validItemStatuses,
} = require('../util');
const updateTransactions = require('../update_transactions');
const router = express.Router();
/**
* First exchanges a public token for a private token via the Plaid API
* and then stores the newly created item in the DB.
*
* @param {string} publicToken public token returned from the onSuccess call back in Link.
* @param {string} institutionId the Plaid institution ID of the new item.
* @param {string} userId the Plaid user ID of the active user.
*/
router.post(
'/',
asyncWrapper(async (req, res) => {
const { publicToken, institutionId, userId } = req.body;
// prevent duplicate items for the same institution per user.
const existingItem = await retrieveItemByPlaidInstitutionId(
institutionId,
userId
);
if (existingItem)
throw new Boom('You have already linked an item at this institution.', {
statusCode: 409,
});
// exchange the public token for a private access token and store with the item.
const response = await plaid.itemPublicTokenExchange({
public_token: publicToken,
});
const accessToken = response.data.access_token;
const itemId = response.data.item_id;
const newItem = await createItem(
institutionId,
accessToken,
itemId,
userId
);
// Make an initial call to fetch transactions and enable SYNC_UPDATES_AVAILABLE webhook sending
// for this item.
updateTransactions(itemId).then(() => {
// Notify frontend to reflect any transactions changes.
req.io.emit('NEW_TRANSACTIONS_DATA', { itemId: newItem.id });
});
res.json(sanitizeItems(newItem));
})
);
/**
* Retrieves a single item.
*
* @param {string} itemId the ID of the item.
* @returns {Object[]} an array containing a single item.
*/
router.get(
'/:itemId',
asyncWrapper(async (req, res) => {
const { itemId } = req.params;
const item = await retrieveItemById(itemId);
res.json(sanitizeItems(item));
})
);
/**
* Updates a single item.
*
* @param {string} itemId the ID of the item.
* @returns {Object[]} an array containing a single item.
*/
router.put(
'/:itemId',
asyncWrapper(async (req, res) => {
const { itemId } = req.params;
const { status } = req.body;
if (status) {
if (!isValidItemStatus(status)) {
throw new Boom(
'Cannot set item status. Please use an accepted value.',
{
statusCode: 400,
acceptedValues: [validItemStatuses.values()],
}
);
}
await updateItemStatus(itemId, status);
const item = await retrieveItemById(itemId);
res.json(sanitizeItems(item));
} else {
throw new Boom('You must provide updated item information.', {
statusCode: 400,
acceptedKeys: ['status'],
});
}
})
);
/**
* Deletes a single item and related accounts and transactions.
* Also removes the item from the Plaid API
* access_token associated with the Item is no longer valid
* https://plaid.com/docs/#remove-item-request
* @param {string} itemId the ID of the item.
* @returns status of 204 if successful
*/
router.delete(
'/:itemId',
asyncWrapper(async (req, res) => {
const { itemId } = req.params;
const { plaid_access_token: accessToken } = await retrieveItemById(itemId);
/* eslint-disable camelcase */
try {
const response = await plaid.itemRemove({
access_token: accessToken,
});
const removed = response.data.removed;
const status_code = response.data.status_code;
} catch (error) {
if (!removed)
throw new Boom('Item could not be removed in the Plaid API.', {
statusCode: status_code,
});
}
await deleteItem(itemId);
res.sendStatus(204);
})
);
/**
* Retrieves all accounts associated with a single item.
*
* @param {string} itemId the ID of the item.
* @returns {Object[]} an array of accounts.
*/
router.get(
'/:itemId/accounts',
asyncWrapper(async (req, res) => {
const { itemId } = req.params;
const accounts = await retrieveAccountsByItemId(itemId);
res.json(sanitizeAccounts(accounts));
})
);
/**
* Retrieves all transactions associated with a single item.
*
* @param {string} itemId the ID of the item.
* @returns {Object[]} an array of transactions.
*/
router.get(
'/:itemId/transactions',
asyncWrapper(async (req, res) => {
const { itemId } = req.params;
const transactions = await retrieveTransactionsByItemId(itemId);
res.json(sanitizeTransactions(transactions));
})
);
/**
* -- This endpoint will only work in the sandbox enviornment --
* Forces an Item into an ITEM_LOGIN_REQUIRED (bad) error state.
* An ITEM_LOGIN_REQUIRED webhook will be fired after a call to this endpoint.
* https://plaid.com/docs/#managing-item-states
*
* @param {string} itemId the Plaid ID of the item.
* @return {Object} the response from the Plaid API.
*/
router.post(
'/sandbox/item/reset_login',
asyncWrapper(async (req, res) => {
const { itemId } = req.body;
const { plaid_access_token: accessToken } = await retrieveItemById(itemId);
const resetResponse = await plaid.sandboxItemResetLogin({
access_token: accessToken,
});
res.json(resetResponse.data);
})
);
module.exports = router;