39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
function parse(data) {
|
|
if (typeof data !== 'object' || data === null) {
|
|
throw new Error(`Bad Data, expected an object, got: ${data === null ? null : typeof data}`);
|
|
}
|
|
|
|
if ((typeof data.title) !== 'string') {
|
|
throw new Error(`Bad Title: ${data.title}`);
|
|
}
|
|
|
|
if (!['string', 'number'].includes(typeof data.amount)) {
|
|
throw new Error(`Bad amount: ${data.amount}`);
|
|
}
|
|
|
|
if (typeof data.amount === 'string') {
|
|
const value = Number(data.amount.replaceAll(/[^\d]/g, ''));
|
|
data.amount = Number.isNaN(value) ? 0 : value;
|
|
}
|
|
|
|
|
|
return data;
|
|
}
|
|
|
|
function makeTransaction(data, account) {
|
|
return {
|
|
account,
|
|
date: new Date().toLocaleDateString('sv-SE'), // "YYYY-MM-DD"
|
|
payee_name: data.title,
|
|
// Actual considers this an integer representing a decimal value so the last two digits of the integer
|
|
// are placed after the decimal point (unclear why but okay...)
|
|
// It has to be negative or actual will think it's a deposit
|
|
amount: data.amount * -100,
|
|
cleared: true,
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
makeTransaction,
|
|
parse
|
|
};
|