Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add velobank support #44

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion converters/santanderConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ const columns = [
];

const parseAmount = (amount) => parseFloat(amount.replace(",", "."));
function normalisePayer(payer) {
return payer.replace(/^.*PLN /, '')
}
function descriptionToPayer(description) {
if (description.startsWith("Suma nagród za płatności")) {
return "Santander returns";
}
return description;
return normalisePayer(description);
}

function parsePayer(payer, description) {
return payer || descriptionToPayer(description);
}
Expand Down
2 changes: 1 addition & 1 deletion converters/santanderConverter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('should be able to convert Santander CSV files format', (done) => {
};
const input = `2022-10-23,25-09-2022,'11 2222 3333 4444,Owner address,PLN,,,17,
21-10-2022,19-04-2019,Description 1,KASOWNIK 12,11 2222 3333,"-3,20",,1,
20-10-2022,19-04-2019,Description 2,H AND M GAL. BALTYCKA,22 3333 44444,"-48,78",,2,`
20-10-2022,19-04-2019,VISA SEL 123456******1234 PŁATNOŚĆ KARTĄ 48.78 PLN H AND M GAL. BALTYCKA,,22 3333 44444,"-48,78",,2,`
const expected = `19.04.2019,-3.2,Transport,Public transport,Credit Card,,,KASOWNIK 12,,,Santander
19.04.2019,-48.78,Personal,Clothing,Credit Card,,,H AND M GAL. BALTYCKA,,,Santander
`
Expand Down
33 changes: 33 additions & 0 deletions converters/velobankConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { parse } = require('csv-parse/sync')
const { transform } = require('stream-transform')
const categoryResolver = require('../categoryResolver')
const { sanitize } = require('../utils')

const columns = ['date', 'realDate', 'description', 'amount', 'balance']

const parseAmount = amount => parseFloat(amount.replace(' ', '').replace(',', '.'))

const parseDescription = description => {
const descriptionParts = description.split(',')
if (descriptionParts.length === 1) {
return descriptionParts[0]
}
const payerPart = descriptionParts[1]
return payerPart.replace('w ', '').replace(/\d+ PLN/, '').trim()
}

const getExpenseManagerRecord = recordCategoryResolver => record => {
const payer = parseDescription(record[columns[2]]);
const amount = parseAmount(record[columns[3]])
const { category, subCategory } = recordCategoryResolver(payer, amount);
return record[columns[0]] + ','
+ amount + ',' + category + ',' + subCategory
+ ',Credit Card,,,' + sanitize(payer)
+ ',,,GetIn\n'
}

exports.convertCvsFileData = (input, categoriesMapping) => {
const getExpenseManagerWithMapping = getExpenseManagerRecord(categoryResolver.resolveCategory(categoriesMapping))
return transform(parse(input, { delimiter: ',', columns, relax: true, relax_column_count: true, bom: true }),
record => getExpenseManagerWithMapping(record))
}
31 changes: 31 additions & 0 deletions converters/velobankConverter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const citiConverter = require('./velobankConverter')

const categoriesMapping = {
'Play': { category: 'Utilities', subCategory: 'Telephone' },
'Employer': { category: 'Income', subCategory: 'Salary' },
'Deposit': { category: 'Income', subCategory: 'Deposit' }
};

test('should be able to convert Velobank manually created CSV files format', (done) => {
const input = `30.10.2019,24.11.2023,Employer,"9 839,29 PLN","1 574,59 PLN"
28.10.2019,24.11.2023,"Operacja kartą na kwotę 17,99 PLN w Play, GDANSK, PL","-10,00 PLN","2 374,59 PLN"`
const expected = `30.10.2019,9839.29,Income,Salary,Credit Card,,,Employer,,,GetIn
28.10.2019,-10,Utilities,Telephone,Credit Card,,,Play,,,GetIn
`
let transformedData = '';

const converter = citiConverter.convertCvsFileData(input, categoriesMapping)
.on('readable', () => {
let row = converter.read()
while (row) {
transformedData += row
row = converter.read()
}
})
.on('finish', () => {
setTimeout(() => {
expect(transformedData).toEqual(expected)
done()
})
})
})
14 changes: 14 additions & 0 deletions e2e/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ test('should download converted csv without mapping for Santander', async t => {
await t.expect(file).eql('30.10.2019,9839.29,Income,,Credit Card,,,Employer,,,Santander\n28.10.2019,-10,,,Credit Card,,,Play,,,Santander\n')
})
.after(() => unlinkSync(expectedFile))

test('should download converted csv without mapping for Velobank', async t => {
expectedFile = join(`${homedir()}`, 'Downloads', 'testDataVelobank-converted.csv')
await t
.setFilesToUpload(Selector('#file'), 'testDataVelobank.csv')
.click(Selector('#velobank'))
.click(Selector('button'))

await waitForFile(expectedFile)
const file = readFileSync(expectedFile, 'utf-8')

await t.expect(file).eql('30.10.2019,9839.29,Income,,Credit Card,,,Employer,,,GetIn\n28.10.2019,-10,,,Credit Card,,,Play,,,GetIn\n')
})
.after(() => unlinkSync(expectedFile))
2 changes: 2 additions & 0 deletions e2e/testDataVelobank.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
30.10.2019,24.11.2023,Employer,"9 839,29 PLN","1 574,59 PLN"
28.10.2019,24.11.2023,"Operacja kartą na kwotę 17,99 PLN w Play, GDANSK, PL","-10,00 PLN","2 374,59 PLN"
8 changes: 6 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
<input type="radio" id="santander" name="bank" value="santander"/>
</div>
<div>
<label for="pekao">Pekao</label>
<input type="radio" id="pekao" name="bank" value="pekao"/>
<label for="velobank">Velobank</label>
<input type="radio" id="velobank" name="bank" value="velobank"/>
</div>
<div>
<label for="monobank">Monobank</label>
<input type="radio" id="monobank" name="bank" value="monobank"/>
</div>
<div>
<label for="pekao">Pekao</label>
<input type="radio" id="pekao" name="bank" value="pekao"/>
</div>
<div>
<label for="pko">PKO</label>
<input type="radio" id="pko" name="bank" value="pko"/>
Expand Down
6 changes: 5 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const nestConverter = require("./converters/nestConverter");
const santanderConverter = require("./converters/santanderConverter");
const pekaoConverter = require("./converters/pekaoConverter");
const monobankConverter = require("./converters/monobankConverter");
const velobankConverter = require("./converters/velobankConverter");

const app = express();
const port = process.env.PORT || 3000;
Expand Down Expand Up @@ -39,6 +40,8 @@ function selectConverter(bank) {
return pekaoConverter;
case "monobank":
return monobankConverter;
case "velobank":
return velobankConverter;
default:
return cityConverter;
}
Expand All @@ -63,7 +66,8 @@ app.post("/", ({ files, body: { bank } }, response) => {
.on("error", getHttpErrorHandler(response))
.pipe(response);
} catch (err) {
response.status(500).send(err);
console.error('Unexpected error is occured', err)
response.status(500);
}
} else {
response.status(400).send("No file was uploaded.");
Expand Down
3 changes: 3 additions & 0 deletions mappingNormalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ exports.normalizeCategories = (categories) => categories && Object.keys(categori
const category = categories[categoryKey];
return Object.keys(category).reduce((subCategoriesWithPayers, subCategoryKey) => {
const payers = category[subCategoryKey] || []
if (!Array.isArray(payers)) {
throw new Error(`Payers for "${subCategoryKey}" is not an array`)
}
return payers.reduce((payersWithSubCategory, payer) =>
({ ...payersWithSubCategory, [payer]: { category: categoryKey, subCategory: subCategoryKey } }), subCategoriesWithPayers)
}, payerByCategory)
Expand Down
Loading