Route stops distance and other fixes

This commit is contained in:
Stephanos A
2026-07-01 13:31:31 +03:00
parent 0db0d3af78
commit 4f1fc6c439
5 changed files with 52 additions and 163 deletions

View File

@@ -42,4 +42,5 @@ export class UpdateRouteDto {
@ApiPropertyOptional() @IsOptional() @IsString() description?: string;
@ApiPropertyOptional({ example: true }) @IsOptional() @IsBoolean() active?: boolean;
@ApiPropertyOptional({ example: '2027-12-31T23:59:59Z' }) @IsOptional() @IsDateString() effectiveUntil?: string;
@ApiPropertyOptional({ type: [RouteStopInputDto] }) @IsOptional() @IsArray() @ValidateNested({ each: true }) @Type(() => RouteStopInputDto) stops?: RouteStopInputDto[];
}

View File

@@ -81,7 +81,8 @@ export class RoutesService {
async updateRoute(id: string, dto: UpdateRouteDto) {
const route = await this.prisma.route.findUnique({ where: { id } });
if (!route) throw new NotFoundException('Route not found');
return this.prisma.route.update({
await this.prisma.route.update({
where: { id },
data: {
name: dto.name,
@@ -89,6 +90,22 @@ export class RoutesService {
active: dto.active,
effectiveUntil: dto.effectiveUntil ? new Date(dto.effectiveUntil) : undefined,
},
});
if (dto.stops && dto.stops.length >= 2) {
await this.prisma.routeStop.deleteMany({ where: { routeId: id } });
await this.prisma.routeStop.createMany({
data: dto.stops.map(s => ({
routeId: id,
stationId: s.stationId,
sequence: s.sequence,
distanceKm: s.distanceKm != null ? parseFloat(String(s.distanceKm)) : null,
})),
});
}
return this.prisma.route.findUnique({
where: { id },
include: { stops: { orderBy: { sequence: 'asc' } } },
});
}
@@ -128,6 +145,7 @@ export class RoutesService {
const station = await this.prisma.station.findUnique({ where: { id: dto.stationId } });
if (!station) throw new NotFoundException(`Station ${dto.stationId} not found`);
if (!station.isOperational) throw new BadRequestException(`Station ${dto.stationId} is not operational`);
const existing = await this.prisma.routeStop.findUnique({
where: { routeId_sequence: { routeId, sequence: dto.sequence } },