// Contacts the check correctly does not flag, with the rule that explains it. Computed, never hand-listed. import { channelOf, expectedInquiries, horizon, isQualifyingReply } from "./check"; import type { CheckConfig, IsoDate, Snapshot } from "./types"; export type NotFlagged = | { kind: "other_form"; contactId: string; at: IsoDate; since: IsoDate } | { kind: "added_without_inquiry"; contactId: string; at: IsoDate; source: string } | { kind: "returning_client_answered"; contactId: string; at: IsoDate; since: IsoDate; minutes: number } | { kind: "answered_by_call"; contactId: string; at: IsoDate }; const DAY = 86_400_000; const t = (d: IsoDate) => new Date(d).getTime(); export function notFlagged(s: Snapshot, cfg: CheckConfig, now: IsoDate): NotFlagged[] { const limit = horizon(s, now); const out: NotFlagged[] = []; const inquiries = expectedInquiries(s, cfg); const inquiryContacts = new Set(inquiries.map((x) => x.submission.contactId)); for (const c of s.contacts) { if (inquiryContacts.has(c.id)) continue; const other = s.submissions.find((x) => x.contactId === c.id && t(x.createdAt) > t(limit) - DAY && t(x.createdAt) <= t(limit)); if (other) out.push({ kind: "other_form", contactId: c.id, at: other.createdAt, since: c.dateAdded }); else if (t(c.dateAdded) > t(limit) - DAY && t(c.dateAdded) <= t(limit)) out.push({ kind: "added_without_inquiry", contactId: c.id, at: c.dateAdded, source: c.source ?? "" }); } for (const { submission, contact } of inquiries) { if (!contact || t(submission.createdAt) > t(limit)) continue; const reply = s.messages .filter((m) => t(m.dateAdded) <= t(limit) && isQualifyingReply(m, contact, submission.createdAt, cfg)) .sort((a, b) => t(a.dateAdded) - t(b.dateAdded))[0]; if (!reply) continue; if (channelOf(reply) === "Call") out.push({ kind: "answered_by_call", contactId: contact.id, at: reply.dateAdded }); else if (t(contact.dateAdded) < t(submission.createdAt) - 30 * DAY) out.push({ kind: "returning_client_answered", contactId: contact.id, at: submission.createdAt, since: contact.dateAdded, minutes: Math.max(1, Math.round((t(reply.dateAdded) - t(submission.createdAt)) / 60000)), }); } return out; }