This repository has been archived by the owner on Feb 10, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 684
Pluggy.ai bank sync #530
Open
lelemm
wants to merge
13
commits into
actualbudget:master
Choose a base branch
from
lelemm:feature/pluggy.ai
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+464
−0
Open
Pluggy.ai bank sync #530
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d2dcbcb
Pluggy.ai bank sync
lelemm 263014a
added md
lelemm fa957ee
better error handling
lelemm 70e9257
Merge remote-tracking branch 'org/master' into feature/pluggy.ai
lelemm 4b4dc01
Merge remote-tracking branch 'org/master' into feature/pluggy.ai
lelemm 9671955
getting the wrong information
lelemm ff642ea
fix foreigner currency
lelemm bc8093d
Merge remote-tracking branch 'org/master' into feature/pluggy.ai
lelemm 38da7dd
added round to remove more decimals
lelemm 69da681
removed trunc in favor of round
lelemm 1c8e24f
added another place to round in 2 decimals
lelemm bf0a0bc
Merge branch 'master' into feature/pluggy.ai
lelemm 8e894b6
Merge remote-tracking branch 'org/master' into feature/pluggy.ai
lelemm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
import express from 'express'; | ||
import { SecretName, secretsService } from '../services/secrets-service.js'; | ||
import { handleError } from '../app-gocardless/util/handle-error.js'; | ||
import { requestLoggerMiddleware } from '../util/middlewares.js'; | ||
import { pluggyaiService } from './pluggyai-service.js'; | ||
|
||
const app = express(); | ||
export { app as handlers }; | ||
app.use(express.json()); | ||
app.use(requestLoggerMiddleware); | ||
|
||
app.post( | ||
'/status', | ||
handleError(async (req, res) => { | ||
const clientId = secretsService.get(SecretName.pluggyai_clientId); | ||
const configured = clientId != null; | ||
|
||
res.send({ | ||
status: 'ok', | ||
data: { | ||
configured: configured, | ||
}, | ||
}); | ||
}), | ||
); | ||
|
||
app.post( | ||
'/accounts', | ||
handleError(async (req, res) => { | ||
try { | ||
await pluggyaiService.setToken(); | ||
const itemIds = secretsService | ||
.get(SecretName.pluggyai_itemIds) | ||
.split(',') | ||
.map((item) => item.trim()); | ||
|
||
let accounts = []; | ||
|
||
for (const item of itemIds) { | ||
const partial = await pluggyaiService.getAccountsByItemId(item); | ||
accounts = accounts.concat(partial.results); | ||
} | ||
|
||
res.send({ | ||
status: 'ok', | ||
data: { | ||
accounts: accounts, | ||
}, | ||
}); | ||
} catch (error) { | ||
res.send({ | ||
status: 'ok', | ||
data: { | ||
error: error.message, | ||
}, | ||
}); | ||
} | ||
}), | ||
); | ||
|
||
app.post( | ||
'/transactions', | ||
handleError(async (req, res) => { | ||
const { accountId, startDate, endDate } = req.body; | ||
|
||
try { | ||
let transactions = []; | ||
await pluggyaiService.setToken(); | ||
let result = await pluggyaiService.getTransactionsByAccountId( | ||
accountId, | ||
startDate, | ||
endDate, | ||
500, | ||
1, | ||
); | ||
transactions = transactions.concat(result.results); | ||
const totalPages = result.totalPages; | ||
while (result.page != totalPages) { | ||
result = await pluggyaiService.getTransactionsByAccountId( | ||
accountId, | ||
startDate, | ||
endDate, | ||
500, | ||
result.page + 1, | ||
); | ||
transactions = transactions.concat(result.results); | ||
} | ||
|
||
const account = await pluggyaiService.getAccountById(accountId); | ||
|
||
let startingBalance = parseInt( | ||
Math.round(account.balance * 100).toString(), | ||
); | ||
if (account.type === 'CREDIT') { | ||
startingBalance = -startingBalance; | ||
} | ||
const date = getDate(new Date(account.updatedAt)); | ||
|
||
const balances = [ | ||
{ | ||
balanceAmount: { | ||
amount: startingBalance, | ||
currency: account.currencyCode, | ||
}, | ||
balanceType: 'expected', | ||
referenceDate: date, | ||
}, | ||
{ | ||
balanceAmount: { | ||
amount: startingBalance, | ||
currency: account.currencyCode, | ||
}, | ||
balanceType: 'interimAvailable', | ||
referenceDate: date, | ||
}, | ||
]; | ||
|
||
const all = []; | ||
const booked = []; | ||
const pending = []; | ||
|
||
for (const trans of transactions) { | ||
const newTrans = {}; | ||
|
||
let dateToUse = 0; | ||
|
||
if (trans.status === 'PENDING') { | ||
newTrans.booked = false; | ||
} else { | ||
newTrans.booked = true; | ||
} | ||
dateToUse = trans.date; | ||
|
||
const transactionDate = new Date(dateToUse); | ||
|
||
if (transactionDate < startDate) { | ||
continue; | ||
} | ||
|
||
newTrans.date = getDate(transactionDate); | ||
|
||
newTrans.payeeName = ''; | ||
if ( | ||
trans.merchant && | ||
(trans.merchant.name || trans.merchant.businessName) | ||
) { | ||
newTrans.payeeName = | ||
trans.merchant.name || trans.merchant.businessName; | ||
} else if ( | ||
trans.type === 'DEBIT' && | ||
trans.paymentData && | ||
trans.paymentData.receiver && | ||
trans.paymentData.receiver.name | ||
) { | ||
newTrans.payeeName = trans.paymentData.receiver.name; | ||
} else if ( | ||
trans.type === 'CREDIT' && | ||
trans.paymentData && | ||
trans.paymentData.payer && | ||
trans.paymentData.payer.name | ||
) { | ||
newTrans.payeeName = trans.paymentData.payer.name; | ||
} else if ( | ||
trans.type === 'DEBIT' && | ||
trans.paymentData && | ||
trans.paymentData.receiver && | ||
trans.paymentData.receiver.documentNumber && | ||
trans.paymentData.receiver.documentNumber.value | ||
) { | ||
newTrans.payeeName = trans.paymentData.receiver.documentNumber.value; | ||
} else if ( | ||
trans.type === 'CREDIT' && | ||
trans.paymentData && | ||
trans.paymentData.payer && | ||
trans.paymentData.payer.documentNumber && | ||
trans.paymentData.payer.documentNumber.value | ||
) { | ||
newTrans.payeeName = trans.paymentData.payer.documentNumber.value; | ||
} | ||
|
||
newTrans.remittanceInformationUnstructured = trans.descriptionRaw; | ||
let amountInCurrency = trans.amountInAccountCurrency ?? trans.amount; | ||
amountInCurrency = Math.round(amountInCurrency * 100) / 100; | ||
|
||
newTrans.transactionAmount = { | ||
amount: | ||
account.type === 'BANK' ? amountInCurrency : -amountInCurrency, | ||
currency: trans.currencyCode, | ||
}; | ||
newTrans.transactionId = trans.id; | ||
newTrans.valueDate = newTrans.bookingDate; //always undefined? | ||
|
||
if (newTrans.booked) { | ||
booked.push(newTrans); | ||
} else { | ||
pending.push(newTrans); | ||
} | ||
all.push(newTrans); | ||
} | ||
|
||
res.send({ | ||
status: 'ok', | ||
data: { | ||
balances, | ||
startingBalance, | ||
transactions: { all, booked, pending }, | ||
}, | ||
}); | ||
} catch (error) { | ||
res.send({ | ||
status: 'ok', | ||
data: { | ||
error: error.message, | ||
}, | ||
}); | ||
} | ||
return; | ||
}), | ||
); | ||
|
||
function getDate(date) { | ||
return date.toISOString().split('T')[0]; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure request body date fields are reliably parsed before comparison.
Currently, you compare
transactionDate < startDate
directly, butstartDate
might be a string fromreq.body
. Confirm it’s converted to aDate
object to avoid unexpected behaviors.📝 Committable suggestion