mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 00:38:11 +00:00
refactor: ( bookings ) unify authenticated booking flow with guest service
This commit is contained in:
@@ -254,11 +254,10 @@ export class PassengersController {
|
||||
}
|
||||
|
||||
try {
|
||||
const passenger = await this.prisma.passenger.findUnique({
|
||||
where: { iamUserId: req.user.id },
|
||||
});
|
||||
if (!passenger) return null;
|
||||
return this.service.getProfile(passenger.id);
|
||||
// Identity (name, DOB, gender, nationality, Fayda status) lives on the IAM user record,
|
||||
// not the Passenger row — return the full booking-form payload built from it so an
|
||||
// already-verified passenger's form can prefill and lock. See getMyProfile.
|
||||
return await this.service.getMyProfile(req.user.id);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ type IamUserRow = {
|
||||
name: { en: string; am: string } | null;
|
||||
phone_number: string | null;
|
||||
metadata: Record<string, any> | null;
|
||||
verified_by?: string | null;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
@@ -237,6 +238,66 @@ export class PassengersService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Full passenger-form payload for the logged-in user, sourced from the IAM user record
|
||||
* (iam.users) — where the Fayda-verified identity actually lives — rather than the sparse
|
||||
* Passenger row. The booking passenger form (/booking/passengers) calls this via
|
||||
* GET /passengers/me to prefill (and lock) an already-verified passenger's details.
|
||||
*
|
||||
* Identity fields don't depend on a Passenger row existing; only `id` (used later to tag the
|
||||
* primary passenger on the booking) does, and it's null if no Passenger row is linked yet.
|
||||
*/
|
||||
async getMyProfile(iamUserId: string) {
|
||||
const [passenger, iamRows] = await Promise.all([
|
||||
this.prisma.passenger.findUnique({ where: { iamUserId } }),
|
||||
this.dataSource.query<IamUserRow[]>(
|
||||
`SELECT id, email, name, phone_number, metadata, verified_by FROM iam.users WHERE id = $1 LIMIT 1`,
|
||||
[iamUserId],
|
||||
),
|
||||
]);
|
||||
|
||||
const iam = iamRows[0] ?? null;
|
||||
if (!iam && !passenger) return null;
|
||||
|
||||
const meta = iam?.metadata ?? {};
|
||||
const faydaVerified =
|
||||
iam?.verified_by === 'fayda' ||
|
||||
meta.faydaVerified === true ||
|
||||
meta.faydaVerified === 'true';
|
||||
|
||||
// Fayda writes gender as { am, en }; older/manual records may store a plain string.
|
||||
const gender =
|
||||
meta.gender && typeof meta.gender === 'object'
|
||||
? (meta.gender.en ?? meta.gender.am ?? null)
|
||||
: (meta.gender ?? null);
|
||||
|
||||
const fullName = iam?.name?.en ?? iam?.name?.am ?? null;
|
||||
|
||||
// Stored as ISO by the Fayda upsert; tolerate a "/"-separated legacy value.
|
||||
const rawDob = meta.dateOfBirth ?? meta.birthdate ?? null;
|
||||
const dateOfBirth = rawDob ? String(rawDob).replace(/\//g, '-') : null;
|
||||
|
||||
// Nationality isn't always in metadata; a Fayda-verified holder is Ethiopian by definition.
|
||||
const nationality = meta.nationality ?? (faydaVerified ? 'ETHIOPIAN' : null);
|
||||
|
||||
return {
|
||||
id: passenger?.id ?? null,
|
||||
fullName,
|
||||
email: iam?.email ?? meta.email ?? null,
|
||||
phone: iam?.phone_number ?? meta.phoneNumber ?? null,
|
||||
gender,
|
||||
dateOfBirth,
|
||||
nationality,
|
||||
faydaVerified,
|
||||
faydaSub: meta.sub ?? null,
|
||||
passportNumber: meta.passportNumber ?? null,
|
||||
passportCountry: meta.passportCountry ?? null,
|
||||
passportIssueDate: meta.passportIssueDate ?? null,
|
||||
passportExpiryDate: meta.passportExpiryDate ?? null,
|
||||
passportIssuingAuthority: meta.passportIssuingAuthority ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
async getStats(passengerId: string) {
|
||||
const [totalTrips, totalSpendResult, loyalty] = await Promise.all([
|
||||
this.prisma.booking.count({ where: { passengerId, status: 'BOARDED' as any } }),
|
||||
|
||||
Reference in New Issue
Block a user