Skip to content

Instantly share code, notes, and snippets.

@Harshakvarma
Created September 5, 2019 04:52
Show Gist options
  • Save Harshakvarma/e0602b394fae6d646c0e807b348eefe8 to your computer and use it in GitHub Desktop.
Save Harshakvarma/e0602b394fae6d646c0e807b348eefe8 to your computer and use it in GitHub Desktop.
const USER = require("../../src/db/userDB");
const moment = require("moment");
const Sequelize = require('sequelize');
const config = require("config");
const plaid = require('plaid');
const util = require('util');
const pe = require('parse-error');
var PLAID_CLIENT_ID = config.get("plaidCred.PLAID_CLIENT_ID");
var PLAID_SECRET = config.get("plaidCred.PLAID_SECRET");
var PLAID_PUBLIC_KEY = config.get("plaidCred.PLAID_PUBLIC_KEY");
var PLAID_ENV = config.get("plaidCred.PLAID_ENV");
const plaidClient = new plaid.Client(
PLAID_CLIENT_ID,
PLAID_SECRET,
PLAID_PUBLIC_KEY,
plaid.environments[PLAID_ENV],
{ version: '2018-05-22' }
);
let err, data = {};
let QUERY = `SELECT * FROM USER_PLAID_ITEM_ID A WHERE A.PLAID_ACCESS_TOKEN LIKE '%production%' AND A.ACTIVE_FLAG=1` //+' AND A.USER_ID=20173 LIMIT 1'
process_plaid_webhooks()
async function process_plaid_webhooks() {
[err, data] = await to(USER.UserPlaidWebhookLog.sequelize.query(QUERY, { type:Sequelize.QueryTypes.SELECT}))
if (err) {
prettyPrintResponse({"error_status": "Plaid Webhook:","error": err});
} else {
if (data) {
// console.log(QUERY)
// prettyPrintResponse({"RESPONSE":data})
await asyncForEach(data, async (element) => {
prettyPrintResponse({"element": element})
console.log("______________")
plaidClient.removeItem(element.PLAID_ACCESS_TOKEN, (error, result) => {
prettyPrintResponse(result);
if (error != null) {
prettyPrintResponse(error);
}
const isRemoved = result.removed;
if (isRemoved) {
process_REMOVE_ITEM(element)
}
})
});
}
}
}
async function process_REMOVE_ITEM(element){
console.log("XXXXXXXXXXXXXXXX process_REMOVE_ITEM XXXXXXXXXXXXXX")
let fields = {
activeFlag : 0,
updatedTimestamp : moment().format(),
}
let Condition = {
where: {
userId : element.USER_ID,
plaidItemId : element.PLAID_ITEM_ID,
},
order: [['createdTimestamp', 'ASC']],
limit: 1
};
let err, data = null;
[err, data] = await to(USER.UserPlaidItemId.update(fields, Condition))
if (err) console.log("ERROR IN UPDATE TABLE " + element.USER_ID);
if (!data) console.log({ error: "ERROR IN UPDATE TABLE ", data: element.USER_ID });
prettyPrintResponse("+++ DELETED PLAID AND UPDATED TABLE FOR USER " + element.USER_ID)
}
////////////// Helper fucntions
let to = function(promise) {
return promise
.then(data => {
return [null, data];
}).catch(err =>
[pe(err)]
);
}
var prettyPrintResponse = response => {
console.log(util.inspect(response, { colors: true, depth: 4 }));
};
/// https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const delay = time => new Promise(res=>setTimeout(res,time));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment