import { packageStorage } from 'kody:runtime' import * as s from 'remix/data-schema' import { parseInput, strictObject } from './domain/validation.ts' import { listInquiries, maxStoredInquiries } from './store/inquiries.ts' export type ListInquiriesInput = { limit?: number; since?: string } const listInquiriesInput = strictObject({ limit: s.optional( s .number() .refine( (n: number) => Number.isInteger(n) && n > 0 && n <= maxStoredInquiries, `must be 1–${maxStoredInquiries}`, ), ), since: s.optional( s .string() .refine( (value: string) => !Number.isNaN(Date.parse(value)), 'must be an ISO date', ), ), }) /** * Contact-form messages, newest first. Each one is also emailed to her when * notifyOnInquiry is on (see update-config); `notified` says whether that email went out. * * @param raw - `{ limit?: number (default 20), since?: ISO date }` * @returns `{ total, inquiries: [{ id, receivedAt, name, email, phone?, sessionType?, preferredDate?, message, notified }] }` * * @example * import listInquiriesExport from 'kody:@kentcdodds/photo-portfolio/list-inquiries' * const { inquiries } = await listInquiriesExport({ since: '2026-10-01' }) */ export default async function listInquiriesExport( raw: ListInquiriesInput = {}, ) { const input = parseInput( listInquiriesInput, raw, 'list-inquiries', ) const all = await listInquiries(packageStorage()) const since = input.since ? Date.parse(input.since) : null const matching = since === null ? all : all.filter((inquiry) => Date.parse(inquiry.receivedAt) >= since) return { total: matching.length, inquiries: matching.slice(0, input.limit ?? 20), } }