Skip to content

Commit

Permalink
added logs function
Browse files Browse the repository at this point in the history
  • Loading branch information
sangam2109 committed Jul 5, 2024
1 parent b54f713 commit c1cfc70
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 78 deletions.
39 changes: 39 additions & 0 deletions backend/models/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const mongoose = require('mongoose');

const logEntrySchema = new mongoose.Schema({
timestamp: {
type: Date,
default: Date.now,
required: true
},
user: {
type: String,
required: true,
},
logMessage: {
type: String,
required: true
}
});

// Middleware to ensure only the latest 200 entries are kept
logEntrySchema.pre('save', async function (next) {
try {
const count = await mongoose.models.LogEntry.countDocuments();
if (count >= 200) {
// Find and delete the oldest entry
const oldestEntry = await mongoose.models.LogEntry.findOne().sort({ timestamp: 1 });

if (oldestEntry) {
await oldestEntry.deleteOne();
}
}
next();
} catch (error) {
next(error);
}
});

const LogEntry = mongoose.model('LogEntry', logEntrySchema);

module.exports = LogEntry;
35 changes: 28 additions & 7 deletions backend/routes/UserProfileData/PlacementData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const SignUpdata = require('../../models/UserInfo').SignUp;
const router = express.Router();
const fetchuser = require('../../middleware/fetchUser');
const isAdmin = require('../../middleware/isAdmin');
const logEntry = require('../../models/logs')
const getUserCrn = require('../../utils/getAdminDetails')

// Route to create or update a user's placement data
router.post('/', fetchuser, async (req, res) => {
Expand Down Expand Up @@ -65,7 +67,13 @@ router.post('/updatelock', fetchuser, isAdmin, async (req, res) => {
if (!userData) {
return res.status(404).json({ success: false, message: 'User data not found' });
}

const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for ${crn} is updated to ${lock} in Placement Data`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand All @@ -75,8 +83,9 @@ router.post('/updatelock', fetchuser, isAdmin, async (req, res) => {

router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
try {
// Get all users with the role "user"
const usersToUpdate = await SignUpdata.find({ role: 'user' });
const { batch } = req.body

const usersToUpdate = await SignUpdata.find({ role: 'user', 'userInfo.batch': batch });

if (!usersToUpdate) {
return res.status(404).json({ message: 'No users found' });
Expand All @@ -95,7 +104,13 @@ router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
throw err; // Propagate error to stop execution
}
}));

const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for all Students of ${batch} is updated to true for placement Details`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand All @@ -104,9 +119,9 @@ router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
});
router.post('/unverifyall', fetchuser, isAdmin, async (req, res) => {
try {
// Get all users with the role "user"
const usersToUpdate = await SignUpdata.find({ role: 'user' });
const { batch } = req.body

const usersToUpdate = await SignUpdata.find({ role: 'user', 'userInfo.batch': batch });
if (!usersToUpdate) {
return res.status(404).json({ message: 'No users found' });
}
Expand All @@ -124,7 +139,13 @@ router.post('/unverifyall', fetchuser, isAdmin, async (req, res) => {
throw err; // Propagate error to stop execution
}
}));

const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for all Students of ${batch} is updated to false for placement Details`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand Down
46 changes: 37 additions & 9 deletions backend/routes/UserProfileData/Training1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const SignUpdata = require('../../models/UserInfo').SignUp;
const router = express.Router();
const fetchuser = require('../../middleware/fetchUser');
const isAdmin = require('../../middleware/isAdmin');
const logEntry = require('../../models/logs')
const getUserCrn = require('../../utils/getAdminDetails')
// Route to create a new user profile
router.post('/', fetchuser, async (req, res) => {
try {
Expand Down Expand Up @@ -51,6 +53,13 @@ router.post('/updatelock', fetchuser, isAdmin, async (req, res) => {
if (!userData) {
return res.status(404).json({ success: false, message: 'User data not found' });
}
const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for ${crn} is updated to ${lock} in tr101`
});
newLogEntry.save()

// Respond with the updated user data
res.status(200).json({ success: true });
Expand All @@ -76,9 +85,12 @@ router.get('/:crn', fetchuser, async (req, res) => {
router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
try {
// Get all users with the role "user"
const usersToUpdate = await SignUpdata.find({ role: 'user' });
if (!usersToUpdate) {
return res.status(404).json({ message: 'No users found' });
const { batch } = req.body

const usersToUpdate = await SignUpdata.find({ role: 'user', 'userInfo.batch': batch });

if (usersToUpdate.length === 0) {
return res.status(404).json({ message: 'No users found with the specified batch and role' });
}

// Update the lock status for all users
Expand All @@ -87,14 +99,20 @@ router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
if (user.tr101) {
user.tr101.lock = true; // Set lock status to true
await user.save();
}
}
return user;
} catch (err) {
console.error(`Error updating user with CRN ${user.crn}: ${err.message}`);
throw err; // Propagate error to stop execution
}
}));

