mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat(server): listen to Plaid webhooks
This commit is contained in:
@@ -45,14 +45,14 @@ export class PlaidApplication {
|
||||
*/
|
||||
public webhooks(
|
||||
tenantId: number,
|
||||
webhookType: string,
|
||||
plaidItemId: string,
|
||||
webhookType: string,
|
||||
webhookCode: string
|
||||
) {
|
||||
return this.plaidWebhooks.webhooks(
|
||||
tenantId,
|
||||
webhookType,
|
||||
plaidItemId,
|
||||
webhookType,
|
||||
webhookCode
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { PlaidClientWrapper } from '@/lib/Plaid';
|
||||
import { Service } from 'typedi';
|
||||
import config from '@/config';
|
||||
|
||||
@Service()
|
||||
export class PlaidLinkTokenService {
|
||||
@@ -11,7 +12,7 @@ export class PlaidLinkTokenService {
|
||||
async getLinkToken(tenantId: number) {
|
||||
const accessToken = null;
|
||||
|
||||
// must include transactions in order to receive transactions webhooks
|
||||
// Must include transactions in order to receive transactions webhooks
|
||||
const products = ['transactions'];
|
||||
const linkTokenParams = {
|
||||
user: {
|
||||
@@ -22,13 +23,9 @@ export class PlaidLinkTokenService {
|
||||
products,
|
||||
country_codes: ['US'],
|
||||
language: 'en',
|
||||
// webhook: httpTunnel.public_url + '/services/webhook',
|
||||
webhook: config.plaid.linkWebhook,
|
||||
access_token: accessToken,
|
||||
};
|
||||
// If user has entered a redirect uri in the .env file
|
||||
// if (redirect_uri.indexOf('http') === 0) {
|
||||
// linkTokenParams.redirect_uri = redirect_uri;
|
||||
// }
|
||||
const plaidInstance = new PlaidClientWrapper();
|
||||
const createResponse = await plaidInstance.linkTokenCreate(linkTokenParams);
|
||||
|
||||
|
||||
@@ -13,22 +13,24 @@ export class PlaidWebooks {
|
||||
* @param {string} plaidItemId
|
||||
* @param {string} webhookCode
|
||||
*/
|
||||
async webhooks(
|
||||
public async webhooks(
|
||||
tenantId: number,
|
||||
webhookType: string,
|
||||
plaidItemId: string,
|
||||
webhookType: string,
|
||||
webhookCode: string
|
||||
) {
|
||||
const _webhookType = webhookType.toLowerCase();
|
||||
|
||||
// There are five types of webhooks: AUTH, TRANSACTIONS, ITEM, INCOME, and ASSETS.
|
||||
// @TODO implement handling for remaining webhook types.
|
||||
const webhookHandlerMap = {
|
||||
transactions: this.handleTransactionsWebooks,
|
||||
item: this.itemsHandler,
|
||||
transactions: this.handleTransactionsWebooks.bind(this),
|
||||
item: this.itemsHandler.bind(this),
|
||||
};
|
||||
const webhookHandler =
|
||||
webhookHandlerMap[webhookType] || this.unhandledWebhook;
|
||||
webhookHandlerMap[_webhookType] || this.unhandledWebhook;
|
||||
|
||||
await webhookHandler(tenantId, webhookCode, plaidItemId);
|
||||
await webhookHandler(tenantId, plaidItemId, webhookCode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,7 +39,7 @@ export class PlaidWebooks {
|
||||
* @param {string} webhookCode
|
||||
* @param {string} plaidItemId
|
||||
*/
|
||||
async unhandledWebhook(
|
||||
private async unhandledWebhook(
|
||||
webhookType: string,
|
||||
webhookCode: string,
|
||||
plaidItemId: string
|
||||
@@ -114,37 +116,24 @@ export class PlaidWebooks {
|
||||
*/
|
||||
public async itemsHandler(
|
||||
tenantId: number,
|
||||
webhookCode: string,
|
||||
plaidItemId: string
|
||||
plaidItemId: string,
|
||||
webhookCode: string
|
||||
): Promise<void> {
|
||||
switch (webhookCode) {
|
||||
case 'WEBHOOK_UPDATE_ACKNOWLEDGED':
|
||||
this.serverLogAndEmitSocket('is updated', plaidItemId, error);
|
||||
break;
|
||||
case 'ERROR': {
|
||||
this.serverLogAndEmitSocket(
|
||||
`ERROR: ${error.error_code}: ${error.error_message}`,
|
||||
itemId,
|
||||
error.error_code
|
||||
);
|
||||
break;
|
||||
}
|
||||
case 'PENDING_EXPIRATION': {
|
||||
const { id: itemId } = await retrieveItemByPlaidItemId(plaidItemId);
|
||||
await updateItemStatus(itemId, 'bad');
|
||||
|
||||
this.serverLogAndEmitSocket(
|
||||
`user needs to re-enter login credentials`,
|
||||
itemId,
|
||||
error
|
||||
);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
this.serverLogAndEmitSocket(
|
||||
'unhandled webhook type received.',
|
||||
plaidItemId,
|
||||
error
|
||||
webhookCode,
|
||||
plaidItemId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user