79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
var cashTransactionModel = require('../models/cashTransactionModel.js');
|
|
module.exports = {
|
|
|
|
list: async function (req, res) {
|
|
|
|
try{
|
|
|
|
const transactions = await cashTransactionModel.find()
|
|
const data = {
|
|
messages: transactions
|
|
};
|
|
return res.render('cash', data);
|
|
}catch (err) {
|
|
return res.status(500).json({
|
|
message: 'Error when getting transactions.',
|
|
error: err
|
|
});
|
|
};
|
|
},
|
|
|
|
create: async function (req, res) {
|
|
const rawString = req.body.messageBody;
|
|
|
|
const transaction = new cashTransactionModel({
|
|
raw: rawString,
|
|
day: 0,
|
|
month: 0,
|
|
year: 0,
|
|
amount: 0,
|
|
type: 0,
|
|
company: "",
|
|
});
|
|
|
|
const datePattern = /(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}/;
|
|
const amountPattern = /\d{1,3}(?:[.]\d{3})*(?:[.,]\d{2})(?=\sEUR)/;
|
|
const companyPattern1 = /(?<=(UR,|\sod)\s).*?(?=\s(na\s|za\s|Inf))/;
|
|
|
|
if (rawString.includes("ZAVRNITEV POS NAKUP")) transaction.type = 2;
|
|
else if (rawString.includes("POS NAKUP")) transaction.type = 1;
|
|
else if (rawString.includes("BA DVIG")) transaction.type = 3;
|
|
else if (rawString.includes("priliv")) transaction.type = 4;
|
|
else if (rawString.includes("SDD")) transaction.type = 5;
|
|
else if (rawString.includes("SPLET/TEL NAKUP")) transaction.type = 6;
|
|
else if (rawString.includes("PREDAVTORIZACIJE")) transaction.type = 7;
|
|
|
|
if(transaction.type != 0){
|
|
const amountMatch = rawString.match(amountPattern);
|
|
if (amountMatch){
|
|
const amount = amountMatch[0].replace('.', '').replace(',', '.');
|
|
transaction.amount = parseFloat( amount);
|
|
}
|
|
|
|
const companyMatch1 = rawString.match(companyPattern1);
|
|
if (companyMatch1) transaction.company = companyMatch1[0];
|
|
|
|
const dateMatch = rawString.match(datePattern);
|
|
if (dateMatch) {
|
|
const date = dateMatch[0].split('.');
|
|
transaction.day = date[0];
|
|
transaction.month = date[1];
|
|
transaction.year = date[2];
|
|
}
|
|
}
|
|
const trans = await transaction.save()
|
|
if(trans){
|
|
return res.status(201).json(trans);
|
|
}
|
|
else{
|
|
return res.status(400).json({message: "something went wrong"});
|
|
}
|
|
},
|
|
|
|
delete: async function (req, res) {
|
|
cashTransactionModel.deleteMany({})
|
|
.then(data =>{
|
|
res.status(201).json({message:"OK"});
|
|
});
|
|
},
|
|
}; |