mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
feat(tours): add non-motorized day tour planning
This commit is contained in:
@@ -13,6 +13,23 @@ import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { seedUser, sessionCookie } from './harness';
|
||||
|
||||
vi.mock('../../src/config', async () => {
|
||||
const { readEnv } = await import('../../src/app-config');
|
||||
const env = readEnv();
|
||||
return {
|
||||
ENCRYPTION_KEY: 'assignments-e2e-inert-encryption-key',
|
||||
JWT_SECRET: 'assignments-e2e-inert-jwt-secret',
|
||||
updateJwtSecret: vi.fn(),
|
||||
DEFAULT_LANGUAGE: env.app.defaultLanguage,
|
||||
SESSION_DURATION: env.session.duration,
|
||||
SESSION_DURATION_MS: env.session.durationMs,
|
||||
SESSION_DURATION_SECONDS: env.session.durationSeconds,
|
||||
SESSION_DURATION_REMEMBER: env.session.durationRemember,
|
||||
SESSION_DURATION_REMEMBER_MS: env.session.durationRememberMs,
|
||||
SESSION_DURATION_REMEMBER_SECONDS: env.session.durationRememberSeconds,
|
||||
};
|
||||
});
|
||||
|
||||
const { db } = vi.hoisted(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const Database = require('better-sqlite3');
|
||||
@@ -30,7 +47,8 @@ const { db } = vi.hoisted(() => {
|
||||
description TEXT, lat REAL, lng REAL, address TEXT, category_id INTEGER, price REAL, currency TEXT,
|
||||
place_time TEXT, end_time TEXT, duration_minutes INTEGER DEFAULT 60, notes TEXT, image_url TEXT,
|
||||
transport_mode TEXT DEFAULT 'walking', google_place_id TEXT, google_ftid TEXT, osm_id TEXT, amap_poi_id TEXT, website TEXT, phone TEXT,
|
||||
stop_type TEXT, fill_percent INTEGER);`);
|
||||
stop_type TEXT, fill_percent INTEGER, route_geometry TEXT);`);
|
||||
tmp.exec('CREATE TABLE tours (place_id INTEGER PRIMARY KEY);');
|
||||
tmp.exec(`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, notes TEXT,
|
||||
assignment_time TEXT, assignment_end_time TEXT, leg_transport_mode TEXT,
|
||||
@@ -132,6 +150,47 @@ describe('Assignments e2e (real auth guard + temp SQLite)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('200 list projects Tour facets once per assignment and leaves other tracks ordinary', async () => {
|
||||
const legacyGeometry = JSON.stringify([[48, 11, 600], [48.01, 11.02, 650]]);
|
||||
const tourGeometry = JSON.stringify([[48.02, 11.03, 700], [48.03, 11.04, 750]]);
|
||||
db.prepare('INSERT INTO places (id, trip_id, name, route_geometry) VALUES (3, 5, ?, ?), (4, 5, ?, ?)')
|
||||
.run('Legacy track', legacyGeometry, 'Tour', tourGeometry);
|
||||
db.prepare('INSERT INTO tours (place_id) VALUES (4)').run();
|
||||
const assignmentIds = [
|
||||
seedAssignment(3, 2, 0),
|
||||
seedAssignment(3, 3, 1),
|
||||
seedAssignment(3, 4, 2),
|
||||
seedAssignment(3, 4, 3),
|
||||
];
|
||||
|
||||
const res = await request(server).get('/api/trips/5/days/3/assignments').set('Cookie', sessionCookie(1));
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const assignments = res.body.assignments as Array<{
|
||||
id: number;
|
||||
place_id: number;
|
||||
order_index: number;
|
||||
tour_place_id: number | null;
|
||||
tour_route_geometry: string | null;
|
||||
}>;
|
||||
expect(assignments).toHaveLength(4);
|
||||
expect(assignments.map(a => a.id)).toEqual(assignmentIds);
|
||||
expect(assignments.map(a => a.order_index)).toEqual([0, 1, 2, 3]);
|
||||
expect(assignments[0]).toMatchObject({ place_id: 2, tour_place_id: null, tour_route_geometry: null });
|
||||
expect(db.prepare('SELECT route_geometry FROM places WHERE id = 3').get()).toEqual({ route_geometry: legacyGeometry });
|
||||
expect(db.prepare('SELECT place_id FROM tours WHERE place_id = 3').get()).toBeUndefined();
|
||||
expect(db.prepare('SELECT place_id FROM tours WHERE place_id = 4').get()).toEqual({ place_id: 4 });
|
||||
expect(assignments[1]).toMatchObject({ place_id: 3, tour_place_id: null, tour_route_geometry: null });
|
||||
expect(assignments.slice(2).map(a => ({
|
||||
place_id: a.place_id,
|
||||
tour_place_id: a.tour_place_id,
|
||||
tour_route_geometry: a.tour_route_geometry,
|
||||
}))).toEqual([
|
||||
{ place_id: 4, tour_place_id: 4, tour_route_geometry: tourGeometry },
|
||||
{ place_id: 4, tour_place_id: 4, tour_route_geometry: tourGeometry },
|
||||
]);
|
||||
});
|
||||
|
||||
it('201 create, 404 place', async () => {
|
||||
reconcileTripSkeletons.mockClear();
|
||||
const ok = await request(server).post('/api/trips/5/days/3/assignments').set('Cookie', sessionCookie(1)).send({ place_id: 2 });
|
||||
|
||||
@@ -15,6 +15,23 @@ import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { seedUser, sessionCookie } from './harness';
|
||||
|
||||
vi.mock('../../src/config', async () => {
|
||||
const { readEnv } = await import('../../src/app-config');
|
||||
const env = readEnv();
|
||||
return {
|
||||
ENCRYPTION_KEY: 'days-e2e-inert-encryption-key',
|
||||
JWT_SECRET: 'days-e2e-inert-jwt-secret',
|
||||
updateJwtSecret: vi.fn(),
|
||||
DEFAULT_LANGUAGE: env.app.defaultLanguage,
|
||||
SESSION_DURATION: env.session.duration,
|
||||
SESSION_DURATION_MS: env.session.durationMs,
|
||||
SESSION_DURATION_SECONDS: env.session.durationSeconds,
|
||||
SESSION_DURATION_REMEMBER: env.session.durationRemember,
|
||||
SESSION_DURATION_REMEMBER_MS: env.session.durationRememberMs,
|
||||
SESSION_DURATION_REMEMBER_SECONDS: env.session.durationRememberSeconds,
|
||||
};
|
||||
});
|
||||
|
||||
const { db } = vi.hoisted(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const Database = require('better-sqlite3');
|
||||
@@ -34,7 +51,9 @@ const { db } = vi.hoisted(() => {
|
||||
tmp.exec(`CREATE TABLE places (id INTEGER PRIMARY KEY AUTOINCREMENT, trip_id INTEGER NOT NULL, name TEXT,
|
||||
description TEXT, lat REAL, lng REAL, address TEXT, category_id INTEGER, price REAL, currency TEXT,
|
||||
place_time TEXT, end_time TEXT, duration_minutes INTEGER, notes TEXT, image_url TEXT, transport_mode TEXT,
|
||||
google_place_id TEXT, google_ftid TEXT, osm_id TEXT, amap_poi_id TEXT, website TEXT, phone TEXT, stop_type TEXT, fill_percent INTEGER);`);
|
||||
google_place_id TEXT, google_ftid TEXT, osm_id TEXT, amap_poi_id TEXT, website TEXT, phone TEXT, stop_type TEXT,
|
||||
fill_percent INTEGER, route_geometry TEXT);`);
|
||||
tmp.exec('CREATE TABLE tours (place_id INTEGER PRIMARY KEY);');
|
||||
tmp.exec(`CREATE TABLE day_assignments (id INTEGER PRIMARY KEY AUTOINCREMENT, day_id INTEGER NOT NULL,
|
||||
place_id INTEGER NOT NULL, order_index INTEGER DEFAULT 0, notes TEXT,
|
||||
assignment_time TEXT, assignment_end_time TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP);`);
|
||||
@@ -120,6 +139,7 @@ describe('Days + day-notes e2e (real auth guard + temp SQLite, real day SQL)', (
|
||||
|
||||
beforeEach(() => {
|
||||
db.prepare('DELETE FROM day_notes').run();
|
||||
db.prepare('DELETE FROM day_assignments').run();
|
||||
checkPermission.mockReturnValue(true);
|
||||
});
|
||||
|
||||
@@ -138,6 +158,45 @@ describe('Days + day-notes e2e (real auth guard + temp SQLite, real day SQL)', (
|
||||
expect(res.body.days[0]).toMatchObject({ id: 3, trip_id: 5, day_number: 1, assignments: [], notes_items: [] });
|
||||
});
|
||||
|
||||
it('200 list projects Tour facets once per assignment and leaves other tracks ordinary', async () => {
|
||||
const legacyGeometry = JSON.stringify([[48, 11, 600], [48.01, 11.02, 650]]);
|
||||
const tourGeometry = JSON.stringify([[48.02, 11.03, 700], [48.03, 11.04, 750]]);
|
||||
db.prepare('INSERT INTO places (id, trip_id, name, route_geometry) VALUES (2, 5, ?, ?), (3, 5, ?, ?), (4, 5, ?, ?)')
|
||||
.run('Ordinary', null, 'Legacy track', legacyGeometry, 'Tour', tourGeometry);
|
||||
db.prepare('INSERT INTO tours (place_id) VALUES (4)').run();
|
||||
const assignmentIds = [2, 3, 4, 4].map((placeId, orderIndex) => Number(
|
||||
db.prepare('INSERT INTO day_assignments (day_id, place_id, order_index) VALUES (3, ?, ?)')
|
||||
.run(placeId, orderIndex).lastInsertRowid,
|
||||
));
|
||||
|
||||
const res = await request(server).get('/api/trips/5/days').set('Cookie', sessionCookie(1));
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const assignments = res.body.days[0].assignments as Array<{
|
||||
id: number;
|
||||
order_index: number;
|
||||
tour_place_id: number | null;
|
||||
tour_route_geometry: string | null;
|
||||
place: { id: number };
|
||||
}>;
|
||||
expect(assignments).toHaveLength(4);
|
||||
expect(assignments.map(a => a.id)).toEqual(assignmentIds);
|
||||
expect(assignments.map(a => a.order_index)).toEqual([0, 1, 2, 3]);
|
||||
expect(assignments[0]).toMatchObject({ place: { id: 2 }, tour_place_id: null, tour_route_geometry: null });
|
||||
expect(db.prepare('SELECT route_geometry FROM places WHERE id = 3').get()).toEqual({ route_geometry: legacyGeometry });
|
||||
expect(db.prepare('SELECT place_id FROM tours WHERE place_id = 3').get()).toBeUndefined();
|
||||
expect(db.prepare('SELECT place_id FROM tours WHERE place_id = 4').get()).toEqual({ place_id: 4 });
|
||||
expect(assignments[1]).toMatchObject({ place: { id: 3 }, tour_place_id: null, tour_route_geometry: null });
|
||||
expect(assignments.slice(2).map(a => ({
|
||||
place_id: a.place.id,
|
||||
tour_place_id: a.tour_place_id,
|
||||
tour_route_geometry: a.tour_route_geometry,
|
||||
}))).toEqual([
|
||||
{ place_id: 4, tour_place_id: 4, tour_route_geometry: tourGeometry },
|
||||
{ place_id: 4, tour_place_id: 4, tour_route_geometry: tourGeometry },
|
||||
]);
|
||||
});
|
||||
|
||||
it('201 create day (real insert, auto day_number), 404 trip when not accessible', async () => {
|
||||
const ok = await request(server).post('/api/trips/5/days').set('Cookie', sessionCookie(1)).send({ date: '2026-07-01' });
|
||||
expect(ok.status).toBe(201);
|
||||
|
||||
@@ -30,6 +30,8 @@ const { db } = vi.hoisted(() => {
|
||||
google_place_id TEXT, google_ftid TEXT, osm_id TEXT, amap_poi_id TEXT, website TEXT, phone TEXT, transport_mode TEXT,
|
||||
route_geometry TEXT, route_color TEXT, stop_type TEXT, fill_percent INTEGER, email TEXT, opening_hours TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP);`);
|
||||
// PlacesService projects only tours.place_id and checks for a facet before deletion.
|
||||
tmp.exec('CREATE TABLE tours (place_id INTEGER PRIMARY KEY);');
|
||||
tmp.exec(`CREATE TABLE categories (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, color TEXT, icon TEXT);`);
|
||||
tmp.exec(`CREATE TABLE tags (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, color TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP);`);
|
||||
@@ -123,7 +125,7 @@ describe('Places e2e (real auth guard + temp SQLite)', () => {
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.exec('DELETE FROM trips; DELETE FROM places; DELETE FROM place_tags; DELETE FROM place_ratings; DELETE FROM day_assignments; DELETE FROM days;');
|
||||
db.exec('DELETE FROM trips; DELETE FROM tours; DELETE FROM places; DELETE FROM place_tags; DELETE FROM place_ratings; DELETE FROM day_assignments; DELETE FROM days;');
|
||||
canAccessTrip.mockReturnValue({ id: 5, user_id: 1 });
|
||||
checkPermission.mockReturnValue(true);
|
||||
});
|
||||
@@ -144,6 +146,18 @@ describe('Places e2e (real auth guard + temp SQLite)', () => {
|
||||
expect(res.body.places[0]).toMatchObject({ id: 1, name: 'Spot', trip_id: 5, tags: [], ratings: [] });
|
||||
});
|
||||
|
||||
it('marks Tour-backed Places without changing ordinary Place rows', async () => {
|
||||
db.prepare("INSERT INTO places (id, trip_id, name) VALUES (1, 5, 'Plain'), (2, 5, 'Tour')").run();
|
||||
db.prepare('INSERT INTO tours (place_id) VALUES (2)').run();
|
||||
|
||||
const all = await request(server).get('/api/trips/5/places').set('Cookie', sessionCookie(1));
|
||||
expect(all.status).toBe(200);
|
||||
expect(all.body.places).toHaveLength(2);
|
||||
const tourPlaceIdById = new Map(all.body.places.map((place: { id: number; tour_place_id: number | null }) => [place.id, place.tour_place_id]));
|
||||
expect(tourPlaceIdById.get(1)).toBeNull();
|
||||
expect(tourPlaceIdById.get(2)).toBe(2);
|
||||
});
|
||||
|
||||
it('200 list scoped to the trip', async () => {
|
||||
db.prepare("INSERT INTO places (trip_id, name) VALUES (5, 'Mine')").run();
|
||||
db.prepare("INSERT INTO places (trip_id, name) VALUES (6, 'Theirs')").run();
|
||||
@@ -171,7 +185,7 @@ describe('Places e2e (real auth guard + temp SQLite)', () => {
|
||||
db.prepare("INSERT INTO places (id, trip_id, name) VALUES (1, 5, 'A'), (2, 5, 'B'), (3, 6, 'Foreign')").run();
|
||||
const ok = await request(server).post('/api/trips/5/places/bulk-delete').set('Cookie', sessionCookie(1)).send({ ids: [1, 2, 3] });
|
||||
expect(ok.status).toBe(200);
|
||||
expect(ok.body).toEqual({ deleted: [1, 2], count: 2 });
|
||||
expect(ok.body).toEqual({ deleted: [1, 2], count: 2, tourPlaceIds: [] });
|
||||
// The foreign trip's place is untouched.
|
||||
expect(db.prepare('SELECT id FROM places ORDER BY id').all()).toEqual([{ id: 3 }]);
|
||||
|
||||
@@ -297,7 +311,7 @@ describe('Places e2e (real auth guard + temp SQLite)', () => {
|
||||
|
||||
const ok = await request(server).delete('/api/trips/5/places/9').set('Cookie', sessionCookie(1));
|
||||
expect(ok.status).toBe(200);
|
||||
expect(ok.body).toEqual({ success: true });
|
||||
expect(ok.body).toEqual({ success: true, tourPlaceIds: [] });
|
||||
expect(db.prepare('SELECT id FROM places WHERE id = 9').get()).toBeUndefined();
|
||||
|
||||
const foreign = await request(server).delete('/api/trips/5/places/10').set('Cookie', sessionCookie(1));
|
||||
|
||||
@@ -81,6 +81,8 @@ const { db } = vi.hoisted(() => {
|
||||
tmp.exec(`CREATE TABLE places (id INTEGER PRIMARY KEY AUTOINCREMENT, trip_id INTEGER NOT NULL, name TEXT,
|
||||
image_url TEXT, address TEXT, lat REAL, lng REAL, category_id INTEGER, description TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP);`);
|
||||
// The trip bundle's PlacesService projection joins the Tours facet by place_id.
|
||||
tmp.exec('CREATE TABLE tours (place_id INTEGER PRIMARY KEY);');
|
||||
// PlacesService.list joins categories and batch-loads tags/ratings.
|
||||
tmp.exec('CREATE TABLE categories (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, color TEXT, icon TEXT);');
|
||||
tmp.exec('CREATE TABLE tags (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, color TEXT, created_at DATETIME);');
|
||||
|
||||
Reference in New Issue
Block a user