mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
feat: Apple Wallet passes for bookings
"Add to Apple Wallet" on a booking in the iOS app turns it into a TREK pass: a boarding pass from and to for flights, trains, buses and boats, check-in and check-out for a stay, pickup and drop-off for a car, the time for anything else, with the confirmation code as a QR code. Wallet brings it up on the lock screen around its time and place: times carry the offset of the endpoint's zone (looked up from the coordinates where the endpoint has none), stays and restaurants their place's coordinates. The labels come from the booking forms' own strings, so nothing new to translate on the pass. Only liketrek holds the Pass Type ID certificate, so the server sends the pass's texts to the TREK services' signer (TREK_WALLET_PASS_URL, push.liketrek.com) once per tap; it stores nothing, and TREK_SERVICES=off turns it off with the other services. Texts are cleaned of links and shortened here, the answer must be a .pkpass within 512 KB, and a user gets 20 passes a minute. - New wallet-pass domain: GET /api/trips/:tripId/reservations/:id/wallet-pass behind the trip guard, the booking checked to belong to the trip; trip title and place come from the reservations domain (findPassContext) - MWalletButton in the transport and booking sheets, only where the app binary has the wallet feature; undated bookings and failures are told apart - TREK_WALLET_PASS_URL in .env.example, compose, Helm and the wiki
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import type { Reservations } from '../entities/Reservations.entity';
|
||||
import { castIntegerKysely, concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
|
||||
import { coalesceOverride, columnRef, dayDistance } from '../dialect/sql-functions';
|
||||
import { DayAssignments } from '../entities/DayAssignments.entity';
|
||||
import { Days } from '../entities/Days.entity';
|
||||
import { coalesceOverride, columnRef, dayDistance } from '../dialect/sql-functions';
|
||||
import { castIntegerKysely, concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
|
||||
import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility';
|
||||
import type { DayAssignmentRow } from './day-assignment-rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
import type { Reservations } from '../entities/Reservations.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import { ROADTRIP_RESERVATION_TYPES } from '@trek/shared';
|
||||
import {
|
||||
publicReservationExpr,
|
||||
publicStayExists,
|
||||
type ReservationVisibilityKyselyDB,
|
||||
} from './_shared/reservation-visibility';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
import type { DayAssignmentRow } from './day-assignment-rows';
|
||||
import type {
|
||||
PublicApiAccommodationKyselyDB,
|
||||
PublicApiAccommodationRow,
|
||||
@@ -17,6 +20,7 @@ import type {
|
||||
PublicApiUnplannedPlaceRow,
|
||||
PublicApiUnscheduledReservationRow,
|
||||
} from './reservation-public-api-rows';
|
||||
import { ROADTRIP_RESERVATION_TYPES } from '@trek/shared';
|
||||
|
||||
// The public API row types moved to their own module; re-exported so callers keep one import path.
|
||||
export type {
|
||||
@@ -64,7 +68,9 @@ type ReservationJoinKyselyDB = Pick<DB, 'reservations' | 'days' | 'places' | 'da
|
||||
* TEXT column, widened to `string | number | null` so the plain `number`
|
||||
* their statements bind against it type-checks (R2's ruling).
|
||||
*/
|
||||
type ReservationsNumericAccommodationBind = Omit<DB['reservations'], 'accommodation_id'> & { accommodation_id: string | number | null };
|
||||
type ReservationsNumericAccommodationBind = Omit<DB['reservations'], 'accommodation_id'> & {
|
||||
accommodation_id: string | number | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Kysely typing for `restampLinkedReservation` (DY23). `accommodation_id:
|
||||
@@ -218,7 +224,8 @@ export interface CalendarStayRow {
|
||||
reservation_title: string | null;
|
||||
}
|
||||
|
||||
type CalendarStayKyselyDB = ReservationVisibilityKyselyDB & Pick<DB, 'day_accommodations' | 'reservations' | 'days' | 'places'>;
|
||||
type CalendarStayKyselyDB = ReservationVisibilityKyselyDB &
|
||||
Pick<DB, 'day_accommodations' | 'reservations' | 'days' | 'places'>;
|
||||
|
||||
/**
|
||||
* RS20 (`ReservationsService.listUpcoming`) — the CTE + `UNION ALL`
|
||||
@@ -268,7 +275,10 @@ interface EntriesRow extends UpcomingReservationRow {
|
||||
at_time: string | null;
|
||||
}
|
||||
|
||||
type UpcomingReservationsKyselyDB = Pick<DB, 'trips' | 'trip_members' | 'reservations' | 'days' | 'places' | 'day_accommodations'>;
|
||||
type UpcomingReservationsKyselyDB = Pick<
|
||||
DB,
|
||||
'trips' | 'trip_members' | 'reservations' | 'days' | 'places' | 'day_accommodations'
|
||||
>;
|
||||
|
||||
/**
|
||||
* `share.service.ts:251` (`publicEndpointsByReservation`) — `SELECT
|
||||
@@ -378,7 +388,42 @@ type SharePublicAccommodationKyselyDB = ReservationVisibilityKyselyDB & Pick<DB,
|
||||
*/
|
||||
type FlightsMissingEndpointsKyselyDB = Pick<DB, 'reservations' | 'reservation_endpoints'>;
|
||||
|
||||
/** `findPassContext`: the booking's trip title and the coordinates of its place or stay. */
|
||||
type PassContextKyselyDB = Pick<DB, 'reservations' | 'trips' | 'places' | 'day_accommodations'>;
|
||||
|
||||
/** What a booking's Wallet pass needs beyond the booking: where it is, and the trip's name. */
|
||||
export interface ReservationPassContext {
|
||||
trip_title: string | null;
|
||||
place_lat: number | null;
|
||||
place_lng: number | null;
|
||||
place_name: string | null;
|
||||
}
|
||||
|
||||
export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
/**
|
||||
* The trip's title and the place a booking is at (its own place, else its
|
||||
* stay's), for its Apple Wallet pass (WalletPassService). Scoped to the trip,
|
||||
* so a booking of another trip reads as none.
|
||||
*/
|
||||
async findPassContext(id: number, trip_id: number): Promise<ReservationPassContext | undefined> {
|
||||
const row = await this.kysely<PassContextKyselyDB>()
|
||||
.selectFrom('reservations as r')
|
||||
.innerJoin('trips as t', 't.id', 'r.trip_id')
|
||||
.leftJoin('places as p', 'p.id', 'r.place_id')
|
||||
.leftJoin('day_accommodations as ap', 'ap.id', 'r.accommodation_id')
|
||||
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
|
||||
.where('r.id', '=', id)
|
||||
.where('r.trip_id', '=', trip_id)
|
||||
.select([
|
||||
't.title as trip_title',
|
||||
(eb) => eb.fn.coalesce('p.lat', 'acc_p.lat').as('place_lat'),
|
||||
(eb) => eb.fn.coalesce('p.lng', 'acc_p.lng').as('place_lng'),
|
||||
(eb) => eb.fn.coalesce('p.name', 'acc_p.name').as('place_name'),
|
||||
])
|
||||
.executeTakeFirst();
|
||||
return row as ReservationPassContext | undefined;
|
||||
}
|
||||
|
||||
private joinedQuery() {
|
||||
return this.kysely<ReservationJoinKyselyDB>()
|
||||
.selectFrom('reservations as r')
|
||||
@@ -415,7 +460,9 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* projection for one reservation, no `ORDER BY` (a single row).
|
||||
*/
|
||||
async findWithJoins(id: number | string): Promise<ReservationJoinRow | undefined> {
|
||||
const row = await this.joinedQuery().where('r.id', '=', id as number).executeTakeFirst();
|
||||
const row = await this.joinedQuery()
|
||||
.where('r.id', '=', id as number)
|
||||
.executeTakeFirst();
|
||||
return row as ReservationJoinRow | undefined;
|
||||
}
|
||||
|
||||
@@ -435,11 +482,17 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
}
|
||||
|
||||
/** RS45 (`ReservationsService.remove`'s pre-image read) — `SELECT id, title, type, accommodation_id FROM reservations WHERE id = ? AND trip_id = ?`. */
|
||||
async findHeaderInTrip(id: number, trip_id: number): Promise<{ id: number; title: string; type: string | null; accommodation_id: string | null } | undefined> {
|
||||
async findHeaderInTrip(
|
||||
id: number,
|
||||
trip_id: number,
|
||||
): Promise<{ id: number; title: string; type: string | null; accommodation_id: string | null } | undefined> {
|
||||
return this.qb('r')
|
||||
.select(['r.id', 'r.title', 'r.type', 'r.accommodation_id'])
|
||||
.where({ id, trip: trip_id })
|
||||
.execute<{ id: number; title: string; type: string | null; accommodation_id: string | null } | undefined>('get', false);
|
||||
.execute<{ id: number; title: string; type: string | null; accommodation_id: string | null } | undefined>(
|
||||
'get',
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -448,19 +501,38 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* reservations WHERE trip_id = ? AND (type != 'hotel' OR accommodation_id
|
||||
* IS NULL) AND reservation_time IS NOT NULL`.
|
||||
*/
|
||||
async listResyncCandidates(trip_id: number): Promise<{
|
||||
id: number; reservation_time: string | null; reservation_end_time: string | null;
|
||||
day_id: number | null; end_day_id: number | null;
|
||||
}[]> {
|
||||
async listResyncCandidates(trip_id: number): Promise<
|
||||
{
|
||||
id: number;
|
||||
reservation_time: string | null;
|
||||
reservation_end_time: string | null;
|
||||
day_id: number | null;
|
||||
end_day_id: number | null;
|
||||
}[]
|
||||
> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('r')
|
||||
.select(['r.id', 'r.reservation_time', 'r.reservation_end_time', columnRef(platform, 'r.day_id').as('day_id'), columnRef(platform, 'r.end_day_id').as('end_day_id')])
|
||||
.select([
|
||||
'r.id',
|
||||
'r.reservation_time',
|
||||
'r.reservation_end_time',
|
||||
columnRef(platform, 'r.day_id').as('day_id'),
|
||||
columnRef(platform, 'r.end_day_id').as('end_day_id'),
|
||||
])
|
||||
.where({
|
||||
trip: trip_id,
|
||||
$or: [{ type: { $ne: 'hotel' } }, { accommodation_id: null }],
|
||||
reservation_time: { $ne: null },
|
||||
})
|
||||
.execute<{ id: number; reservation_time: string | null; reservation_end_time: string | null; day_id: number | null; end_day_id: number | null }[]>('all', false);
|
||||
.execute<
|
||||
{
|
||||
id: number;
|
||||
reservation_time: string | null;
|
||||
reservation_end_time: string | null;
|
||||
day_id: number | null;
|
||||
end_day_id: number | null;
|
||||
}[]
|
||||
>('all', false);
|
||||
}
|
||||
|
||||
/** RS13 (`resyncReservationDays`'s per-row write) — `UPDATE reservations SET day_id = ?, end_day_id = ? WHERE id = ?`. */
|
||||
@@ -530,42 +602,48 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* is handed — no SQL-side COALESCE. `id: number` (rule 21 — the SAME
|
||||
* `toRowId`-parsed value `findInTrip`'s gate read used).
|
||||
*/
|
||||
async updateReservation(id: number, write: {
|
||||
title: string;
|
||||
reservation_time: string | null;
|
||||
reservation_end_time: string | null;
|
||||
location: string | null;
|
||||
confirmation_number: string | null;
|
||||
notes: string | null;
|
||||
url: string | null;
|
||||
day_id: number | null;
|
||||
end_day_id: number | null;
|
||||
place_id: number | null;
|
||||
assignment_id: number | null;
|
||||
status: string;
|
||||
type: string;
|
||||
accommodation_id: string | null;
|
||||
metadata: string | null;
|
||||
needs_review: number;
|
||||
}): Promise<void> {
|
||||
await this.nativeUpdate({ id }, {
|
||||
title: write.title,
|
||||
reservation_time: write.reservation_time,
|
||||
reservation_end_time: write.reservation_end_time,
|
||||
location: write.location,
|
||||
confirmation_number: write.confirmation_number,
|
||||
notes: write.notes,
|
||||
url: write.url,
|
||||
day: write.day_id,
|
||||
endDay: write.end_day_id,
|
||||
place: write.place_id,
|
||||
assignment: write.assignment_id,
|
||||
status: write.status,
|
||||
type: write.type,
|
||||
accommodation_id: write.accommodation_id,
|
||||
metadata: write.metadata,
|
||||
needs_review: write.needs_review,
|
||||
});
|
||||
async updateReservation(
|
||||
id: number,
|
||||
write: {
|
||||
title: string;
|
||||
reservation_time: string | null;
|
||||
reservation_end_time: string | null;
|
||||
location: string | null;
|
||||
confirmation_number: string | null;
|
||||
notes: string | null;
|
||||
url: string | null;
|
||||
day_id: number | null;
|
||||
end_day_id: number | null;
|
||||
place_id: number | null;
|
||||
assignment_id: number | null;
|
||||
status: string;
|
||||
type: string;
|
||||
accommodation_id: string | null;
|
||||
metadata: string | null;
|
||||
needs_review: number;
|
||||
},
|
||||
): Promise<void> {
|
||||
await this.nativeUpdate(
|
||||
{ id },
|
||||
{
|
||||
title: write.title,
|
||||
reservation_time: write.reservation_time,
|
||||
reservation_end_time: write.reservation_end_time,
|
||||
location: write.location,
|
||||
confirmation_number: write.confirmation_number,
|
||||
notes: write.notes,
|
||||
url: write.url,
|
||||
day: write.day_id,
|
||||
endDay: write.end_day_id,
|
||||
place: write.place_id,
|
||||
assignment: write.assignment_id,
|
||||
status: write.status,
|
||||
type: write.type,
|
||||
accommodation_id: write.accommodation_id,
|
||||
metadata: write.metadata,
|
||||
needs_review: write.needs_review,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -585,15 +663,34 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
}
|
||||
|
||||
/** DY14 (`DaysService.restampReservationDates`) — `SELECT id, day_id, end_day_id, reservation_time, reservation_end_time FROM reservations WHERE trip_id = ?`. */
|
||||
async listForRestamp(trip_id: number): Promise<{
|
||||
id: number; day_id: number | null; end_day_id: number | null;
|
||||
reservation_time: string | null; reservation_end_time: string | null;
|
||||
}[]> {
|
||||
async listForRestamp(trip_id: number): Promise<
|
||||
{
|
||||
id: number;
|
||||
day_id: number | null;
|
||||
end_day_id: number | null;
|
||||
reservation_time: string | null;
|
||||
reservation_end_time: string | null;
|
||||
}[]
|
||||
> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('r')
|
||||
.select(['r.id', columnRef(platform, 'r.day_id').as('day_id'), columnRef(platform, 'r.end_day_id').as('end_day_id'), 'r.reservation_time', 'r.reservation_end_time'])
|
||||
.select([
|
||||
'r.id',
|
||||
columnRef(platform, 'r.day_id').as('day_id'),
|
||||
columnRef(platform, 'r.end_day_id').as('end_day_id'),
|
||||
'r.reservation_time',
|
||||
'r.reservation_end_time',
|
||||
])
|
||||
.where({ trip: trip_id })
|
||||
.execute<{ id: number; day_id: number | null; end_day_id: number | null; reservation_time: string | null; reservation_end_time: string | null }[]>('all', false);
|
||||
.execute<
|
||||
{
|
||||
id: number;
|
||||
day_id: number | null;
|
||||
end_day_id: number | null;
|
||||
reservation_time: string | null;
|
||||
reservation_end_time: string | null;
|
||||
}[]
|
||||
>('all', false);
|
||||
}
|
||||
|
||||
/** DY15 — `UPDATE reservations SET reservation_time = ? WHERE id = ?`. */
|
||||
@@ -632,7 +729,14 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.case()
|
||||
.when('reservation_time', 'is', null)
|
||||
.then(eb.val(date))
|
||||
.else(concatKysely(platform, eb, { value: date }, { expression: substringKysely(platform, eb, 'reservation_time', 11) }))
|
||||
.else(
|
||||
concatKysely(
|
||||
platform,
|
||||
eb,
|
||||
{ value: date },
|
||||
{ expression: substringKysely(platform, eb, 'reservation_time', 11) },
|
||||
),
|
||||
)
|
||||
.end(),
|
||||
}))
|
||||
.where('accommodation_id', '=', accommodation_id)
|
||||
@@ -980,7 +1084,12 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
}
|
||||
|
||||
/** RS20 — see {@link UpcomingReservationRow}'s docstring. */
|
||||
async listUpcomingForUser(user_id: number, today: string, now_hhmm: string, limit: number): Promise<UpcomingReservationRow[]> {
|
||||
async listUpcomingForUser(
|
||||
user_id: number,
|
||||
today: string,
|
||||
now_hhmm: string,
|
||||
limit: number,
|
||||
): Promise<UpcomingReservationRow[]> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
|
||||
// The `COALESCE(place name, nameless-stay's-linked-booking title, trip
|
||||
@@ -995,10 +1104,15 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.with('visible_trips', (qb) =>
|
||||
qb
|
||||
.selectFrom('trips as t')
|
||||
.leftJoin('trip_members as tm', (join) => join.onRef('tm.trip_id', '=', 't.id').on('tm.user_id', '=', user_id))
|
||||
.leftJoin('trip_members as tm', (join) =>
|
||||
join.onRef('tm.trip_id', '=', 't.id').on('tm.user_id', '=', user_id),
|
||||
)
|
||||
.select(['t.id', 't.title', 't.cover_image'])
|
||||
.where((eb) =>
|
||||
eb.and([eb.or([eb('t.user_id', '=', user_id), eb('tm.user_id', 'is not', null)]), eb('t.is_archived', '=', 0)]),
|
||||
eb.and([
|
||||
eb.or([eb('t.user_id', '=', user_id), eb('tm.user_id', 'is not', null)]),
|
||||
eb('t.is_archived', '=', 0),
|
||||
]),
|
||||
),
|
||||
)
|
||||
.with('entries', (qb) => {
|
||||
@@ -1048,8 +1162,8 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.select((eb) => [
|
||||
'a.id as id',
|
||||
'a.trip_id as trip_id',
|
||||
eb
|
||||
.fn.coalesce(
|
||||
eb.fn
|
||||
.coalesce(
|
||||
eb.ref('p.name'),
|
||||
eb
|
||||
.selectFrom('reservations as res')
|
||||
@@ -1089,8 +1203,8 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.select((eb) => [
|
||||
'a.id as id',
|
||||
'a.trip_id as trip_id',
|
||||
eb
|
||||
.fn.coalesce(
|
||||
eb.fn
|
||||
.coalesce(
|
||||
eb.ref('p.name'),
|
||||
eb
|
||||
.selectFrom('reservations as res')
|
||||
@@ -1125,7 +1239,21 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
return bookings.unionAll(checkins).unionAll(checkouts);
|
||||
})
|
||||
.selectFrom('entries')
|
||||
.select(['id', 'trip_id', 'title', 'type', 'status', 'location', 'reservation_time', 'confirmation_number', 'trip_title', 'trip_cover', 'day_date', 'place_name', 'place_image'])
|
||||
.select([
|
||||
'id',
|
||||
'trip_id',
|
||||
'title',
|
||||
'type',
|
||||
'status',
|
||||
'location',
|
||||
'reservation_time',
|
||||
'confirmation_number',
|
||||
'trip_title',
|
||||
'trip_cover',
|
||||
'day_date',
|
||||
'place_name',
|
||||
'place_image',
|
||||
])
|
||||
.where('at_date', 'is not', null)
|
||||
.where((eb) =>
|
||||
eb.or([
|
||||
@@ -1146,7 +1274,18 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<ShareEndpointsKyselyDB>()
|
||||
.selectFrom('reservation_endpoints as e')
|
||||
.innerJoin('reservations as r', 'r.id', 'e.reservation_id')
|
||||
.select(['e.reservation_id', 'e.role', 'e.sequence', 'e.name', 'e.code', 'e.lat', 'e.lng', 'e.timezone', 'e.local_date', 'e.local_time'])
|
||||
.select([
|
||||
'e.reservation_id',
|
||||
'e.role',
|
||||
'e.sequence',
|
||||
'e.name',
|
||||
'e.code',
|
||||
'e.lat',
|
||||
'e.lng',
|
||||
'e.timezone',
|
||||
'e.local_date',
|
||||
'e.local_time',
|
||||
])
|
||||
.where('r.trip_id', '=', trip_id as number)
|
||||
.orderBy('e.reservation_id', 'asc')
|
||||
.orderBy('e.sequence', 'asc')
|
||||
@@ -1170,9 +1309,22 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<SharePublicReservationKyselyDB>()
|
||||
.selectFrom('reservations as r')
|
||||
.select([
|
||||
'r.id', 'r.trip_id', 'r.day_id', 'r.end_day_id', 'r.place_id', 'r.accommodation_id',
|
||||
'r.title', 'r.type', 'r.status', 'r.location', 'r.reservation_time', 'r.reservation_end_time',
|
||||
'r.notes', 'r.url', 'r.metadata', 'r.created_at',
|
||||
'r.id',
|
||||
'r.trip_id',
|
||||
'r.day_id',
|
||||
'r.end_day_id',
|
||||
'r.place_id',
|
||||
'r.accommodation_id',
|
||||
'r.title',
|
||||
'r.type',
|
||||
'r.status',
|
||||
'r.location',
|
||||
'r.reservation_time',
|
||||
'r.reservation_end_time',
|
||||
'r.notes',
|
||||
'r.url',
|
||||
'r.metadata',
|
||||
'r.created_at',
|
||||
])
|
||||
.where('r.trip_id', '=', trip_id as number)
|
||||
.where((eb) => publicReservationExpr(eb, 'r.ingest_state'))
|
||||
@@ -1189,9 +1341,19 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.where((eb) => publicStayExists(eb))
|
||||
.innerJoin('places as p', 'p.id', 'a.place_id')
|
||||
.select([
|
||||
'a.id', 'a.trip_id', 'a.place_id', 'a.start_day_id', 'a.end_day_id',
|
||||
'a.check_in', 'a.check_in_end', 'a.check_out', 'a.notes',
|
||||
'p.name as place_name', 'p.address as place_address', 'p.lat as place_lat', 'p.lng as place_lng',
|
||||
'a.id',
|
||||
'a.trip_id',
|
||||
'a.place_id',
|
||||
'a.start_day_id',
|
||||
'a.end_day_id',
|
||||
'a.check_in',
|
||||
'a.check_in_end',
|
||||
'a.check_out',
|
||||
'a.notes',
|
||||
'p.name as place_name',
|
||||
'p.address as place_address',
|
||||
'p.lat as place_lat',
|
||||
'p.lng as place_lng',
|
||||
])
|
||||
.execute();
|
||||
return rows as SharePublicAccommodationRow[];
|
||||
@@ -1247,7 +1409,17 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.leftJoin('places as p', 'p.id', 'a.place_id')
|
||||
.leftJoin('days as ds', 'ds.id', 'a.start_day_id')
|
||||
.leftJoin('days as de', 'de.id', 'a.end_day_id')
|
||||
.select(['p.name', 'p.address', 'p.lat', 'p.lng', 'ds.date as start_date', 'de.date as end_date', 'a.check_in', 'a.check_out', 'a.notes'])
|
||||
.select([
|
||||
'p.name',
|
||||
'p.address',
|
||||
'p.lat',
|
||||
'p.lng',
|
||||
'ds.date as start_date',
|
||||
'de.date as end_date',
|
||||
'a.check_in',
|
||||
'a.check_out',
|
||||
'a.notes',
|
||||
])
|
||||
.where('a.trip_id', '=', trip_id)
|
||||
.orderBy('ds.date', 'asc')
|
||||
.execute();
|
||||
@@ -1260,12 +1432,24 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.selectFrom('places as p')
|
||||
.leftJoin('categories as c', 'c.id', 'p.category_id')
|
||||
.select((eb) => [
|
||||
'p.name', 'p.address', 'p.lat', 'p.lng', 'p.place_time', 'p.end_time',
|
||||
'p.duration_minutes', 'p.notes', 'p.transport_mode', 'c.name as category',
|
||||
'p.name',
|
||||
'p.address',
|
||||
'p.lat',
|
||||
'p.lng',
|
||||
'p.place_time',
|
||||
'p.end_time',
|
||||
'p.duration_minutes',
|
||||
'p.notes',
|
||||
'p.transport_mode',
|
||||
'c.name as category',
|
||||
])
|
||||
.where('p.trip_id', '=', trip_id)
|
||||
.where((eb) => eb.not(eb.exists(eb.selectFrom('day_assignments as da').select('da.id').whereRef('da.place_id', '=', 'p.id'))))
|
||||
.where((eb) => eb.not(eb.exists(eb.selectFrom('day_accommodations as a').select('a.id').whereRef('a.place_id', '=', 'p.id'))))
|
||||
.where((eb) =>
|
||||
eb.not(eb.exists(eb.selectFrom('day_assignments as da').select('da.id').whereRef('da.place_id', '=', 'p.id'))),
|
||||
)
|
||||
.where((eb) =>
|
||||
eb.not(eb.exists(eb.selectFrom('day_accommodations as a').select('a.id').whereRef('a.place_id', '=', 'p.id'))),
|
||||
)
|
||||
.orderBy('p.created_at', 'asc')
|
||||
.orderBy('p.id', 'asc')
|
||||
.execute();
|
||||
@@ -1305,7 +1489,10 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** BG54/BG85 — `SELECT id, metadata FROM reservations WHERE id = ? AND trip_id = ?`. */
|
||||
async getIdAndMetadata(id: number | string, trip_id: number | string): Promise<{ id: number; metadata: string | null } | undefined> {
|
||||
async getIdAndMetadata(
|
||||
id: number | string,
|
||||
trip_id: number | string,
|
||||
): Promise<{ id: number; metadata: string | null } | undefined> {
|
||||
return await this.kysely<Pick<DB, 'reservations'>>()
|
||||
.selectFrom('reservations')
|
||||
.select(['id', 'metadata'])
|
||||
@@ -1352,7 +1539,9 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
}
|
||||
|
||||
/** ATI2 (`airtrail-import.service.ts#importAirtrailFlights`, existing-flight signature dedup) — `SELECT r.id, r.reservation_time, r.metadata FROM reservations r WHERE r.trip_id = ? AND r.type = 'flight'`. */
|
||||
async listFlightReservationsForTrip(trip_id: number): Promise<{ id: number; reservation_time: string | null; metadata: string | null }[]> {
|
||||
async listFlightReservationsForTrip(
|
||||
trip_id: number,
|
||||
): Promise<{ id: number; reservation_time: string | null; metadata: string | null }[]> {
|
||||
return this.qb('r')
|
||||
.select(['r.id', 'r.reservation_time', 'r.metadata'])
|
||||
.where({ trip: trip_id, type: 'flight' })
|
||||
@@ -1370,8 +1559,16 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* untracked reservation. Preserved exactly, not "fixed" with an added
|
||||
* `uow.transactional` wrap.
|
||||
*/
|
||||
async linkAirtrailMultiLeg(id: number, external_id: string, external_owner_user_id: number, external_synced_at: string): Promise<void> {
|
||||
await this.nativeUpdate({ id }, { external_source: 'airtrail', external_id, external_owner_user_id, sync_enabled: 0, external_synced_at });
|
||||
async linkAirtrailMultiLeg(
|
||||
id: number,
|
||||
external_id: string,
|
||||
external_owner_user_id: number,
|
||||
external_synced_at: string,
|
||||
): Promise<void> {
|
||||
await this.nativeUpdate(
|
||||
{ id },
|
||||
{ external_source: 'airtrail', external_id, external_owner_user_id, sync_enabled: 0, external_synced_at },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1381,8 +1578,24 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* external_hash = ?, external_synced_at = ? WHERE id = ?`. Same
|
||||
* pre-existing tx gap as {@link linkAirtrailMultiLeg} — report don't fix.
|
||||
*/
|
||||
async linkAirtrailSingleFlight(id: number, external_id: string, external_owner_user_id: number, external_hash: string, external_synced_at: string): Promise<void> {
|
||||
await this.nativeUpdate({ id }, { external_source: 'airtrail', external_id, external_owner_user_id, sync_enabled: 1, external_hash, external_synced_at });
|
||||
async linkAirtrailSingleFlight(
|
||||
id: number,
|
||||
external_id: string,
|
||||
external_owner_user_id: number,
|
||||
external_hash: string,
|
||||
external_synced_at: string,
|
||||
): Promise<void> {
|
||||
await this.nativeUpdate(
|
||||
{ id },
|
||||
{
|
||||
external_source: 'airtrail',
|
||||
external_id,
|
||||
external_owner_user_id,
|
||||
sync_enabled: 1,
|
||||
external_hash,
|
||||
external_synced_at,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/** ATL2 (`airtrail-link.service.ts#detach`) — `UPDATE reservations SET sync_enabled = 0 WHERE id = ?`. */
|
||||
@@ -1397,12 +1610,36 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* via `columnRef` (the `listResyncCandidates` precedent above) — the
|
||||
* mirror column, not a bare select.
|
||||
*/
|
||||
async findAirtrailLinked(id: number): Promise<{ id: number; trip_id: number; external_id: string | null; external_owner_user_id: number | null; sync_enabled: number | null } | undefined> {
|
||||
async findAirtrailLinked(id: number): Promise<
|
||||
| {
|
||||
id: number;
|
||||
trip_id: number;
|
||||
external_id: string | null;
|
||||
external_owner_user_id: number | null;
|
||||
sync_enabled: number | null;
|
||||
}
|
||||
| undefined
|
||||
> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('r')
|
||||
.select(['r.id', columnRef(platform, 'r.trip_id').as('trip_id'), 'r.external_id', 'r.external_owner_user_id', 'r.sync_enabled'])
|
||||
.select([
|
||||
'r.id',
|
||||
columnRef(platform, 'r.trip_id').as('trip_id'),
|
||||
'r.external_id',
|
||||
'r.external_owner_user_id',
|
||||
'r.sync_enabled',
|
||||
])
|
||||
.where({ id, external_source: 'airtrail' })
|
||||
.execute<{ id: number; trip_id: number; external_id: string | null; external_owner_user_id: number | null; sync_enabled: number | null } | undefined>('get', false);
|
||||
.execute<
|
||||
| {
|
||||
id: number;
|
||||
trip_id: number;
|
||||
external_id: string | null;
|
||||
external_owner_user_id: number | null;
|
||||
sync_enabled: number | null;
|
||||
}
|
||||
| undefined
|
||||
>('get', false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1420,12 +1657,17 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* external_id, external_hash FROM reservations WHERE external_source =
|
||||
* 'airtrail' AND sync_enabled = 1 AND external_owner_user_id = ?`.
|
||||
*/
|
||||
async listAirtrailSyncCandidatesForOwner(owner_user_id: number): Promise<{ id: number; trip_id: number; external_id: string | null; external_hash: string | null }[]> {
|
||||
async listAirtrailSyncCandidatesForOwner(
|
||||
owner_user_id: number,
|
||||
): Promise<{ id: number; trip_id: number; external_id: string | null; external_hash: string | null }[]> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('r')
|
||||
.select(['r.id', columnRef(platform, 'r.trip_id').as('trip_id'), 'r.external_id', 'r.external_hash'])
|
||||
.where({ external_source: 'airtrail', sync_enabled: 1, external_owner_user_id: owner_user_id })
|
||||
.execute<{ id: number; trip_id: number; external_id: string | null; external_hash: string | null }[]>('all', false);
|
||||
.execute<{ id: number; trip_id: number; external_id: string | null; external_hash: string | null }[]>(
|
||||
'all',
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1452,12 +1694,20 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* EXISTS guard, same shape as `Reservations.repository.ts`'s own
|
||||
* `listUnplannedPlacesForPublicApi` NOT EXISTS precedent (`eb.not(eb.exists(...))`).
|
||||
*/
|
||||
async listFlightsMissingEndpoints(): Promise<{ id: number; metadata: string | null; reservation_time: string | null; reservation_end_time: string | null }[]> {
|
||||
async listFlightsMissingEndpoints(): Promise<
|
||||
{ id: number; metadata: string | null; reservation_time: string | null; reservation_end_time: string | null }[]
|
||||
> {
|
||||
const rows = await this.kysely<FlightsMissingEndpointsKyselyDB>()
|
||||
.selectFrom('reservations as r')
|
||||
.select(['r.id', 'r.metadata', 'r.reservation_time', 'r.reservation_end_time'])
|
||||
.where('r.type', '=', 'flight')
|
||||
.where((eb) => eb.not(eb.exists(eb.selectFrom('reservation_endpoints as e').select('e.id').whereRef('e.reservation_id', '=', 'r.id'))))
|
||||
.where((eb) =>
|
||||
eb.not(
|
||||
eb.exists(
|
||||
eb.selectFrom('reservation_endpoints as e').select('e.id').whereRef('e.reservation_id', '=', 'r.id'),
|
||||
),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
return rows;
|
||||
}
|
||||
@@ -1470,18 +1720,17 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
/** RPL4/RPL5's shared select list, the columns of `RoadtripCarrierRow`. */
|
||||
private roadtripCarrierQuery() {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('r')
|
||||
.select([
|
||||
'r.id',
|
||||
'r.type',
|
||||
'r.title',
|
||||
columnRef(platform, 'r.day_id').as('day_id'),
|
||||
columnRef(platform, 'r.end_day_id').as('end_day_id'),
|
||||
'r.reservation_time',
|
||||
'r.reservation_end_time',
|
||||
'r.metadata',
|
||||
'r.day_plan_position',
|
||||
]);
|
||||
return this.qb('r').select([
|
||||
'r.id',
|
||||
'r.type',
|
||||
'r.title',
|
||||
columnRef(platform, 'r.day_id').as('day_id'),
|
||||
columnRef(platform, 'r.end_day_id').as('end_day_id'),
|
||||
'r.reservation_time',
|
||||
'r.reservation_end_time',
|
||||
'r.metadata',
|
||||
'r.day_plan_position',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1504,9 +1753,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
* NULL`. Which of them count as rides is `undatedRides` (shared) alone.
|
||||
*/
|
||||
async listRoadtripUndated(trip_id: number): Promise<RoadtripCarrierRow[]> {
|
||||
return this.roadtripCarrierQuery()
|
||||
.where({ trip: trip_id, day: null })
|
||||
.execute<RoadtripCarrierRow[]>('all', false);
|
||||
return this.roadtripCarrierQuery().where({ trip: trip_id, day: null }).execute<RoadtripCarrierRow[]>('all', false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user