feat(atlas): show the most recently visited countries first on the dashboard (#2648)

The place and endpoint reads behind the passport tile now carry the last day
of each started trip, and getTravelStats orders the countries by their latest
visit, with countries only marked by hand after them. The country set is
unchanged; repository, parity and service tests pin the order.
This commit is contained in:
Maurice
2026-11-13 09:38:35 +01:00
parent 924b4e0787
commit 74af74fb5e
7 changed files with 159 additions and 25 deletions
@@ -19,6 +19,11 @@ interface PlaceCountryRow {
country_code: string | null;
}
/** A place of a started trip, with the trip's last day (its end, or its start when it has none) for ordering by recency. */
export interface StartedTripPlaceCountryRow extends PlaceCountryRow {
trip_last_date: string | null;
}
type PlaceRegionsKyselyDB = Pick<DB, 'place_regions' | 'places' | 'trips' | 'trip_members'>;
/**
@@ -192,22 +197,32 @@ export class PlaceRegionsRepository extends TrekRepository<PlaceRegions> {
/**
* AT44 (`getTravelStats`) — `SELECT DISTINCT p.id, p.lat, p.lng, p.address,
* pr.country_code FROM places p JOIN trips t ON p.trip_id = t.id LEFT JOIN
* pr.country_code, COALESCE(t.end_date, t.start_date) AS trip_last_date
* FROM places p JOIN trips t ON p.trip_id = t.id LEFT JOIN
* trip_members tm ON t.id = tm.trip_id LEFT JOIN place_regions pr ON
* pr.place_id = p.id WHERE (t.user_id = ? OR tm.user_id = ?) AND
* COALESCE(t.start_date, t.end_date) IS NOT NULL AND COALESCE(t.start_date,
* t.end_date) <= ? ORDER BY p.id` — "only started trips count" (#1048).
* Every place comes back, a place without a cached region with a null
* country (#2875). `today` (UTC) is resolved by the caller once
* (`todayUtc()`) and bound here.
* (`todayUtc()`) and bound here. A place sits on one trip, so the trip's
* last day adds no row to the DISTINCT; the dashboard orders the countries
* by it (#2648).
*/
async listStartedTripPlaceCountriesForUser(userId: number, today: string): Promise<PlaceCountryRow[]> {
async listStartedTripPlaceCountriesForUser(userId: number, today: string): Promise<StartedTripPlaceCountryRow[]> {
return await this.db()
.selectFrom('places as p')
.innerJoin('trips as t', 't.id', 'p.trip_id')
.leftJoin('trip_members as tm', 'tm.trip_id', 't.id')
.leftJoin('place_regions as pr', 'pr.place_id', 'p.id')
.select(['p.id', 'p.lat', 'p.lng', 'p.address', 'pr.country_code'])
.select((eb) => [
'p.id',
'p.lat',
'p.lng',
'p.address',
'pr.country_code',
eb.fn.coalesce('t.end_date', 't.start_date').as('trip_last_date'),
])
.distinct()
.where((eb) => eb.or([eb('t.user_id', '=', userId), eb('tm.user_id', '=', userId)]))
.where((eb) => eb(eb.fn.coalesce('t.start_date', 't.end_date'), 'is not', null))
@@ -201,9 +201,12 @@ export class ReservationEndpointsRepository extends TrekRepository<ReservationEn
* = ?) AND e.role IN ('from', 'to') AND COALESCE(t.start_date, t.end_date)
* IS NOT NULL AND COALESCE(t.start_date, t.end_date) <= date('now') AND
* ${TRAVELER_OWNS}`. `today` resolved by the caller once (`todayUtc()`),
* the `TripsRepository.activeTrip`/`lastStartedTrip` precedent.
* the `TripsRepository.activeTrip`/`lastStartedTrip` precedent. The row also
* carries `COALESCE(t.end_date, t.start_date) AS trip_last_date`, by which
* the dashboard orders the countries (#2648); an endpoint sits on one trip,
* so it adds no row to the DISTINCT.
*/
async listOwnedEndpointsForUser(userId: number, today: string): Promise<TravelerOwnedEndpointRow[]> {
async listOwnedEndpointsForUser(userId: number, today: string): Promise<TravelerOwnedEndpointWithTripDateRow[]> {
const rows = await this.kysely<TravelerOwnedEndpointsWithTripsKyselyDB>()
.selectFrom('reservation_endpoints as e')
.innerJoin('reservations as r', 'r.id', 'e.reservation_id')
@@ -228,6 +231,7 @@ export class ReservationEndpointsRepository extends TrekRepository<ReservationEn
.else(eb.fn.coalesce('r.reservation_time', 'r.reservation_end_time'))
.end()
.as('fallback_time'),
eb.fn.coalesce('t.end_date', 't.start_date').as('trip_last_date'),
])
.distinct()
.where((eb) => eb.or([eb('t.user_id', '=', userId), eb('tm.user_id', '=', userId)]))
@@ -236,7 +240,7 @@ export class ReservationEndpointsRepository extends TrekRepository<ReservationEn
.where((eb) => eb(eb.fn.coalesce('t.start_date', 't.end_date'), '<=', today))
.where((eb) => travelerOwnsExpr(eb, userId))
.execute();
return rows as TravelerOwnedEndpointRow[];
return rows as TravelerOwnedEndpointWithTripDateRow[];
}
/**
@@ -318,6 +322,11 @@ export interface TravelerOwnedEndpointRow {
fallback_time: string | null;
}
/** AT45's projection: AT6's, plus the last day of the trip the endpoint is on. */
export interface TravelerOwnedEndpointWithTripDateRow extends TravelerOwnedEndpointRow {
trip_last_date: string | null;
}
/** {@link ReservationEndpointsRepository.listOwnedEndpointsForTrips}'s `reservation_endpoints`/`reservations`/`reservation_travelers` tables (`r` aliased for `travelerOwnsExpr`, R7). */
type TravelerOwnedEndpointsKyselyDB = ReservationTravelersOwnsKyselyDB &
Pick<DB, 'reservation_endpoints' | 'reservations'>;