|
| 1 | +import { SystemLog } from "../models/log.model.js"; |
| 2 | +import { asyncHandler } from "../utils/asyncHandler.js"; |
| 3 | +import { ApiResponse } from "../utils/ApiResponse.js"; |
| 4 | + |
| 5 | +// @desc Get Admin Activity Logs |
| 6 | +// @route GET /api/v1/admin/logs/activity |
| 7 | +// @access Private (Admin) |
| 8 | +export const getActivityLogs = asyncHandler(async (req, res) => { |
| 9 | + const page = parseInt(req.query.page) || 1; |
| 10 | + const limit = parseInt(req.query.limit) || 50; |
| 11 | + const skip = (page - 1) * limit; |
| 12 | + |
| 13 | + const logs = await SystemLog.find({ type: 'ADMIN_ACTIVITY' }) |
| 14 | + .sort({ createdAt: -1 }) |
| 15 | + .skip(skip) |
| 16 | + .limit(limit); |
| 17 | + |
| 18 | + const totalLogs = await SystemLog.countDocuments({ type: 'ADMIN_ACTIVITY' }); |
| 19 | + |
| 20 | + res.status(200).json( |
| 21 | + new ApiResponse(200, { |
| 22 | + logs, |
| 23 | + totalPages: Math.ceil(totalLogs / limit), |
| 24 | + currentPage: page, |
| 25 | + totalLogs |
| 26 | + }, "Activity logs fetched successfully") |
| 27 | + ); |
| 28 | +}); |
| 29 | + |
| 30 | +// @desc Get Public Access Logs |
| 31 | +// @route GET /api/v1/admin/logs/access |
| 32 | +// @access Private (Admin) |
| 33 | +export const getAccessLogs = asyncHandler(async (req, res) => { |
| 34 | + const page = parseInt(req.query.page) || 1; |
| 35 | + const limit = parseInt(req.query.limit) || 50; |
| 36 | + const skip = (page - 1) * limit; |
| 37 | + |
| 38 | + const logs = await SystemLog.find({ type: 'PUBLIC_ACCESS' }) |
| 39 | + .sort({ createdAt: -1 }) |
| 40 | + .skip(skip) |
| 41 | + .limit(limit); |
| 42 | + |
| 43 | + const totalLogs = await SystemLog.countDocuments({ type: 'PUBLIC_ACCESS' }); |
| 44 | + |
| 45 | + res.status(200).json( |
| 46 | + new ApiResponse(200, { |
| 47 | + logs, |
| 48 | + totalPages: Math.ceil(totalLogs / limit), |
| 49 | + currentPage: page, |
| 50 | + totalLogs |
| 51 | + }, "Access logs fetched successfully") |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +// @desc Clear Admin Activity Logs |
| 56 | +// @route DELETE /api/v1/admin/logs/activity |
| 57 | +// @access Private (Admin) |
| 58 | +export const clearActivityLogs = asyncHandler(async (req, res) => { |
| 59 | + await SystemLog.deleteMany({ type: 'ADMIN_ACTIVITY' }); |
| 60 | + |
| 61 | + if (req.admin) { |
| 62 | + await SystemLog.create({ |
| 63 | + type: 'ADMIN_ACTIVITY', |
| 64 | + adminId: req.admin._id, |
| 65 | + adminName: req.admin.name || 'Admin', |
| 66 | + action: 'Cleared all admin activity logs', |
| 67 | + details: { ip: req.ip || req.headers['x-forwarded-for'] || req.connection?.remoteAddress } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + res.status(200).json(new ApiResponse(200, null, "Activity logs cleared successfully")); |
| 72 | +}); |
| 73 | + |
| 74 | +// @desc Clear Public Access Logs |
| 75 | +// @route DELETE /api/v1/admin/logs/access |
| 76 | +// @access Private (Admin) |
| 77 | +export const clearAccessLogs = asyncHandler(async (req, res) => { |
| 78 | + await SystemLog.deleteMany({ type: 'PUBLIC_ACCESS' }); |
| 79 | + |
| 80 | + if (req.admin) { |
| 81 | + await SystemLog.create({ |
| 82 | + type: 'ADMIN_ACTIVITY', |
| 83 | + adminId: req.admin._id, |
| 84 | + adminName: req.admin.name || 'Admin', |
| 85 | + action: 'Cleared all public access logs', |
| 86 | + details: { ip: req.ip || req.headers['x-forwarded-for'] || req.connection?.remoteAddress } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + res.status(200).json(new ApiResponse(200, null, "Access logs cleared successfully")); |
| 91 | +}); |
0 commit comments