67 lines
3.5 KiB
TypeScript
67 lines
3.5 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { pool } from '../db/pool';
|
|
import { importStatement } from './import';
|
|
|
|
const makeStatement = (sourceIds: string[]) => ({
|
|
schemaVersion: '1.0', bank: 'TEST',
|
|
statement: { accountNumber: 'metadata-test', currency: 'RUB', openingBalance: 0, closingBalance: 100, exportedAt: '2026-08-20T12:00:00+03:00' },
|
|
transactions: sourceIds.map((sourceId) => ({ operationAt: '2026-08-20T10:00:00+03:00', amountSigned: 100, commission: 0, description: 'Пополнение', sourceId })),
|
|
});
|
|
|
|
const duplicateStatement = {
|
|
schemaVersion: '1.0', bank: 'TEST',
|
|
statement: { accountNumber: 'fingerprint-test', currency: 'RUB', openingBalance: 0, closingBalance: 200, exportedAt: '2026-08-20T12:00:00+03:00' },
|
|
transactions: Array.from({ length: 2 }, () => ({ operationAt: '2026-08-20T00:00:00+03:00', amountSigned: 100, commission: 0, description: 'Пополнение' })),
|
|
};
|
|
const overlapStatement = {
|
|
...duplicateStatement,
|
|
statement: { ...duplicateStatement.statement, accountNumber: 'overlap-test' },
|
|
};
|
|
const singleStatement = {
|
|
...overlapStatement,
|
|
statement: { ...overlapStatement.statement, closingBalance: 100 },
|
|
transactions: overlapStatement.transactions.slice(0, 1),
|
|
};
|
|
|
|
async function run(): Promise<void> {
|
|
try {
|
|
await importStatement(makeStatement(['first', 'first-second']));
|
|
const positions = await pool.query("SELECT source_position FROM transactions t JOIN accounts a ON a.id = t.account_id WHERE a.bank = 'TEST' AND a.account_number = 'metadata-test' ORDER BY source_position");
|
|
assert.deepEqual(positions.rows.map((row) => Number(row.source_position)), [0, 1]);
|
|
const account = await pool.query("UPDATE accounts SET account_type = 'savings', status = 'closed' WHERE bank = 'TEST' AND account_number = 'metadata-test' RETURNING id");
|
|
assert.equal(account.rows.length, 1);
|
|
await importStatement(makeStatement(['second']));
|
|
const result = await pool.query('SELECT account_type, status FROM accounts WHERE id = $1', [account.rows[0].id]);
|
|
assert.deepEqual(result.rows[0], { account_type: 'savings', status: 'closed' });
|
|
const duplicateResult = await importStatement(duplicateStatement);
|
|
if ('status' in duplicateResult) throw new Error(duplicateResult.message);
|
|
assert.deepEqual(duplicateResult, {
|
|
accountId: duplicateResult.accountId,
|
|
isNewAccount: true,
|
|
accountNumberMasked: 'fingerp******test',
|
|
imported: 2,
|
|
duplicatesSkipped: 0,
|
|
totalInFile: 2,
|
|
});
|
|
await importStatement(singleStatement);
|
|
const overlapResult = await importStatement(overlapStatement);
|
|
if ('status' in overlapResult) throw new Error(overlapResult.message);
|
|
assert.deepEqual(overlapResult, {
|
|
accountId: overlapResult.accountId,
|
|
isNewAccount: false,
|
|
accountNumberMasked: 'overla******test',
|
|
imported: 1,
|
|
duplicatesSkipped: 1,
|
|
totalInFile: 2,
|
|
});
|
|
console.log('import metadata SQL: OK');
|
|
} finally {
|
|
await pool.query("DELETE FROM transactions WHERE account_id IN (SELECT id FROM accounts WHERE bank = 'TEST' AND account_number IN ('metadata-test', 'fingerprint-test', 'overlap-test'))");
|
|
await pool.query("DELETE FROM imports WHERE account_id IN (SELECT id FROM accounts WHERE bank = 'TEST' AND account_number IN ('metadata-test', 'fingerprint-test', 'overlap-test'))");
|
|
await pool.query("DELETE FROM accounts WHERE bank = 'TEST' AND account_number IN ('metadata-test', 'fingerprint-test', 'overlap-test')");
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
run();
|