feat(mail-ingest): read forwarded booking mails and add what passes the checks to its trip

Addresses per person and per trip (random base32 token, stored hashed and
encrypted), an intake every transport shares, and a job that reads each
mail once under a lease: filters without AI, machine-readable data always,
one AI request with the person's trips, the server's checks G1 to G9, then
one transaction per mail that adds what passed, keeps the rest in the inbox
with a reason, and notifies the forwarder and co-travellers. Undo within
7 days, manual apply, filing as documents, a prefilled form and a nightly
retention complete it; on a managed install the operator's switch decides.
This commit is contained in:
Maurice
2026-11-13 16:04:26 +01:00
parent 340454cfc7
commit de7de4b8de
36 changed files with 4038 additions and 54 deletions
+83
View File
@@ -6,8 +6,15 @@ import {
assistantChangePreviewWriteSchema,
assistantMessagePartWriteSchema,
assistantStoredPartsSchema,
bookingImportPreviewItemSchema,
ferrataSchema,
hikingPreferencesSchema,
mailIngestAttachmentSchema,
mailIngestCheckResultSchema,
mailIngestCheckSchema,
mailIngestFieldSchema,
mailIngestHintSchema,
mailIngestSuggestedTripSchema,
obstacleSnapshotSchema,
paddleGeometrySchema,
paddleLockSchema,
@@ -26,9 +33,15 @@ import {
type DawarichTrackSegment,
type AssistantChangePreview,
type AssistantMessagePart,
type BookingImportPreviewItem,
type Ferrata,
type GeometryPoint,
type HikingPreferences,
type MailIngestAttachment,
type MailIngestCheck,
type MailIngestCheckResult,
type MailIngestField,
type MailIngestHint,
type ObstacleSnapshot,
type PaddleGeometry,
type PaddleLock,
@@ -531,3 +544,73 @@ export const PLACE_DETAILS_CACHE_PAYLOAD: JsonColumn<Record<string, unknown> | n
schema: z.record(z.string(), z.unknown()).nullable(),
fallback: () => null,
};
// ── The booking mail import (mail-ingest) ──────────────────────────────────
/** The booking read from a mail, as the inbox and the form show it; null for an entry that stands for the mail. */
export const MAIL_INGEST_ITEM: JsonColumn<BookingImportPreviewItem | null> = {
column: 'mail_ingest_candidates.item_json',
schema: bookingImportPreviewItemSchema.nullable(),
fallback: () => null,
};
/** The server's checks on the booking, by name. A broken row reads as none run. */
export const MAIL_INGEST_CHECKS: JsonColumn<Partial<Record<MailIngestCheck, MailIngestCheckResult>>> = {
column: 'mail_ingest_candidates.checks_json',
schema: z.partialRecord(mailIngestCheckSchema, mailIngestCheckResultSchema),
fallback: () => ({}),
};
/** The fields of the booking marked for a second look. */
export const MAIL_INGEST_UNCERTAIN_FIELDS: JsonColumn<MailIngestField[]> = {
column: 'mail_ingest_candidates.uncertain_fields',
schema: z.array(mailIngestFieldSchema),
fallback: () => [],
};
const mailIngestMatchSchema = z.object({
/** A change or cancellation of an earlier booking rather than a new one. */
kind: z.enum(['new_booking', 'change', 'cancellation']),
/** The trip the AI proposed to create when none fits; never created on its own. */
suggested_new_trip: mailIngestSuggestedTripSchema.nullable(),
});
export type MailIngestMatch = z.infer<typeof mailIngestMatchSchema>;
/** How the booking was matched to a trip, past the suggested trip's own column. */
export const MAIL_INGEST_MATCH: JsonColumn<MailIngestMatch> = {
column: 'mail_ingest_candidates.match_json',
schema: mailIngestMatchSchema,
fallback: () => ({ kind: 'new_booking', suggested_new_trip: null }),
};
const idList = z.array(z.number().int().positive());
const mailIngestCreatedSchema = z.object({
reservation_ids: idList,
place_ids: idList,
budget_item_ids: idList,
file_ids: idList,
/** The booking as it was written, to tell an edit since then. */
fingerprint: z.string().nullable(),
});
export type MailIngestCreated = z.infer<typeof mailIngestCreatedSchema>;
/** What adding the booking created, for its undo. A broken row undoes nothing. */
export const MAIL_INGEST_CREATED: JsonColumn<MailIngestCreated> = {
column: 'mail_ingest_candidates.created_ids_json',
schema: mailIngestCreatedSchema,
fallback: () => ({ reservation_ids: [], place_ids: [], budget_item_ids: [], file_ids: [], fingerprint: null }),
};
/** What the inbox notes about a mail without holding it back. */
export const MAIL_INGEST_HINTS: JsonColumn<MailIngestHint[]> = {
column: 'mail_ingest_messages.hints',
schema: z.array(mailIngestHintSchema),
fallback: () => [],
};
/** The attachments a mail carried that a booking travels in, without their bytes. */
export const MAIL_INGEST_ATTACHMENTS: JsonColumn<MailIngestAttachment[]> = {
column: 'mail_ingest_messages.attachments',
schema: z.array(mailIngestAttachmentSchema),
fallback: () => [],
};
@@ -49,6 +49,11 @@ export interface MailIngestCandidateInsert {
resolution: string;
reason: string | null;
createdAt: DbTimestamp;
/** For an entry decided as it is written: the trip it went to, what it created, until when it can be undone. */
resolvedTripId?: number | null;
createdIdsJson?: string | null;
resolvedAt?: DbTimestamp | null;
undoUntil?: DbTimestamp | null;
}
/** What deciding an entry writes. */
@@ -107,6 +112,8 @@ type CandidatesKyselyDB = Pick<
| 'places'
| 'users'
| 'reservations'
| 'day_assignments'
| 'day_accommodations'
| 'budget_item_payers'
>;
@@ -144,6 +151,10 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
resolution: row.resolution,
reason: row.reason,
created_at: row.createdAt,
resolved_trip_id: row.resolvedTripId ?? null,
created_ids_json: row.createdIdsJson ?? null,
resolved_at: row.resolvedAt ?? null,
undo_until: row.undoUntil ?? null,
})
.returning('id')
.executeTakeFirstOrThrow();
@@ -265,18 +276,41 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
.execute();
}
/** The dated days of these trips. */
async listDayDates(tripIds: readonly number[]): Promise<Array<{ trip_id: number; date: string }>> {
/** One trip the person owns or is a member of and that is not archived, whatever its dates; null otherwise. */
async findOpenTrip(userId: number, tripId: number): Promise<MailIngestTripRow | null> {
const row = await this.db()
.selectFrom('trips as t')
.select(['t.id', 't.title', 't.start_date', 't.end_date', 't.user_id', 't.currency'])
.where('t.id', '=', tripId)
.where((eb) =>
eb.or([
eb('t.user_id', '=', userId),
eb.exists(
eb
.selectFrom('trip_members as m')
.select('m.id')
.whereRef('m.trip_id', '=', 't.id')
.where('m.user_id', '=', userId),
),
]),
)
.where((eb) => eb.or([eb('t.is_archived', 'is', null), eb('t.is_archived', '=', 0)]))
.executeTakeFirst();
return row ?? null;
}
/** The dated days of these trips, by date. */
async listDatedDays(tripIds: readonly number[]): Promise<Array<{ id: number; trip_id: number; date: string }>> {
if (tripIds.length === 0) return [];
const rows = await this.db()
.selectFrom('days')
.select(['trip_id', 'date'])
.select(['id', 'trip_id', 'date'])
.where('trip_id', 'in', [...tripIds])
.where('date', 'is not', null)
.orderBy('trip_id', 'asc')
.orderBy('date', 'asc')
.execute();
return rows.flatMap((row) => (row.date === null ? [] : [{ trip_id: row.trip_id, date: row.date }]));
return rows.flatMap((row) => (row.date === null ? [] : [{ id: row.id, trip_id: row.trip_id, date: row.date }]));
}
/** The places of these trips that have a position, oldest first. */
@@ -346,6 +380,16 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
return row !== undefined;
}
/** Whether a booking, a day plan entry or a stay still names the place. */
async placeInUse(placeId: number): Promise<boolean> {
const [booking, planned, stay] = await Promise.all([
this.db().selectFrom('reservations').select('id').where('place_id', '=', placeId).executeTakeFirst(),
this.db().selectFrom('day_assignments').select('id').where('place_id', '=', placeId).executeTakeFirst(),
this.db().selectFrom('day_accommodations').select('id').where('place_id', '=', placeId).executeTakeFirst(),
]);
return booking !== undefined || planned !== undefined || stay !== undefined;
}
/** How many payers the costs name: a cost someone settled with moves balances. */
async countPayers(budgetItemIds: readonly number[]): Promise<number> {
if (budgetItemIds.length === 0) return 0;
@@ -158,22 +158,21 @@ export class MailIngestMessagesRepository extends TrekRepository<MailIngestMessa
.orderBy('id', 'asc')
.limit(limit)
.execute();
const claimed: number[] = [];
for (const { id } of due) {
const result = await this.db()
.updateTable('mail_ingest_messages')
.set((eb) => ({ status: 'processing', lease_until: leaseUntil, attempts: eb('attempts', '+', 1) }))
.where('id', '=', id)
.where((eb) =>
eb.or([
eb('status', '=', 'queued'),
eb.and([eb('status', '=', 'processing'), eb('lease_until', '<', now)]),
]),
)
.executeTakeFirst();
if (Number(result.numUpdatedRows) > 0) claimed.push(id);
}
return claimed;
const won = await Promise.all(due.map(({ id }) => this.claim(id, now, leaseUntil)));
return due.flatMap(({ id }, i) => (won[i] ? [id] : []));
}
/** One mail's claim: it is still queued, or its last claim ran out at `now`. */
private async claim(id: number, now: DbTimestamp, leaseUntil: DbTimestamp): Promise<boolean> {
const result = await this.db()
.updateTable('mail_ingest_messages')
.set((eb) => ({ status: 'processing', lease_until: leaseUntil, attempts: eb('attempts', '+', 1) }))
.where('id', '=', id)
.where((eb) =>
eb.or([eb('status', '=', 'queued'), eb.and([eb('status', '=', 'processing'), eb('lease_until', '<', now)])]),
)
.executeTakeFirst();
return Number(result.numUpdatedRows) > 0;
}
/** Writes what the pipeline made of the mail and lets go of its claim. */