Update bookings.service.ts

This commit is contained in:
Abubeker Yasin
2026-08-31 14:24:59 +03:00
parent a60bcf8163
commit 31121db07b

View File

@@ -99,8 +99,8 @@ export class BookingsService {
* findByIamUserId (the portal's own history) so the two can never disagree about
* what a phone number owns.
*
* Each sub-lookup is independently catch-and-warn: a phone match is a best-effort
* widening, and one unavailable source must not fail the whole listing.
* The IAM lookup is catch-and-warn: a phone match is a best-effort widening, and an
* unavailable IAM must not fail the whole listing.
*/
private async buildPhoneOwnershipClauses(
variants: string[],
@@ -126,47 +126,13 @@ export class BookingsService {
})).map(p => p.id)
: [];
// Guest bookings store phone in TravelerProfile.notes JSON (created for every guest
// booking). Catches cases where contactPhone was null but the profile recorded it.
const travelerRows = await this.dataSource
.query<{ passengerId: string }[]>(
`SELECT DISTINCT passenger_id AS "passengerId"
FROM passenger.traveler_profiles
WHERE notes IS NOT NULL
AND (notes::jsonb->>'phone') = ANY($1::text[])`,
[variants],
)
.catch((err: unknown) => {
this.logger.warn(`TravelerProfile phone lookup failed: ${err instanceof Error ? err.message : String(err)}`);
return [] as { passengerId: string }[];
});
// Guests who saved their profile (savePassengerDetails:true) have a
// SavedPassengerProfile row with phone + deviceId; guest bookings stash that
// deviceId in Booking.userAgent.
const savedProfileRows = await this.dataSource
.query<{ deviceId: string }[]>(
`SELECT DISTINCT device_id AS "deviceId"
FROM passenger.saved_passenger_profiles
WHERE phone = ANY($1::text[]) AND device_id IS NOT NULL`,
[variants],
)
.catch((err: unknown) => {
this.logger.warn(`SavedPassengerProfile phone lookup failed: ${err instanceof Error ? err.message : String(err)}`);
return [] as { deviceId: string }[];
});
const allPassengerIds = [...new Set([...iamPassengerIds, ...travelerRows.map(r => r.passengerId)])];
const guestDeviceIds = savedProfileRows.map(r => r.deviceId);
return {
clauses: [
{ contactPhone: { in: variants } },
{ passenger: { user: { phone: { in: variants } } } },
...(allPassengerIds.length > 0 ? [{ passengerId: { in: allPassengerIds } }] : []),
...(guestDeviceIds.length > 0 ? [{ userAgent: { in: guestDeviceIds } }] : []),
...(iamPassengerIds.length > 0 ? [{ passengerId: { in: iamPassengerIds } }] : []),
],
passengerIds: allPassengerIds,
passengerIds: iamPassengerIds,
};
}