smc_cet_workingnotes_backend/app/controllers/working.notes.controller.js
2022-06-08 12:20:51 +05:30

127 lines
4.5 KiB
JavaScript

const { sequelize,Workingnotes} = require('../models')
const { logMsg } = require('../services/logger.js');
exports.createNotes = async (req, res) =>
{
try
{
for (const value of req.body.notes)
{
notes = JSON.stringify(value);
data = {
losid: req.body.losid,
notes: notes,
sheet_no : value.index + 1,
user_id: req.body.user_id,
createdby: req.body.createdby
};
// insert working notes data
await Workingnotes.create(data)
.then(data=>{ logMsg.info("Notes insert Success");})
.catch(err=>{ logMsg.info("Notes insert Failed");});
}
res.send({'status':200 , message:"Success." ,'data': "no data"});
} catch(err) {
res.status(500).send({'status':404,
message: err.message || "Some error occurred while inserting statements." ,'data': "No data"
}); } //end of try catch
};
exports.updateNotes = async (req, res) =>
{
try
{
for (const value of req.body.notes)
{
sheet_no = value.index + 1;
notesData = await Workingnotes.findOne({where :{losid : req.body.losid , sheet_no : sheet_no , is_active:1}});
if(notesData != undefined)
{
id = notesData.dataValues.id;
notes = JSON.stringify(value);
// update working notes data
await Workingnotes.update({notes: notes , updatedby: req.body.updatedby} , { where: { id: id } })
.then(data=>{ logMsg.info("Notes update Success");})
.catch(err=>{ logMsg.info("Notes update Failed");});
}else{
notes = JSON.stringify(value);
data = {
losid: req.body.losid,
notes: notes,
sheet_no : value.index + 1,
user_id: req.body.user_id,
createdby: req.body.createdby
};
// insert working notes data
await Workingnotes.create(data)
.then(data=>{ logMsg.info("Notes insert Success");})
.catch(err=>{ logMsg.info("Notes insert Failed");});
}
}
res.send({'status':200 , message:"Success." ,'data': "no data"});
} catch(err) {
res.status(500).send({'status':404,
message: err.message || "Some error occurred while inserting statements." ,'data': "No data"
}); } //end of try catch
};
exports.notesList = async (req, res) =>
{
try
{
notesData = await Workingnotes.findAll({where :{losid : req.query.losid, is_active:1}}).catch(err=>{console.log(err);});
res.send({'status':200 , message:"Success." ,'data': notesData});
} catch(err) {
res.status(500).send({'status':404,
message: err.message || "Some error occurred while inserting statements." ,'data': "No data"
}); } //end of try catch
};
exports.deleteNotes = async (req, res) =>
{
try
{
for (const value of req.body.id)
{
// update working notes data (is_active = 0)
await Workingnotes.update({is_active: 0} , { where: { id: value } })
.then(data=>{ logMsg.info("Notes is_active = 0 update Success");})
.catch(err=>{ logMsg.info("Notes is_active = 0 update Failed");});
}
res.send({'status':200 , message:"Success." ,'data': "no data"});
} catch(err) {
res.status(500).send({'status':404,
message: err.message || "Some error occurred while inserting statements." ,'data': "No data"
}); } //end of try catch
};