smc_cet_workingnotes_backend/app/controllers/working.notes.controller.js
2022-06-02 11:49:01 +05:30

140 lines
5.2 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,
module_name: req.body.module_name,
entity_reference: req.body.entity_reference,
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 , module_name : req.body.module_name, 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,
module_name: req.body.module_name,
entity_reference: req.body.entity_reference,
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
{
if(req.query.entity_reference == undefined)
{
notesData = await Workingnotes.findAll({where :{losid : req.query.losid , module_name : req.query.module_name , is_active:1}})
.catch(err=>{console.log(err);});
res.send({'status':200 , message:"Success." ,'data': notesData});
}else{
notesData = await Workingnotes.findAll({where :{losid : req.query.losid , module_name : req.query.module_name , entity_reference : req.query.entity_reference, is_active:1}});
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
};