Public: anyone can read this; the crew can change it.
Each deal shows a Messages record: every reminder sent for it, newest first, with who, when and the exact words approved #26
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/23-each-deal-shows-a-messages-record-every → main 2 commits fork
13 files
changed +200
−17
README.md +4
| @@ -38,6 +38,10 @@ since they were last set, so the screen always looks like today. | ||
| 38 | 38 | Decided against a message? Beside Send, **Don't send** asks why (up to 200 |
| 39 | 39 | characters). The reminder stops counting as "to send" and reads "Not sent: |
| 40 | 40 | <reason> · <who>"; the reason is kept, and **Undo** makes it a draft again. |
| 41 | Every message you sent is kept: open 1418 Alder Lane's page and find | |
| 42 | **Messages**, newest first, each with who it went to, which date and | |
| 43 | moment, the day it was sent, and (tap it open) the exact words you approved. | |
| 44 | In the terminal: open the deal with o, then press s. | |
| 41 | 45 | 5. **Move a date, with a reason.** Open the same marker again and use Move |
| 42 | 46 | this date: pick a new date and say why ("Inspector delayed"). The marker |
| 43 | 47 | jumps to the new date, and the sheet lists the move, the reason and who |
contract/openapi.json +12
| @@ -1086,6 +1086,7 @@ | ||
| 1086 | 1086 | }, |
| 1087 | 1087 | "sent": { |
| 1088 | 1088 | "type": "array", |
| 1089 | "description": "The Messages record: every reminder marked sent, newest first, with the words as approved. The date's label and the party's name and role are kept as they were, so an entry reads the same after either is removed.", | |
| 1089 | 1090 | "items": { |
| 1090 | 1091 | "type": "object", |
| 1091 | 1092 | "properties": { |
| @@ -1098,6 +1099,17 @@ | ||
| 1098 | 1099 | "party_id": { |
| 1099 | 1100 | "type": "integer" |
| 1100 | 1101 | }, |
| 1102 | "label": { | |
| 1103 | "type": "string", | |
| 1104 | "description": "The date it was about, as named when sent." | |
| 1105 | }, | |
| 1106 | "party_name": { | |
| 1107 | "type": "string" | |
| 1108 | }, | |
| 1109 | "party_role": { | |
| 1110 | "type": "string", | |
| 1111 | "description": "The party's role when sent; empty on a reminder sent before roles were kept." | |
| 1112 | }, | |
| 1101 | 1113 | "moment": { |
| 1102 | 1114 | "enum": [ |
| 1103 | 1115 | "before", |
docs/TECHNICAL.md +5 −1
| @@ -22,7 +22,11 @@ marked sent stay on record. Over the API: `PATCH` or `DELETE | ||
| 22 | 22 | |
| 23 | 23 | Sending is simulated: *Send* records that a reminder went out and the |
| 24 | 24 | words approved with it; nothing leaves the app. The record is shown on the |
| 25 | page, in `/api/deals/:id`, in MCP and in the terminal. | |
| 25 | page (the **Messages** section of `/deals/:id`, newest first, each entry | |
| 26 | opening to the approved words), in `/api/deals/:id` and the MCP `get_deal` as | |
| 27 | `sent`, and in the terminal (open a deal with `o`, then `s`: the sent reminders, | |
| 28 | newest first; enter opens the words; `b` goes back). The party's role is stored with the send, so an | |
| 29 | entry reads the same after the party or the date is removed. | |
| 26 | 30 | |
| 27 | 31 | *Don't send* declines a reminder with a reason (1 to 200 characters), kept in |
| 28 | 32 | `reminder_declined` and shown as "Not sent: reason · who". A declined reminder |
src/db.test.ts +15 −1
| @@ -64,6 +64,20 @@ test("sample deals boot looking like today, and a changed one is left alone", () | ||
| 64 | 64 | expect(again.query("SELECT value FROM app_meta WHERE key = 'sample_anchor'").get()).toEqual({ value: today() }); |
| 65 | 65 | }); |
| 66 | 66 | |
| 67 | test("a reminder sent under the name Sample data does not pass as the sample's own", () => { | |
| 68 | const dir = mkdtempSync(join(tmpdir(), "app-spoof-")); | |
| 69 | const db = openDb(dir); | |
| 70 | db.exec("DELETE FROM deal; DELETE FROM app_meta"); | |
| 71 | seed(db, addDays(today(), -30)); | |
| 72 | const orchard = db.query<{ id: number }, []>("SELECT id FROM deal WHERE address = '9 Orchard Row'").get()!.id; | |
| 73 | const d = db.query<{ id: number; due: string }, [number]>("SELECT id, due FROM deadline WHERE deal_id = ?1 AND kind = 'closing'").get(orchard)!; | |
| 74 | const p = db.query<{ id: number }, [number]>("SELECT id FROM party WHERE deal_id = ?1").get(orchard)!; | |
| 75 | db.query("INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, moment, due, by_name) VALUES (?1, ?2, ?3, 'Closing', 'X', 'day_of', ?4, 'Sample data')").run(orchard, d.id, p.id, d.due); | |
| 76 | db.close(); | |
| 77 | const again = openDb(dir); | |
| 78 | expect(again.query<{ due: string }, [number]>("SELECT due FROM deadline WHERE id = ?1").get(d.id)!.due).toBe(d.due); | |
| 79 | }); | |
| 80 | ||
| 67 | 81 | test("a database whose reminder columns came from another migration order still opens", () => { |
| 68 | 82 | const dir = mkdtempSync(join(tmpdir(), "app-db-order-")); |
| 69 | 83 | openDb(dir).close(); |
| @@ -72,5 +86,5 @@ test("a database whose reminder columns came from another migration order still | ||
| 72 | 86 | raw.close(); |
| 73 | 87 | const db = openDb(dir); |
| 74 | 88 | expect(check(db).version).toBe(migrations.length); |
| 75 | expect(db.query("SELECT subject, body FROM reminder").all()).toEqual([]); | |
| 89 | expect(db.query("SELECT subject, body FROM reminder WHERE sample = 0").all()).toEqual([]); | |
| 76 | 90 | }); |
src/db.ts +10
| @@ -143,6 +143,16 @@ export const migrations: string[] = [ | ||
| 143 | 143 | UNIQUE (deadline_id, party_id, moment, due) |
| 144 | 144 | ); |
| 145 | 145 | CREATE INDEX IF NOT EXISTS reminder_declined_deal ON reminder_declined (deal_id)`, |
| 146 | // The party's role when it was sent, kept beside their name so the record | |
| 147 | // reads the same after the party is removed. Rows whose party is already | |
| 148 | // gone carry '' and are shown by name alone. | |
| 149 | `ALTER TABLE reminder ADD COLUMN party_role TEXT NOT NULL DEFAULT ''; | |
| 150 | UPDATE reminder SET party_role = coalesce((SELECT role FROM party WHERE party.id = reminder.party_id), '')`, | |
| 151 | // Which sent reminders the sample data wrote itself: set only by the seed, | |
| 152 | // never from a signer's name, so the sample refresh can tell its own from a | |
| 153 | // person's. | |
| 154 | `ALTER TABLE reminder ADD COLUMN sample INTEGER NOT NULL DEFAULT 0; | |
| 155 | UPDATE reminder SET sample = 1 WHERE by_name = 'Sample data' AND deal_id IN (SELECT id FROM deal WHERE author_id = 'sample')`, | |
| 146 | 156 | ]; |
| 147 | 157 | |
| 148 | 158 | export function openDb(dir = dataDir): Database { |
src/deals.test.ts +15
| @@ -315,6 +315,21 @@ test("an edited contact shows in the next draft; a removed party gets none", () | ||
| 315 | 315 | expect(removeParty(db, alice, { id: lender.value.id })).toMatchObject({ ok: false, error: "not_found" }); |
| 316 | 316 | }); |
| 317 | 317 | |
| 318 | test("the record keeps the party's role and the date's label after both are removed, newest first", () => { | |
| 319 | const deal = open({ contractDate: addDays(today(), -5), closingDate: addDays(today(), 30) }); | |
| 320 | const hoa = addDeadline(db, alice, { dealId: deal.id, label: "HOA review", due: addDays(today(), 9), owes: "buyer" }); | |
| 321 | expect(hoa.ok).toBe(true); | |
| 322 | const id = hoa.ok ? hoa.value.id : 0; | |
| 323 | const buyer = deal.parties.find((p) => p.role === "buyer")!; | |
| 324 | const seller = deal.parties.find((p) => p.role === "seller")!; | |
| 325 | sendReminder(db, alice, { deadlineId: id, partyId: buyer.id, moment: "before" }); | |
| 326 | sendReminder(db, alice, { deadlineId: id, partyId: seller.id, moment: "before", subject: "Edited", body: "Mine"}); | |
| 327 | removeDeadline(db, alice, { id }); | |
| 328 | const record = fresh(deal.id).sent; | |
| 329 | expect(record.map((r) => [r.party_role, r.label])).toEqual([["seller", "HOA review"], ["buyer", "HOA review"]]); | |
| 330 | expect(record[0]!.body).toBe("Mine"); | |
| 331 | }); | |
| 332 | ||
| 318 | 333 | test("a standard date cannot be removed; a custom one goes with its drafts, and a sent reminder stays", () => { |
| 319 | 334 | const deal = open(); |
| 320 | 335 | const inspection = due(deal, "inspection"); |
src/deals.ts +6 −4
| @@ -72,6 +72,8 @@ export interface Sent { | ||
| 72 | 72 | /** The date's and the party's words when it was sent: they stay if either is removed. */ |
| 73 | 73 | readonly label: string; |
| 74 | 74 | readonly party_name: string; |
| 75 | /** The party's role when it was sent; '' on a reminder sent before it was kept. */ | |
| 76 | readonly party_role: PartyRole | ""; | |
| 75 | 77 | readonly moment: Moment; |
| 76 | 78 | readonly due: string; |
| 77 | 79 | readonly by_name: string; |
| @@ -176,7 +178,7 @@ function view(db: Database, deal: Deal): DealView { | ||
| 176 | 178 | })), |
| 177 | 179 | sent: db |
| 178 | 180 | .query<Sent, [number]>( |
| 179 | "SELECT * FROM reminder WHERE deal_id = ?1 ORDER BY id", | |
| 181 | "SELECT * FROM reminder WHERE deal_id = ?1 ORDER BY sent_at DESC, id DESC", | |
| 180 | 182 | ) |
| 181 | 183 | .all(deal.id), |
| 182 | 184 | declined: db |
| @@ -664,11 +666,11 @@ export function sendReminder( | ||
| 664 | 666 | if (body === undefined) |
| 665 | 667 | return within(db, who, dealId, fail("invalid", `The message is 1 to ${BODY_MAX} characters.`)); |
| 666 | 668 | const sent = db |
| 667 | .query<Sent, [number, number, number, string, string, string, string, string, string, string]>( | |
| 668 | `INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, moment, due, by_name, subject, body) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10) | |
| 669 | .query<Sent, [number, number, number, string, string, string, string, string, string, string, string]>( | |
| 670 | `INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, party_role, moment, due, by_name, subject, body) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11) | |
| 669 | 671 | ON CONFLICT (deadline_id, party_id, moment, due) DO UPDATE SET sent_at = sent_at RETURNING *`, |
| 670 | 672 | ) |
| 671 | .get(found.value.deal.id, deadline.id, party.id, deadline.label, party.name, input.moment, deadline.due, authorOf(who).name, subject, body)!; | |
| 673 | .get(found.value.deal.id, deadline.id, party.id, deadline.label, party.name, party.role, input.moment, deadline.due, authorOf(who).name, subject, body)!; | |
| 672 | 674 | return within(db, who, dealId, ok(sent)); |
| 673 | 675 | } |
| 674 | 676 |
src/pages/deal.ts +23 −2
| @@ -4,10 +4,11 @@ | ||
| 4 | 4 | * same sheet the clock does. And the form that opens a new deal. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { addDays, relative, spoken } from "../dates.ts"; | |
| 8 | import { canChange, canRemove, type DealView, type Party, type Plan, type PlannedDate } from "../deals.ts"; | |
| 7 | import { addDays, relative, short, spoken } from "../dates.ts"; | |
| 8 | import { canChange, canRemove, type DealView, type Party, type Plan, type PlannedDate, type Sent } from "../deals.ts"; | |
| 9 | 9 | import { DEFAULT_LENGTHS, PARTY_LABEL, PARTY_ROLES } from "../deadlines.ts"; |
| 10 | 10 | import { escape } from "../http.ts"; |
| 11 | import { MOMENT_LABEL } from "../reminders.ts"; | |
| 11 | 12 | import type { Who } from "../who.ts"; |
| 12 | 13 | import { layout } from "./layout.ts"; |
| 13 | 14 | import { badge, dueLine, owedBy, sheet, sheetId } from "./sheet.ts"; |
| @@ -31,6 +32,22 @@ const partyControls = (p: Party, to: string): string => `<div class="party-contr | ||
| 31 | 32 | <form method="post" action="/parties/${p.id}/remove"><input type="hidden" name="to" value="${escape(to)}"><button class="button button-quiet button-small" aria-label="Remove ${escape(p.name)}">Remove</button></form> |
| 32 | 33 | </div>`; |
| 33 | 34 | |
| 35 | /** The record of what went out: each sent reminder with the words approved, newest first. */ | |
| 36 | function messages(deal: DealView): string { | |
| 37 | if (deal.sent.length === 0) return `<p class="muted">Nothing sent yet</p>`; | |
| 38 | const live = new Set(deal.deadlines.map((d) => d.id)); | |
| 39 | const item = (m: Sent): string => { | |
| 40 | const role = m.party_role === "" ? "" : ` · ${escape(PARTY_LABEL[m.party_role])}`; | |
| 41 | const removed = live.has(m.deadline_id) ? "" : " (date removed)"; | |
| 42 | const words = | |
| 43 | m.subject === "" | |
| 44 | ? `<p class="muted">The words were not kept for this one.</p>` | |
| 45 | : `<div class="draft-text"><p><b>${escape(m.subject)}</b></p><p class="draft-body">${escape(m.body)}</p></div>`; | |
| 46 | return `<li class="draft"><details><summary><span><b>${escape(m.party_name)}</b> <span class="muted">${role} · ${escape(m.label)}${removed}, ${escape(short(m.due))} · ${escape(MOMENT_LABEL[m.moment])}</span><br>${escape(m.subject === "" ? "(no subject kept)" : m.subject)}</span><span class="state" data-sent>Sent ${escape(short(m.sent_at.slice(0, 10)))}</span></summary>${words}</details></li>`; | |
| 47 | }; | |
| 48 | return `<ul class="drafts">${deal.sent.map(item).join("")}</ul>`; | |
| 49 | } | |
| 50 | ||
| 34 | 51 | export function dealPage(input: { |
| 35 | 52 | who: Who; |
| 36 | 53 | deal: DealView; |
| @@ -104,6 +121,10 @@ export function dealPage(input: { | ||
| 104 | 121 | <h2>Parties</h2> |
| 105 | 122 | ${parties} |
| 106 | 123 | </section> |
| 124 | <section class="card" id="messages"> | |
| 125 | <h2>Messages</h2> | |
| 126 | ${messages(deal)} | |
| 127 | </section> | |
| 107 | 128 | ${deal.deadlines.map((d) => sheet(deal, d, who, on, path)).join("")} |
| 108 | 129 | </div> |
| 109 | 130 | ${forms} |
src/routes.ts +5
| @@ -167,12 +167,17 @@ export const DEAL: Schema = { | ||
| 167 | 167 | }, |
| 168 | 168 | sent: { |
| 169 | 169 | type: "array", |
| 170 | description: | |
| 171 | "The Messages record: every reminder marked sent, newest first, with the words as approved. The date's label and the party's name and role are kept as they were, so an entry reads the same after either is removed.", | |
| 170 | 172 | items: { |
| 171 | 173 | type: "object", |
| 172 | 174 | properties: { |
| 173 | 175 | id: { type: "integer" }, |
| 174 | 176 | deadline_id: { type: "integer" }, |
| 175 | 177 | party_id: { type: "integer" }, |
| 178 | label: { type: "string", description: "The date it was about, as named when sent." }, | |
| 179 | party_name: { type: "string" }, | |
| 180 | party_role: { type: "string", description: "The party's role when sent; empty on a reminder sent before roles were kept." }, | |
| 176 | 181 | moment: { enum: ["before", "day_of"] }, |
| 177 | 182 | due: DATE, |
| 178 | 183 | by_name: { type: "string" }, |
src/sample.ts +22 −3
| @@ -24,6 +24,8 @@ interface Sample { | ||
| 24 | 24 | readonly done?: readonly string[]; |
| 25 | 25 | readonly custom?: readonly [string, number, PartyRole, string][]; |
| 26 | 26 | readonly moved?: readonly [string, number, string]; |
| 27 | /** A reminder already sent, in the agent's own edited words: date kind, party role, moment, subject, body. */ | |
| 28 | readonly sent?: readonly [string, PartyRole, "before" | "day_of", string, string]; | |
| 27 | 29 | } |
| 28 | 30 | |
| 29 | 31 | const SAMPLES: readonly Sample[] = [ |
| @@ -40,6 +42,13 @@ const SAMPLES: readonly Sample[] = [ | ||
| 40 | 42 | ["other_agent", "Jordan Ellis, Crestline Homes", "(555) 014-2290"], |
| 41 | 43 | ], |
| 42 | 44 | done: ["inspection", "title", "appraisal"], |
| 45 | sent: [ | |
| 46 | "inspection", | |
| 47 | "buyer", | |
| 48 | "before", | |
| 49 | "Alder Lane: your inspection period ends in three days", | |
| 50 | "Hi Maya and Tom,\n\nYour inspection period ends in three days. If the report left anything you want the seller to fix, tell me by tomorrow and I will put the request in writing before it closes.\n\nThanks,\nYour agent", | |
| 51 | ], | |
| 43 | 52 | }, |
| 44 | 53 | { |
| 45 | 54 | address: "77 Harbor View Dr, Unit 4B", |
| @@ -141,8 +150,8 @@ export function isUntouched(state: SampleState, anchor: string): boolean { | ||
| 141 | 150 | /** |
| 142 | 151 | * Bring the sample deals nobody has touched up to `on`, then remember `on` as |
| 143 | 152 | * the day they are anchored to. A deal a person changed is left where it is. |
| 144 | * (A deal with a sent reminder is a changed one, so no reminder row needs to | |
| 145 | * follow a date.) | |
| 153 | * (A reminder the sample itself sent follows its date; any other sent | |
| 154 | * reminder makes the deal a changed one.) | |
| 146 | 155 | */ |
| 147 | 156 | export function refresh(db: Database, on = today()): void { |
| 148 | 157 | db.transaction(() => { |
| @@ -163,7 +172,7 @@ export function refresh(db: Database, on = today()): void { | ||
| 163 | 172 | "SELECT count(*) AS n FROM party WHERE deal_id = ?1 AND role NOT IN ('buyer', 'seller')", |
| 164 | 173 | ).get(deal.id)!.n; |
| 165 | 174 | const reminders = db.query<{ n: number }, [number]>( |
| 166 | "SELECT count(*) AS n FROM reminder WHERE deal_id = ?1", | |
| 175 | "SELECT count(*) AS n FROM reminder WHERE deal_id = ?1 AND sample = 0", | |
| 167 | 176 | ).get(deal.id)!.n; |
| 168 | 177 | const state: SampleState = { |
| 169 | 178 | address: deal.address, |
| @@ -186,6 +195,9 @@ export function refresh(db: Database, on = today()): void { | ||
| 186 | 195 | db.query("UPDATE deal SET contract_date = ?1 WHERE id = ?2").run(shifted.contractDate, deal.id); |
| 187 | 196 | for (const d of shifted.deadlines) db.query("UPDATE deadline SET due = ?1, done_at = ?2 WHERE id = ?3").run(d.due, d.doneAt, d.id); |
| 188 | 197 | for (const m of shifted.moves) db.query("UPDATE deadline_move SET from_due = ?1, to_due = ?2 WHERE id = ?3").run(m.fromDue, m.toDue, m.id); |
| 198 | // The sample's sent reminder follows its date, so it stays "yesterday's". | |
| 199 | const days = `+${daysBetween(anchor, on)} days`; | |
| 200 | db.query("UPDATE reminder SET due = date(due, ?1), sent_at = strftime('%Y-%m-%dT%H:%M:%fZ', sent_at, ?1) WHERE deal_id = ?2 AND sample = 1").run(days, deal.id); | |
| 189 | 201 | } |
| 190 | 202 | db.query("UPDATE app_meta SET value = ?1 WHERE key = 'sample_anchor'").run(on); |
| 191 | 203 | }).immediate(); |
| @@ -213,6 +225,13 @@ export function seed(db: Database, on = today()): void { | ||
| 213 | 225 | db.query("UPDATE deadline SET done_at = ?1 WHERE deal_id = ?2 AND kind = ?3").run(`${addDays(on, -1)}T17:00:00.000Z`, deal.id, kind); |
| 214 | 226 | for (const [label, days, owes, what] of s.custom ?? []) |
| 215 | 227 | db.query("INSERT INTO deadline (deal_id, kind, label, due, owes, what) VALUES (?1, 'custom', ?2, ?3, ?4, ?5)").run(deal.id, label, addDays(on, days), owes, what); |
| 228 | if (s.sent !== undefined) { | |
| 229 | const [kind, role, moment, subject, body] = s.sent; | |
| 230 | const d = db.query<{ id: number; label: string; due: string }, [number, string]>("SELECT id, label, due FROM deadline WHERE deal_id = ?1 AND kind = ?2").get(deal.id, kind)!; | |
| 231 | const p = db.query<{ id: number; name: string }, [number, string]>("SELECT id, name FROM party WHERE deal_id = ?1 AND role = ?2").get(deal.id, role)!; | |
| 232 | const at = `${moment === "before" ? addDays(d.due, -3) : d.due}T08:30:00.000Z`; | |
| 233 | db.query("INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, party_role, moment, due, by_name, subject, body, sent_at, sample) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, 1)").run(deal.id, d.id, p.id, d.label, p.name, role, moment, d.due, SAMPLE.name, subject, body, at); | |
| 234 | } | |
| 216 | 235 | if (s.moved !== undefined) { |
| 217 | 236 | const [kind, days, reason] = s.moved; |
| 218 | 237 | const d = db.query<{ id: number; due: string }, [number, string]>("SELECT id, due FROM deadline WHERE deal_id = ?1 AND kind = ?2").get(deal.id, kind)!; |
src/server.test.ts +26
| @@ -487,6 +487,32 @@ test("the clock's forms: mark done, move with a reason, send a reminder, and com | ||
| 487 | 487 | expect(movedRow.due).toBe("2027-01-04"); |
| 488 | 488 | expect(movedRow.moves.map((m) => m.reason)).toEqual(["Lender extension"]); |
| 489 | 489 | expect(after.sent.some((s) => s.deadline_id === second!.id)).toBe(true); |
| 490 | // The Messages record on the page: the edited words, escaped, with who and which moment. | |
| 491 | const page = await (await api(`/deals/${deal.id}`, {}, alice)).text(); | |
| 492 | const messages = page.slice(page.indexOf('id="messages"')); | |
| 493 | expect(messages).toContain("Edited <i>subject</i>"); | |
| 494 | expect(messages).toContain("My own words"); | |
| 495 | expect(messages).not.toContain("<i>subject</i>"); | |
| 496 | expect(messages).toContain("Morning of"); | |
| 497 | expect(messages).toContain("3 days before"); | |
| 498 | expect(messages).not.toContain("Nothing sent yet"); | |
| 499 | }); | |
| 500 | ||
| 501 | test("a deal with nothing sent says so, and the sample's sent reminder reads back on its page", async () => { | |
| 502 | const created = await api("/api/deals", { method: "POST", ...jsonBody(NEW_DEAL) }, alice); | |
| 503 | const fresh = (await created.json()) as { id: number; sent: unknown[] }; | |
| 504 | expect(fresh.sent).toEqual([]); | |
| 505 | const page = await (await api(`/deals/${fresh.id}`, {}, alice)).text(); | |
| 506 | expect(page).toContain("<h2>Messages</h2>"); | |
| 507 | expect(page).toContain('<p class="muted">Nothing sent yet</p>'); | |
| 508 | const deals = (await (await api("/api/deals", {}, alice)).json()) as { id: number; address: string; sent: { party_role: string; subject: string }[] }[]; | |
| 509 | const alder = deals.find((d) => d.address === "1418 Alder Lane")!; | |
| 510 | expect(alder.sent[0]!.party_role).toBe("buyer"); | |
| 511 | const alderPage = await (await api(`/deals/${alder.id}`, {}, alice)).text(); | |
| 512 | expect(alderPage).toContain(alder.sent[0]!.subject); | |
| 513 | expect(alderPage).toContain("Maya & Tom Okafor"); | |
| 514 | // Reading follows the deal read permission: a guest sees none of it. | |
| 515 | expect((await fetch(`${base}/deals/${alder.id}`)).status).toBe(401); | |
| 490 | 516 | }); |
| 491 | 517 | |
| 492 | 518 | test("POST /tick answers today's unsent reminders, and changes nothing", async () => { |
src/terminal.test.ts +26 −2
| @@ -7,7 +7,7 @@ import { mkdtempSync } from "node:fs"; | ||
| 7 | 7 | import { tmpdir } from "node:os"; |
| 8 | 8 | import { join } from "node:path"; |
| 9 | 9 | import { addDays, today } from "./dates.ts"; |
| 10 | import { addDeadline, addParty, createDeal, getDeal, listDeals, type DealView } from "./deals.ts"; | |
| 10 | import { addDeadline, addParty, createDeal, getDeal, listDeals, sendReminder, type DealView } from "./deals.ts"; | |
| 11 | 11 | import { openDb } from "./db.ts"; |
| 12 | 12 | import { terminal, type Terminal } from "./terminal.ts"; |
| 13 | 13 | import type { Who } from "./who.ts"; |
| @@ -113,7 +113,7 @@ test("r shows today's reminders; enter reads a draft; s sends it", () => { | ||
| 113 | 113 | expect(lines(t).join("\n")).toContain("(simulated)"); |
| 114 | 114 | // A late reminder, once sent, leaves today's list; one due today stays, marked sent. |
| 115 | 115 | expect(under(t)).not.toBe(row); |
| 116 | const sent = db.query<{ n: number }, []>("SELECT count(*) AS n FROM reminder").get()!.n; | |
| 116 | const sent = db.query<{ n: number }, []>("SELECT count(*) AS n FROM reminder WHERE sample = 0").get()!.n; | |
| 117 | 117 | expect(sent).toBe(1); |
| 118 | 118 | // `s` sent the draft unchanged: its words are what the record keeps. |
| 119 | 119 | const kept = db.query<{ body: string }, []>("SELECT body FROM reminder").get()!.body; |
| @@ -264,3 +264,27 @@ test("n declines a reminder with a reason, u undoes it; anon is not asked to typ | ||
| 264 | 264 | press(guest, "n", "x", "\r"); |
| 265 | 265 | expect(db.query<{ n: number }, []>("SELECT count(*) AS n FROM reminder_declined").get()!.n).toBe(before); |
| 266 | 266 | }); |
| 267 | ||
| 268 | test("s on a deal lists what was sent, newest first, with the words; b comes back; a guest sees nothing", () => { | |
| 269 | const inspection = date("inspection"); | |
| 270 | const [buyer, seller] = deal().parties; | |
| 271 | expect(sendReminder(db, alice, { deadlineId: inspection.id, partyId: buyer!.id, moment: "before", subject: "First <b>words</b>", body: "Hello\nfrom me" }).ok).toBe(true); | |
| 272 | expect(sendReminder(db, alice, { deadlineId: inspection.id, partyId: seller!.id, moment: "before", subject: "Second words", body: "Later one" }).ok).toBe(true); | |
| 273 | const t = terminal(db, alice, context); | |
| 274 | goTo(t, "12 Elm St"); | |
| 275 | press(t, "o", "s"); | |
| 276 | const screen = lines(t).join("\n"); | |
| 277 | expect(screen).toContain("Messages — 12 Elm St"); | |
| 278 | expect(screen.indexOf(seller!.name)).toBeLessThan(screen.indexOf(buyer!.name)); | |
| 279 | expect(screen).toContain("sent " + today()); | |
| 280 | expect(screen).not.toContain("Later one"); | |
| 281 | press(t, "j", "\r"); | |
| 282 | expect(lines(t).join("\n")).toContain("First <b>words</b>"); | |
| 283 | expect(lines(t).join("\n")).toContain("from me"); | |
| 284 | t.press("b"); | |
| 285 | expect(lines(t).join("\n")).toContain("Parties"); | |
| 286 | const guest = terminal(db, { role: "anon" }, context); | |
| 287 | press(guest, "s"); | |
| 288 | expect(lines(guest).join("\n")).not.toContain("First <b>words</b>"); | |
| 289 | expect(lines(guest).join("\n")).not.toContain("Messages —"); | |
| 290 | }); |
src/terminal.ts +31 −4
| @@ -51,7 +51,7 @@ const bold = (s: string): string => `${esc}1m${s}${esc}0m`; | ||
| 51 | 51 | const COLOUR: Readonly<Record<Urgency, string>> = { overdue: "31", soon: "33", later: "36", done: "2" }; |
| 52 | 52 | const tint = (u: Urgency, s: string): string => `${esc}${COLOUR[u]}m${s}${esc}0m`; |
| 53 | 53 | |
| 54 | type View = "dates" | "reminders" | "deal"; | |
| 54 | type View = "dates" | "reminders" | "deal" | "messages"; | |
| 55 | 55 | interface Row { |
| 56 | 56 | readonly deal: DealView; |
| 57 | 57 | readonly date: DeadlineView; |
| @@ -122,10 +122,12 @@ export function terminal( | ||
| 122 | 122 | const editKey = (d: Draft): string => `${d.deadline_id}:${d.party_id}:${d.moment}`; |
| 123 | 123 | /** The draft with any words typed for it; a sent one is as it went. */ |
| 124 | 124 | const edited = (d: Draft): Draft => (unsent(d) ? { ...d, ...edits.get(editKey(d)) } : d); |
| 125 | // On a deal the cursor walks its dates, then its parties. | |
| 125 | // On a deal the cursor walks its dates, then its parties; in its Messages, the sent reminders. | |
| 126 | 126 | const rowCount = (): number => |
| 127 | 127 | view === "reminders" |
| 128 | 128 | ? reminderRows().length |
| 129 | : view === "messages" | |
| 130 | ? (openDeal()?.sent.length ?? 0) | |
| 129 | 131 | : view === "deal" |
| 130 | 132 | ? dateRows().length + (openDeal()?.parties.length ?? 0) |
| 131 | 133 | : dateRows().length; |
| @@ -331,7 +333,16 @@ export function terminal( | ||
| 331 | 333 | } |
| 332 | 334 | if (key === "j") cursor = Math.min(cursor + 1, Math.max(0, rowCount() - 1)); |
| 333 | 335 | else if (key === "k") cursor = Math.max(cursor - 1, 0); |
| 334 | else if (key === "d" || key === "b" || key === "\x1b") { | |
| 336 | else if (view === "messages" && (key === "b" || key === "\x1b")) { | |
| 337 | view = "deal"; | |
| 338 | cursor = 0; | |
| 339 | reading = undefined; | |
| 340 | } else if (view === "messages" && key === "\r") reading = reading === cursor ? undefined : cursor; | |
| 341 | else if (view === "deal" && key === "s") { | |
| 342 | view = "messages"; | |
| 343 | cursor = 0; | |
| 344 | reading = undefined; | |
| 345 | } else if (key === "d" || key === "b" || key === "\x1b") { | |
| 335 | 346 | view = "dates"; |
| 336 | 347 | cursor = 0; |
| 337 | 348 | } else if (key === "r") { |
| @@ -361,6 +372,7 @@ export function terminal( | ||
| 361 | 372 | showDone = !showDone; |
| 362 | 373 | cursor = 0; |
| 363 | 374 | } else if (view === "reminders") onReminder(key); |
| 375 | else if (view === "messages") return; | |
| 364 | 376 | else if (!(view === "deal" && onDeal(key))) onDate(key); |
| 365 | 377 | cursor = Math.min(cursor, Math.max(0, rowCount() - 1)); |
| 366 | 378 | } |
| @@ -389,6 +401,19 @@ export function terminal( | ||
| 389 | 401 | if (!listed.ok) lines.push(listed.message); |
| 390 | 402 | else if (rows.length === 0) lines.push(dim("Nothing open. Every date is done.")); |
| 391 | 403 | rows.forEach((row, i) => lines.push(dateLine(row, i, true))); |
| 404 | } else if (view === "messages") { | |
| 405 | const deal = openDeal(); | |
| 406 | if (deal === undefined) lines.push(dim("That deal is gone.")); | |
| 407 | else { | |
| 408 | lines.push(bold(`Messages — ${deal.address}`), ""); | |
| 409 | if (deal.sent.length === 0) lines.push(dim("Nothing sent yet")); | |
| 410 | deal.sent.forEach((m, i) => { | |
| 411 | const role = m.party_role === "" ? "" : ` (${PARTY_LABEL[m.party_role].toLowerCase()})`; | |
| 412 | const text = `${m.party_name}${role} · ${m.label}, ${spoken(m.due)} · ${MOMENT_LABEL[m.moment].toLowerCase()}`; | |
| 413 | lines.push(`${i === cursor ? "▸" : " "} ${dim(`sent ${m.sent_at.slice(0, 10)}`.padEnd(16))} ${i === cursor ? bold(text) : text}`); | |
| 414 | if (reading === i) lines.push("", ` ${bold(m.subject === "" ? "(words not kept)" : m.subject)}`, ...m.body.split("\n").map((l) => ` ${l}`), ""); | |
| 415 | }); | |
| 416 | } | |
| 392 | 417 | } else if (view === "deal") { |
| 393 | 418 | const deal = openDeal(); |
| 394 | 419 | if (deal === undefined) lines.push(dim("That deal is gone.")); |
| @@ -422,8 +447,10 @@ export function terminal( | ||
| 422 | 447 | const keys = |
| 423 | 448 | view === "reminders" |
| 424 | 449 | ? "j/k move · enter read · e edit · s send · n don't send · u undo · d dates · q quit" |
| 450 | : view === "messages" | |
| 451 | ? "j/k move · enter read · b back to the deal · d dates · q quit" | |
| 425 | 452 | : view === "deal" |
| 426 | ? "j/k move · x done · u reopen · m move · a add date · p add party · e edit party · X remove date/party · D remove deal · b back · q quit" | |
| 453 | ? "j/k move · s messages · x done · u reopen · m move · a add date · p add party · e edit party · X remove date/party · D remove deal · b back · q quit" | |
| 427 | 454 | : `j/k move · x done · u reopen · m move · o open deal · n new deal · c check dates · h ${showDone ? "hide" : "show"} done · r reminders · q quit`; |
| 428 | 455 | lines.push(dim(me.role === "anon" ? `${keys} · sign in with a key the forge knows to change anything` : keys)); |
| 429 | 456 | } |
new above
offline