diff --git a/apps/edr-passenger-api/src/modules/reports/reports.controller.ts b/apps/edr-passenger-api/src/modules/reports/reports.controller.ts index 82a07f98f..74bad3514 100644 --- a/apps/edr-passenger-api/src/modules/reports/reports.controller.ts +++ b/apps/edr-passenger-api/src/modules/reports/reports.controller.ts @@ -24,6 +24,12 @@ export class ReportsController { return this.service.listSchedulesForPicker(); } + @Get('passengers/list') + @ApiOperation({ summary: 'Flat passenger list for a specific schedule' }) + getPassengerList(@Query('scheduleId') scheduleId: string) { + return this.service.getPassengerList(scheduleId); + } + @Get('passengers') @ApiOperation({ summary: 'Passengers report for a specific schedule' }) getOccupancyReport(@Query('scheduleId') scheduleId: string) { diff --git a/apps/edr-passenger-api/src/modules/reports/reports.service.ts b/apps/edr-passenger-api/src/modules/reports/reports.service.ts index 9037188e2..daf1a7538 100644 --- a/apps/edr-passenger-api/src/modules/reports/reports.service.ts +++ b/apps/edr-passenger-api/src/modules/reports/reports.service.ts @@ -286,6 +286,35 @@ export class ReportsService { }; } + async getPassengerList(scheduleId: string) { + const seats = await this.prisma.bookingSeat.findMany({ + where: { + scheduleId, + booking: { status: { in: ['CONFIRMED', 'BOARDED'] } }, + }, + include: { + booking: { select: { bookingRef: true, status: true, originStationId: true, destinationStationId: true } }, + seat: { include: { coach: { select: { number: true, coachType: { select: { name: true } } } } } }, + }, + orderBy: [{ seat: { coach: { number: 'asc' } } }], + }); + + return seats.map(bs => ({ + bookingRef: bs.booking.bookingRef, + bookingStatus: bs.booking.status, + passengerName: bs.passengerName, + dateOfBirth: bs.dateOfBirth, + passengerCategory: bs.passengerCategory, + idDocumentType: bs.idDocumentType, + idDocumentNumber: bs.idDocumentNumber, + passportNumber: bs.passportNumber, + passportCountry: bs.passportCountry, + seatLabel: bs.seatLabelSnapshot, + coachNumber: bs.seat?.coach?.number ?? null, + coachType: (bs.seat?.coach as any)?.coachType?.name ?? null, + })); + } + async listSchedulesForPicker() { const schedules = await this.prisma.trainSchedule.findMany({ select: { diff --git a/apps/edr-passenger-web/backoffice/src/app/reports/passengers/page.tsx b/apps/edr-passenger-web/backoffice/src/app/reports/passengers/page.tsx index 83b803331..fe39e7e51 100644 --- a/apps/edr-passenger-web/backoffice/src/app/reports/passengers/page.tsx +++ b/apps/edr-passenger-web/backoffice/src/app/reports/passengers/page.tsx @@ -18,42 +18,82 @@ interface PassengersReport { byDestination: { stationName: string; passengers: number }[]; } +interface PassengerRow { + bookingRef: string; + bookingStatus: string; + passengerName: string; + dateOfBirth: string | null; + passengerCategory: string; + idDocumentType: string | null; + idDocumentNumber: string | null; + passportNumber: string | null; + passportCountry: string | null; + seatLabel: string | null; + coachNumber: string | null; + coachType: string | null; +} + +type Tab = 'occupancy' | 'list'; + export default function PassengersReportPage() { const [scheduleId, setScheduleId] = useState(''); - const [search, setSearch] = useState(''); - const [submittedId, setSubmittedId] = useState(''); + const [tab, setTab] = useState('occupancy'); + const [listSearch, setListSearch] = useState(''); const { data: schedules = [], isLoading: loadingSchedules } = useQuery({ queryKey: ['report-schedules'], queryFn: () => apiClient.get('/reports/schedules'), }); - const filtered = search.trim() - ? schedules.filter(s => s.label.toLowerCase().includes(search.toLowerCase())) - : schedules; - - const selected = schedules.find(s => s.id === scheduleId); - const { data, isLoading, isError } = useQuery({ - queryKey: ['passengers-report', submittedId], - queryFn: () => apiClient.get(`/reports/passengers?scheduleId=${submittedId}`), - enabled: !!submittedId, + queryKey: ['passengers-report', scheduleId], + queryFn: () => apiClient.get(`/reports/passengers?scheduleId=${scheduleId}`), + enabled: !!scheduleId, }); - const doExport = () => { - if (!data) return; - const rows = data.byCoach.map((c) => [c.coachNumber, c.coachType, String(c.totalSeats), String(c.booked), `${c.occupancyRate}%`]); - const headers = ['Coach', 'Type', 'Total Seats', 'Booked', 'Occupancy']; - const csv = [headers.join(','), ...rows.map((r) => r.join(','))].join('\n'); + const { data: passengerList = [], isLoading: listLoading } = useQuery({ + queryKey: ['passengers-list', scheduleId], + queryFn: () => apiClient.get(`/reports/passengers/list?scheduleId=${scheduleId}`), + enabled: !!scheduleId, + }); + + const filteredList = listSearch.trim() + ? passengerList.filter(p => + p.passengerName.toLowerCase().includes(listSearch.toLowerCase()) || + p.bookingRef.toLowerCase().includes(listSearch.toLowerCase()) || + (p.idDocumentNumber ?? '').toLowerCase().includes(listSearch.toLowerCase()) || + (p.passportNumber ?? '').toLowerCase().includes(listSearch.toLowerCase()), + ) + : passengerList; + + const downloadCsv = (csv: string, filename: string) => { const blob = new Blob([csv], { type: 'text/csv' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); - a.href = url; - a.download = `passengers-report-${submittedId}.csv`; - a.click(); + a.href = url; a.download = filename; a.click(); URL.revokeObjectURL(url); }; + const doExportOccupancy = () => { + if (!data) return; + const rows = data.byCoach.map(c => [c.coachNumber, c.coachType, String(c.totalSeats), String(c.booked), `${c.occupancyRate}%`]); + const csv = [['Coach', 'Type', 'Total Seats', 'Booked', 'Occupancy'].join(','), ...rows.map(r => r.join(','))].join('\n'); + downloadCsv(csv, `occupancy-${scheduleId}.csv`); + }; + + const doExportList = () => { + if (!passengerList.length) return; + const headers = ['Booking Ref', 'Status', 'Name', 'DOB', 'Category', 'ID Type', 'ID Number', 'Passport', 'Country', 'Seat', 'Coach', 'Class']; + const rows = passengerList.map(p => [ + p.bookingRef, p.bookingStatus, p.passengerName, p.dateOfBirth ?? '', + p.passengerCategory, p.idDocumentType ?? '', p.idDocumentNumber ?? '', + p.passportNumber ?? '', p.passportCountry ?? '', + p.seatLabel ?? '', p.coachNumber ?? '', p.coachType ?? '', + ].map(v => `"${String(v).replace(/"/g, '""')}"`)); + const csv = [headers.join(','), ...rows.map(r => r.join(','))].join('\n'); + downloadCsv(csv, `passengers-${scheduleId}.csv`); + }; + return (
@@ -61,52 +101,31 @@ export default function PassengersReportPage() {

Occupancy and passenger breakdown for a schedule

- {/* Schedule picker */} + {/* Schedule selector */}
-
- { setSearch(e.target.value); setScheduleId(''); }} - onFocus={(e) => { setSearch(e.target.value); }} - /> - {(search || scheduleId) && ( - - )} -
- {search && !scheduleId && ( -
- {filtered.length === 0 - ?

No schedules found

- : filtered.map(s => ( - - )) - } -
- )} +
- setSubmittedId(scheduleId)} disabled={!scheduleId || isLoading}> - Load Report - - {data && ( - - Export CSV - + {data && tab === 'occupancy' && ( + Export CSV + )} + {passengerList.length > 0 && tab === 'list' && ( + Export CSV )}
- {isLoading &&

Loading…

} + {(isLoading || listLoading) &&

Loading…

} {isError &&

Failed to load report.

}
@@ -122,118 +141,189 @@ export default function PassengersReportPage() {
- {/* Summary cards */} -
-
-
-

Total Seats

-
-
-

{data.summary.totalSeats}

-
-
-
-

Passengers

-
-
-

{data.summary.totalPassengers}

-
-
-
-

Occupancy Rate

-
-
-

{data.summary.occupancyRate}%

-
-
-
-
+ {/* Tabs */} +
+ {(['occupancy', 'list'] as Tab[]).map(t => ( + + ))}
- {/* By Coach */} -
-

By Coach

-
- - - - - - - - - - - - {data.byCoach.map((c) => ( - - - - - - - - ))} - -
CoachTypeSeatsBookedOccupancy
{c.coachNumber}{c.coachType}{c.totalSeats}{c.booked} + {/* Occupancy tab */} + {tab === 'occupancy' && ( +
+
+
+
+

Total Seats

+
+
+

{data.summary.totalSeats}

+
+
+
+

Passengers

+
+
+

{data.summary.totalPassengers}

+
+
+
+

Occupancy Rate

+
+
+

{data.summary.occupancyRate}%

+
+
+
+
+
+ +
+

By Coach

+
+ + + + + + + + + + {data.byCoach.map(c => ( + + + + + + + + ))} + +
CoachTypeSeatsBookedOccupancy
{c.coachNumber}{c.coachType}{c.totalSeats}{c.booked} +
+
+
+
+ {c.occupancyRate}% +
+
+
+
+ +
+
+

By Class

+
+ {data.byClass.map(c => ( +
+
+ {c.className} + {c.booked}/{c.totalSeats} +
-
+
- {c.occupancyRate}% + {c.occupancyRate}%
-
-
-
- - {/* By Class + By Origin/Destination */} -
-
-

By Class

-
- {data.byClass.map((c) => ( -
-
- {c.className} - {c.booked}/{c.totalSeats} -
-
-
-
- {c.occupancyRate}% -
+ ))}
- ))} +
+
+

By Boarding Station

+
+ {data.byOrigin.map(o => ( +
+ {o.stationName} + {o.passengers} +
+ ))} + {data.byOrigin.length === 0 &&

No data

} +
+
+
+

By Alighting Station

+
+ {data.byDestination.map(d => ( +
+ {d.stationName} + {d.passengers} +
+ ))} + {data.byDestination.length === 0 &&

No data

} +
+
+ )} -
-

By Boarding Station

-
- {data.byOrigin.map((o) => ( -
- {o.stationName} - {o.passengers} -
- ))} - {data.byOrigin.length === 0 &&

No data

} + {/* Passenger List tab */} + {tab === 'list' && ( +
+ setListSearch(e.target.value)} + /> +
+ + + + + + + + + + + + + + + {filteredList.map((p, i) => ( + + + + + + + + + + + ))} + {filteredList.length === 0 && ( + + )} + +
#NameCategoryID / PassportSeatCoachBooking RefStatus
{i + 1}{p.passengerName} + + {p.passengerCategory} + + + {p.idDocumentNumber ?? p.passportNumber ?? '—'} + {p.passportCountry && ({p.passportCountry})} + {p.seatLabel ?? '—'} + {p.coachNumber ?? '—'} + {p.coachType && ({p.coachType})} + {p.bookingRef} + + {p.bookingStatus} + +
No passengers found
- -
-

By Alighting Station

-
- {data.byDestination.map((d) => ( -
- {d.stationName} - {d.passengers} -
- ))} - {data.byDestination.length === 0 &&

No data

} -
-
-
+ )} )}