You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sims-hq/apps/hq/src/repos-reminders.ts

292 lines
13 KiB
TypeScript

import { uuidv7 } from '@sims/domain'
import { ownerScope } from './auth'
import { writeAudit } from './audit'
import type { DB } from './db'
import {
QUOTE_FOLLOWUP_DEFAULT_BODY, QUOTE_FOLLOWUP_DEFAULT_SUBJECT, type ReminderRuleKind,
} from './reminder-templates'
/** Reminder rows + settings helpers (D12 plain-repo pattern). The UNIQUE key
* (rule_kind, subject_id, due_period) is the whole idempotency story — every
* create goes through INSERT OR IGNORE, so catch-up after downtime is safe. */
export type ReminderStatus = 'queued' | 'sent' | 'failed' | 'dismissed'
export interface Reminder {
id: string; ruleKind: ReminderRuleKind; subjectId: string; duePeriod: string
clientId: string; docId: string | null; status: ReminderStatus
policyApplied: 'auto' | 'manual'; error: string | null; createdAt: string; sentAt: string | null
}
interface ReminderRow {
id: string; rule_kind: string; subject_id: string; due_period: string; client_id: string
doc_id: string | null; status: string; policy_applied: string; error: string | null
created_at: string; sent_at: string | null
}
function toReminder(r: ReminderRow): Reminder {
return {
id: r.id, ruleKind: r.rule_kind as ReminderRuleKind, subjectId: r.subject_id,
duePeriod: r.due_period, clientId: r.client_id, docId: r.doc_id,
status: r.status as ReminderStatus, policyApplied: r.policy_applied as 'auto' | 'manual',
error: r.error, createdAt: r.created_at, sentAt: r.sent_at,
}
}
export function getReminder(db: DB, id: string): Reminder | null {
const row = db.prepare(`SELECT * FROM reminder WHERE id=?`).get(id) as ReminderRow | undefined
return row === undefined ? null : toReminder(row)
}
export interface UpsertReminderInput {
ruleKind: ReminderRuleKind; subjectId: string; duePeriod: string; clientId: string
docId?: string | null; policyApplied?: 'auto' | 'manual'; now?: string
}
/** Create the reminder for a (rule, subject, period) exactly once. Returns the
* existing row's id with created:false when the key is already present. */
export function upsertReminder(db: DB, input: UpsertReminderInput): { id: string; created: boolean } {
const id = uuidv7()
const now = input.now ?? new Date().toISOString()
const res = db.prepare(
// Portability quirk: INSERT OR IGNORE is SQLite/Postgres dialect (standard SQL has MERGE).
`INSERT OR IGNORE INTO reminder
(id, rule_kind, subject_id, due_period, client_id, doc_id, status, policy_applied, error, created_at, sent_at)
VALUES (?, ?, ?, ?, ?, ?, 'queued', ?, NULL, ?, NULL)`,
).run(
id, input.ruleKind, input.subjectId, input.duePeriod, input.clientId,
input.docId ?? null, input.policyApplied ?? 'manual', now,
)
if (res.changes === 1) {
writeAudit(db, 'system', 'create', 'reminder', id, undefined, {
ruleKind: input.ruleKind, subjectId: input.subjectId, duePeriod: input.duePeriod,
})
return { id, created: true }
}
const existing = db.prepare(
`SELECT id FROM reminder WHERE rule_kind=? AND subject_id=? AND due_period=?`,
).get(input.ruleKind, input.subjectId, input.duePeriod) as { id: string }
return { id: existing.id, created: false }
}
export interface ReminderFilter { status?: ReminderStatus; ruleKind?: ReminderRuleKind; clientId?: string }
export function listReminders(db: DB, filter: ReminderFilter = {}): Reminder[] {
let sql = `SELECT * FROM reminder WHERE 1=1`
const args: unknown[] = []
if (filter.status !== undefined) { sql += ` AND status=?`; args.push(filter.status) }
if (filter.ruleKind !== undefined) { sql += ` AND rule_kind=?`; args.push(filter.ruleKind) }
if (filter.clientId !== undefined) { sql += ` AND client_id=?`; args.push(filter.clientId) }
sql += ` ORDER BY id DESC`
return (db.prepare(sql).all(...args) as ReminderRow[]).map(toReminder)
}
/** Human labels for queue rows — exhaustive so a new rule kind cannot ship unlabelled. */
export const RULE_LABELS: Record<ReminderRuleKind, string> = {
invoice_overdue: 'Invoice overdue', renewal_due: 'Renewal due', amc_expiring: 'AMC expiring',
follow_up: 'Follow-up', recurring_generated: 'Recurring invoice', email_bounced: 'Email bounced',
quote_followup: 'Quote follow-up',
}
export interface QueueItem extends Reminder {
clientName: string
docNo: string | null
/** Derived owner (spec A3): doc_id → document.created_by; null when the reminder has no document. */
ownerId: string | null
/** Queue row label, e.g. 'Quote follow-up (d7)'. */
label: string
}
export interface QueueOpts {
/** From res.locals.staff — never the request body. Staff are forced to their own rows. */
viewerRole?: string; viewerId?: string
/** Narrow to one owner — managerial viewers only (ignored for staff via ownerScope). */
ownerId?: string
/** Narrow to one status; default is the working set queued|failed. */
status?: ReminderStatus
page?: number; pageSize?: number
}
export interface QueuePage { rows: QueueItem[]; total: number; page: number; pageSize: number }
const QUEUE_PAGE_SIZE = 50
const QUEUE_MAX_PAGE_SIZE = 200
interface QueueRow extends ReminderRow { client_name: string | null; doc_no: string | null; owner_id: string | null }
/** The manual queue: everything awaiting a human — queued items and failed sends.
* Paginated with an honest total (rule 8); owner scope derives via
* doc_id → document.created_by (spec A3 — no owner column on reminder). */
export function listQueue(db: DB, opts: QueueOpts = {}): QueuePage {
const page = Math.max(opts.page ?? 1, 1)
const pageSize = Math.min(Math.max(opts.pageSize ?? QUEUE_PAGE_SIZE, 1), QUEUE_MAX_PAGE_SIZE)
const scoped = opts.viewerRole !== undefined && opts.viewerId !== undefined
? ownerScope({ id: opts.viewerId, role: opts.viewerRole }, opts.ownerId)
: opts.ownerId
let where = `r.status IN ('queued','failed')`
const args: unknown[] = []
if (opts.status !== undefined) { where = `r.status = ?`; args.push(opts.status) }
// Owner scope derives via doc_id → document.created_by. Rows with NO document
// (renewal_due, amc_expiring, follow_up, email_bounced) have no derived owner —
// they are shared work and stay visible to every viewer, staff included.
if (scoped !== undefined) { where += ` AND (d.created_by = ? OR r.doc_id IS NULL)`; args.push(scoped) }
const total = (db.prepare(
`SELECT COUNT(*) AS n FROM reminder r LEFT JOIN document d ON d.id = r.doc_id WHERE ${where}`,
).get(...args) as { n: number }).n
const rows = db.prepare(
`SELECT r.*, c.name AS client_name, d.doc_no, d.created_by AS owner_id
FROM reminder r
LEFT JOIN client c ON c.id = r.client_id
LEFT JOIN document d ON d.id = r.doc_id
WHERE ${where}
ORDER BY r.id DESC
LIMIT ? OFFSET ?`,
).all(...args, pageSize, (page - 1) * pageSize) as QueueRow[]
return {
rows: rows.map((r) => ({
...toReminder(r),
clientName: r.client_name ?? '',
docNo: r.doc_no,
ownerId: r.owner_id,
label: r.rule_kind === 'quote_followup'
? `${RULE_LABELS.quote_followup} (${r.due_period})`
: RULE_LABELS[r.rule_kind as ReminderRuleKind] ?? r.rule_kind,
})),
total, page, pageSize,
}
}
/** Queue totals (queued/failed) under the same owner scope listQueue applies. */
export function queueCounts(db: DB, ownerId?: string): { queued: number; failed: number } {
const rows = db.prepare(
`SELECT r.status, COUNT(*) AS n FROM reminder r LEFT JOIN document d ON d.id = r.doc_id
WHERE r.status IN ('queued','failed')${ownerId !== undefined ? ` AND (d.created_by = ? OR r.doc_id IS NULL)` : ''}
GROUP BY r.status`,
).all(...(ownerId !== undefined ? [ownerId] : [])) as { status: string; n: number }[]
const by = new Map(rows.map((r) => [r.status, r.n]))
return { queued: by.get('queued') ?? 0, failed: by.get('failed') ?? 0 }
}
export function setReminderStatus(
db: DB, userId: string, id: string, status: ReminderStatus,
opts: { error?: string | null; sentAt?: string | null } = {},
): Reminder {
const before = getReminder(db, id)
if (before === null) throw new Error('Reminder not found')
db.prepare(`UPDATE reminder SET status=?, error=?, sent_at=? WHERE id=?`).run(
status,
opts.error !== undefined ? opts.error : before.error,
opts.sentAt !== undefined ? opts.sentAt : before.sentAt,
id,
)
const after = getReminder(db, id)!
writeAudit(db, userId, 'update', 'reminder', id, { status: before.status }, { status: after.status, error: after.error })
return after
}
export function dismissReminder(db: DB, userId: string, id: string): Reminder {
const before = getReminder(db, id)
if (before === null) throw new Error('Reminder not found')
if (before.status === 'sent') throw new Error('Cannot dismiss a reminder that was already sent')
return setReminderStatus(db, userId, id, 'dismissed')
}
/**
* STOP cleanup (spec §7): a quote that is accepted/lost/converted stops chasing.
* Dismisses every still-open (queued|failed) quote_followup for the quote — one
* setReminderStatus per row so each dismissal is audited (F14), never a bulk UPDATE.
* Callers (markStatus / convertDocument) run this inside their own transaction.
*/
export function dismissQuoteFollowups(db: DB, userId: string, quoteId: string): number {
const rows = db.prepare(
`SELECT id FROM reminder
WHERE rule_kind='quote_followup' AND subject_id=? AND status IN ('queued','failed')`,
).all(quoteId) as { id: string }[]
for (const r of rows) setReminderStatus(db, userId, r.id, 'dismissed')
return rows.length
}
// ---------- dated reminder schedule (rule 3 / D-REMIND — spec §4, §7) ----------
/** Rule kinds whose cadence lives in reminder_schedule (dated config rows). */
export type ScheduleRuleKind = 'quote_followup' | 'invoice_overdue'
export interface ResolvedSchedule {
ruleKind: ScheduleRuleKind
dayOffsets: number[] // ascending, deduped day milestones since the anchor event
subject: string | null // message text; null only where the code template owns it
body: string | null
source: 'db' | 'default' // whether a dated row resolved or the code constant applied
}
/** Code-constant fallbacks — the feature works before any row is seeded, and a
* wiped/missing row can never silence the reminder engine. Cadence *changes*
* are new dated rows, never edits here (rule 3). */
export const SCHEDULE_DEFAULTS: Record<
ScheduleRuleKind, { dayOffsets: number[]; subject: string | null; body: string | null }
> = {
quote_followup: {
dayOffsets: [3, 7, 14],
subject: QUOTE_FOLLOWUP_DEFAULT_SUBJECT,
body: QUOTE_FOLLOWUP_DEFAULT_BODY,
},
// invoice_overdue mail text stays in reminder-templates.ts — only the cadence is dated here.
invoice_overdue: { dayOffsets: [7, 15, 30], subject: null, body: null },
}
/** Parse a day_offsets CSV ('3,7,14') → ascending unique positive integers. */
function parseDayOffsets(csv: string): number[] {
const days = csv.split(',')
.map((s) => Number(s.trim()))
.filter((n) => Number.isInteger(n) && n > 0)
return [...new Set(days)].sort((a, b) => a - b)
}
/** Resolve the schedule active on `today` (YYYY-MM-DD): the reminder_schedule row
* where effective_from <= today AND (effective_to IS NULL OR effective_to > today),
* latest effective_from winning. No usable row → the code-constant default. A row
* that sets cadence but leaves subject/body NULL inherits the default message text. */
export function resolveSchedule(db: DB, ruleKind: ScheduleRuleKind, today: string): ResolvedSchedule {
const fallback = SCHEDULE_DEFAULTS[ruleKind]
const row = db.prepare(
`SELECT day_offsets, subject, body FROM reminder_schedule
WHERE rule_kind=? AND effective_from <= ? AND (effective_to IS NULL OR effective_to > ?)
ORDER BY effective_from DESC, id DESC LIMIT 1`, // id DESC breaks effective_from ties deterministically (UUIDv7 is time-ordered → latest insert wins on any engine)
).get(ruleKind, today, today) as { day_offsets: string; subject: string | null; body: string | null } | undefined
if (row !== undefined) {
const dayOffsets = parseDayOffsets(row.day_offsets)
if (dayOffsets.length > 0) {
return {
ruleKind, dayOffsets,
subject: row.subject ?? fallback.subject, body: row.body ?? fallback.body,
source: 'db',
}
}
// Row present but no usable offsets — never let bad config silence the engine.
}
return { ruleKind, dayOffsets: [...fallback.dayOffsets], subject: fallback.subject, body: fallback.body, source: 'default' }
}
// ---------- settings helpers (shared by scheduler + bounce poller) ----------
export function getSetting(db: DB, key: string): string | null {
const row = db.prepare(`SELECT value FROM setting WHERE key=?`).get(key) as { value: string } | undefined
return row === undefined ? null : row.value
}
export function setSetting(db: DB, userId: string, key: string, value: string): void {
const before = getSetting(db, key)
db.prepare(
// Portability quirk: ON CONFLICT upsert is SQLite/Postgres dialect (standard SQL has MERGE).
`INSERT INTO setting (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value`,
).run(key, value)
writeAudit(db, userId, before === null ? 'create' : 'update', 'setting', key, before === null ? undefined : { value: before }, { value })
}
export function getNumberSetting(db: DB, key: string, fallback: number): number {
const raw = getSetting(db, key)
if (raw === null) return fallback
const n = Number(raw)
return Number.isFinite(n) ? n : fallback
}