|
| 1 | +import { |
| 2 | + EnrichedTransaction, ExportTransactionsFunction, OutputVendor, OutputVendorName |
| 3 | +} from '@/originalBudgetTrackingApp/commonTypes'; |
| 4 | +import { mergeTransactions, sortByDate } from '@/originalBudgetTrackingApp/transactions/transactions'; |
| 5 | +import { TransactionInstallments } from 'israeli-bank-scrapers-core/lib/transactions'; |
| 6 | +import { promises as fs } from 'fs'; |
| 7 | +import stringify from 'csv-stringify/lib/sync'; |
| 8 | +import parse from 'csv-parse/lib/sync'; |
| 9 | + |
| 10 | +export function parseTransactions(csvText: string) { |
| 11 | + return parse(csvText, { |
| 12 | + columns: true, |
| 13 | + cast: (value, context) => { |
| 14 | + return parseColumn(value, context.column); |
| 15 | + } |
| 16 | + }) as EnrichedTransaction[]; |
| 17 | +} |
| 18 | + |
| 19 | +function parseColumn(value:string, column) { |
| 20 | + switch (column) { |
| 21 | + case 'chargedAmount': |
| 22 | + case 'identifier': |
| 23 | + case 'originalAmount': |
| 24 | + return value === '' ? undefined : parseFloat(value); |
| 25 | + |
| 26 | + case 'installments': |
| 27 | + return parseInstallments(value); |
| 28 | + |
| 29 | + default: |
| 30 | + return value; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function parseInstallments(value: string) { |
| 35 | + if (value === '') { |
| 36 | + return undefined; |
| 37 | + } |
| 38 | + const separator = value.indexOf('/'); |
| 39 | + if (separator !== -1) { |
| 40 | + const installments: TransactionInstallments = { |
| 41 | + number: parseInt(value.substring(0, separator - 1), 10), |
| 42 | + total: parseInt(value.substring(separator + 1), 10) |
| 43 | + }; |
| 44 | + return installments; |
| 45 | + } |
| 46 | + return value; |
| 47 | +} |
| 48 | + |
| 49 | +export const serializeTransactions = (transactions: EnrichedTransaction[]) => { |
| 50 | + return stringify(transactions, { |
| 51 | + header: true, |
| 52 | + columns: [ |
| 53 | + { key: 'date' }, |
| 54 | + { key: 'description' }, |
| 55 | + { key: 'chargedAmount' }, |
| 56 | + { key: 'memo' }, |
| 57 | + { key: 'category' }, |
| 58 | + { key: 'accountNumber' }, |
| 59 | + { key: 'installments' }, |
| 60 | + { key: 'originalAmount' }, |
| 61 | + { key: 'originalCurrency' }, |
| 62 | + { key: 'processedDate' }, |
| 63 | + { key: 'identifier' }, |
| 64 | + { key: 'hash' }, |
| 65 | + { key: 'status' }, |
| 66 | + { key: 'type' } |
| 67 | + ], |
| 68 | + cast: { |
| 69 | + object: (value, context) => { |
| 70 | + if (context.column === 'installments' && !context.header) { |
| 71 | + return `${value.number} / ${value.total}`; |
| 72 | + } |
| 73 | + return `${value}`; |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +const exportTransactions: ExportTransactionsFunction = async ({ transactionsToCreate, outputVendorsConfig }) => { |
| 80 | + const { filePath } = outputVendorsConfig.csv!.options; |
| 81 | + const savedTransactions = await parseTransactionsFile(filePath); |
| 82 | + const mergedTransactions = mergeTransactions(savedTransactions, transactionsToCreate); |
| 83 | + const sorted = sortByDate(mergedTransactions); |
| 84 | + await writeCsvFile(filePath, serializeTransactions(sorted)); |
| 85 | +}; |
| 86 | + |
| 87 | +const parseTransactionsFile = async (filename: string) => { |
| 88 | + try { |
| 89 | + const content = await fs.readFile(filename, { encoding: 'utf8' }); |
| 90 | + return parseTransactions(content); |
| 91 | + } catch (err) { |
| 92 | + if (err.code === 'ENOENT') { |
| 93 | + return [] as EnrichedTransaction[]; |
| 94 | + } |
| 95 | + throw err; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +async function writeCsvFile(filePath: string, csvText: string) { |
| 100 | + await fs.writeFile(filePath, csvText); |
| 101 | +} |
| 102 | + |
| 103 | +export default { |
| 104 | + name: OutputVendorName.CSV, |
| 105 | + exportTransactions |
| 106 | +} as OutputVendor; |
0 commit comments