refactor: Move audit logging from controllers into services
Add auditContext parameter to service mutating functions. Services now
call audit log functions internally when auditContext is provided.
Controllers pass { userId, ipAddress, userAgent } and no longer import
audit service or fetch extra data for audit purposes.
Files modified:
- 10 service files: added audit imports and auditContext parameter
- 9 controller files: removed audit imports and calls
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { companies, companyReminders } from '../db/schema.js';
|
||||
import { eq, desc, sql, and, lte, gte, isNull, or, inArray } from 'drizzle-orm';
|
||||
import { NotFoundError, BadRequestError } from '../utils/errors.js';
|
||||
import { getAccessibleResourceIds } from '../middlewares/auth/resourceAccessMiddleware.js';
|
||||
import { logCompanyReminderCreated, logCompanyReminderUpdated, logCompanyReminderDeleted } from './audit.service.js';
|
||||
|
||||
const ensureCompanyExists = async (companyId) => {
|
||||
const [company] = await db
|
||||
@@ -44,7 +45,7 @@ export const getRemindersByCompanyId = async (companyId) => {
|
||||
return reminders;
|
||||
};
|
||||
|
||||
export const createReminder = async (companyId, data) => {
|
||||
export const createReminder = async (companyId, data, auditContext = null) => {
|
||||
await ensureCompanyExists(companyId);
|
||||
|
||||
const description = data.description?.trim();
|
||||
@@ -62,10 +63,14 @@ export const createReminder = async (companyId, data) => {
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (auditContext) {
|
||||
await logCompanyReminderCreated(auditContext.userId, reminder.id, companyId, data.dueDate, auditContext.ipAddress, auditContext.userAgent);
|
||||
}
|
||||
|
||||
return reminder;
|
||||
};
|
||||
|
||||
export const updateReminder = async (companyId, reminderId, data) => {
|
||||
export const updateReminder = async (companyId, reminderId, data, auditContext = null) => {
|
||||
const reminder = await getReminderById(reminderId);
|
||||
|
||||
if (reminder.companyId !== companyId) {
|
||||
@@ -91,10 +96,14 @@ export const updateReminder = async (companyId, reminderId, data) => {
|
||||
.where(eq(companyReminders.id, reminderId))
|
||||
.returning();
|
||||
|
||||
if (auditContext) {
|
||||
await logCompanyReminderUpdated(auditContext.userId, reminderId, companyId, reminder.dueDate, data.dueDate, auditContext.ipAddress, auditContext.userAgent);
|
||||
}
|
||||
|
||||
return updatedReminder;
|
||||
};
|
||||
|
||||
export const deleteReminder = async (companyId, reminderId) => {
|
||||
export const deleteReminder = async (companyId, reminderId, auditContext = null) => {
|
||||
const reminder = await getReminderById(reminderId);
|
||||
|
||||
if (reminder.companyId !== companyId) {
|
||||
@@ -103,6 +112,10 @@ export const deleteReminder = async (companyId, reminderId) => {
|
||||
|
||||
await db.delete(companyReminders).where(eq(companyReminders.id, reminderId));
|
||||
|
||||
if (auditContext) {
|
||||
await logCompanyReminderDeleted(auditContext.userId, reminderId, companyId, reminder.dueDate, auditContext.ipAddress, auditContext.userAgent);
|
||||
}
|
||||
|
||||
return { success: true, message: 'Reminder bol odstránený' };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user