feat(public-api): next_trip in /api/v1/stats counts down to the next trip

The counterpart of last_trip for a homepage widget: the nearest trip that has not started yet, with its countries and the whole days until it starts, or null when nothing is planned. A trip under way stays last_trip, so the two never name the same trip. (#2542)
This commit is contained in:
Maurice
2026-10-01 09:22:37 +02:00
parent e24da67218
commit ba3dc65926
7 changed files with 164 additions and 11 deletions
+45 -8
View File
@@ -1118,6 +1118,49 @@ export class AtlasService {
`, userId, userId);
if (!trip) return null;
return {
title: trip.title,
start_date: trip.start_date,
end_date: trip.end_date,
countries: this.tripCountries(trip.id),
};
}
/**
* The trip the user goes on next, the counterpart of lastTrip for a widget that
* counts down (#2542).
*
* Next means not started yet: a trip that is under way is still the last trip,
* so the two never name the same one. Only a trip with a start date qualifies,
* because a trip with only an end date has nothing to count down to. The days
* are counted in SQLite's own calendar, the same `date('now')` lastTrip cuts
* on, so the switch from next to last happens at one moment for both fields.
*/
nextTrip(userId: number): { title: string; start_date: string; end_date: string | null; days_until: number; countries: string[] } | null {
const trip = this.db.get<{ id: number; title: string; start_date: string; end_date: string | null; days_until: number }>(`
SELECT t.id, t.title, t.start_date, t.end_date,
CAST(julianday(t.start_date) - julianday(date('now')) AS INTEGER) AS days_until
FROM trips t
LEFT JOIN trip_members tm ON t.id = tm.trip_id
WHERE (t.user_id = ? OR tm.user_id = ?)
AND t.start_date IS NOT NULL
AND t.start_date > date('now')
ORDER BY t.start_date ASC, t.id ASC
LIMIT 1
`, userId, userId);
if (!trip) return null;
return {
title: trip.title,
start_date: trip.start_date,
end_date: trip.end_date,
days_until: trip.days_until,
countries: this.tripCountries(trip.id),
};
}
/** The countries a trip's places resolved to in `place_regions`, most-visited first. */
private tripCountries(tripId: number): string[] {
const rows = this.db.all<{ country_code: string; places: number }>(`
SELECT pr.country_code, COUNT(DISTINCT p.id) AS places
FROM place_regions pr
@@ -1125,14 +1168,8 @@ export class AtlasService {
WHERE p.trip_id = ? AND pr.country_code IS NOT NULL
GROUP BY pr.country_code
ORDER BY places DESC, pr.country_code ASC
`, trip.id);
return {
title: trip.title,
start_date: trip.start_date,
end_date: trip.end_date,
countries: rows.map(r => r.country_code.toUpperCase()),
};
`, tripId);
return rows.map(r => r.country_code.toUpperCase());
}
getTravelStats(userId: number) {
@@ -53,6 +53,7 @@ export class PublicStatsController {
const travel = this.atlas.getTravelStats(userId);
const last = this.atlas.lastTrip(userId);
const next = this.atlas.nextTrip(userId);
// Counts, not the arrays behind them. A consumer that wants the members asks
// /api/v1/trips; this endpoint exists for the one that wants a number.
@@ -71,6 +72,14 @@ export class PublicStatsController {
country: last.countries[0] ?? null,
countries: last.countries,
},
next_trip: next && {
title: next.title,
start_date: next.start_date,
end_date: next.end_date,
days_until: next.days_until,
country: next.countries[0] ?? null,
countries: next.countries,
},
};
}
}