diff --git a/apps/api/src/ingest/service.ts b/apps/api/src/ingest/service.ts index fc6bab6..4aaae0c 100644 --- a/apps/api/src/ingest/service.ts +++ b/apps/api/src/ingest/service.ts @@ -116,6 +116,7 @@ export class IngestService { * The shared pipeline for FTP files and uploads from the browser or desktop. `file` is moved into storage * (or deleted when it is a duplicate). An explicit `eventId` skips the clock (uploading into an open event), * unless that event was already put away and the photo was taken outside its window. + * An archive import is the exception: its event is kept either way. */ async ingest( file: string, @@ -156,8 +157,12 @@ export class IngestService { const embedded = await readEmbeddedIptc(file); const capturedAt = camera.capturedAt ? new Date(camera.capturedAt.getTime() + (origin.clockOffsetSec ?? 0) * 1000) : undefined; const at = capturedAt ?? new Date(); - // A pin left on a game already put away must not swallow the next event. A photo taken during that game still stays. - const eventId = (await this.requestedEvent(orgId, origin.eventId, at)) ?? (await this.routeToEvent(orgId, origin.userId, at)); + // Archive imports name their own event. A file with no camera time, or a time outside that event's window, + // must not fall through onto whatever is live today. + const eventId = + origin.source === "CLOUD" + ? await this.namedEvent(orgId, origin.eventId) + : (await this.requestedEvent(orgId, origin.eventId, at)) ?? (await this.routeToEvent(orgId, origin.userId, at)); const storageKey = storage.originalKey(orgId, sha256, ext === ".jpeg" ? ".jpg" : ext); const size = (await stat(file)).size; await storage.adopt(file, storageKey); @@ -256,6 +261,16 @@ export class IngestService { return { status: "ingested", photoId: photo.id, eventId: updated.eventId }; } + /** + * The event an archive import names. Kept as given, including when that event is already put away: the + * clock must not move it into a live event. + */ + private async namedEvent(organizationId: string, requested: string | undefined): Promise { + if (!requested) return null; + const event = await this.deps.db.event.findFirst({ where: { id: requested, organizationId }, select: { id: true } }); + return event?.id ?? null; + } + /** * The event asked for on the upload (the one open in the library, or the desktop's pinned FTP target). * Kept, unless it is a game already put away and this photo was taken outside that game's window: then the diff --git a/apps/api/test/library.test.ts b/apps/api/test/library.test.ts index 23ecf9d..db94e3c 100644 --- a/apps/api/test/library.test.ts +++ b/apps/api/test/library.test.ts @@ -210,6 +210,30 @@ describe("library", () => { expect(await send("today.jpg", "2026:10:19 12:05:00")).toMatchObject({ status: "ingested", eventId: press.id }); }); + it("an archive import stays in its put-away event when the photo was taken outside that window", async () => { + const { orgId, admin, photographer, event } = await setup(); + await ctx.db.event.update({ where: { id: event.id }, data: { archivedAt: new Date("2026-09-24T00:00:00Z") } }); + const live = ( + await ctx.app.inject({ + method: "POST", + url: `/v1/orgs/${orgId}/events`, + headers: bearer(admin), + payload: { home: "Treino", startsAt: "2026-10-19T16:00:00Z" }, + }) + ).json() as { id: string }; + const file = path.join(dir, "archive.jpg"); + await writeFile(file, await jpeg({ color: "#abcdef", taken: "2026:10:19 18:05:00" })); + const out = await service.ingest(file, { + organizationId: orgId, + userId: photographer.id, + source: "CLOUD", + originalName: "PS_SNAF_1.jpg", + eventId: event.id, + }); + expect(out).toMatchObject({ status: "ingested", eventId: event.id }); + expect(live).not.toBe(event.id); + }); + it("deletes an event: its photos go back to Sem evento; put-away events are listed apart", async () => { const { orgId, admin, foto, event } = await setup(); const outcome = await service.ingestFile(await drop("foto.r1", "a.jpg", await jpeg())); diff --git a/apps/app/src/library/store.test.ts b/apps/app/src/library/store.test.ts index 1c66504..b1340de 100644 --- a/apps/app/src/library/store.test.ts +++ b/apps/app/src/library/store.test.ts @@ -105,4 +105,13 @@ describe("library store", () => { u = libraryReducer(u, { type: "loaded", scope: "e1", photos: [photo("a", "1")], done: true }); expect([u.focus, u.wanted]).toEqual(["a", null]); }); + + it("drops cached photos the server no longer returns, and keeps one that arrived meanwhile", () => { + let s = libraryReducer(initialLibrary, { type: "load", scope: "e1" }); + s = libraryReducer(s, { type: "loaded", scope: "e1", photos: [photo("old", "1"), photo("a", "2")], done: false }); + s = libraryReducer(s, { type: "message", message: { type: "photo.added", photo: photo("live", "3") } }); + s = libraryReducer(s, { type: "loaded", scope: "e1", photos: [photo("a", "2")], done: true, drop: ["old", "a"] }); + expect(s.photos.map((p) => p.id)).toEqual(["a", "live"]); + expect(s.loading).toBe(false); + }); }); diff --git a/apps/app/src/library/store.ts b/apps/app/src/library/store.ts index d9e5651..c6e6644 100644 --- a/apps/app/src/library/store.ts +++ b/apps/app/src/library/store.ts @@ -17,7 +17,7 @@ export const initialLibrary: LibraryState = { scope: null, photos: [], loading: export type LibraryAction = | { type: "load"; scope: string; focus?: string | null } - | { type: "loaded"; scope: string; photos: PhotoSummary[]; done: boolean } + | { type: "loaded"; scope: string; photos: PhotoSummary[]; done: boolean; drop?: string[] } | { type: "message"; message: LibraryMessage } | { type: "patch"; ids: string[]; patch: PhotoPatch } | { type: "select"; id: string; mode: "single" | "toggle" | "range"; visible: string[] } @@ -64,8 +64,13 @@ export function libraryReducer(state: LibraryState, action: LibraryAction): Libr return { ...initialLibrary, scope: action.scope, loading: true, wanted: action.focus ?? null }; case "loaded": { if (action.scope !== state.scope) return state; - const seen = new Set(state.photos.map((p) => p.id)); - const photos = [...state.photos, ...action.photos.filter((p) => !seen.has(p.id))]; + // A cached folder is shown first. Once the server has answered, photos that were only in that cache + // (they have since moved to another event) leave. Anything that arrived meanwhile stays. + const drop = new Set(action.drop ?? []); + const incoming = new Set(action.photos.map((p) => p.id)); + const prior = drop.size ? state.photos.filter((p) => !drop.has(p.id) || incoming.has(p.id)) : state.photos; + const seen = new Set(prior.map((p) => p.id)); + const photos = [...prior, ...action.photos.filter((p) => !seen.has(p.id))]; // The active photo from another module (or the last visit): selected as soon as its page arrives. if (state.wanted && photos.some((p) => p.id === state.wanted)) { const id = state.wanted; diff --git a/apps/app/src/pages/LibraryPage.tsx b/apps/app/src/pages/LibraryPage.tsx index f8273fa..f237344 100644 --- a/apps/app/src/pages/LibraryPage.tsx +++ b/apps/app/src/pages/LibraryPage.tsx @@ -119,10 +119,11 @@ export function LibraryPage() { const page = await api.listPhotos(orgId, { event: scope, offset, limit: PAGE }); const done = offset + page.photos.length >= page.total || page.photos.length === 0; all.push(...page.photos); - dispatch({ type: "loaded", scope, photos: page.photos, done }); + dispatch({ type: "loaded", scope, photos: page.photos, done: false }); if (done) break; } void cache.setPhotos(orgId, scope, all); + dispatch({ type: "loaded", scope, photos: all, done: true, drop: (kept ?? []).map((p) => p.id) }); } catch (err) { if (!isNetworkError(err)) throw err; // No network: stay on the cache already shown. Mark the folder loaded so the empty state can appear.