mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
feat(server): api endpoint to get Plaid link token
This commit is contained in:
103
packages/server/src/lib/Plaid/Plaid.ts
Normal file
103
packages/server/src/lib/Plaid/Plaid.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { forEach } from 'lodash';
|
||||
import { Configuration, PlaidApi, PlaidEnvironments } from 'plaid';
|
||||
import { createPlaidApiEvent } from './PlaidApiEventsDBSync';
|
||||
import config from '@/config';
|
||||
|
||||
const OPTIONS = { clientApp: 'Plaid-Pattern' };
|
||||
|
||||
// We want to log requests to / responses from the Plaid API (via the Plaid client), as this data
|
||||
// can be useful for troubleshooting.
|
||||
|
||||
/**
|
||||
* Logging function for Plaid client methods that use an access_token as an argument. Associates
|
||||
* the Plaid API event log entry with the item and user the request is for.
|
||||
*
|
||||
* @param {string} clientMethod the name of the Plaid client method called.
|
||||
* @param {Array} clientMethodArgs the arguments passed to the Plaid client method.
|
||||
* @param {Object} response the response from the Plaid client.
|
||||
*/
|
||||
const defaultLogger = async (clientMethod, clientMethodArgs, response) => {
|
||||
const accessToken = clientMethodArgs[0].access_token;
|
||||
// const { id: itemId, user_id: userId } = await retrieveItemByPlaidAccessToken(
|
||||
// accessToken
|
||||
// );
|
||||
// await createPlaidApiEvent(1, 1, clientMethod, clientMethodArgs, response);
|
||||
|
||||
console.log(response);
|
||||
};
|
||||
|
||||
/**
|
||||
* Logging function for Plaid client methods that do not use access_token as an argument. These
|
||||
* Plaid API event log entries will not be associated with an item or user.
|
||||
*
|
||||
* @param {string} clientMethod the name of the Plaid client method called.
|
||||
* @param {Array} clientMethodArgs the arguments passed to the Plaid client method.
|
||||
* @param {Object} response the response from the Plaid client.
|
||||
*/
|
||||
const noAccessTokenLogger = async (
|
||||
clientMethod,
|
||||
clientMethodArgs,
|
||||
response
|
||||
) => {
|
||||
console.log(response);
|
||||
|
||||
// await createPlaidApiEvent(
|
||||
// undefined,
|
||||
// undefined,
|
||||
// clientMethod,
|
||||
// clientMethodArgs,
|
||||
// response
|
||||
// );
|
||||
};
|
||||
|
||||
// Plaid client methods used in this app, mapped to their appropriate logging functions.
|
||||
const clientMethodLoggingFns = {
|
||||
accountsGet: defaultLogger,
|
||||
institutionsGet: noAccessTokenLogger,
|
||||
institutionsGetById: noAccessTokenLogger,
|
||||
itemPublicTokenExchange: noAccessTokenLogger,
|
||||
itemRemove: defaultLogger,
|
||||
linkTokenCreate: noAccessTokenLogger,
|
||||
transactionsSync: defaultLogger,
|
||||
sandboxItemResetLogin: defaultLogger,
|
||||
};
|
||||
// Wrapper for the Plaid client. This allows us to easily log data for all Plaid client requests.
|
||||
export class PlaidClientWrapper {
|
||||
constructor() {
|
||||
// Initialize the Plaid client.
|
||||
const configuration = new Configuration({
|
||||
basePath: PlaidEnvironments[config.plaid.env],
|
||||
baseOptions: {
|
||||
headers: {
|
||||
'PLAID-CLIENT-ID': config.plaid.clientId,
|
||||
'PLAID-SECRET':
|
||||
config.plaid.env === 'development'
|
||||
? config.plaid.secretDevelopment
|
||||
: config.plaid.secretSandbox,
|
||||
'Plaid-Version': '2020-09-14',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
this.client = new PlaidApi(configuration);
|
||||
|
||||
// Wrap the Plaid client methods to add a logging function.
|
||||
forEach(clientMethodLoggingFns, (logFn, method) => {
|
||||
this[method] = this.createWrappedClientMethod(method, logFn);
|
||||
});
|
||||
}
|
||||
|
||||
// Allows us to log API request data for troubleshooting purposes.
|
||||
createWrappedClientMethod(clientMethod, log) {
|
||||
return async (...args) => {
|
||||
try {
|
||||
const res = await this.client[clientMethod](...args);
|
||||
await log(clientMethod, args, res);
|
||||
return res;
|
||||
} catch (err) {
|
||||
await log(clientMethod, args, err?.response?.data);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
48
packages/server/src/lib/Plaid/PlaidApiEventsDBSync.ts
Normal file
48
packages/server/src/lib/Plaid/PlaidApiEventsDBSync.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Creates a single Plaid api event log entry.
|
||||
*
|
||||
* @param {string} itemId the item id in the request.
|
||||
* @param {string} userId the user id in the request.
|
||||
* @param {string} plaidMethod the Plaid client method called.
|
||||
* @param {Array} clientMethodArgs the arguments passed to the Plaid client method.
|
||||
* @param {Object} response the Plaid api response object.
|
||||
*/
|
||||
export const createPlaidApiEvent = async (
|
||||
itemId,
|
||||
userId,
|
||||
plaidMethod,
|
||||
clientMethodArgs,
|
||||
response
|
||||
) => {
|
||||
const {
|
||||
error_code: errorCode,
|
||||
error_type: errorType,
|
||||
request_id: requestId,
|
||||
} = response;
|
||||
const query = {
|
||||
text: `
|
||||
INSERT INTO plaid_api_events_table
|
||||
(
|
||||
item_id,
|
||||
user_id,
|
||||
plaid_method,
|
||||
arguments,
|
||||
request_id,
|
||||
error_type,
|
||||
error_code
|
||||
)
|
||||
VALUES
|
||||
($1, $2, $3, $4, $5, $6, $7);
|
||||
`,
|
||||
values: [
|
||||
itemId,
|
||||
userId,
|
||||
plaidMethod,
|
||||
JSON.stringify(clientMethodArgs),
|
||||
requestId,
|
||||
errorType,
|
||||
errorCode,
|
||||
],
|
||||
};
|
||||
// await db.query(query);
|
||||
};
|
||||
1
packages/server/src/lib/Plaid/index.ts
Normal file
1
packages/server/src/lib/Plaid/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './Plaid';
|
||||
Reference in New Issue
Block a user