Booking currency updates, audit logging updates

This commit is contained in:
Stephanos A
2026-07-15 00:39:53 +03:00
parent 5aa1892e19
commit 25fdf88a77
26 changed files with 169 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { FleetController } from './fleet.controller';
import { FleetService } from './fleet.service';
import { AuditModule } from '../../common/audit.module';
@Module({ controllers: [FleetController], providers: [FleetService], exports: [FleetService] })
@Module({ imports: [AuditModule], controllers: [FleetController], providers: [FleetService], exports: [FleetService] })
export class FleetModule {}

View File

@@ -3,6 +3,7 @@ import { PrismaService } from '../../common/prisma.service';
import { CreateTrainDto, CreateCoachDto, UpdateCoachDto, AssignCoachDto, ListCoachesDto, CreateCoachTypeDto, UpdateCoachTypeDto, CreateClassDto, UpdateClassDto, GenerateSeatMapDto } from './fleet.dto';
import { SeatKind } from '@prisma/client';
import { DeleteOperationException } from '../../common/exceptions/delete-operation.exception';
import { AuditService } from '../../common/audit.service';
// Parses '2+2' → [2, 2], '2+2+2' → [2, 2, 2]
function parseArrangement(arrangement: string): number[] {
@@ -147,7 +148,7 @@ type SeatRow = {
@Injectable()
export class FleetService {
constructor(private prisma: PrismaService) {}
constructor(private prisma: PrismaService, private auditService: AuditService) {}
async createCoachType(dto: CreateCoachTypeDto) {
return this.prisma.coachType.create({
@@ -333,8 +334,8 @@ export class FleetService {
});
}
createTrain(dto: CreateTrainDto) {
return this.prisma.train.create({
async createTrain(dto: CreateTrainDto) {
const train = await this.prisma.train.create({
data: {
number: dto.number,
name: dto.name,
@@ -344,12 +345,14 @@ export class FleetService {
isActive: dto.isActive ?? true,
},
});
await this.auditService.log({ action: 'CREATE', entityType: 'Train', entityId: train.id, newData: { number: train.number, name: train.name } });
return train;
}
async updateTrain(id: string, dto: CreateTrainDto) {
const train = await this.prisma.train.findUnique({ where: { id } });
if (!train) throw new NotFoundException('Train not found');
return this.prisma.train.update({
const updated = await this.prisma.train.update({
where: { id },
data: {
number: dto.number,
@@ -360,6 +363,8 @@ export class FleetService {
...(dto.isActive !== undefined && { isActive: dto.isActive }),
},
});
await this.auditService.log({ action: 'UPDATE', entityType: 'Train', entityId: id, newData: { number: dto.number, name: dto.name } });
return updated;
}
async deleteTrain(id: string, cascade = false) {
@@ -436,7 +441,9 @@ export class FleetService {
await this.prisma.trainSchedule.deleteMany({ where: { id: { in: scheduleIds } } });
}
return this.prisma.train.delete({ where: { id } });
const deleted = await this.prisma.train.delete({ where: { id } });
await this.auditService.log({ action: 'DELETE', entityType: 'Train', entityId: id, oldData: { number: train.number, name: train.name } });
return deleted;
}
async restoreTrain(id: string) {
@@ -519,6 +526,7 @@ export class FleetService {
await this.prisma.seat.createMany({ data: seats });
}
await this.auditService.log({ action: 'CREATE', entityType: 'Coach', entityId: coach.id, newData: { number: coach.number, capacity: coach.capacity } });
return coach;
}
@@ -526,7 +534,7 @@ export class FleetService {
const coach = await this.prisma.coach.findUnique({ where: { id } });
if (!coach) throw new NotFoundException('Coach not found');
return this.prisma.coach.update({
const updated = await this.prisma.coach.update({
where: { id },
data: {
number: dto.number,
@@ -537,6 +545,8 @@ export class FleetService {
},
include: { coachType: true },
});
await this.auditService.log({ action: 'UPDATE', entityType: 'Coach', entityId: id, newData: { number: dto.number, status: dto.status } });
return updated;
}
async deleteCoach(id: string, cascade = false) {
@@ -611,7 +621,9 @@ export class FleetService {
await this.prisma.seat.deleteMany({ where: { coachId: id } });
return this.prisma.coach.delete({ where: { id } });
const deleted = await this.prisma.coach.delete({ where: { id } });
await this.auditService.log({ action: 'DELETE', entityType: 'Coach', entityId: id, oldData: { number: coach.number } });
return deleted;
}
async assignCoach(dto: AssignCoachDto) {