const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for all Students of ${batch} is updated to true for tr101`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand All @@ -104,9 +122,12 @@ router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
router.post('/unverifyall', fetchuser, isAdmin, async (req, res) => {
try {
// Get all users with the role "user"
const usersToUpdate = await SignUpdata.find({ role: 'user' });
if (!usersToUpdate) {
return res.status(404).json({ message: 'No users found' });
const { batch } = req.body

const usersToUpdate = await SignUpdata.find({ role: 'user', 'userInfo.batch': batch });

if (usersToUpdate.length === 0) {
return res.status(404).json({ message: 'No users found with the specified batch and role' });
}

// Update the lock status for all users
Expand All @@ -115,13 +136,20 @@ router.post('/unverifyall', fetchuser, isAdmin, async (req, res) => {
if (user.tr101) {
user.tr101.lock = false; // Set lock status to true
await user.save();
}
}
return user;
} catch (err) {
console.error(`Error updating user with CRN ${user.crn}: ${err.message}`);
throw err; // Propagate error to stop execution
}
}));
const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for all Students of ${batch} is updated to false for tr101`
});
newLogEntry.save()

// Respond with the updated user data
res.status(200).json({ success: true });
Expand Down
37 changes: 31 additions & 6 deletions backend/routes/UserProfileData/Training2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const SignUpdata = require('../../models/UserInfo').SignUp;
const router = express.Router();
const fetchuser = require('../../middleware/fetchUser');
const isAdmin = require('../../middleware/isAdmin');
const logEntry = require('../../models/logs')
const getUserCrn = require('../../utils/getAdminDetails')
// Route to create a new user profile
router.post('/', fetchuser, async (req, res) => {
try {
Expand Down Expand Up @@ -48,6 +50,13 @@ router.post('/updatelock', fetchuser, isAdmin, async (req, res) => {
if (!userData) {
return res.status(404).json({ success: false, message: 'User data not found' });
}
const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for ${crn} is updated to ${lock} in tr102`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand All @@ -73,7 +82,9 @@ router.get('/:crn', fetchuser, async (req, res) => {
router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
try {
// Get all users with the role "user"
const usersToUpdate = await SignUpdata.find({ role: 'user' });
const { batch } = req.body

const usersToUpdate = await SignUpdata.find({ role: 'user', 'userInfo.batch': batch });

if (!usersToUpdate) {
return res.status(404).json({ message: 'No users found' });
Expand All @@ -85,14 +96,20 @@ router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
if (user.tr102) {
user.tr102.lock = true; // Set lock status to true
await user.save();
}
}
return user;
} catch (err) {
console.error(`Error updating user with CRN ${user.crn}: ${err.message}`);
throw err; // Propagate error to stop execution
}
}));

const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for all Students of ${batch} is updated to true for tr102`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand All @@ -103,7 +120,9 @@ router.post('/verifyall', fetchuser, isAdmin, async (req, res) => {
router.post('/unverifyall', fetchuser, isAdmin, async (req, res) => {
try {
// Get all users with the role "user"
const usersToUpdate = await SignUpdata.find({ role: 'user' });
const { batch } = req.body

const usersToUpdate = await SignUpdata.find({ role: 'user', 'userInfo.batch': batch });

if (!usersToUpdate) {
return res.status(404).json({ message: 'No users found' });
Expand All @@ -115,14 +134,20 @@ router.post('/unverifyall', fetchuser, isAdmin, async (req, res) => {
if (user.tr102) {
user.tr102.lock = false; // Set lock status to true
await user.save();
}
}
return user;
} catch (err) {
console.error(`Error updating user with CRN ${user.crn}: ${err.message}`);
throw err; // Propagate error to stop execution
}
}));

const token = req.header('auth-token')
const adminCrn = getUserCrn(token)
const newLogEntry = new logEntry({
user: adminCrn,
logMessage: `Verified Status for all Students of ${batch} is updated to false for tr102`
});
newLogEntry.save()
// Respond with the updated user data
res.status(200).json({ success: true });
} catch (error) {
Expand Down
Loading

0 comments on commit c1cfc70

Please sign in to comment.