Segment based seat assignement added

This commit is contained in:
Roba Boru
2026-05-27 15:11:23 +03:00
parent 3c6bd724f2
commit 2ceb993c60
10 changed files with 630 additions and 290 deletions

View File

@@ -102,7 +102,63 @@ export class SchedulesService {
},
});
if (!schedule) throw new NotFoundException('Schedule not found');
return schedule;
// Compute effective seat statuses from SeatHold + JourneySegment
// (seat.status DB column is no longer written during booking)
const allSeatIds = schedule.coachAssignments.flatMap(a => a.coach.seats.map(s => s.id));
const effectiveStatuses = await this.resolveEffectiveStatuses(id, allSeatIds);
return {
...schedule,
coachAssignments: schedule.coachAssignments.map(a => ({
...a,
coach: {
...a.coach,
seats: a.coach.seats.map(s => ({
...s,
status: effectiveStatuses.get(s.id) ?? s.status,
})),
},
})),
};
}
/**
* Computes effective seat status for a schedule by checking active SeatHolds
* and confirmed JourneySegments. The DB seat.status column is not written
* during segment-based booking, so this overlay is required.
* Priority: BLOCKED (physical) > BOOKED (confirmed) > HELD (active hold) > AVAILABLE
*/
private async resolveEffectiveStatuses(
scheduleId: string,
seatIds: string[],
): Promise<Map<string, string>> {
const statusMap = new Map<string, string>();
if (seatIds.length === 0) return statusMap;
const [activeHolds, bookedSegments] = await Promise.all([
this.prisma.seatHold.findMany({
where: { scheduleId, expiresAt: { gt: new Date() }, seatIds: { hasSome: seatIds } },
select: { seatIds: true },
}),
this.prisma.journeySegment.findMany({
where: {
scheduleId,
seatId: { in: seatIds },
journey: { status: { in: ['CONFIRMED', 'PENDING_PAYMENT'] } },
},
select: { seatId: true },
}),
]);
for (const hold of activeHolds)
for (const seatId of hold.seatIds)
if (seatIds.includes(seatId)) statusMap.set(seatId, 'HELD');
for (const seg of bookedSegments)
if (seg.seatId) statusMap.set(seg.seatId, 'BOOKED');
return statusMap;
}
updateScheduleStatus(id: string, dto: UpdateScheduleStatusDto) {