csvparser/ts/commerzbank/csv-commerzbank.classes.csvcommerzbank.ts

99 lines
3.6 KiB
TypeScript

import * as plugins from '../csvparser.plugins.js';
import * as interfaces from './interfaces/index.js';
export class CsvCommerzbank extends plugins.portablefinance
.AcCsvParser<interfaces.ICommerzbankTransaction> {
// INSTANCE
public paymentProviderName = 'Commerzbank';
public description: string = `a csv parser for parsing downloaded csv transaction files from Commerzbank`;
private csvDescriptorArray: plugins.portablefinance.ICsvDescriptor[] = [];
constructor() {
super();
}
addCsvDecriptor(csvDescriptorArg: plugins.portablefinance.ICsvDescriptor): void {
this.csvDescriptorArray.push(csvDescriptorArg);
}
public async getTransactions(): Promise<plugins.portablefinance.IMonetaryTransaction[]> {
const payments: interfaces.ICommerzbankOriginalTransaction[] = [];
for (const csvDescriptor of this.csvDescriptorArray) {
const csvInstance = await plugins.smartcsv.Csv.createCsvFromString(csvDescriptor.contentString, {
headers: true,
unquote: true,
});
payments.push(
...(await csvInstance.exportAsObject())
);
}
const finalTransactionArray: interfaces.ICommerzbankTransaction[] = [];
for (const transaction of payments) {
// transaction.Buchungstag = transaction.Wertstellung;
console.log(transaction);
const finalTransaction: interfaces.ICommerzbankTransaction = {
simpleTransaction: null,
transactionHash: null,
original: transaction,
amount: plugins.smartmoney.parseEuropeanNumberString(transaction.Betrag),
currency: transaction.Währung,
description: transaction.Buchungstext,
transactionDate: plugins.smarttime.ExtendedDate.fromEuropeanDate(transaction.Buchungstag),
valuationDate: plugins.smarttime.ExtendedDate.fromEuropeanDate(transaction.Wertstellung),
transactionType: ((): interfaces.TTransactionType => {
switch (transaction.Umsatzart) {
case 'Gutschrift':
return 'Credit';
case 'Lastschrift':
return 'Debit';
case 'Zinsen/Entgelte':
return 'BankFees';
case 'Überweisung':
return 'ActiveTransfer';
default:
throw new Error(`unknown transactiontype ${transaction.Umsatzart}`);
}
})(),
};
// lets assign the transactionHash
finalTransaction.transactionHash = await plugins.smarthash.sha265FromObject({
description: finalTransaction.description,
amount: finalTransaction.amount,
date: finalTransaction.valuationDate,
});
finalTransaction.simpleTransaction = {
id: finalTransaction.transactionHash,
accountId: null,
name: finalTransaction.description,
amount: finalTransaction.amount,
description: finalTransaction.description,
date: finalTransaction.transactionDate,
};
finalTransactionArray.push(finalTransaction);
}
return finalTransactionArray.map((commerzbankTransaction) => {
const fin2021Transaction: plugins.portablefinance.IMonetaryTransaction = {
id: null,
data: {
additionalIds: [],
amount: commerzbankTransaction.amount,
date: commerzbankTransaction.transactionDate.getTime(),
description: commerzbankTransaction.description,
name: commerzbankTransaction.description,
originAccountId: null,
originTransactionId: commerzbankTransaction.transactionHash,
paymentAccountId: null,
justForLooks: null,
},
};
return fin2021Transaction;
});
}
}