add password check in cash

This commit is contained in:
Nikola Petrov 2023-10-03 12:51:22 +02:00
parent d18ab5edf9
commit dd676b353f

View File

@ -1,25 +1,37 @@
var cashTransactionModel = require('../models/cashTransactionModel.js'); var cashTransactionModel = require('../models/cashTransactionModel.js');
var UserModel = require('../models/userModel');
module.exports = { module.exports = {
list: function (req, res) { list: async function (req, res) {
cashTransactionModel.find() const password = req.body.pass;
.then(transactions => { try{
var data = { const userFound = await UserModel.findOne({ pass: password });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
const transactions = await cashTransactionModel.find()
const data = {
messages: transactions messages: transactions
}; };
return res.render('cash', data); return res.render('cash', data);
}) }catch (err) {
.catch(err => {
return res.status(500).json({ return res.status(500).json({
message: 'Error when getting transactions.', message: 'Error when getting transactions.',
error: err error: err
}); });
}); };
}, },
create: async function (req, res) { create: async function (req, res) {
const rawString = req.body.messageBody; const rawString = req.body.messageBody;
const password = req.body.pass;
const userFound = await UserModel.findOne({ pass: password });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
const transaction = new cashTransactionModel({ const transaction = new cashTransactionModel({
raw: rawString, raw: rawString,
day: 0, day: 0,
@ -69,7 +81,12 @@ module.exports = {
} }
}, },
delete: function (req, res) { delete: async function (req, res) {
const password = req.body.pass;
const userFound = await UserModel.findOne({ pass: password });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
cashTransactionModel.deleteMany({}) cashTransactionModel.deleteMany({})
.then(data =>{ .then(data =>{
res.status(201).json({message:"OK"}); res.status(201).json({message:"OK"});