mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
feat(mail-ingest): the inbox a person reads, and moving an added booking to another trip
The inbox pages a person's entries with the mail each came from and the trip it suggests (named only when the reader can see it), shows a mail as plain text, and takes an uploaded .eml, reading it at once. Move writes the booking anew in the other trip and removes it from the first in one transaction.
This commit is contained in:
@@ -87,6 +87,14 @@ interface MailIngestTripPlaceRow {
|
||||
lng: number;
|
||||
}
|
||||
|
||||
/** A trip as the inbox names it to a person who can see it. */
|
||||
export interface MailIngestTripRefRow {
|
||||
id: number;
|
||||
title: string;
|
||||
start_date: string | null;
|
||||
end_date: string | null;
|
||||
}
|
||||
|
||||
/** The person a mail is for, as the pipeline needs them. */
|
||||
export interface MailIngestPersonRow {
|
||||
id: number;
|
||||
@@ -223,6 +231,47 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
|
||||
return Number(row.n);
|
||||
}
|
||||
|
||||
/**
|
||||
* The person's entries, waiting (`open`) or decided, newest first; with
|
||||
* `beforeId` only those older than it, so a page follows on from the last.
|
||||
*/
|
||||
async listForUser(
|
||||
userId: number,
|
||||
open: boolean,
|
||||
beforeId: number | null,
|
||||
limit: number,
|
||||
): Promise<MailIngestCandidateRow[]> {
|
||||
let query = this.db()
|
||||
.selectFrom('mail_ingest_candidates')
|
||||
.selectAll()
|
||||
.where('user_id', '=', userId)
|
||||
.where('resolution', open ? '=' : '<>', 'pending');
|
||||
if (beforeId !== null) query = query.where('id', '<', beforeId);
|
||||
return query.orderBy('id', 'desc').limit(limit).execute();
|
||||
}
|
||||
|
||||
/** The entries made since `since`, counted by what became of them. */
|
||||
async tallyResolutionsSince(since: DbTimestamp): Promise<Array<{ resolution: string; n: number }>> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('mail_ingest_candidates')
|
||||
.select((eb) => ['resolution', eb.fn.countAll<number>().as('n')])
|
||||
.where('created_at', '>=', since)
|
||||
.groupBy('resolution')
|
||||
.execute();
|
||||
return rows.map((row) => ({ resolution: row.resolution, n: Number(row.n) }));
|
||||
}
|
||||
|
||||
/** How many entries wait for each person who has any, the most first; who they are is left out. */
|
||||
async pendingPerUser(): Promise<number[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('mail_ingest_candidates')
|
||||
.select((eb) => ['user_id', eb.fn.countAll<number>().as('n')])
|
||||
.where('resolution', '=', 'pending')
|
||||
.groupBy('user_id')
|
||||
.execute();
|
||||
return rows.map((row) => Number(row.n)).sort((a, b) => b - a);
|
||||
}
|
||||
|
||||
/** Whether a booking of this content went into the trip from a mail and was undone: never added again on its own. */
|
||||
async wasUndoneInTrip(tripId: number, contentHash: string): Promise<boolean> {
|
||||
const row = await this.db()
|
||||
@@ -299,6 +348,31 @@ export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCan
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The trips among `tripIds` the person owns or is a member of, archived or
|
||||
* not: a suggestion names only a trip its reader can see.
|
||||
*/
|
||||
async listTripRefs(userId: number, tripIds: readonly number[]): Promise<MailIngestTripRefRow[]> {
|
||||
if (tripIds.length === 0) return [];
|
||||
return this.db()
|
||||
.selectFrom('trips as t')
|
||||
.select(['t.id', 't.title', 't.start_date', 't.end_date'])
|
||||
.where('t.id', 'in', [...tripIds])
|
||||
.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),
|
||||
),
|
||||
]),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** 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 [];
|
||||
|
||||
@@ -35,6 +35,9 @@ export interface MailIngestMessageRow {
|
||||
|
||||
const _mailIngestMessageRowKeys: AssertRowKeys<MailIngestMessageRow, MailIngestMessages> = true;
|
||||
|
||||
/** A mail as the inbox lists it: everything but the stored mail and its sender's signature data. */
|
||||
export type MailIngestMessageHead = Omit<MailIngestMessageRow, 'raw_eml_enc' | 'auth_results'>;
|
||||
|
||||
/** A mail as a transport hands it in, before anything has read it. */
|
||||
interface MailIngestMessageInsert {
|
||||
transport: string;
|
||||
@@ -112,6 +115,62 @@ export class MailIngestMessagesRepository extends TrekRepository<MailIngestMessa
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
/** These mails without their content, for the inbox rows that name them. */
|
||||
async listHeads(ids: readonly number[]): Promise<MailIngestMessageHead[]> {
|
||||
if (ids.length === 0) return [];
|
||||
return this.db()
|
||||
.selectFrom('mail_ingest_messages')
|
||||
.select([
|
||||
'id',
|
||||
'transport',
|
||||
'message_id_hdr',
|
||||
'dedupe_hash',
|
||||
'address_id',
|
||||
'user_id',
|
||||
'trip_id',
|
||||
'from_addr',
|
||||
'from_verified',
|
||||
'subject',
|
||||
'received_at',
|
||||
'size',
|
||||
'raw_purged_at',
|
||||
'status',
|
||||
'reason',
|
||||
'hints',
|
||||
'attachments',
|
||||
'forwarding_code',
|
||||
'attempts',
|
||||
'next_attempt_at',
|
||||
'lease_until',
|
||||
'ai_used',
|
||||
'processed_at',
|
||||
])
|
||||
.where('id', 'in', [...ids])
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The mails that came in since `since`, counted by status and reason, for the admin's status card. */
|
||||
async tallySince(since: DbTimestamp): Promise<Array<{ status: string; reason: string | null; n: number }>> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('mail_ingest_messages')
|
||||
.select((eb) => ['status', 'reason', eb.fn.countAll<number>().as('n')])
|
||||
.where('received_at', '>=', since)
|
||||
.groupBy(['status', 'reason'])
|
||||
.execute();
|
||||
return rows.map((row) => ({ status: row.status, reason: row.reason, n: Number(row.n) }));
|
||||
}
|
||||
|
||||
/** How many mails, of everyone, an AI read since `since`. */
|
||||
async countAllAiReadSince(since: DbTimestamp): Promise<number> {
|
||||
const row = await this.db()
|
||||
.selectFrom('mail_ingest_messages')
|
||||
.select((eb) => eb.fn.countAll<number>().as('n'))
|
||||
.where('ai_used', '=', 1)
|
||||
.where('received_at', '>=', since)
|
||||
.executeTakeFirstOrThrow();
|
||||
return Number(row.n);
|
||||
}
|
||||
|
||||
/** How many mails came in on the address since `since`, for its daily limit. */
|
||||
async countForAddressSince(addressId: number, since: DbTimestamp): Promise<number> {
|
||||
const row = await this.db()
|
||||
|
||||
Reference in New Issue
Block a user