mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(api): expose shortlisted places, detached bookings and the bucket list
Reading the surface back against real data turned up three things it dropped without saying so. Places with no day were invisible, because the payload only reached places through day_assignments. On a live instance that is half of them, and they are the ones carrying a coordinate and no claim about when — exactly what an integration can act on. They come back under `unplanned_places`, minus the hotel, which is already reported as an accommodation. Bookings with no day were dropped by the day filter. reservations.day_id is nullable and deleting a day detaches its bookings rather than removing them, so this is not a hypothetical. The bucket list had no endpoint at all. It hangs off the user rather than a trip, so it gets its own, and it answers whether or not the Atlas addon is switched on: the addon decides what TREK shows, not what a key can read. Also fixes ?include=notes returning the bare trip: places, notes and reservations are reported on days, so asking for one now implies days instead of quietly answering with nothing. The module stays a leaf. Reaching for AtlasService pulled AuthModule and the storage registry in behind it and broke the surface's own boot, so the one SELECT lives here like every other query in the file.
This commit is contained in:
@@ -34,7 +34,8 @@ const { db } = vi.hoisted(() => {
|
||||
day_number INTEGER NOT NULL, date TEXT NOT NULL, notes TEXT, title TEXT);
|
||||
CREATE TABLE places (id INTEGER PRIMARY KEY AUTOINCREMENT, trip_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL, address TEXT, lat REAL, lng REAL, category_id INTEGER,
|
||||
place_time TEXT, end_time TEXT, duration_minutes INTEGER, notes TEXT, transport_mode TEXT);
|
||||
place_time TEXT, end_time TEXT, duration_minutes INTEGER, notes TEXT, transport_mode TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP);
|
||||
CREATE TABLE categories (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);
|
||||
CREATE TABLE day_assignments (id INTEGER PRIMARY KEY AUTOINCREMENT, day_id INTEGER NOT NULL,
|
||||
place_id INTEGER NOT NULL, order_index INTEGER NOT NULL DEFAULT 0);
|
||||
@@ -47,6 +48,9 @@ const { db } = vi.hoisted(() => {
|
||||
day_id INTEGER, end_day_id INTEGER, place_id INTEGER, assignment_id INTEGER, title TEXT,
|
||||
accommodation_id TEXT, reservation_time TEXT, reservation_end_time TEXT, location TEXT,
|
||||
confirmation_number TEXT, notes TEXT, status TEXT, type TEXT);
|
||||
CREATE TABLE bucket_list (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL, lat REAL, lng REAL, country_code TEXT, notes TEXT,
|
||||
target_date TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP);
|
||||
`);
|
||||
return { db: tmp };
|
||||
});
|
||||
@@ -128,6 +132,12 @@ describe('Public API v1 e2e (real guard + real SQL)', () => {
|
||||
db.prepare("INSERT INTO places (id, trip_id, name) VALUES (3, 1, 'Hotel Alba')").run();
|
||||
// Deliberately inserted out of order to prove order_index decides the sequence.
|
||||
db.prepare('INSERT INTO day_assignments (day_id, place_id, order_index) VALUES (1, 2, 1)').run();
|
||||
// Ein Ort auf der Shortlist: Koordinaten, aber noch kein Tag.
|
||||
db.prepare("INSERT INTO places (id, trip_id, name, lat, lng, notes) VALUES (4, 1, 'Boboli-Garten', 43.762, 11.248, 'vielleicht')").run();
|
||||
// Eine Buchung, die keinen Tag (mehr) hat.
|
||||
db.prepare("INSERT INTO reservations (trip_id, day_id, type, title, location, reservation_time, status) VALUES (1, NULL, 'flight', 'LH 1234', 'FRA', '2026-06-14T08:00', 'confirmed')").run();
|
||||
db.prepare("INSERT INTO bucket_list (user_id, name, lat, lng, country_code, notes, target_date) VALUES (1, 'Hokkaido', 43.06, 141.35, 'JP', 'im Winter', '2027-02-01')").run();
|
||||
db.prepare("INSERT INTO bucket_list (user_id, name, lat, lng) VALUES (2, 'Bobs Traumziel', 1.0, 2.0)").run();
|
||||
db.prepare('INSERT INTO day_assignments (day_id, place_id, order_index) VALUES (1, 1, 0)').run();
|
||||
db.prepare("INSERT INTO day_notes (day_id, trip_id, text, time, sort_order) VALUES (1, 1, 'Tickets mitnehmen', '09:00', 0)").run();
|
||||
db.prepare(
|
||||
@@ -267,6 +277,39 @@ describe('Public API v1 e2e (real guard + real SQL)', () => {
|
||||
expect(JSON.stringify(res.body)).not.toContain('@example.com');
|
||||
});
|
||||
|
||||
it('returns shortlisted places, which are half of a real trip', async () => {
|
||||
const res = await get('/api/v1/trips/1?include=places', ADA_TOKEN);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.unplanned_places).toEqual([
|
||||
expect.objectContaining({ name: 'Boboli-Garten', lat: 43.762, lng: 11.248, notes: 'vielleicht' }),
|
||||
]);
|
||||
// Hotel Alba has no day either, but it is the accommodation and is
|
||||
// reported there — a shortlist that includes your hotel is not a shortlist.
|
||||
expect(res.body.unplanned_places.map((p: { name: string }) => p.name)).not.toContain('Hotel Alba');
|
||||
// And the scheduled ones still sit on their day, not in both places.
|
||||
expect(res.body.days[0].places.map((p: { name: string }) => p.name)).toEqual(['Uffizien', 'Ponte Vecchio']);
|
||||
});
|
||||
|
||||
it('returns bookings that lost their day instead of dropping them', async () => {
|
||||
const res = await get('/api/v1/trips/1?include=reservations', ADA_TOKEN);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.unscheduled_reservations).toEqual([
|
||||
expect.objectContaining({ type: 'flight', title: 'LH 1234', location: 'FRA' }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('implies days when asked for something that lives on one', async () => {
|
||||
// The section Evgenii named first. Before days were implied this answered
|
||||
// the trip summary and nothing else, without saying so.
|
||||
const res = await get('/api/v1/trips/1?include=notes', ADA_TOKEN);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.days).toBeDefined();
|
||||
expect(res.body.days[0].day_notes.length).toBeGreaterThan(0);
|
||||
// Still only what was asked for: no places came along for the ride.
|
||||
expect(res.body.days[0].places).toEqual([]);
|
||||
expect(res.body.unplanned_places).toBeUndefined();
|
||||
});
|
||||
|
||||
it('never exposes internal ids or foreign keys', async () => {
|
||||
const res = await get('/api/v1/trips/1', ADA_TOKEN);
|
||||
const serialised = JSON.stringify(res.body);
|
||||
@@ -293,4 +336,27 @@ describe('Public API v1 e2e (real guard + real SQL)', () => {
|
||||
expect((await get('/api/v1/trips/abc', ADA_TOKEN)).status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bucket list', () => {
|
||||
it("returns the caller's own wishlist, with coordinates", async () => {
|
||||
const res = await get('/api/v1/bucket-list', ADA_TOKEN);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.items).toEqual([
|
||||
{ name: 'Hokkaido', lat: 43.06, lng: 141.35, country_code: 'JP', notes: 'im Winter', target_date: '2027-02-01' },
|
||||
]);
|
||||
});
|
||||
|
||||
it("does not leak another user's wishlist", async () => {
|
||||
const res = await get('/api/v1/bucket-list', BOB_TOKEN);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.items).toEqual([
|
||||
{ name: 'Bobs Traumziel', lat: 1, lng: 2, country_code: null, notes: null, target_date: null },
|
||||
]);
|
||||
});
|
||||
|
||||
it('needs a token like everything else here', async () => {
|
||||
const res = await request(server).get('/api/v1/bucket-list');
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -25,6 +25,15 @@ const TRIP = {
|
||||
updated_at: '2026-06-01 10:00:00',
|
||||
};
|
||||
|
||||
const ITEM = {
|
||||
name: 'Hokkaido',
|
||||
lat: 43.06,
|
||||
lng: 141.35,
|
||||
country_code: 'JP',
|
||||
notes: null,
|
||||
target_date: null,
|
||||
};
|
||||
|
||||
/** The limiter always allows unless a test says otherwise. */
|
||||
function makeController(svc: Partial<PublicApiService>, allow = true) {
|
||||
const rl = { check: vi.fn().mockReturnValue(allow) } as unknown as RateLimitService;
|
||||
@@ -47,6 +56,29 @@ function thrown(fn: () => unknown): { status: number; body: unknown } {
|
||||
}
|
||||
|
||||
describe('PublicApiController', () => {
|
||||
describe('GET /api/v1/bucket-list', () => {
|
||||
it('returns the caller’s wishlist and passes the id from the guard, never the query', () => {
|
||||
const listBucketList = vi.fn().mockReturnValue([ITEM]);
|
||||
expect(makeController({ listBucketList }).listBucketList(req(7))).toEqual({ items: [ITEM] });
|
||||
expect(listBucketList).toHaveBeenCalledWith(7);
|
||||
});
|
||||
|
||||
it('401s when the guard left no user behind', () => {
|
||||
const listBucketList = vi.fn();
|
||||
expect(thrown(() => makeController({ listBucketList }).listBucketList(reqWithoutUser()))).toEqual({
|
||||
status: 401,
|
||||
body: { error: 'API token required', code: 'API_TOKEN_REQUIRED' },
|
||||
});
|
||||
expect(listBucketList).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('counts against the same rate budget as everything else', () => {
|
||||
const listBucketList = vi.fn();
|
||||
expect(thrown(() => makeController({ listBucketList }, false).listBucketList(req(7))).status).toBe(429);
|
||||
expect(listBucketList).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/trips', () => {
|
||||
it('returns the accessible trips for the token owner', () => {
|
||||
const listTrips = vi.fn().mockReturnValue([TRIP]);
|
||||
|
||||
@@ -23,7 +23,7 @@ function contextWith(headers: Record<string, string | undefined>) {
|
||||
}
|
||||
|
||||
function makeGuard(verify: (raw: string) => User | null) {
|
||||
return new ApiTokenGuard({ verifyMcpToken: verify } as unknown as TokenService);
|
||||
return new ApiTokenGuard({ verifyApiToken: verify } as unknown as TokenService);
|
||||
}
|
||||
|
||||
/** Run the guard, expecting a refusal; return its { status, body }. */
|
||||
@@ -69,6 +69,15 @@ describe('ApiTokenGuard', () => {
|
||||
expect(verify).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('never reaches for the MCP verifier, so the two credentials cannot swap', () => {
|
||||
const verifyApiToken = vi.fn().mockReturnValue(USER);
|
||||
const verifyMcpToken = vi.fn();
|
||||
const guard = new ApiTokenGuard({ verifyApiToken, verifyMcpToken } as unknown as TokenService);
|
||||
guard.canActivate(contextWith({ authorization: 'Bearer trek_abc123' }));
|
||||
expect(verifyApiToken).toHaveBeenCalledTimes(1);
|
||||
expect(verifyMcpToken).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('401s on a token the store does not know, without saying which part was wrong', () => {
|
||||
const verify = vi.fn().mockReturnValue(null);
|
||||
expect(
|
||||
|
||||
Reference in New Issue
Block a user