Track imports in DB, show history in Data section, allow deleting transactions of a specific import instead of clearing all.
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { pool } from '../db/pool';
|
|
import type { Import } from '@family-budget/shared';
|
|
|
|
export async function getImports(): Promise<Import[]> {
|
|
const result = await pool.query(
|
|
`SELECT
|
|
i.id,
|
|
i.imported_at,
|
|
i.account_id,
|
|
a.alias AS account_alias,
|
|
i.bank,
|
|
i.account_number_masked,
|
|
i.imported_count,
|
|
i.duplicates_skipped,
|
|
i.total_in_file
|
|
FROM imports i
|
|
LEFT JOIN accounts a ON a.id = i.account_id
|
|
ORDER BY i.imported_at DESC`,
|
|
);
|
|
|
|
return result.rows.map((r) => ({
|
|
id: Number(r.id),
|
|
importedAt: r.imported_at.toISOString(),
|
|
accountId: r.account_id != null ? Number(r.account_id) : null,
|
|
accountAlias: r.account_alias ?? null,
|
|
bank: r.bank,
|
|
accountNumberMasked: r.account_number_masked,
|
|
importedCount: Number(r.imported_count),
|
|
duplicatesSkipped: Number(r.duplicates_skipped),
|
|
totalInFile: Number(r.total_in_file),
|
|
}));
|
|
}
|
|
|
|
export async function deleteImport(id: number): Promise<{ deleted: number }> {
|
|
const result = await pool.query(
|
|
'DELETE FROM transactions WHERE import_id = $1 RETURNING id',
|
|
[id],
|
|
);
|
|
const deleted = result.rowCount ?? 0;
|
|
|
|
await pool.query('DELETE FROM imports WHERE id = $1', [id]);
|
|
|
|
return { deleted };
|
|
}
|