generated from nyu-software-engineering/generic-mern-stack-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from software-students-fall2021/alan-remove-a…
…ccount Allow users to remove accounts
- Loading branch information
Showing
5 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require("dotenv").config({ silent: true }); | ||
const { Configuration, PlaidApi, PlaidEnvironments } = require("plaid"); | ||
const UserModel = require("../model/user"); | ||
|
||
// Initialize the Plaid client | ||
const PLAID_CLIENT_ID = process.env.PLAID_CLIENT_ID; | ||
const PLAID_SECRET = process.env.PLAID_SECRET; | ||
const PLAID_ENV = process.env.PLAID_ENV || "sandbox"; | ||
|
||
const configuration = new Configuration({ | ||
basePath: PlaidEnvironments[PLAID_ENV], | ||
baseOptions: { | ||
headers: { | ||
"PLAID-CLIENT-ID": PLAID_CLIENT_ID, | ||
"PLAID-SECRET": PLAID_SECRET, | ||
"Plaid-Version": "2020-09-14", | ||
}, | ||
}, | ||
}); | ||
|
||
const plaidClient = new PlaidApi(configuration); | ||
|
||
/** | ||
* Removes the access token of the account specified from the DB | ||
* @param {*} curAccountName | ||
* @param {*} accessTokensArr | ||
* @param {*} userId | ||
* @returns true if account was removed, false otherwise | ||
*/ | ||
const removeAccount = async (curAccountName, accessTokensArr, userId) => { | ||
try { | ||
for (const token of accessTokensArr) { | ||
const tempAccount = await plaidClient.accountsGet({ | ||
access_token: token.access_token, | ||
}); | ||
// prevent null errors if tempAccount.data.accounts is null or anything is null | ||
const accountArr = tempAccount?.data?.accounts ? tempAccount?.data?.accounts : []; | ||
// check each bank's accounts to see if account to be removed exists | ||
for (const accountObj of accountArr) { | ||
if (accountObj.name === curAccountName) { | ||
UserModel.updateOne( | ||
{ _id: userId }, | ||
{ $pull: { access_token: token } }, | ||
function (err) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log("Successfully removed access token from db."); | ||
} | ||
} | ||
); | ||
return true; // stop iterating, account already removed | ||
} | ||
} | ||
} | ||
// if we get here, then we did not remove anything | ||
return false; | ||
} catch (error) { | ||
console.log(error); | ||
return false; | ||
} | ||
}; | ||
|
||
module.exports = removeAccount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters