Fixed pending problmes
This commit is contained in:
@ -207,10 +207,36 @@ export async function pullData(amount = 100) {
|
||||
state.push(`Found ${transactions.length} transactions for account ID: ${account.id}`);
|
||||
|
||||
const cardRegEx = /\d{4}$/;
|
||||
|
||||
// Check if any pending need to be updated
|
||||
|
||||
state.push(`Checking for pending transactions for account ID: ${account.id}`);
|
||||
let currentPend =
|
||||
await db`SELECT id, amount, description, date from transaction where account_id = ${account.id} AND pending=true`;
|
||||
for (const pend of currentPend) {
|
||||
const found = transactions.find((t) => t.transactionId === pend.id);
|
||||
if (!found) {
|
||||
const updated = transactions.find(
|
||||
(t) => t.amount == pend.amount && new Date(t.postedDate) == pend.date
|
||||
);
|
||||
if (updated) {
|
||||
state.push(
|
||||
`I think I found an updated transaction: ${updated.statementDescription} ${updated.amount} for ${pend.description} ${pend.amount}`
|
||||
);
|
||||
|
||||
await db`UPDATE budget_transaction SET transaction_id = ${updated.transactionId} WHERE transaction_id = ${pend.id}`;
|
||||
} else {
|
||||
state.push(`Orphaning no longer pending budget transaction with no new parent`);
|
||||
await db`UPDATE budget_transaction SET transaction_id = null WHERE transaction_id = ${pend.id}`;
|
||||
}
|
||||
state.push(`Removing pending transaction: ${pend.id}`);
|
||||
await db`DELETE FROM transaction WHERE id = ${pend.id}`;
|
||||
}
|
||||
}
|
||||
|
||||
for (const transaction of transactions) {
|
||||
const amount = Number(transaction.amount);
|
||||
const date = new Date(transaction.postedDate);
|
||||
const id = transaction.hostTranNumber;
|
||||
const payee = transaction.description || '';
|
||||
const statementDescription = transaction.statementDescription;
|
||||
const card = cardRegEx.test(statementDescription)
|
||||
@ -218,6 +244,11 @@ export async function pullData(amount = 100) {
|
||||
: null;
|
||||
const pending = transaction.extended?.allTransactionType == 1 ? true : false;
|
||||
const accountId = transaction.accountId;
|
||||
|
||||
const id = pending
|
||||
? `${transaction.postedDate}:${transaction.amount}`
|
||||
: transaction.hostTranNumber;
|
||||
|
||||
await db`INSERT INTO transaction (
|
||||
id,
|
||||
account_id,
|
||||
@ -248,6 +279,17 @@ export async function pullData(amount = 100) {
|
||||
pending = EXCLUDED.pending`;
|
||||
}
|
||||
}
|
||||
|
||||
state.push('Orphaning transactions');
|
||||
|
||||
const orphaned = await db`SELECT bt.id as id
|
||||
FROM budget_transaction bt
|
||||
LEFT OUTER JOIN transaction t ON bt.transaction_id = t.id
|
||||
WHERE t.id IS NULL;`;
|
||||
for (const orphan of orphaned) {
|
||||
state.push(`Orphaning transaction: ${orphan.id}`);
|
||||
await db`UPDATE budget_transaction set transaction_id = null where id = ${orphan.id}`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in pullData:', error);
|
||||
state.push(`Error: ${error.message}`);
|
||||
|
||||
Reference in New Issue
Block a user