feat: ( audit ) resolve the actor from the session and audit all backoffice mutations

This commit is contained in:
Abubeker Yasin
2026-08-18 15:13:22 +03:00
parent 33100a31ae
commit 0eb64ad9a1
32 changed files with 3068 additions and 420 deletions

View File

@@ -2,6 +2,22 @@ import { Injectable, NotFoundException, ConflictException } from '@nestjs/common
import { PrismaService } from '../../common/prisma.service';
import { DeleteOperationException } from '../../common/exceptions/delete-operation.exception';
import { AuditService } from '../../common/audit.service';
import { AUDIT_ACTIONS, AUDIT_ENTITIES } from '../../common/audit.actions';
import { snapshot } from '../../common/audit-snapshot';
/**
* Seat classes carry the per-km tariff rate, so `baseFareMinor` is the field an auditor is
* actually chasing — logging only the name made a price change indistinguishable from a rename.
*/
const SEAT_CLASS_AUDIT_FIELDS = [
'coachTypeId',
'name',
'description',
'baseFareMinor',
'premiumMinor',
'insuranceFeeMinor',
'isActive',
] as const;
@Injectable()
export class SeatClassesService {
@@ -30,7 +46,13 @@ export class SeatClassesService {
...(basePrice !== undefined && { baseFareMinor: basePrice }),
};
const updated = await this.prisma.seatClass.update({ where: { id }, data });
await this.auditService.log({ action: 'UPDATE', entityType: 'SeatClass', entityId: id, newData: { name: updated.name } });
await this.auditService.log({
action: AUDIT_ACTIONS.UPDATE,
entityType: AUDIT_ENTITIES.SeatClass,
entityId: id,
oldData: snapshot(sc, SEAT_CLASS_AUDIT_FIELDS),
newData: snapshot(updated, SEAT_CLASS_AUDIT_FIELDS),
});
return updated;
}
@@ -42,7 +64,12 @@ export class SeatClassesService {
...(basePrice !== undefined && { baseFareMinor: basePrice }),
};
const sc = await this.prisma.seatClass.create({ data });
await this.auditService.log({ action: 'CREATE', entityType: 'SeatClass', entityId: sc.id, newData: { name: sc.name } });
await this.auditService.log({
action: AUDIT_ACTIONS.CREATE,
entityType: AUDIT_ENTITIES.SeatClass,
entityId: sc.id,
newData: snapshot(sc, SEAT_CLASS_AUDIT_FIELDS),
});
return sc;
} catch (e: any) {
if (e.code === 'P2002') throw new ConflictException(`Seat class "${dto.name}" already exists`);
@@ -76,7 +103,13 @@ export class SeatClassesService {
}
const deleted = await this.prisma.seatClass.delete({ where: { id } });
await this.auditService.log({ action: 'DELETE', entityType: 'SeatClass', entityId: id, oldData: { name: sc.name } });
await this.auditService.log({
action: AUDIT_ACTIONS.DELETE,
entityType: AUDIT_ENTITIES.SeatClass,
entityId: id,
oldData: snapshot(sc, SEAT_CLASS_AUDIT_FIELDS),
newData: { cascade, fareRulesDeleted: cascade ? totalFareRules : 0 },
});
return deleted;
}
}