audit logic
This commit is contained in:
213
water-api/src/controllers/audit.controller.ts
Normal file
213
water-api/src/controllers/audit.controller.ts
Normal file
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* Audit Controller
|
||||
* Handles HTTP requests for audit log operations
|
||||
*/
|
||||
|
||||
import { Response } from 'express';
|
||||
import { AuthenticatedRequest } from '../types';
|
||||
import * as auditService from '../services/audit.service';
|
||||
|
||||
/**
|
||||
* GET /audit-logs
|
||||
* Get audit logs with filters and pagination (admin only)
|
||||
*/
|
||||
export async function getAuditLogs(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const {
|
||||
userId,
|
||||
action,
|
||||
tableName,
|
||||
recordId,
|
||||
startDate,
|
||||
endDate,
|
||||
success,
|
||||
page = '1',
|
||||
limit = '50',
|
||||
} = req.query;
|
||||
|
||||
const filters: auditService.AuditLogFilters = {
|
||||
userId: userId as string,
|
||||
action: action as auditService.AuditAction,
|
||||
tableName: tableName as string,
|
||||
recordId: recordId as string,
|
||||
startDate: startDate ? new Date(startDate as string) : undefined,
|
||||
endDate: endDate ? new Date(endDate as string) : undefined,
|
||||
success: success === 'true' ? true : success === 'false' ? false : undefined,
|
||||
page: parseInt(page as string, 10),
|
||||
limit: parseInt(limit as string, 10),
|
||||
};
|
||||
|
||||
const result = await auditService.getAuditLogs(filters);
|
||||
|
||||
const totalPages = Math.ceil(result.total / filters.limit!);
|
||||
const hasNextPage = filters.page! < totalPages;
|
||||
const hasPreviousPage = filters.page! > 1;
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Audit logs retrieved successfully',
|
||||
data: result.logs,
|
||||
pagination: {
|
||||
page: filters.page,
|
||||
limit: filters.limit,
|
||||
total: result.total,
|
||||
totalPages,
|
||||
hasNextPage,
|
||||
hasPreviousPage,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching audit logs:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to fetch audit logs',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/:id
|
||||
* Get a single audit log by ID (admin only)
|
||||
*/
|
||||
export async function getAuditLogById(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const log = await auditService.getAuditLogById(id);
|
||||
|
||||
if (!log) {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: 'Audit log not found',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Audit log retrieved successfully',
|
||||
data: log,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching audit log:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to fetch audit log',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/record/:tableName/:recordId
|
||||
* Get audit logs for a specific record (admin only)
|
||||
*/
|
||||
export async function getAuditLogsForRecord(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { tableName, recordId } = req.params;
|
||||
|
||||
const logs = await auditService.getAuditLogsForRecord(tableName, recordId);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Audit logs retrieved successfully',
|
||||
data: logs,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching audit logs for record:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to fetch audit logs for record',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/statistics
|
||||
* Get audit statistics (admin only)
|
||||
*/
|
||||
export async function getAuditStatistics(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { days = '30' } = req.query;
|
||||
const daysNum = parseInt(days as string, 10);
|
||||
|
||||
const stats = await auditService.getAuditStatistics(daysNum);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Audit statistics retrieved successfully',
|
||||
data: stats,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching audit statistics:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to fetch audit statistics',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/my-activity
|
||||
* Get current user's own audit logs
|
||||
*/
|
||||
export async function getMyActivity(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const user = req.user;
|
||||
if (!user) {
|
||||
res.status(401).json({
|
||||
success: false,
|
||||
error: 'User not authenticated',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const { page = '1', limit = '50' } = req.query;
|
||||
|
||||
const filters: auditService.AuditLogFilters = {
|
||||
userId: user.userId,
|
||||
page: parseInt(page as string, 10),
|
||||
limit: parseInt(limit as string, 10),
|
||||
};
|
||||
|
||||
const result = await auditService.getAuditLogs(filters);
|
||||
|
||||
const totalPages = Math.ceil(result.total / filters.limit!);
|
||||
const hasNextPage = filters.page! < totalPages;
|
||||
const hasPreviousPage = filters.page! > 1;
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Your activity logs retrieved successfully',
|
||||
data: result.logs,
|
||||
pagination: {
|
||||
page: filters.page,
|
||||
limit: filters.limit,
|
||||
total: result.total,
|
||||
totalPages,
|
||||
hasNextPage,
|
||||
hasPreviousPage,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching user activity:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message || 'Failed to fetch activity logs',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user