This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhandleTransferWebhook.js
140 lines (127 loc) · 4.23 KB
/
handleTransferWebhook.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
/**
* @file Defines the handler for Transfers webhooks.
* https://plaid.com/docs/transfer/webhooks/
*/
const {
createEvent,
retrieveEvents,
retrieveTransferByPlaidTransferId,
updateTransferStatus,
retrieveAllTransfers,
retrieveAppStatus,
updateAppStatus,
} = require('../db/queries');
const plaid = require('../plaid');
/**
* Handles all Transfers webhook events.
*
* @param {Object} requestBody the request body of an incoming webhook event.
* @param {Object} io a socket.io server instance.
*/
const transfersHandler = async (requestBody, io) => {
const { webhook_code: webhookCode } = requestBody;
const serverLogAndEmitSocket = async (webhookCode, status) => {
try {
if (status === 'no events') {
await io.emit('NO_NEW_EVENTS');
} else {
console.log(
`WEBHOOK: TRANSFERS: ${webhookCode}: transfer webhook received}`
);
// use websocket to notify the client that a webhook has been received and handled
if (webhookCode != null) {
try {
await io.emit(webhookCode);
} catch (err) {
console.log('error', err);
}
}
}
} catch (err) {
console.log(err);
}
};
switch (webhookCode) {
case 'TRANSFER_EVENTS_UPDATE': {
try {
const appStatus = await retrieveAppStatus();
let afterId = appStatus[0].number_of_events;
let hasEventsToFetch = true;
const batchSize = 25; // 25 is the maximum number of events returned
let allNewPlaidEvents = [];
while (hasEventsToFetch) {
const sycnRequest = {
after_id: afterId,
count: batchSize,
};
const syncResponse = await plaid.transferEventSync(sycnRequest);
allNewPlaidEvents = allNewPlaidEvents.concat(
syncResponse.data.transfer_events
);
if (syncResponse.data.transfer_events.length === batchSize) {
afterId += syncResponse.data.transfer_events.length;
} else {
hasEventsToFetch = false;
}
}
if (allNewPlaidEvents.length === 0) {
console.log('no new events');
await serverLogAndEmitSocket(webhookCode, 'no events');
break;
}
// to update app's business checking account balance, track sweep amount totals from new events
let newSweepAmountToAdd = 0;
const newEventsAddedToDatabase = allNewPlaidEvents.map(async event => {
const transfer = await retrieveTransferByPlaidTransferId(
event.transfer_id
);
if (transfer != null) {
const newEvent = await createEvent(
event.event_id,
transfer.user_id,
event.account_id,
event.transfer_id,
event.transfer_type,
event.event_type,
event.transfer_amount,
event.sweep_amount,
event.sweep_id,
event.failure_reason,
event.timepstamp
);
if (event.sweep_amount != null) {
newSweepAmountToAdd += Number(event.sweep_amount);
}
const transferGetResponse = await plaid.transferGet({
transfer_id: newEvent.transfer_id,
});
await updateTransferStatus(
transferGetResponse.data.transfer.status,
transferGetResponse.data.transfer.sweep_status,
newEvent.transfer_id
);
return newEvent;
}
});
await Promise.all(newEventsAddedToDatabase);
const oldAccountBalance = appStatus[0].app_account_balance;
const newAccountBalance = oldAccountBalance + newSweepAmountToAdd;
const newNumberOfEvents =
appStatus[0].number_of_events + allNewPlaidEvents.length;
await updateAppStatus(newAccountBalance, newNumberOfEvents);
const newStatus = await retrieveAppStatus();
await serverLogAndEmitSocket(webhookCode, null);
break;
} catch (err) {
console.log(err);
}
}
default:
serverLogAndEmitSocket(
'unhandled webhook type received.',
plaidItemId,
error
);
}
};
module.exports = transfersHandler;