Public: anyone can read this; the crew can change it.
An approved message opens ready to send in the agent's own Mail or Messages app, from the lead's page #62
Check · Review · Land · Live done — Landed and live in production.
mason/speed-to-lead:mason/60-an-approved-message-opens-ready-to-send → main 1 commit fork
4 files
changed +143
−4
README.md +8 −3
| @@ -305,9 +305,14 @@ from the bottom of the desk, so the clocks read fresh for a demonstration. | ||
| 305 | 305 | |
| 306 | 306 | ## What is real and what is not, in this prototype |
| 307 | 307 | |
| 308 | - **Nothing is actually sent.** "Send" marks the message sent, shows it in | |
| 309 | the lead's history with the time and who approved it. No email or text | |
| 310 | leaves the app. | |
| 308 | - **Nothing is actually sent by the app.** "Send" marks the message sent, shows it in | |
| 309 | the lead's history with the time and who approved it. This is how a | |
| 310 | message really leaves: each sent message in the history has an "Open in | |
| 311 | Mail" (`mailto:`) or "Open in Messages" (`sms:`) link that opens it, | |
| 312 | signature included, ready to send in your own app. No link for a draft | |
| 313 | (approve first), for a lead with no address for that channel, or for a text | |
| 314 | to a lead who asked to stop. Texts use `sms:<number>?body=…`, which | |
| 315 | Android and current iOS read; older iOS wanted `?&body=`. | |
| 311 | 316 | - **No CRM import.** Leads arrive by the form on the desk ("Log a lead by |
| 312 | 317 | hand") or by `POST /leads` (below), which a website form can call. |
| 313 | 318 | - **No login of its own.** You sign in with the forge that runs the app. |
src/handoff.test.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { addLead, getLead, optOutTexts, sendReply, type Result } from "./actions.ts"; | |
| 6 | import { openDb } from "./db.ts"; | |
| 7 | import { handoff } from "./handoff.ts"; | |
| 8 | import { currentAgent } from "./profile.ts"; | |
| 9 | import { leadPage } from "./pages/lead.ts"; | |
| 10 | import type { Who } from "./who.ts"; | |
| 11 | ||
| 12 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 13 | const T0 = new Date("2026-03-01T17:00:00Z"); | |
| 14 | const must = <T>(r: Result<T>): T => { | |
| 15 | if (!r.ok) throw new Error(r.message); | |
| 16 | return r.value; | |
| 17 | }; | |
| 18 | const LEAD = { email: "[email protected]", phone: "(206) 555-0199", opted_out_at: null }; | |
| 19 | const msg = (o: object = {}) => ({ | |
| 20 | status: "sent" as const, | |
| 21 | channel: "email" as const, | |
| 22 | day: 0, | |
| 23 | body: "Hi & bye #1\nShe said \"yes\"", | |
| 24 | signature: "Alex\nLic 1", | |
| 25 | ...o, | |
| 26 | }); | |
| 27 | ||
| 28 | test("an email opens in Mail with to, subject and the approved body plus signature", () => { | |
| 29 | const h = handoff(msg(), LEAD)!; | |
| 30 | expect(h.label).toBe("Open in Mail"); | |
| 31 | expect(h.href).toBe( | |
| 32 | "mailto:dana%40example.com?subject=Re%3A%20your%20inquiry&body=Hi%20%26%20bye%20%231%0D%0AShe%20said%20%22yes%22%0D%0A%0D%0AAlex%0D%0ALic%201", | |
| 33 | ); | |
| 34 | }); | |
| 35 | ||
| 36 | test("a text opens in Messages with the number and body", () => { | |
| 37 | const h = handoff(msg({ channel: "text" }), LEAD)!; | |
| 38 | expect(h.label).toBe("Open in Messages"); | |
| 39 | expect(h.href).toBe("sms:2065550199?body=Hi%20%26%20bye%20%231%0AShe%20said%20%22yes%22%0A%0AAlex%0ALic%201"); | |
| 40 | expect(handoff(msg({ channel: "text" }), { ...LEAD, phone: "+1 206 555 0199" })!.href).toContain("sms:%2B12065550199?"); | |
| 41 | }); | |
| 42 | ||
| 43 | test("no link for a draft, an opted-out text, or a missing address", () => { | |
| 44 | for (const status of ["draft", "scheduled", "due", "held", "skipped"] as const) expect(handoff(msg({ status }), LEAD)).toBeNull(); | |
| 45 | expect(handoff(msg({ channel: "text" }), { ...LEAD, opted_out_at: "2026-03-02T00:00:00Z" })).toBeNull(); | |
| 46 | expect(handoff(msg({ channel: "text" }), { ...LEAD, phone: "" })).toBeNull(); | |
| 47 | expect(handoff(msg(), { ...LEAD, email: "" })).toBeNull(); | |
| 48 | expect(handoff(msg(), { ...LEAD, opted_out_at: "2026-03-02T00:00:00Z" })).not.toBeNull(); | |
| 49 | }); | |
| 50 | ||
| 51 | const arrive = (consent: boolean) => { | |
| 52 | const db = openDb(mkdtempSync(join(tmpdir(), "app-handoff-"))); | |
| 53 | const { id } = must( | |
| 54 | addLead(db, alice, { name: "Dana", email: "[email protected]", phone: "(206) 555-0199", source: "website", message: "Hi", consent }, T0), | |
| 55 | ); | |
| 56 | const show = () => | |
| 57 | leadPage({ who: alice, agent: currentAgent(db), lead: must(getLead(db, alice, { id })), mayUpdate: true, next: null, now: T0.getTime() + 1000 }); | |
| 58 | return { db, id, show }; | |
| 59 | }; | |
| 60 | ||
| 61 | test("the lead's page links a sent email only once sent, with the attribute escaped", () => { | |
| 62 | const { db, id, show } = arrive(false); | |
| 63 | expect(show()).not.toContain("data-handoff"); | |
| 64 | must(sendReply(db, alice, { id, body: "Ready & waiting" }, new Date(T0.getTime() + 60_000))); | |
| 65 | const page = show(); | |
| 66 | expect(page).toContain("Open in Mail"); | |
| 67 | expect(page).toContain('href="mailto:dana%40example.com?subject='); | |
| 68 | expect(page).toContain("Ready%20%26%20waiting"); | |
| 69 | expect(page).toContain("&body="); | |
| 70 | expect(page).not.toContain("&body"); | |
| 71 | }); | |
| 72 | ||
| 73 | test("a sent text links to Messages until the lead opts out", () => { | |
| 74 | const { db, id, show } = arrive(true); | |
| 75 | must(sendReply(db, alice, { id, body: "Ready" }, new Date(T0.getTime() + 60_000))); | |
| 76 | expect(show()).toContain("Open in Messages"); | |
| 77 | must(optOutTexts(db, alice, { id }, new Date(T0.getTime() + 120_000))); | |
| 78 | expect(show()).not.toContain("Open in Messages"); | |
| 79 | }); |
src/handoff.ts added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | /** | |
| 2 | * The handoff: the app sends nothing itself, so an approved message becomes a | |
| 3 | * `mailto:` or `sms:` link that opens it, ready to send, in the agent's own | |
| 4 | * Mail or Messages app. Built from the message as it was approved, signature | |
| 5 | * included; every value is percent-encoded. Drafts get none: a person approves first. | |
| 6 | * | |
| 7 | * Texts use `sms:<number>?body=<text>`, which Android and current iOS both | |
| 8 | * read. (Older iOS wanted `?&body=`; we do not write that form.) | |
| 9 | */ | |
| 10 | ||
| 11 | import type { Message } from "./leads.ts"; | |
| 12 | ||
| 13 | /** A phone number as a dialable string: digits, with a leading + kept; "" when none. */ | |
| 14 | const dialable = (phone: string): string => { | |
| 15 | const digits = phone.replace(/\D/g, ""); | |
| 16 | return digits === "" ? "" : `${phone.trim().startsWith("+") ? "+" : ""}${digits}`; | |
| 17 | }; | |
| 18 | ||
| 19 | /** The words as approved: body, a blank line, the signature. */ | |
| 20 | const approvedText = (m: Pick<Message, "body" | "signature">, eol: string): string => | |
| 21 | `${m.body}\n\n${m.signature}`.replace(/\r?\n/g, eol); | |
| 22 | ||
| 23 | /** The subject an email was sent under. */ | |
| 24 | export const subjectOf = (m: Pick<Message, "day">): string => | |
| 25 | m.day === 0 ? "Re: your inquiry" : "Following up"; | |
| 26 | ||
| 27 | /** | |
| 28 | * The link and its label for a sent message, or null: not sent yet, no address | |
| 29 | * for that channel, or a text to a lead who has asked to stop. | |
| 30 | */ | |
| 31 | export function handoff( | |
| 32 | m: Pick<Message, "status" | "channel" | "body" | "signature" | "day">, | |
| 33 | lead: { email: string; phone: string; opted_out_at: string | null }, | |
| 34 | ): { href: string; label: string } | null { | |
| 35 | if (m.status !== "sent") return null; | |
| 36 | if (m.channel === "email") { | |
| 37 | const to = lead.email.trim(); | |
| 38 | if (to === "") return null; | |
| 39 | return { | |
| 40 | href: `mailto:${encodeURIComponent(to)}?subject=${encodeURIComponent(subjectOf(m))}&body=${encodeURIComponent(approvedText(m, "\r\n"))}`, | |
| 41 | label: "Open in Mail", | |
| 42 | }; | |
| 43 | } | |
| 44 | const number = dialable(lead.phone); | |
| 45 | if (number === "" || lead.opted_out_at !== null) return null; | |
| 46 | return { | |
| 47 | href: `sms:${encodeURIComponent(number)}?body=${encodeURIComponent(approvedText(m, "\n"))}`, | |
| 48 | label: "Open in Messages", | |
| 49 | }; | |
| 50 | } |
src/pages/lead.ts +6 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | import { callLine, MAX_NOTE, OUTCOME_LABEL, OUTCOMES } from "../calls.ts"; |
| 8 | 8 | import { channelFor, isAnswer, MAX_BODY } from "../drafts.ts"; |
| 9 | 9 | import { escape } from "../http.ts"; |
| 10 | import { handoff } from "../handoff.ts"; | |
| 10 | 11 | import { MAX_ADDRESS, MAX_AHEAD_DAYS, showingLine, showingWhen } from "../showings.ts"; |
| 11 | 12 | import { elapsed, between } from "../clock.ts"; |
| 12 | 13 | import { goesLabel, isQuiet, nextOpening } from "../quiet.ts"; |
| @@ -128,6 +129,10 @@ export function leadPage(input: { | ||
| 128 | 129 | ...lead.calls.map((c) => ({ at: c.at, call: c })), |
| 129 | 130 | ...(lead.showing === null ? [] : [{ at: lead.showing.booked_at, showing: lead.showing }]), |
| 130 | 131 | ].sort((a, b) => Date.parse(a.at) - Date.parse(b.at)); |
| 132 | const link = (m: Message): string => { | |
| 133 | const h = handoff(m, lead); | |
| 134 | return h === null ? "" : `<p class="hint"><a href="${escape(h.href)}" data-handoff>${h.label}</a></p>`; | |
| 135 | }; | |
| 131 | 136 | const thread = |
| 132 | 137 | history.length === 0 |
| 133 | 138 | ? "" |
| @@ -149,7 +154,7 @@ export function leadPage(input: { | ||
| 149 | 154 | </li>` |
| 150 | 155 | : `<li> |
| 151 | 156 | <p class="meta">${titleOf(m!)} · ${CHANNEL[m!.channel]} · <time datetime="${escape(m!.sent_at ?? "")}">${escape(when(m!.sent_at ?? m!.due_at))}</time>${m!.sent_by ? ` · approved by ${escape(m!.sent_by)}` : ""}</p> |
| 152 | ${words(m!)} | |
| 157 | ${words(m!)}${link(m!)} | |
| 153 | 158 | </li>`, |
| 154 | 159 | ) |
| 155 | 160 | .join("")}</ol></section>`; |
new above
offline