Public: anyone can read this; the crew can change it.

The desk lists the agent's upcoming showings in time order, each linked to its lead #76

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/speed-to-lead:mason/59-the-desk-lists-the-agent-s-upcoming-show → main 1 commit fork

  • Merged. mason/59-the-desk-lists-the-agent-s-upcoming-show was merged into main as 17f2de3.

Review

Sign in to review this change.
11 files changed +141 −7
README.md +1
@@ -65,6 +65,7 @@ what you see right after a restart. Do step 1 first.
6565 lead's plan as "After the showing". The sample **Rosa Alvarez** had a
6666 showing yesterday, so her note is already waiting on a fresh desk. A
6767 cancelled showing has none; a second showing gets a second note.
68 A fresh desk also lists upcoming showings above the leads (soonest first, at most five, each linked to its lead): **Aisha Bello** has one in two days.
6869 8. Set your profile (admin to save). Press "Your profile" at the top, fill
6970 in your name, brokerage, license number, phone and email, and save. Every
7071 draft not yet sent takes it at once.
docs/design/areas/desk.md +7
@@ -12,3 +12,10 @@ The agent opens the desk and a lead's page on a phone in the meeting. Under
1212
1313 No new component class: the rules extend `.lead-row`, `.actions`, `.states`,
1414 `.button`, `.views`, `.search` and `.plan`.
15
16 # Desk: the showings list
17
18 Above the record and the leads, while a showing is ahead: `.showings`, a
19 `section` holding an `h2`, a plain `ul` of `.showing-when` (the time, as the
20 badge words it), the lead's name as a link, and the address in `.muted`.
21 At most five, then "and N more". Absent, not empty, when none is ahead.
src/after-showing.test.ts +3 −1
@@ -227,7 +227,9 @@ test("a sample desk has a showing from yesterday and its note ready now", () =>
227227 .query<{ name: string; status: string; body: string }, [number]>(
228228 "SELECT lead.name, message.status, message.body FROM message JOIN lead ON lead.id = message.lead_id WHERE message.day = ?1",
229229 )
230 .all(AFTER_SHOWING_DAY);
230 .all(AFTER_SHOWING_DAY)
231 // A showing still ahead (Aisha's) drafts its note too, but it is not due.
232 .filter((m) => m.name === "Rosa Alvarez");
231233 expect(ready).toHaveLength(1);
232234 expect(ready[0]!.status).toBe("due");
233235 expect(ready[0]!.body).toContain("418 Juniper Ln");
src/leads.ts +18
@@ -232,6 +232,24 @@ export const showingOf = (db: Database, id: number): Showing | null =>
232232 .query<Showing, [number]>("SELECT * FROM showing WHERE lead_id = ?1")
233233 .get(id);
234234
235 /** A showing still ahead, with the lead it is for. */
236 export interface Ahead {
237 readonly id: number;
238 readonly name: string;
239 readonly at: string;
240 readonly address: string;
241 }
242
243 /** Every showing after `now`, soonest first (cancelled ones are gone from the table). */
244 export const showingsAhead = (db: Database, now: number): Ahead[] =>
245 db
246 .query<Ahead, [string]>(
247 `SELECT lead.id AS id, lead.name AS name, showing.at AS at, showing.address AS address
248 FROM showing JOIN lead ON lead.id = showing.lead_id
249 WHERE showing.at > ?1 ORDER BY showing.at, lead.id`,
250 )
251 .all(new Date(now).toISOString());
252
235253 export function detail(db: Database, id: number): LeadDetail | null {
236254 const found = findLead(db, id);
237255 return found === null
src/pages/desk.ts +22 −2
@@ -9,9 +9,9 @@ import { escape } from "../http.ts";
99 import { recordLine, sourceLine } from "../clock.ts";
1010 import { isAfterShowing, isAnswer } from "../drafts.ts";
1111 import { goesLabel, RECORD_NOTE } from "../quiet.ts";
12 import { showingWhen } from "../showings.ts";
12 import { MAX_LISTED, showingWhen } from "../showings.ts";
1313 import { MAX_EMAIL_TEXT, missingLine } from "../intake.ts";
14 import { inView, noteLine, type DeskLead, type NewLead, type ResponseRecord, type View } from "../leads.ts";
14 import { inView, type Ahead, noteLine, type DeskLead, type NewLead, type ResponseRecord, type View } from "../leads.ts";
1515 import type { Agent } from "../env.ts";
1616 import type { Who } from "../who.ts";
1717
@@ -23,6 +23,8 @@ export function deskPage(input: {
2323 show?: View | undefined;
2424 q?: string | undefined;
2525 counts?: Record<View, number> | undefined;
26 /** Every showing still ahead, soonest first; the desk lists the first few. */
27 ahead?: readonly Ahead[] | undefined;
2628 mayAdd: boolean;
2729 mayReset: boolean;
2830 notice?: string | undefined;
@@ -74,6 +76,7 @@ export function deskPage(input: {
7476 leads.length === 0
7577 ? `<p class="empty">${q === "" ? EMPTY[show] : `No lead matches '${escape(q)}'${show === "all" ? "" : " in this view"}.`}</p>`
7678 : `<ul class="desk">${leads.map((lead) => row(lead, now, agent.timeZone)).join("")}</ul>`;
79 const showings = showingsList(input.ahead ?? [], now, agent.timeZone);
7780 const tab = deskTitle(counts?.needs ?? leads.filter((l) => inView(l, "needs")).length);
7881 return layout({
7982 who,
@@ -83,6 +86,7 @@ export function deskPage(input: {
8386 tab,
8487 body: `<div id="desk" data-live="lead.added lead.changed" data-title="${escape(tab)}">
8588 <div class="desk-head"><h1>Leads</h1>${summary}</div>
89 ${showings}
8690 ${record}
8791 ${search}
8892 ${views}
@@ -145,6 +149,22 @@ const EMPTY: Record<View, string> = {
145149 all: "No leads yet. New inquiries appear here the moment they arrive.",
146150 };
147151
152 /** The next showings, soonest first, each linking to its lead; nothing at all when none is ahead. */
153 function showingsList(ahead: readonly Ahead[], now: number, zone: string): string {
154 if (ahead.length === 0) return "";
155 const more = ahead.length - MAX_LISTED;
156 return `<section class="showings" aria-label="Showings">
157 <h2>Showings</h2>
158 <ul>${ahead
159 .slice(0, MAX_LISTED)
160 .map(
161 (s) =>
162 `<li><span class="showing-when">${escape(showingWhen(s.at, now, zone))}</span> <a href="/leads/${s.id}">${escape(s.name)}</a>${s.address === "" ? "" : ` <span class="muted">${escape(s.address)}</span>`}</li>`,
163 )
164 .join("")}</ul>${more > 0 ? `\n <p class="muted">and ${more} more</p>` : ""}
165 </section>`;
166 }
167
148168 /** A follow-up due while a showing is ahead waits until the day after it. */
149169 const held = (lead: DeskLead, due: string): string =>
150170 lead.showing !== null && due < lead.showing.hold_until ? lead.showing.hold_until : due;
src/routes.ts +2 −1
@@ -68,7 +68,7 @@ import { MAX_NOTE, OUTCOMES } from "./calls.ts";
6868 import { MAX_ADDRESS, MAX_AHEAD_DAYS } from "./showings.ts";
6969 import { MAX_EMAIL_TEXT } from "./intake.ts";
7070 import { LOST_REASONS, lostSaid } from "./lost.ts";
71 import { MAX_INBOUND, MAX_LEAD_NOTE, MAX_QUERY, Q_HELP, VIEW_HELP, VIEWS, type NewLead } from "./leads.ts";
71 import { MAX_INBOUND, MAX_LEAD_NOTE, MAX_QUERY, Q_HELP, showingsAhead, VIEW_HELP, VIEWS, type NewLead } from "./leads.ts";
7272 import { events } from "./live.ts";
7373 import { rpc } from "./mcp.ts";
7474 import { deskPage } from "./pages/desk.ts";
@@ -559,6 +559,7 @@ function desk(
559559 show: leads.ok ? leads.value.show : undefined,
560560 q: leads.ok ? leads.value.q : undefined,
561561 counts: leads.ok ? leads.value.counts : undefined,
562 ahead: leads.ok ? showingsAhead(c.db, Date.now()) : undefined,
562563 mayAdd: permit(c.who, "lead", "create") === "yes",
563564 mayReset: permit(c.who, "lead", "delete") === "yes",
564565 notice: pasted?.notice ?? refused ?? notice,
src/sample.ts +8 −1
@@ -34,6 +34,8 @@ interface Sample extends NewLead {
3434 readonly closed?: Closing;
3535 /** A showing already held, this long ago (rounded to the agent's morning after it). */
3636 readonly showedAgo?: number;
37 /** A showing booked ahead: this many days from today at 10:00 in the agent's zone. */
38 readonly showingInDays?: number;
3739 /** The agent's private note on the lead, as of the first reply or the inquiry. */
3840 readonly note?: string;
3941 }
@@ -69,7 +71,7 @@ export const SAMPLES: readonly Sample[] = [
6971 person("David Chen", "website", false, "We're thinking about selling our townhouse in the spring. Any idea what it might be worth in today's market?", { ago: 3 * M + 10 * S }),
7072 person("Priya Raman", "open_house", true, "Loved the kitchen at Sunday's open house on Alder Ct. Are the sellers flexible on price at all?", { ago: 18 * M }),
7173 person("James O'Neill", "referral", false, "Sarah Patel said you helped her family relocate last year. We're moving from Denver in August and don't know the area.", { ago: 47 * M }),
72 person("Aisha Bello", "zillow", false, "Pre-approved up to $550k. Looking for 3 bedrooms with a yard, ideally near good schools.", { ago: 2 * H + 12 * M, note: "Pre-approved to $550k. Wants 3 bed with a yard near Lincoln Elementary. Lease ends in June, so moving by then." }),
74 person("Aisha Bello", "zillow", false, "Pre-approved up to $550k. Looking for 3 bedrooms with a yard, ideally near good schools.", { ago: 2 * H + 12 * M, showingInDays: 2, note: "Pre-approved to $550k. Wants 3 bed with a yard near Lincoln Elementary. Lease ends in June, so moving by then." }),
7375 person("Tom Becker", "website", true, "Do you work with first-time buyers? We have no idea where to start.", { ago: 5 * H + 30 * M }),
7476 person("Lena Novak", "open_house", false, "Signed in at the open house on Birch St. Could you send me the disclosures and HOA details?", { ago: 26 * H }),
7577 person("Marcus Lee", "zillow", true, "Can I tour 92 Harbor View Dr this week? Available most evenings after 6.", { ago: 2 * D + 3 * H, answeredIn: 48 * S, touched: [1], note: "Works nights: call after 6. Cash buyer, relocating from Austin; wants a short closing." }),
@@ -106,6 +108,11 @@ export function seed(db: Database, now: Date): number {
106108 held = wallAt(held, -1, 10, agent.timeZone) ?? held - D;
107109 bookShowing(db, findLead(db, id)!, iso(held), "418 Juniper Ln", iso(replied(sample, asked)), agent);
108110 }
111 if (sample.showingInDays !== undefined) {
112 const agent = currentAgent(db);
113 const ahead = wallAt(t, sample.showingInDays, 10, agent.timeZone) ?? t + sample.showingInDays * D;
114 bookShowing(db, findLead(db, id)!, iso(ahead), "27 Alder Ct", iso(asked), agent);
115 }
109116 if (sample.note !== undefined)
110117 writeNote(db, findLead(db, id)!, sample.note, iso(asked + 30 * M));
111118 if (sample.closed !== undefined) close(db, id, sample.closed, iso(t - 2 * D));
src/server.test.ts +1 −1
@@ -636,7 +636,7 @@ test("the desk views: links with counts, aria-current, bad value is all, JSON ma
636636 leads: { id: number }[];
637637 };
638638 expect(json.show).toBe(show);
639 const inHtml = [...page.matchAll(/href="\/leads\/(\d+)"/g)].map((m) => Number(m[1]));
639 const inHtml = [...page.matchAll(/class="lead-row" href="\/leads\/(\d+)"/g)].map((m) => Number(m[1]));
640640 expect(inHtml).toEqual(json.leads.map((l) => l.id));
641641 }
642642 const bad = await (await api("/?show=zzz", {}, alice)).text();
src/showings.test.ts +64 −1
@@ -3,7 +3,7 @@
33 * before it held until the day after and released on cancel, the history
44 * and the desk badge escaped; at every door.
55 */
6 import { afterAll, expect, test } from "bun:test";
6 import { afterAll, describe, expect, test } from "bun:test";
77 import { mkdtempSync } from "node:fs";
88 import { tmpdir } from "node:os";
99 import { join } from "node:path";
@@ -18,6 +18,8 @@ import {
1818 turn,
1919 } from "./actions.ts";
2020 import { openDb } from "./db.ts";
21 import { deskPage } from "./pages/desk.ts";
22 import { showingsAhead, type Ahead } from "./leads.ts";
2123 import { currentAgent, storeAgent } from "./profile.ts";
2224 import { serve } from "./server.ts";
2325 import { localInput, showingLine, showingWhen, timeOf } from "./showings.ts";
@@ -397,3 +399,64 @@ test("booking reads a time with no offset in the profile's zone, and holds follo
397399 const explicit = value(bookShowing(db, alice, { id, when: "2026-03-07T10:00:00Z" }, at(1)));
398400 expect(explicit.showing?.at).toBe("2026-03-07T10:00:00.000Z");
399401 });
402
403 describe("the desk's list of upcoming showings", () => {
404 const NOW = Date.parse("2026-03-01T10:00:00Z");
405 const listing = (ahead: Ahead[], who: Who = alice) =>
406 deskPage({ who, agent: currentAgent(fresh()), leads: who.role === "anon" ? undefined : [], ahead, mayAdd: false, mayReset: false });
407 const a = (id: number, days: number, name = `Lead ${id}`, address = ""): Ahead => ({
408 id,
409 name,
410 address,
411 at: new Date(NOW + days * DAY).toISOString(),
412 });
413
414 test("lists soonest first, linked, with the address", () => {
415 const db = fresh();
416 const mk = (name: string) =>
417 value(addLead(db, alice, { name, email: `${name}@example.com`, phone: "", source: "zillow", message: "Hi", consent: false }, T0)).id;
418 const [x, y, z] = [mk("late"), mk("soon"), mk("past")];
419 value(bookShowing(db, alice, { id: x, when: "2026-03-05T10:00", address: "9 Late St" }, T0));
420 value(bookShowing(db, alice, { id: y, when: "2026-03-03T10:00", address: "" }, T0));
421 value(bookShowing(db, alice, { id: z, when: "2026-03-04T10:00", address: "" }, T0));
422 value(cancelShowing(db, alice, { id: z }, T0));
423 const rows = showingsAhead(db, T0.getTime());
424 expect(rows.map((r) => r.name)).toEqual(["soon", "late"]);
425 const page = listing(rows);
426 expect(page.indexOf(`/leads/${y}">soon`)).toBeLessThan(page.indexOf(`/leads/${x}">late`));
427 expect(page).toContain("9 Late St");
428 expect(page).not.toContain("past");
429 });
430
431 test("a showing already past is not ahead", () => {
432 const db = fresh();
433 const id = value(addLead(db, alice, { name: "Old", email: "[email protected]", phone: "", source: "zillow", message: "Hi", consent: false }, T0)).id;
434 value(bookShowing(db, alice, { id, when: "2026-03-03T10:00", address: "" }, T0));
435 expect(showingsAhead(db, at(3).getTime())).toEqual([]);
436 });
437
438 test("shows five and counts the rest", () => {
439 const page = listing([1, 2, 3, 4, 5, 6, 7].map((i) => a(i, i)));
440 expect(page.match(/class="showing-when"/g)).toHaveLength(5);
441 expect(page).toContain("and 2 more");
442 expect(listing([1, 2, 3, 4, 5].map((i) => a(i, i)))).not.toContain(" more");
443 });
444
445 test("with none ahead there is no list", () => {
446 expect(listing([])).not.toContain("showings");
447 expect(listing([])).not.toContain("Showings");
448 });
449
450 test("names and addresses are escaped", () => {
451 const page = listing([a(1, 1, "<script>x()</script>", "<img src=x onerror=y()>")]);
452 expect(page).not.toContain("<script>x()");
453 expect(page).not.toContain("<img src=x");
454 expect(page).toContain("&lt;script&gt;");
455 });
456
457 test("the signed-out page has no list", () => {
458 const page = listing([a(1, 1, "Secret Name")], anon);
459 expect(page).not.toContain("Secret Name");
460 expect(page).not.toContain('class="showings"');
461 });
462 });
src/showings.ts +3
@@ -9,6 +9,9 @@ export const MAX_ADDRESS = 200;
99 /** How far ahead a showing may be booked. */
1010 export const MAX_AHEAD_DAYS = 90;
1111
12 /** How many upcoming showings the desk lists before "and N more". */
13 export const MAX_LISTED = 5;
14
1215 const DAY_MS = 86_400_000;
1316
1417 /** A showing as stored: one per lead. */
static/app.css +12
@@ -724,3 +724,15 @@ label.check input {
724724 bottom: var(--space-3);
725725 }
726726 }
727
728 .showings {
729 margin-block: var(--space-3);
730 }
731 .showings ul {
732 list-style: none;
733 margin: 0;
734 padding: 0;
735 }
736 .showing-when {
737 font-weight: 600;
738 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs