More graceful track-investments if SimpleFIN is down.
This commit is contained in:
+58
-53
@@ -38,16 +38,20 @@ const getSimplefinBalances = async () => {
|
|||||||
const username = credentials.split(':')[0];
|
const username = credentials.split(':')[0];
|
||||||
const pw = credentials.split(':')[1];
|
const pw = credentials.split(':')[1];
|
||||||
|
|
||||||
const url = `https://beta-bridge.simplefin.org/simplefin/accounts?start-date=${new Date().getTime()}&end-date=${new Date().getTime()}`;
|
try {
|
||||||
const response = await fetch(url, {
|
const url = `https://beta-bridge.simplefin.org/simplefin/accounts?start-date=${new Date().getTime()}&end-date=${new Date().getTime()}`;
|
||||||
headers: {
|
const response = await fetch(url, {
|
||||||
'Authorization': `Basic ${btoa(`${username}:${pw}`)}`
|
headers: {
|
||||||
}
|
'Authorization': `Basic ${btoa(`${username}:${pw}`)}`
|
||||||
});
|
}
|
||||||
const data = await response.json();
|
});
|
||||||
const accounts = data.accounts;
|
const data = await response.json();
|
||||||
const balances = {};
|
const accounts = data.accounts;
|
||||||
accounts.forEach(a => balances[a.name] = parseFloat(a.balance));
|
const balances = {};
|
||||||
|
accounts.forEach(a => balances[a.name] = parseFloat(a.balance));
|
||||||
|
} catch (e) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
return balances;
|
return balances;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,56 +75,57 @@ const zeroTransaction = async (payment) => {
|
|||||||
const categoryId = await ensureCategory(process.env.IMPORTER_INVESTMENT_CATEGORY_NAME || 'Investment');
|
const categoryId = await ensureCategory(process.env.IMPORTER_INVESTMENT_CATEGORY_NAME || 'Investment');
|
||||||
|
|
||||||
const simplefinBalances = await getSimplefinBalances();
|
const simplefinBalances = await getSimplefinBalances();
|
||||||
|
if (simplefinBalances) {
|
||||||
const accounts = await api.getAccounts();
|
const accounts = await api.getAccounts();
|
||||||
for (const account of accounts) {
|
for (const account of accounts) {
|
||||||
if (account.closed) {
|
if (account.closed) {
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
const note = await getAccountNote(account);
|
|
||||||
|
|
||||||
if (note) {
|
|
||||||
const data = await getTransactions(account);
|
|
||||||
|
|
||||||
if (note.indexOf('zeroSmall') > -1) {
|
|
||||||
const payments = data.filter(payment => payment.amount > -10000 && payment.amount < 10000 && payment.amount != 0 && payment.category == categoryId)
|
|
||||||
for (const payment of payments) {
|
|
||||||
if (shouldDrop(payment)) {
|
|
||||||
await zeroTransaction(payment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (note.indexOf('dropPayments') > -1) {
|
const note = await getAccountNote(account);
|
||||||
const payments = data.filter(payment => payment.amount < 0)
|
|
||||||
for (const payment of payments) {
|
if (note) {
|
||||||
if (shouldDrop(payment)) {
|
const data = await getTransactions(account);
|
||||||
await zeroTransaction(payment);
|
|
||||||
|
if (note.indexOf('zeroSmall') > -1) {
|
||||||
|
const payments = data.filter(payment => payment.amount > -10000 && payment.amount < 10000 && payment.amount != 0 && payment.category == categoryId)
|
||||||
|
for (const payment of payments) {
|
||||||
|
if (shouldDrop(payment)) {
|
||||||
|
await zeroTransaction(payment);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (note.indexOf('calcInvestment') > -1) {
|
if (note.indexOf('dropPayments') > -1) {
|
||||||
const currentBalance = await getAccountBalance(account);
|
const payments = data.filter(payment => payment.amount < 0)
|
||||||
const simplefinBalance = parseInt(simplefinBalances[account.name] * 100);
|
for (const payment of payments) {
|
||||||
const diff = simplefinBalance - currentBalance;
|
if (shouldDrop(payment)) {
|
||||||
|
await zeroTransaction(payment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Account:', account.name);
|
if (note.indexOf('calcInvestment') > -1) {
|
||||||
console.log('Simplefin Balance:', simplefinBalance);
|
const currentBalance = await getAccountBalance(account);
|
||||||
console.log('Current Balance:', currentBalance);
|
const simplefinBalance = parseInt(simplefinBalances[account.name] * 100);
|
||||||
console.log('Difference:', diff);
|
const diff = simplefinBalance - currentBalance;
|
||||||
|
|
||||||
if (diff) {
|
console.log('Account:', account.name);
|
||||||
await api.importTransactions(account.id, [{
|
console.log('Simplefin Balance:', simplefinBalance);
|
||||||
date: new Date(),
|
console.log('Current Balance:', currentBalance);
|
||||||
payee: payeeId,
|
console.log('Difference:', diff);
|
||||||
amount: diff,
|
|
||||||
cleared: true,
|
if (diff) {
|
||||||
reconciled: true,
|
await api.importTransactions(account.id, [{
|
||||||
category: categoryId,
|
date: new Date(),
|
||||||
notes: `Update investment balance to ${simplefinBalance / 100}`,
|
payee: payeeId,
|
||||||
}]);
|
amount: diff,
|
||||||
|
cleared: true,
|
||||||
|
reconciled: true,
|
||||||
|
category: categoryId,
|
||||||
|
notes: `Update investment balance to ${simplefinBalance / 100}`,
|
||||||
|
}]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user