parse sms transaction

This commit is contained in:
Nikola Petrov
2023-09-27 21:51:57 +02:00
parent 2a67892f59
commit aefc3fb727
3 changed files with 84 additions and 13 deletions

View File

@@ -19,17 +19,54 @@ module.exports = {
},
create: async function (req, res) {
var transaction = new cashTransactionModel({
raw: req.body.messageBody
});
const rawString = req.body.messageBody;
const transaction = new cashTransactionModel({
raw: rawString,
day: 0,
month: 0,
year: 0,
amount: 0,
type: 0,
company: "",
});
const trans = await transaction.save()
if(trans){
return res.status(201).json({ message: 'Welcome to the API POST' });
}
else{
return res.status(400).json({message: "something went wrong"});
}
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: function (req, res) {