feat(journey): create draft entries from gallery photos

Groups the gallery photos on no entry by local day, then by time gap and
distance, names each group through the geocoder (capped per preview, with
the country or the day as fallback) and writes the groups the traveller
keeps as draft entries with their photos in one transaction. Photos put on
an entry meanwhile or named twice are left out and reported; existing
entries are never touched. REST routes under
/api/journeys/:id/entries/from-photos and the MCP tools
preview_journey_entries_from_photos and create_journey_entries_from_photos
share one service and the journey edit right (#1920).
This commit is contained in:
Maurice
2026-11-30 14:32:40 +01:00
parent ebbd8c5362
commit 9c8a33d400
16 changed files with 2023 additions and 0 deletions
@@ -0,0 +1,110 @@
/**
* `JourneyPhotosRepository.listOffEntries` (#1920): the gallery photos of one
* journey that sit on no entry (a suggestion counts as an entry), with when
* and where they were taken; one without a capture time comes back with
* `taken_at` null, an empty capture time reads as null too.
*/
import { JourneyEntryPhotos } from '../../../../src/db/entities/JourneyEntryPhotos.entity';
import { JourneyPhotos } from '../../../../src/db/entities/JourneyPhotos.entity';
import { TrekPhotos } from '../../../../src/db/entities/TrekPhotos.entity';
import type { JourneyPhotosRepository } from '../../../../src/db/repositories/JourneyPhotos.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { makeJourney, makeJourneyEntry } from '../../../helpers/factories/journeys';
import { insertRow } from '../../../helpers/factories/rows';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let photos: JourneyPhotosRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
photos = t.repo(JourneyPhotos);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
async function galleryPhoto(
journeyId: number,
ownerId: number,
over: Partial<{ taken_at: string; lat: number; lng: number; media_type: string }> = {},
): Promise<number> {
const photoId = await insertRow(t, TrekPhotos, {
provider: 'local',
owner: ownerId,
file_path: 'journey/p.jpg',
...over,
});
return insertRow(t, JourneyPhotos, { journey: journeyId, photo: photoId, sort_order: 0, created_at: Date.now() });
}
describe('JourneyPhotosRepository.listOffEntries', () => {
it('JPOFF-001: lists the journey photos on no entry with their capture time, position and kind, in gallery-row order', async () => {
const { user } = await makeUser(t);
const journey = await makeJourney(t, user.id);
const other = await makeJourney(t, user.id);
const entry = await makeJourneyEntry(t, journey.id, user.id);
const suggestion = await makeJourneyEntry(t, journey.id, user.id, { type: 'skeleton' });
const free = await galleryPhoto(journey.id, user.id, { taken_at: '2026-05-14T07:00:00Z', lat: 45.43, lng: 12.33 });
const clip = await galleryPhoto(journey.id, user.id, {
taken_at: '2026-05-14T08:00:00+02:00',
media_type: 'video',
});
const untimed = await galleryPhoto(journey.id, user.id);
const blank = await galleryPhoto(journey.id, user.id, { taken_at: '' });
const onEntry = await galleryPhoto(journey.id, user.id, { taken_at: '2026-05-14T09:00:00Z' });
const onSuggestion = await galleryPhoto(journey.id, user.id, { taken_at: '2026-05-14T10:00:00Z' });
await galleryPhoto(other.id, user.id, { taken_at: '2026-05-14T11:00:00Z' });
await insertRow(t, JourneyEntryPhotos, { entry: entry.id, journeyPhoto: onEntry, sort_order: 0, created_at: 0 });
await insertRow(t, JourneyEntryPhotos, {
entry: suggestion.id,
journeyPhoto: onSuggestion,
sort_order: 0,
created_at: 0,
});
const rows = await photos.listOffEntries(journey.id);
expect(rows).toEqual([
{
id: free,
photo_id: expect.any(Number),
taken_at: '2026-05-14T07:00:00Z',
lat: 45.43,
lng: 12.33,
media_type: 'image',
},
{
id: clip,
photo_id: expect.any(Number),
taken_at: '2026-05-14T08:00:00+02:00',
lat: null,
lng: null,
media_type: 'video',
},
{ id: untimed, photo_id: expect.any(Number), taken_at: null, lat: null, lng: null, media_type: 'image' },
{ id: blank, photo_id: expect.any(Number), taken_at: null, lat: null, lng: null, media_type: 'image' },
]);
});
it('JPOFF-002: a journey without photos, or one whose photos are all on entries, lists none', async () => {
const { user } = await makeUser(t);
const journey = await makeJourney(t, user.id);
expect(await photos.listOffEntries(journey.id)).toEqual([]);
const entry = await makeJourneyEntry(t, journey.id, user.id);
const linked = await galleryPhoto(journey.id, user.id, { taken_at: '2026-05-14T07:00:00Z' });
await insertRow(t, JourneyEntryPhotos, { entry: entry.id, journeyPhoto: linked, sort_order: 0, created_at: 0 });
expect(await photos.listOffEntries(journey.id)).toEqual([]);
});
});