diff --git a/apps/edr-passenger-api/prisma/migrations/20260101000000_add_configurable_fare_system/migration.sql b/apps/edr-passenger-api/prisma/migrations/20260101000000_add_configurable_fare_system/migration.sql index 15b2502f5..aa6cd855a 100644 --- a/apps/edr-passenger-api/prisma/migrations/20260101000000_add_configurable_fare_system/migration.sql +++ b/apps/edr-passenger-api/prisma/migrations/20260101000000_add_configurable_fare_system/migration.sql @@ -107,7 +107,7 @@ CREATE TABLE "system_features" ( "is_enabled" BOOLEAN NOT NULL DEFAULT false, "config" JSONB, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, + "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "system_features_pkey" PRIMARY KEY ("id") ); diff --git a/apps/edr-passenger-api/src/modules/passengers/passengers.service.ts b/apps/edr-passenger-api/src/modules/passengers/passengers.service.ts index 8805bc3c6..b2b7ae631 100644 --- a/apps/edr-passenger-api/src/modules/passengers/passengers.service.ts +++ b/apps/edr-passenger-api/src/modules/passengers/passengers.service.ts @@ -115,25 +115,35 @@ export class PassengersService { const guestBooking = (passenger as any)?.bookings?.[0] ?? null; const guestSeat = guestBooking?.seats?.[0] ?? null; + // Parse notes JSON to extract phone and other data + let notesData: any = null; + if (profile.notes) { + try { + notesData = typeof profile.notes === 'string' ? JSON.parse(profile.notes) : profile.notes; + } catch { + notesData = null; + } + } + return { id: profile.id, fullName: profile.fullName, - email: localUser?.email ?? iam?.email ?? guestBooking?.contactEmail ?? null, - phone: localUser?.phone ?? iam?.phone_number ?? guestBooking?.contactPhone ?? null, + email: localUser?.email ?? iam?.email ?? notesData?.email ?? guestBooking?.contactEmail ?? null, + phone: localUser?.phone ?? iam?.phone_number ?? notesData?.phone ?? guestBooking?.contactPhone ?? null, gender: profile.gender ?? localUser?.gender ?? iam?.metadata?.gender ?? null, dateOfBirth: profile.dateOfBirth ? new Date(profile.dateOfBirth).toISOString().split('T')[0] : (localUser?.dateOfBirth ? (localUser.dateOfBirth instanceof Date ? localUser.dateOfBirth.toISOString().split('T')[0] : localUser.dateOfBirth) : iam?.metadata?.dateOfBirth ?? null), - nationality: localUser?.nationality ?? iam?.metadata?.nationality ?? (guestSeat?.passportCountry ? (guestSeat.passportCountry === 'Ethiopia' ? 'Ethiopian' : guestSeat.passportCountry) : null), + nationality: localUser?.nationality ?? iam?.metadata?.nationality ?? notesData?.nationality ?? (guestSeat?.passportCountry ? (guestSeat.passportCountry === 'Ethiopia' ? 'Ethiopian' : guestSeat.passportCountry) : null), nationalityCode: localUser?.nationalityCode ?? iam?.metadata?.nationalityCode ?? null, faydaVerified, faydaVerifiedAt: localUser?.faydaVerifiedAt ?? iam?.metadata?.faydaVerifiedAt ?? null, - passportNumber: localUser?.passportNumber ?? iam?.metadata?.passportNumber ?? guestSeat?.passportNumber ?? null, - passportCountry: localUser?.passportCountry ?? iam?.metadata?.passportCountry ?? guestSeat?.passportCountry ?? null, + passportNumber: localUser?.passportNumber ?? iam?.metadata?.passportNumber ?? notesData?.passportNumber ?? guestSeat?.passportNumber ?? null, + passportCountry: localUser?.passportCountry ?? iam?.metadata?.passportCountry ?? notesData?.passportCountry ?? guestSeat?.passportCountry ?? null, passportExpiryDate: localUser?.passportExpiryDate ?? iam?.metadata?.passportExpiryDate ?? null, - idDocumentType: profile.nationalId ? 'NATIONAL_ID' : null, + idDocumentType: profile.nationalId ? 'NATIONAL_ID' : (notesData?.idDocumentType ?? null), verified: faydaVerified, lastLoginAt: localUser?.lastLoginAt ?? null, role: localUser?.role ?? null, diff --git a/apps/edr-passenger-api/src/modules/tickets/tickets.service.ts b/apps/edr-passenger-api/src/modules/tickets/tickets.service.ts index 8e78d710b..3c07d8c40 100644 --- a/apps/edr-passenger-api/src/modules/tickets/tickets.service.ts +++ b/apps/edr-passenger-api/src/modules/tickets/tickets.service.ts @@ -54,11 +54,11 @@ export class TicketsService { ...(filters.dateTo ? { lte: new Date(new Date(filters.dateTo).setHours(23, 59, 59, 999)) } : {}), }; } - // if (filters.coachId) { - // where.seat = { - // coachId: filters.coachId - // }; - // } + if (filters.coachId) { + where.seat = { + coachId: filters.coachId + }; + } const [tickets, total] = await Promise.all([ this.prisma.ticket.findMany({ @@ -67,8 +67,8 @@ export class TicketsService { booking: { include: { schedule: { include: { originStation: true, destinationStation: true, train: true } }, - returnSchedule: { select: { departureAt: true, arrivalAt: true, originStation: true, destinationStation: true } }, - passenger: { select: { id: true, iamUserId: true } }, + returnSchedule: { include: { originStation: true, destinationStation: true } }, + passenger: { include: { travelerProfiles: true } }, seats: { include: { seat: { include: { coach: true } } } }, }, }, @@ -93,9 +93,41 @@ export class TicketsService { return { items: tickets.map((t: any) => { const iam = t.booking?.passenger?.iamUserId ? iamMap.get(t.booking.passenger.iamUserId) : undefined; + + // Extract phone from TravelerProfile notes JSON + let guestPhone = null; + let guestEmail = null; + const matchingProfile = t.booking?.passenger?.travelerProfiles?.find((tp: any) => tp.fullName === t.passengerName); + + // DEBUG: Log to see what we're getting + this.logger.debug(`Ticket ${t.id}: passengerName=${t.passengerName}, profiles count=${t.booking?.passenger?.travelerProfiles?.length || 0}, matchingProfile=${!!matchingProfile}`); + if (matchingProfile) { + this.logger.debug(`Matching profile notes: ${matchingProfile.notes}`); + } + + if (matchingProfile?.notes) { + try { + const notesData = JSON.parse(matchingProfile.notes); + guestPhone = notesData.phone || null; + guestEmail = notesData.email || null; + this.logger.debug(`Extracted from notes: phone=${guestPhone}, email=${guestEmail}`); + } catch (err) { + this.logger.error(`Failed to parse notes JSON: ${err}`); + } + } + + // Fallback to booking contact info if no match in TravelerProfile + if (!guestPhone) guestPhone = t.booking?.contactPhone; + if (!guestEmail) guestEmail = t.booking?.contactEmail; + + this.logger.debug(`Final values: phone=${guestPhone}, email=${guestEmail}`); + const passengerInfo = iam ? { fullName: iam.name?.en ?? iam.name?.am ?? null, email: iam.email, phone: iam.phone_number } - : { fullName: 'Guest', email: t.booking?.contactEmail, phone: null }; + : { fullName: 'Guest', email: guestEmail, phone: guestPhone }; + + this.logger.debug(`Final passenger info: ${JSON.stringify(passengerInfo)}`); + return { id: t.id, ticketNumber: t.barcodePayload, diff --git a/apps/edr-passenger-web/backoffice/src/app/bookings/page.tsx b/apps/edr-passenger-web/backoffice/src/app/bookings/page.tsx index b4cf005e8..d841d144d 100644 --- a/apps/edr-passenger-web/backoffice/src/app/bookings/page.tsx +++ b/apps/edr-passenger-web/backoffice/src/app/bookings/page.tsx @@ -185,7 +185,7 @@ function BookingsPageContent() { }, }, { - key: 'contact', label: 'Contact', + key: 'contact', label: 'Primary contact', render: (booking: any) => (