feat(server): expose the active trip as its own endpoint

GET /api/trips/active answers with the trip a user means by "my trip"
right now: the one running today, else the next one starting, else the
most recently started. Archived trips never qualify.

That order is the dashboard hero's (sortTrips), on purpose - the startup
redirect this is for has to land on the same trip the dashboard features,
or the two would disagree about which trip is yours.

Kept apart from list() because it runs on a first paint: four columns of
one row instead of every trip with its per-trip day and place counts.
This commit is contained in:
Maurice
2026-08-08 21:03:11 +02:00
committed by Maurice
parent aca6c556ed
commit 99ee307034
7 changed files with 188 additions and 4 deletions
+13
View File
@@ -24,6 +24,7 @@ import { diskStorage } from 'multer';
import path from 'path';
import fs from 'fs';
import { v4 as uuidv4 } from 'uuid';
import type { ActiveTripResponse } from '@trek/shared';
import type { User } from '../../types';
import { TripsService } from './trips.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
@@ -76,6 +77,18 @@ export class TripsController {
return { trips: this.trips.list(user.id, archived === '1' ? 1 : 0) };
}
/**
* Where "open TREK straight in my trip" lands. Declared above @Get(':id') —
* a literal segment below it would never be reached.
*/
@Get('active')
active(@CurrentUser() user: User): ActiveTripResponse {
const row = this.trips.activeTrip(user.id);
if (!row) return { trip: null };
const { id, title, start_date, end_date } = row;
return { trip: { id, title, start_date, end_date } };
}
@Get('cover-images/search')
async coverImages(@CurrentUser() user: User, @Query('query') query?: string) {
try {
+30 -1
View File
@@ -3,7 +3,7 @@ import path from 'path';
import fs from 'fs';
import { randomUUID } from 'crypto';
import { DatabaseService } from '../database/database.service';
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
import type { ActiveTrip, TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
import { RealtimeService } from '../realtime/realtime.service';
import { PermissionsService } from '../permissions/permissions.service';
import type { Trip, User } from '../../types';
@@ -416,6 +416,35 @@ export class TripsService {
`).get({ userId, tripId }) as Trip | undefined;
}
/**
* The trip a user most likely means by "my trip" right now: the one running
* today, else the next one starting, else the one that started most recently.
* Archived trips never qualify. Same order the dashboard hero picks its
* spotlight with (client sortTrips) — the two must agree, or "open my trip on
* startup" would land somewhere other than the trip the dashboard features.
*
* Kept separate from list() on purpose: this runs on the very first paint of
* a startup redirect, so it reads four columns of one row instead of every
* trip with its per-trip day/place counts.
*/
activeTrip(userId: number, today = new Date().toISOString().slice(0, 10)) {
return this.db.prepare(`
SELECT t.id, t.title, t.start_date, t.end_date,
CASE
WHEN t.start_date IS NOT NULL AND t.end_date IS NOT NULL AND t.start_date <= :today AND t.end_date >= :today THEN 0
WHEN t.start_date IS NOT NULL AND t.start_date >= :today THEN 1
ELSE 2
END AS relevance
FROM trips t
LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = :userId
WHERE (t.user_id = :userId OR m.user_id IS NOT NULL) AND t.is_archived = 0
ORDER BY relevance ASC,
CASE WHEN relevance < 2 THEN t.start_date END ASC,
CASE WHEN relevance = 2 THEN t.start_date END DESC
LIMIT 1
`).get({ userId, today }) as ActiveTrip & { relevance: number } | undefined;
}
getRaw(tripId: string | number): Trip | undefined {
return this.db.prepare('SELECT * FROM trips WHERE id = ?').get(tripId) as Trip | undefined;
}