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

small: The review page's finished screen says how many were sent and declined this round #251

Merged opened this

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

Merged

mason/deal-clock:mason/244-small-the-review-page-s-finished-screen → main 2 commits fork

  • Merged. mason/244-small-the-review-page-s-finished-screen was merged into main as 9e41467.

Review

Sign in to review this change.
5 files changed +74 −2
src/pages/review.ts +4 −1
@@ -15,6 +15,7 @@ import { heldFrom, momentLabel, type Draft } from "../reminders.ts";
1515 import type { Who } from "../who.ts";
1616 import { layout } from "./layout.ts";
1717 import { hidden } from "./sheet.ts";
18 import { settledLine, type Settled } from "../settled.ts";
1819
1920 export const REVIEW_PATH = "/reminders/today";
2021
@@ -61,6 +62,8 @@ export function reviewPage(input: {
6162 /** How many this round has sent or declined. */
6263 done: number;
6364 notice?: string | undefined;
65 /** What was approved and declined today, for the finished screen. */
66 settled?: Settled | undefined;
6467 open?: readonly string[] | undefined;
6568 error?: ReviewError | undefined;
6669 typed?: Typed | undefined;
@@ -71,7 +74,7 @@ export function reviewPage(input: {
7174 const back = `<a href="/${q === "" ? "" : `?q=${encodeURIComponent(q)}`}">Back to the clock</a>`;
7275 const body =
7376 draft === undefined
74 ? `<section class="card narrow"><h1>Review today's reminders</h1><p>All of today's reminders are sent or declined.</p><p>${back}</p></section>`
77 ? `<section class="card narrow"><h1>Review today's reminders</h1><p>${input.settled === undefined || settledLine(input.settled) === "" ? "All of today's reminders are sent or declined." : `All of today's reminders are sent or declined. ${escape(settledLine(input.settled))}`}</p><p>${back}</p></section>`
7578 : form(draft, { q, at, done, position: done + at + 1, total: done + queue.length, error, typed }, back);
7679 return layout({ body, who, agent, waiting: input.waiting, path: REVIEW_PATH, heading: "Review today's reminders", notice: input.notice, open: input.open, live: false });
7780 }
src/review.test.ts +21
@@ -218,3 +218,24 @@ test("Save contact takes an email and a phone together, and a bad phone keeps bo
218218 expect(after).toContain("[email protected]");
219219 expect(after).toContain("(555) 010-0199");
220220 });
221
222 test("the finished screen says how many were approved and declined today, counted from the records", async () => {
223 const quiet = await get("/reminders/today?q=zzzz-no-such-deal");
224 const before = /You (?:approved|declined)[^<]*today\./.exec(quiet)?.[0];
225 let sent = 0;
226 let declined = 0;
227 for (let i = 0; i < 2; i++) {
228 const one = shown(await get("/reminders/today"));
229 if (i === 0) { await post("/reminders/today/decline", { ...keys(one), reason: "Not needed" }); declined++; }
230 else { await post("/reminders/today/send", { ...keys(one), subject: "s", body: "b" }); sent++; }
231 }
232 const after = await get("/reminders/today?q=zzzz-no-such-deal");
233 const line = /You (?:approved (\d+))?(?: and )?(?:declined (\d+))? today\./.exec(after);
234 expect(line).not.toBeNull();
235 expect(Number(line![1] ?? 0)).toBeGreaterThanOrEqual(sent);
236 expect(Number(line![2] ?? 0)).toBeGreaterThanOrEqual(declined);
237 expect(after).not.toBe(quiet);
238 expect(before === undefined || before !== line![0]).toBe(true);
239 // The count does not come from the query string.
240 expect(await get("/reminders/today?q=zzzz-no-such-deal&done=9")).toContain(line![0]);
241 });
src/routes.ts +2 −1
@@ -101,6 +101,7 @@ import { dealPage, newDealPage } from "./pages/deal.ts";
101101 import { missingPage } from "./pages/missing.ts";
102102 import { refusedPage } from "./pages/refused.ts";
103103 import { knownPeople, narrowPeople, people } from "./people.ts";
104 import { settledOn } from "./settled.ts";
104105 import { reviewHref, reviewPage, type ReviewError, type ReviewField } from "./pages/review.ts";
105106 import { peoplePage } from "./pages/people.ts";
106107 import { activityPage } from "./pages/activity.ts";
@@ -1414,7 +1415,7 @@ export const routes: readonly Route[] = [
14141415 const sent = open === undefined ? undefined : listDeals(c.db, c.who);
14151416 const link = open === undefined || !sent?.ok ? undefined : sent.value.flatMap((d) => d.sent).find((m) => m.id === open)?.hrefs ?? [];
14161417 return html(
1417 reviewPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), queue: queue.value, q, at: count(c.url.searchParams.get("at")), done: count(c.url.searchParams.get("done")), notice, open: link }),
1418 reviewPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), queue: queue.value, q, at: count(c.url.searchParams.get("at")), done: count(c.url.searchParams.get("done")), notice, open: link, settled: queue.value.length === 0 ? settledOn(c.db, agentFor(c).zone ?? DEFAULT_ZONE, dayOf(agentFor(c))) : undefined }),
14181419 200,
14191420 clear,
14201421 );
src/settled.test.ts added +9
@@ -0,0 +1,9 @@
1 import { expect, test } from "bun:test";
2 import { settledLine } from "./settled.ts";
3
4 test("the line names what was approved and declined, and is empty when nothing was", () => {
5 expect(settledLine({ sent: 3, declined: 1 })).toBe("You approved 3 and declined 1 today.");
6 expect(settledLine({ sent: 2, declined: 0 })).toBe("You approved 2 today.");
7 expect(settledLine({ sent: 0, declined: 4 })).toBe("You declined 4 today.");
8 expect(settledLine({ sent: 0, declined: 0 })).toBe("");
9 });
src/settled.ts added +38
@@ -0,0 +1,38 @@
1 /**
2 * What the agent has settled today: reminders approved and reminders declined,
3 * counted from the records themselves (sent_at, declined at) so the finished
4 * screen tells the truth however the page was reached.
5 */
6
7 import type { Database } from "bun:sqlite";
8 import { dayOfStamp } from "./dates.ts";
9
10 export interface Settled {
11 readonly sent: number;
12 readonly declined: number;
13 }
14
15 /** Records written in the last two days, read in one query, then kept if they fall on `day` in `zone`. */
16 export function settledOn(db: Database, zone: string, day: string): Settled {
17 const rows = db
18 .query<{ kind: string; at: string }, []>(
19 `SELECT 's' AS kind, sent_at AS at FROM reminder WHERE sent_at >= strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-2 days')
20 UNION ALL
21 SELECT 'd', at FROM reminder_declined WHERE at >= strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-2 days')`,
22 )
23 .all();
24 let sent = 0;
25 let declined = 0;
26 for (const r of rows) {
27 if (dayOfStamp(zone, r.at) !== day) continue;
28 if (r.kind === "s") sent++;
29 else declined++;
30 }
31 return { sent, declined };
32 }
33
34 /** "You approved 3 and declined 1 today."; empty when nothing was settled today. */
35 export function settledLine(s: Settled): string {
36 const parts = [s.sent > 0 ? `approved ${s.sent}` : "", s.declined > 0 ? `declined ${s.declined}` : ""].filter((p) => p !== "");
37 return parts.length === 0 ? "" : `You ${parts.join(" and ")} today.`;
38 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs