mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 11:55:42 +00:00
Package booking new rules update, cascade delete option updates
This commit is contained in:
@@ -136,9 +136,10 @@ export class StationsController {
|
||||
@UseGuards(JwtGuard)
|
||||
@ApiBearerAuth('JWT-auth')
|
||||
@ApiOperation({ summary: 'Delete station' })
|
||||
@ApiQuery({ name: 'cascade', required: false, type: Boolean, description: 'Force delete with all related data' })
|
||||
@ApiResponse({ status: 200, description: 'Station deleted successfully' })
|
||||
@ApiResponse({ status: 404, description: 'Station not found' })
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
remove(@Param('id') id: string, @Query('cascade') cascade?: string) {
|
||||
return this.service.remove(id, cascade === 'true');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ export class StationsService {
|
||||
return updatedStation;
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
async remove(id: string, cascade = false) {
|
||||
const station = await this.prisma.station.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
@@ -107,24 +107,34 @@ export class StationsService {
|
||||
});
|
||||
if (!station) throw new NotFoundException('Station not found');
|
||||
|
||||
const [routeStopCount, originCount, destCount, stopTimeCount] = await Promise.all([
|
||||
this.prisma.routeStop.count({ where: { stationId: id } }),
|
||||
this.prisma.trainSchedule.count({ where: { originStationId: id } }),
|
||||
this.prisma.trainSchedule.count({ where: { destinationStationId: id } }),
|
||||
(station as any)._count.stopTimes as number,
|
||||
]);
|
||||
if (!cascade) {
|
||||
const [routeStopCount, originCount, destCount, stopTimeCount] = await Promise.all([
|
||||
this.prisma.routeStop.count({ where: { stationId: id } }),
|
||||
this.prisma.trainSchedule.count({ where: { originStationId: id } }),
|
||||
this.prisma.trainSchedule.count({ where: { destinationStationId: id } }),
|
||||
(station as any)._count.stopTimes as number,
|
||||
]);
|
||||
|
||||
const constraints = [];
|
||||
if (routeStopCount > 0)
|
||||
constraints.push({ entityName: 'route', count: routeStopCount, action: 'delete' as const });
|
||||
const scheduleCount = originCount + destCount;
|
||||
if (scheduleCount > 0)
|
||||
constraints.push({ entityName: 'schedule', count: scheduleCount, action: 'delete' as const });
|
||||
if (stopTimeCount > 0)
|
||||
constraints.push({ entityName: 'stop time', count: stopTimeCount, action: 'delete' as const });
|
||||
const constraints = [];
|
||||
if (routeStopCount > 0)
|
||||
constraints.push({ entityName: 'route', count: routeStopCount, action: 'delete' as const });
|
||||
const scheduleCount = originCount + destCount;
|
||||
if (scheduleCount > 0)
|
||||
constraints.push({ entityName: 'schedule', count: scheduleCount, action: 'delete' as const });
|
||||
if (stopTimeCount > 0)
|
||||
constraints.push({ entityName: 'stop time', count: stopTimeCount, action: 'delete' as const });
|
||||
|
||||
if (constraints.length > 0)
|
||||
throw new DeleteOperationException('Station', `${station.name} (${station.code})`, constraints);
|
||||
if (constraints.length > 0)
|
||||
throw new DeleteOperationException('Station', `${station.name} (${station.code})`, constraints);
|
||||
}
|
||||
|
||||
if (cascade) {
|
||||
await this.prisma.tripStopTime.deleteMany({ where: { stationId: id } });
|
||||
await this.prisma.trainSchedule.deleteMany({
|
||||
where: { OR: [{ originStationId: id }, { destinationStationId: id }] },
|
||||
});
|
||||
await this.prisma.routeStop.deleteMany({ where: { stationId: id } });
|
||||
}
|
||||
|
||||
const deleted = await this.prisma.station.delete({ where: { id } });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user