smc_cet_workingnotes_backend/app/controllers/working.notes.controller.js
suseendhiran17 ce4081b6b4 susee:log
2023-07-19 16:07:32 +05:30

242 lines
7.9 KiB
JavaScript

const { sequelize,Workingnotes} = require('../models')
const { logMsg } = require('../services/logger.js');
const fs = require('fs');
const path = require('path');
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
};
exports.readLogFileName = async (req , res) =>
{
try
{
folderName = req;
const logFilePath = 'storage/log';
const fileNames = fs.readdirSync(logFilePath);
// Remove the first element (0th index)
const [, ...data] = fileNames;
return 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.readLogFile = async (req , res) =>
{
try
{
folderName = req.body.folder;
fileName = req.body.file;
const logFilePath = 'storage/log/'+fileName;
fs.readFile(logFilePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send({'status':404,'message': err.message || "Error reading log file." ,'data': "No data"});
}
res.send({'status':200 , message:"Success." ,'data': 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.viewLandingPage = async (req , res) =>
{
try
{
moduleName = [ { "module":"WORKING NOTES","folder":"smc_cet_workingnotes_backend"} ];
img = []
for(const [index,val] of moduleName.entries())
{
fileName = await this.readLogFileName(val.folder);
for (let i = 0; i < fileName.length; i++)
{
filePath = 'storage/log/'+fileName[i];
const logData = fs.readFileSync(filePath);
// Convert the file content to Base64
const base64Data = logData.toString('base64');
// Create Base64 URL from Base64 data
const base64Url = `data:application/octet-stream;base64,${encodeURIComponent(base64Data)}`;
img.push(base64Url)
}
moduleName[index] = await Object.assign(val,{'file':fileName , 'img':img});
};
res.render( 'module' , { 'data' : moduleName } ,function (err, html ) {
res.send(html)
})
}
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.deleteLogFile = async (req , res) =>
{
try
{
folderName = req.body.folder;
fileName = req.body.file;
// folderName = 'smc_cet_creditpd_backend';
// fileName = 'msg.log.2023-07-10';
const logFilePath = 'storage/log/'+fileName;
fs.unlink(logFilePath, (err) => {
if (err) {
console.error(`Error deleting file: ${err}`);
return;
}
console.log('File deleted successfully.');
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
};