Refactor seat class handling

This commit is contained in:
Stephanos A
2026-05-21 14:31:07 +03:00
parent 2e42662729
commit 9151110fd8
14 changed files with 49 additions and 143 deletions

View File

@@ -1,5 +1,6 @@
import { IsString, IsInt, IsOptional } from 'class-validator';
import { IsString, IsInt, IsOptional, IsEnum, IsArray } from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType, OmitType } from '@nestjs/swagger';
import { ServiceClass } from '@prisma/client';
export class CreateTrainServiceDto {
@ApiProperty({ example: '301' }) @IsString() number: string;
@@ -9,7 +10,10 @@ export class CreateTrainServiceDto {
export class CreateCoachDto {
@ApiProperty({ example: 'trip-uuid' }) @IsString() tripId: string;
@ApiProperty({ example: 'A' }) @IsString() label: string;
@ApiProperty({ example: 'seat-class-uuid' }) @IsString() seatClassId: string;
@ApiProperty({ enum: ServiceClass, example: 'ECONOMY_REGULAR' }) @IsEnum(ServiceClass) serviceClass: ServiceClass;
@ApiPropertyOptional({ example: 'seat-class-uuid' }) @IsOptional() @IsString() seatClassId?: string;
@ApiPropertyOptional({ example: 60 }) @IsOptional() @IsInt() capacity?: number;
@ApiPropertyOptional({ example: 1 }) @IsOptional() @IsInt() sequence?: number;
}
export class UpdateCoachDto extends PartialType(OmitType(CreateCoachDto, ['tripId'] as const)) {}
@@ -17,5 +21,5 @@ export class UpdateCoachDto extends PartialType(OmitType(CreateCoachDto, ['tripI
export class CreateSeatBatchDto {
@ApiProperty({ example: 'coach-uuid' }) @IsString() coachId: string;
@ApiProperty({ example: 10 }) @IsInt() rows: number;
@ApiProperty({ example: ['A', 'B', 'C', 'D'] }) cols: string[];
@ApiProperty({ example: ['A', 'B', 'C', 'D'], type: [String] }) @IsArray() @IsString({ each: true }) cols: string[];
}

View File

@@ -12,26 +12,44 @@ export class FleetService {
listCoaches(tripId?: string) {
return this.prisma.coach.findMany({
where: tripId ? { tripId } : undefined,
include: { seatClass: { select: { id: true, name: true, basePrice: true } }, _count: { select: { seats: true } } },
include: { _count: { select: { seats: true } } },
orderBy: { label: 'asc' },
});
}
createCoach(dto: CreateCoachDto) { return this.prisma.coach.create({ data: dto, include: { seatClass: { select: { id: true, name: true, basePrice: true } } } }); }
createCoach(dto: CreateCoachDto) { return this.prisma.coach.create({ data: dto }); }
async updateCoach(id: string, dto: UpdateCoachDto) {
const coach = await this.prisma.coach.findUnique({ where: { id } });
if (!coach) throw new NotFoundException('Coach not found');
return this.prisma.coach.update({ where: { id }, data: dto, include: { seatClass: { select: { id: true, name: true, basePrice: true } } } });
return this.prisma.coach.update({ where: { id }, data: dto });
}
async createSeatBatch(dto: CreateSeatBatchDto) {
const coach = await this.prisma.coach.findUnique({ where: { id: dto.coachId } });
if (!coach) throw new NotFoundException('Coach not found');
const seats = [];
for (let row = 1; row <= dto.rows; row++) for (const col of dto.cols) seats.push({ coachId: dto.coachId, row, col, label: `${row}${col}` });
await this.prisma.seat.createMany({ data: seats, skipDuplicates: true });
return { created: seats.length };
try {
const coach = await this.prisma.coach.findUnique({ where: { id: dto.coachId } });
if (!coach) throw new NotFoundException('Coach not found');
const seats = [];
for (let row = 1; row <= dto.rows; row++) {
for (const col of dto.cols) {
const seatNumber = `${coach.label}${row}${col}`;
seats.push({
coachId: dto.coachId,
row,
col,
label: `${row}${col}`,
seatNumber
});
}
}
await this.prisma.seat.createMany({ data: seats, skipDuplicates: true });
return { created: seats.length };
} catch (error) {
console.error('Error in createSeatBatch:', error);
throw error;
}
}
async getAnalytics() {