diff --git a/apps/api/src/ingest/service.ts b/apps/api/src/ingest/service.ts index 82553a8..6977fa2 100644 --- a/apps/api/src/ingest/service.ts +++ b/apps/api/src/ingest/service.ts @@ -14,6 +14,18 @@ import { jpegFileComplete } from "./jpeg.js"; export const ACCEPTED_EXTENSIONS = new Set([".jpg", ".jpeg"]); +/** + * Open events (treino, long ingest windows without a hard match end) must not keep swallowing photos after the + * photographer has stopped for more than this: a later burst is another session (inbox or another event). + */ +export const INGEST_SESSION_GAP_MS = 60 * 60 * 1000; + +/** True when this capture still belongs to the same shooting session as `lastCapturedAt` (or there is no prior photo). */ +export function withinIngestSession(at: Date, lastCapturedAt: Date | null | undefined): boolean { + if (!lastCapturedAt) return true; + return at.getTime() - lastCapturedAt.getTime() <= INGEST_SESSION_GAP_MS; +} + export type IngestOutcome = | { status: "ingested"; photoId: string; eventId: string | null } | { status: "duplicate"; photoId: string } @@ -170,7 +182,7 @@ export class IngestService { ? origin.eventId : origin.source === "CLOUD" ? await this.namedEvent(orgId, origin.eventId) - : (await this.requestedEvent(orgId, origin.eventId, at)) ?? (await this.routeToEvent(orgId, origin.userId, at)); + : (await this.requestedEvent(orgId, origin.userId, 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); @@ -372,27 +384,34 @@ export class IngestService { } /** - * 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 - * clock decides, so a pin left on yesterday's match does not take today's event. + * The event asked for on the upload (open library / desktop pin). Kept only while the capture sits in that + * event's ingest window and the photographer has not paused more than {@link INGEST_SESSION_GAP_MS} since + * their last photo there — otherwise the clock (or inbox) decides. */ - private async requestedEvent(organizationId: string, requested: string | undefined, at: Date): Promise { + private async requestedEvent( + organizationId: string, + userId: string, + requested: string | undefined, + at: Date, + ): Promise { if (!requested) return undefined; const event = await this.deps.db.event.findFirst({ where: { id: requested, organizationId }, - select: { archivedAt: true, ingestFrom: true, ingestUntil: true }, + select: { ingestFrom: true, ingestUntil: true }, }); if (!event) return undefined; - if (event.archivedAt && (at < event.ingestFrom || at > event.ingestUntil)) return undefined; + if (at < event.ingestFrom || at > event.ingestUntil) return undefined; + if (!(await this.sameSession(requested, userId, at))) return undefined; return requested; } /** * The event this photo belongs to: one the photographer is assigned to (or an event with no assignments, - * open to everyone) whose ingest window contains the capture time. The latest-starting one wins. + * open to everyone) whose ingest window contains the capture time, without a >1 h shooting gap. The + * latest-starting one that still fits wins. */ async routeToEvent(organizationId: string, userId: string, at: Date): Promise { - const event = await this.deps.db.event.findFirst({ + const events = await this.deps.db.event.findMany({ where: { organizationId, archivedAt: null, @@ -401,9 +420,27 @@ export class IngestService { OR: [{ assignments: { some: { userId } } }, { assignments: { none: {} } }], }, orderBy: { startsAt: "desc" }, + take: 20, select: { id: true }, }); - return event?.id ?? null; + for (const event of events) { + if (await this.sameSession(event.id, userId, at)) return event.id; + } + return null; + } + + /** Last capture by this photographer in the event; none yet → new session may start. */ + private async sameSession(eventId: string, userId: string, at: Date): Promise { + const last = await this.deps.db.photo.findFirst({ + where: { + eventId, + uploaderId: userId, + capturedAt: { not: null, lt: at }, + }, + orderBy: { capturedAt: "desc" }, + select: { capturedAt: true }, + }); + return withinIngestSession(at, last?.capturedAt); } /** diff --git a/apps/api/src/ingest/session.test.ts b/apps/api/src/ingest/session.test.ts new file mode 100644 index 0000000..42a1c64 --- /dev/null +++ b/apps/api/src/ingest/session.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from "vitest"; +import { INGEST_SESSION_GAP_MS, withinIngestSession } from "./service.js"; + +describe("withinIngestSession", () => { + const at = new Date("2026-11-03T16:00:00Z"); + + it("allows the first photo of an event", () => { + expect(withinIngestSession(at, null)).toBe(true); + expect(withinIngestSession(at, undefined)).toBe(true); + }); + + it("keeps photos inside a one-hour pause", () => { + expect(withinIngestSession(at, new Date(at.getTime() - INGEST_SESSION_GAP_MS))).toBe(true); + expect(withinIngestSession(at, new Date(at.getTime() - 30 * 60_000))).toBe(true); + }); + + it("ends the session after more than one hour without shots", () => { + expect(withinIngestSession(at, new Date(at.getTime() - INGEST_SESSION_GAP_MS - 1))).toBe(false); + expect(withinIngestSession(at, new Date(at.getTime() - 2 * 60 * 60_000))).toBe(false); + }); +}); diff --git a/apps/api/test/library.test.ts b/apps/api/test/library.test.ts index 7b6f1ff..edd5bfe 100644 --- a/apps/api/test/library.test.ts +++ b/apps/api/test/library.test.ts @@ -210,6 +210,21 @@ describe("library", () => { expect(await send("today.jpg", "2026:10:19 12:05:00")).toMatchObject({ status: "ingested", eventId: press.id }); }); + it("a pin does not keep photos after a one-hour shooting pause (open ingest window)", async () => { + const { orgId, photographer, event } = await setup(); + // Window still open for both bursts — only the gap must end the session. + await ctx.db.event.update({ where: { id: event.id }, data: { ingestUntil: new Date("2026-09-24T06:00:00Z") } }); + const send = async (name: string, taken: string) => { + const file = path.join(dir, name); + await writeFile(file, await jpeg({ color: "#445566", taken })); + return service.ingest(file, { organizationId: orgId, userId: photographer.id, source: "DESKTOP", originalName: name, eventId: event.id }); + }; + expect(await send("a.jpg", "2026:09:23 20:00:00")).toMatchObject({ status: "ingested", eventId: event.id }); + expect(await send("b.jpg", "2026:09:23 20:45:00")).toMatchObject({ status: "ingested", eventId: event.id }); + // >1 h after the last shot: not the same session → Sem evento (no other event fits). + expect(await send("c.jpg", "2026:09:23 22:00:00")).toMatchObject({ status: "ingested", eventId: null }); + }); + 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") } });