-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathupdate_transactions.js
More file actions
113 lines (101 loc) · 3.76 KB
/
Copy pathupdate_transactions.js
File metadata and controls
113 lines (101 loc) · 3.76 KB
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
/**
* @file Defines helpers for updating transactions on an item
*/
const plaid = require('./plaid');
const {
retrieveItemByPlaidItemId,
createAccounts,
createOrUpdateTransactions,
deleteTransactions,
updateItemTransactionsCursor,
} = require('./db/queries');
/**
* Fetches transactions from the Plaid API for a given item.
*
* @param {string} plaidItemId the Plaid ID for the item.
* @returns {Object{}} an object containing transactions and a cursor.
*/
const fetchTransactionUpdates = async (plaidItemId) => {
// the transactions endpoint is paginated, so we may need to hit it multiple times to
// retrieve all available transactions.
// get the access token based on the plaid item id
const {
plaid_access_token: accessToken,
transactions_cursor: lastCursor,
} = await retrieveItemByPlaidItemId(
plaidItemId
);
let cursor = lastCursor;
// New transaction updates since "cursor"
let added = [];
let modified = [];
// Removed transaction ids
let removed = [];
let hasMore = true;
const batchSize = 100;
// A /transactions/sync response is a diff (added/modified/removed) tied to the
// cursor we sent. The whole diff plus the cursor advance is one atomic unit: it
// must be applied all-or-nothing. We therefore accumulate every page here before
// returning, and if any page fails we let the error propagate instead of
// swallowing it. That leaves the caller with nothing to persist and the cursor
// unadvanced, so the next sync (or Plaid's webhook retry) resumes cleanly from
// lastCursor. If we instead persisted a partial page and advanced the cursor,
// the un-fetched transactions would be skipped forever, since the cursor would
// have moved past them.
/* eslint-disable no-await-in-loop */
while (hasMore) {
const request = {
access_token: accessToken,
cursor: cursor,
count: batchSize,
};
const response = await plaid.transactionsSync(request);
const data = response.data;
// Add this page of results
added = added.concat(data.added);
modified = modified.concat(data.modified);
removed = removed.concat(data.removed);
hasMore = data.has_more;
// Update cursor to the next cursor
cursor = data.next_cursor;
}
/* eslint-enable no-await-in-loop */
return { added, modified, removed, cursor, accessToken };
};
/**
* Handles the fetching and storing of new, modified, or removed transactions
*
* @param {string} plaidItemId the Plaid ID for the item.
*/
const updateTransactions = async (plaidItemId) => {
// Fetch new transactions from plaid api.
const {
added,
modified,
removed,
cursor,
accessToken
} = await fetchTransactionUpdates(plaidItemId);
const request = {
access_token: accessToken,
};
const {data: {accounts}} = await plaid.accountsGet(request);
// Apply the full diff and only then advance the cursor. The cursor must move
// last so it is never advanced past changes we haven't stored: if any write
// below throws, the cursor stays put and the next sync replays this same diff
// (the writes are idempotent — upserts keyed on plaid IDs and delete-by-id).
// In production you'd wrap these four writes plus the cursor update in a single
// DB transaction so the diff and cursor commit atomically; we keep them as
// separate statements here for readability in the sample.
await createAccounts(plaidItemId, accounts);
await createOrUpdateTransactions(added.concat(modified));
// Extract transaction IDs from removed objects
await deleteTransactions(removed.map(r => r.transaction_id));
await updateItemTransactionsCursor(plaidItemId, cursor);
return {
addedCount: added.length,
modifiedCount: modified.length,
removedCount: removed.length,
};
};
module.exports = updateTransactions;