-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
68 lines (60 loc) · 1.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { program } from "commander";
import { XMLParser } from "fast-xml-parser";
import { readFile } from "fs/promises";
program
.description(
"CLI to convert SEPA XML files into a multi transfer CSV file that can be used with finom"
)
.argument("<sepa.xml>", "SEPA XML file to convert");
program.parse();
async function main() {
const xml = await readFile(program.args[0]);
const parser = new XMLParser();
const sepa = parser.parse(xml);
const payments = extractPayments(sepa);
const csv = paymentsToCsv(payments);
console.log(csv);
}
main();
interface Payment {
readonly beneficiaryName: string;
readonly iban: string;
readonly bic: string;
readonly amount: number;
readonly currency: string;
readonly reference: string;
}
function extractPayments(sepa: any): Payment[] {
const paymentInfo = sepa.Document?.CstmrCdtTrfInitn?.PmtInf;
if (!paymentInfo) {
throw new Error("could not find payment info in sepa document");
}
const creditorTransactionInfo = paymentInfo.CdtTrfTxInf;
if (!creditorTransactionInfo) {
throw new Error("could not find creditor transaction info");
}
return (
Array.isArray(creditorTransactionInfo)
? creditorTransactionInfo
: [creditorTransactionInfo]
).map((it) => ({
beneficiaryName: it.Cdtr?.Nm,
iban: it.CdtrAcct?.Id?.IBAN,
bic: it.CdtrAgt?.FinInstnId?.BIC,
amount: it.Amt.InstdAmt,
currency: "EUR",
reference: it.RmtInf.Ustrd,
}));
}
function paymentsToCsv(payments: Payment[]): string {
const rows = [`beneficiary_name;iban;bic;amount;currency;reference`];
for (const payment of payments) {
const { beneficiaryName, iban, bic, amount, currency, reference } = payment;
rows.push(
[beneficiaryName, iban, bic, amount.toFixed(2), currency, reference].join(
";"
)
);
}
return rows.join("\n");
}