Public: anyone can read this; the crew can change it.
A deal that falls through is marked Fell through with a reason, leaves the clock for the Closed list labelled so, and can be reopened #106
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/97-a-deal-that-falls-through-is-marked-fell → main 2 commits fork 2 threads · 2 unresolved
19 files
changed +547
−26
README.md +4 −1
| @@ -115,7 +115,10 @@ Signed out, `/` shows what deal-clock does, three short points, a still picture | ||
| 115 | 115 | When the buyer gives a contingency up on purpose, tap Waive instead (it |
| 116 | 116 | asks once): on 2210 Juniper Court the appraisal is already waived, grey |
| 117 | 117 | and marked Waived, still counted as settled, with no reminders; Un-waive |
| 118 | brings it back as it was. | |
| 118 | brings it back as it was. A deal that falls through is marked with | |
| 119 | *Fell through…* at the foot of its page (it asks once and takes a short | |
| 120 | reason) and moves to the Closed list marked Fell through; *Reopen* on its | |
| 121 | page puts it back on the clock with its dates as they were. | |
| 119 | 122 | 8. **Add a party.** Tap the address 2210 Juniper Court to open the deal's |
| 120 | 123 | page. It lists every date and every party. Under "Add a party" pick a |
| 121 | 124 | role (the title company, say), type a name and an email or phone, and |
contract/openapi.json +158
| @@ -631,6 +631,32 @@ | ||
| 631 | 631 | "type": "boolean", |
| 632 | 632 | "description": "The Closing date is marked done; the deal is off the clock." |
| 633 | 633 | }, |
| 634 | "fell_through": { | |
| 635 | "type": "boolean", | |
| 636 | "description": "The deal was marked Fell through: it is off the clock and drafts nothing, and is listed apart from the deals that closed." | |
| 637 | }, | |
| 638 | "fell_at": { | |
| 639 | "type": [ | |
| 640 | "string", | |
| 641 | "null" | |
| 642 | ], | |
| 643 | "format": "date-time", | |
| 644 | "description": "When it was marked fell through; null while it stands." | |
| 645 | }, | |
| 646 | "fell_reason": { | |
| 647 | "type": [ | |
| 648 | "string", | |
| 649 | "null" | |
| 650 | ], | |
| 651 | "description": "The reason given, 1 to 200 characters." | |
| 652 | }, | |
| 653 | "fell_by": { | |
| 654 | "type": [ | |
| 655 | "string", | |
| 656 | "null" | |
| 657 | ], | |
| 658 | "description": "Who marked it." | |
| 659 | }, | |
| 634 | 660 | "business_days": { |
| 635 | 661 | "enum": [ |
| 636 | 662 | 0, |
| @@ -1985,6 +2011,112 @@ | ||
| 1985 | 2011 | } |
| 1986 | 2012 | } |
| 1987 | 2013 | }, |
| 2014 | "/api/deals/{id}/fell": { | |
| 2015 | "post": { | |
| 2016 | "summary": "Mark a deal Fell through with a reason (1 to 200 characters; 422 otherwise, and for a deal already fell through or closed). It leaves the clock, This week, People and reminders, drafts no reminder or status update, is left out of the calendar file, and is listed apart from closed deals. The day and who are kept.", | |
| 2017 | "operationId": "postApiDealsIdFell", | |
| 2018 | "parameters": [ | |
| 2019 | { | |
| 2020 | "name": "id", | |
| 2021 | "in": "path", | |
| 2022 | "required": true, | |
| 2023 | "schema": { | |
| 2024 | "type": "integer" | |
| 2025 | } | |
| 2026 | } | |
| 2027 | ], | |
| 2028 | "requestBody": { | |
| 2029 | "required": true, | |
| 2030 | "content": { | |
| 2031 | "application/json": { | |
| 2032 | "schema": { | |
| 2033 | "type": "object", | |
| 2034 | "properties": { | |
| 2035 | "reason": { | |
| 2036 | "type": "string", | |
| 2037 | "minLength": 1, | |
| 2038 | "maxLength": 200 | |
| 2039 | } | |
| 2040 | }, | |
| 2041 | "required": [ | |
| 2042 | "reason" | |
| 2043 | ] | |
| 2044 | } | |
| 2045 | } | |
| 2046 | } | |
| 2047 | }, | |
| 2048 | "responses": { | |
| 2049 | "200": { | |
| 2050 | "description": "Mark a deal Fell through with a reason (1 to 200 characters; 422 otherwise, and for a deal already fell through or closed). It leaves the clock, This week, People and reminders, drafts no reminder or status update, is left out of the calendar file, and is listed apart from closed deals. The day and who are kept.", | |
| 2051 | "content": { | |
| 2052 | "application/json": { | |
| 2053 | "schema": { | |
| 2054 | "$ref": "#/components/schemas/Deal" | |
| 2055 | } | |
| 2056 | } | |
| 2057 | } | |
| 2058 | }, | |
| 2059 | "default": { | |
| 2060 | "description": "{ error, message }: why not", | |
| 2061 | "content": { | |
| 2062 | "application/json": { | |
| 2063 | "schema": { | |
| 2064 | "$ref": "#/components/schemas/Error" | |
| 2065 | } | |
| 2066 | } | |
| 2067 | } | |
| 2068 | } | |
| 2069 | }, | |
| 2070 | "x-contract": { | |
| 2071 | "resource": "deal", | |
| 2072 | "verb": "update", | |
| 2073 | "least": "user" | |
| 2074 | } | |
| 2075 | } | |
| 2076 | }, | |
| 2077 | "/api/deals/{id}/reopen": { | |
| 2078 | "post": { | |
| 2079 | "summary": "Bring a deal that fell through back onto the clock with its dates as they were. Refused (422) when it has not fallen through.", | |
| 2080 | "operationId": "postApiDealsIdReopen", | |
| 2081 | "parameters": [ | |
| 2082 | { | |
| 2083 | "name": "id", | |
| 2084 | "in": "path", | |
| 2085 | "required": true, | |
| 2086 | "schema": { | |
| 2087 | "type": "integer" | |
| 2088 | } | |
| 2089 | } | |
| 2090 | ], | |
| 2091 | "responses": { | |
| 2092 | "200": { | |
| 2093 | "description": "Bring a deal that fell through back onto the clock with its dates as they were. Refused (422) when it has not fallen through.", | |
| 2094 | "content": { | |
| 2095 | "application/json": { | |
| 2096 | "schema": { | |
| 2097 | "$ref": "#/components/schemas/Deal" | |
| 2098 | } | |
| 2099 | } | |
| 2100 | } | |
| 2101 | }, | |
| 2102 | "default": { | |
| 2103 | "description": "{ error, message }: why not", | |
| 2104 | "content": { | |
| 2105 | "application/json": { | |
| 2106 | "schema": { | |
| 2107 | "$ref": "#/components/schemas/Error" | |
| 2108 | } | |
| 2109 | } | |
| 2110 | } | |
| 2111 | } | |
| 2112 | }, | |
| 2113 | "x-contract": { | |
| 2114 | "resource": "deal", | |
| 2115 | "verb": "update", | |
| 2116 | "least": "user" | |
| 2117 | } | |
| 2118 | } | |
| 2119 | }, | |
| 1988 | 2120 | "/api/audit": { |
| 1989 | 2121 | "get": { |
| 1990 | 2122 | "summary": "The audit trail, newest first; `before` pages back by id.", |
| @@ -2211,6 +2343,32 @@ | ||
| 2211 | 2343 | "type": "boolean", |
| 2212 | 2344 | "description": "The Closing date is marked done; the deal is off the clock." |
| 2213 | 2345 | }, |
| 2346 | "fell_through": { | |
| 2347 | "type": "boolean", | |
| 2348 | "description": "The deal was marked Fell through: it is off the clock and drafts nothing, and is listed apart from the deals that closed." | |
| 2349 | }, | |
| 2350 | "fell_at": { | |
| 2351 | "type": [ | |
| 2352 | "string", | |
| 2353 | "null" | |
| 2354 | ], | |
| 2355 | "format": "date-time", | |
| 2356 | "description": "When it was marked fell through; null while it stands." | |
| 2357 | }, | |
| 2358 | "fell_reason": { | |
| 2359 | "type": [ | |
| 2360 | "string", | |
| 2361 | "null" | |
| 2362 | ], | |
| 2363 | "description": "The reason given, 1 to 200 characters." | |
| 2364 | }, | |
| 2365 | "fell_by": { | |
| 2366 | "type": [ | |
| 2367 | "string", | |
| 2368 | "null" | |
| 2369 | ], | |
| 2370 | "description": "Who marked it." | |
| 2371 | }, | |
| 2214 | 2372 | "business_days": { |
| 2215 | 2373 | "enum": [ |
| 2216 | 2374 | 0, |
docs/TECHNICAL.md +11
| @@ -31,6 +31,17 @@ drafts no reminders, follow-up, business-day flag or after-closing conflict. | ||
| 31 | 31 | the MCP tool `waive_date` and `W` in the terminal all call `waiveDate`, an |
| 32 | 32 | update of the deal, audited as `date.waive`. |
| 33 | 33 | |
| 34 | A deal that falls through has `fell_at`, `fell_reason` and `fell_by` set | |
| 35 | (`deal.fell_through` in the JSON of `GET /api/deals`); `closed` keeps meaning | |
| 36 | "Closing marked done". It is off the clock like a closed deal: no reminders, | |
| 37 | follow-ups or status updates, left out of This week, People and the calendar | |
| 38 | file, and listed in Closed marked Fell through. `POST /deals/:id/fell` and | |
| 39 | `/reopen` (the page; marking needs `confirm=yes`), `POST /api/deals/:id/fell` | |
| 40 | (`{ reason }`, 1 to 200 characters) and `/reopen`, the MCP tools `fell_through` | |
| 41 | and `reopen_deal`, and `F` on a deal in the terminal all call | |
| 42 | `markFellThrough`, an update of the deal, audited as `deal.fell` and | |
| 43 | `deal.reopen`. | |
| 44 | ||
| 34 | 45 | Lead time: every date has `lead_days` (1 to 30, column `deadline.lead_days`, |
| 35 | 46 | default 3; new Financing dates and the sample deals start at 7, new Earnest money |
| 36 | 47 | deposit dates at 1, see `leadOf` in `src/deadlines.ts`). The deposit is the |
src/activity.ts +2
| @@ -26,6 +26,8 @@ const WORDS: Readonly<Record<string, string>> = { | ||
| 26 | 26 | "date.add": "added a date", |
| 27 | 27 | "date.remove": "removed a date", |
| 28 | 28 | "deal.amend": "amended the contract dates", |
| 29 | "deal.fell": "marked a deal fell through", | |
| 30 | "deal.reopen": "brought a deal that fell through back onto the clock", | |
| 29 | 31 | "party.add": "added a party", |
| 30 | 32 | "party.edit": "changed a party", |
| 31 | 33 | "party.remove": "removed a party", |
src/contract.test.ts +16
| @@ -16,6 +16,7 @@ import { | ||
| 16 | 16 | getDeal, |
| 17 | 17 | holdReminder, |
| 18 | 18 | listDeals, |
| 19 | markFellThrough, | |
| 19 | 20 | sendStatusUpdate, |
| 20 | 21 | statusUpdates, |
| 21 | 22 | updateDeal, |
| @@ -197,6 +198,21 @@ test("holding a reminder answers as deal update says, as every identity", () => | ||
| 197 | 198 | expect(holdReminder(db, { role: "anon" }, { dealId: dealBy("u1"), deadlineId: 1, partyId: 1, moment: "day_of" }).ok).toBe(false); |
| 198 | 199 | }); |
| 199 | 200 | |
| 201 | test("marking a deal fell through, and reopening it, answers as deal update says, as every identity", () => { | |
| 202 | for (const identity of identities(contract)) { | |
| 203 | const who = whoIs(identity); | |
| 204 | const { mine, theirs } = actingAs(contract, identity); | |
| 205 | for (const row of [mine, theirs]) { | |
| 206 | const id = dealBy(String(row["author_id"])); | |
| 207 | const said = `fell through as ${identity} on ${String(row["author_id"])}`; | |
| 208 | const allowed = permit(who, "deal", "update", row) === "yes"; | |
| 209 | expect([said, markFellThrough(db, who, { id, fell: true, reason: "Loan denied" }).ok]).toEqual([said, allowed]); | |
| 210 | expect([said, markFellThrough(db, who, { id, fell: false }).ok]).toEqual([said, allowed]); | |
| 211 | } | |
| 212 | } | |
| 213 | expect(markFellThrough(db, { role: "anon" }, { id: dealBy("u1"), fell: true, reason: "x" }).ok).toBe(false); | |
| 214 | }); | |
| 215 | ||
| 200 | 216 | test("the spreadsheet, at GET /deals/csv and GET /api/deals.csv, answers as deal list says, as every identity", async () => { |
| 201 | 217 | for (const path of ["/deals/csv", "/api/deals.csv"]) { |
| 202 | 218 | const route = routes.find((r) => r.method === "GET" && r.path === path)!; |
src/db.ts +4
| @@ -391,6 +391,10 @@ export const migrations: string[] = [ | ||
| 391 | 391 | UNIQUE (deadline_id, party_id, moment, due, move_id) |
| 392 | 392 | ); |
| 393 | 393 | CREATE INDEX IF NOT EXISTS reminder_hold_deal ON reminder_hold (deal_id)`, |
| 394 | // A deal that fell through: the day, the reason and who marked it. Null while it stands; reopening clears all three. | |
| 395 | `ALTER TABLE deal ADD COLUMN fell_at TEXT`, | |
| 396 | `ALTER TABLE deal ADD COLUMN fell_reason TEXT`, | |
| 397 | `ALTER TABLE deal ADD COLUMN fell_by TEXT`, | |
| 394 | 398 | ]; |
| 395 | 399 | |
| 396 | 400 | export function openDb(dir = dataDir): Database { |
src/deals.ts +50
| @@ -42,6 +42,10 @@ export interface Deal { | ||
| 42 | 42 | readonly author_id: string; |
| 43 | 43 | readonly author_name: string; |
| 44 | 44 | readonly created_at: string; |
| 45 | /** When the deal fell through, the reason and who marked it; null while it stands. */ | |
| 46 | readonly fell_at: string | null; | |
| 47 | readonly fell_reason: string | null; | |
| 48 | readonly fell_by: string | null; | |
| 45 | 49 | } |
| 46 | 50 | export interface Party { |
| 47 | 51 | readonly id: number; |
| @@ -149,6 +153,8 @@ export interface DealView extends Deal { | ||
| 149 | 153 | readonly closing: string; |
| 150 | 154 | /** Its Closing date is marked done: the deal has stepped off the clock. */ |
| 151 | 155 | readonly closed: boolean; |
| 156 | /** Marked Fell through: off the clock like a closed deal, kept in the Closed list apart from the ones that closed. */ | |
| 157 | readonly fell_through: boolean; | |
| 152 | 158 | readonly parties: readonly Party[]; |
| 153 | 159 | readonly deadlines: readonly DeadlineView[]; |
| 154 | 160 | readonly sent: readonly Sent[]; |
| @@ -231,6 +237,7 @@ function assemble(deal: Deal, rows: Children): DealView { | ||
| 231 | 237 | closing: |
| 232 | 238 | rows.deadlines.find((d) => d.kind === "closing")?.due ?? deal.contract_date, |
| 233 | 239 | closed: isClosed(rows.deadlines), |
| 240 | fell_through: deal.fell_at !== null, | |
| 234 | 241 | parties: rows.parties, |
| 235 | 242 | deadlines: rows.deadlines.map((d) => ({ |
| 236 | 243 | ...d, |
| @@ -496,6 +503,7 @@ export function openDealAt( | ||
| 496 | 503 | return db |
| 497 | 504 | .query<{ id: number; address: string }, [string]>( |
| 498 | 505 | `SELECT id, address FROM deal WHERE author_id = ?1 |
| 506 | AND fell_at IS NULL | |
| 499 | 507 | AND NOT EXISTS (SELECT 1 FROM deadline WHERE deal_id = deal.id AND kind = 'closing' AND done_at IS NOT NULL) |
| 500 | 508 | ORDER BY id`, |
| 501 | 509 | ) |
| @@ -602,6 +610,48 @@ function update( | ||
| 602 | 610 | return ok(view(db, { ...found.value, address })); |
| 603 | 611 | } |
| 604 | 612 | |
| 613 | /** What the audit trail calls a deal falling through, and its reopening. */ | |
| 614 | export const FELL_WHAT = "deal.fell"; | |
| 615 | export const REOPEN_WHAT = "deal.reopen"; | |
| 616 | ||
| 617 | /** | |
| 618 | * Mark a deal as fell through, with a reason (or bring it back with | |
| 619 | * `fell: false`). It leaves the clock and drafts nothing; its dates are not | |
| 620 | * touched, so reopening puts it back as it was. | |
| 621 | */ | |
| 622 | export function markFellThrough( | |
| 623 | db: Database, | |
| 624 | who: Who, | |
| 625 | input: { id: number; fell: boolean; reason?: unknown }, | |
| 626 | ): Result<DealView> { | |
| 627 | const what = input.fell ? FELL_WHAT : REOPEN_WHAT; | |
| 628 | const found = dealFor(db, who, "update", input.id); | |
| 629 | const dealId = found.ok ? found.value.id : undefined; | |
| 630 | const failed = (r: Result<DealView>): Result<DealView> => within(db, who, what, dealId, r); | |
| 631 | if (!found.ok) return failed(found); | |
| 632 | const deal = found.value; | |
| 633 | let reason: string | null = null; | |
| 634 | if (input.fell) { | |
| 635 | if (deal.fell_at !== null) return failed(fail("invalid", "This deal already fell through.")); | |
| 636 | if (view(db, deal).closed) return failed(fail("invalid", "This deal closed; it did not fall through.")); | |
| 637 | const text = line(input.reason, 1, REASON_MAX); | |
| 638 | if (text === undefined) return failed(fail("invalid", `Say why in 1 to ${REASON_MAX} characters.`, "reason")); | |
| 639 | reason = text; | |
| 640 | } else if (deal.fell_at === null) { | |
| 641 | return failed(fail("invalid", "This deal has not fallen through.")); | |
| 642 | } | |
| 643 | db.query("UPDATE deal SET fell_at = ?1, fell_reason = ?2, fell_by = ?3 WHERE id = ?4").run( | |
| 644 | input.fell ? new Date().toISOString() : null, | |
| 645 | reason, | |
| 646 | input.fell ? authorOf(who).name : null, | |
| 647 | deal.id, | |
| 648 | ); | |
| 649 | return failed(ok(view(db, db.query<Deal, [number]>("SELECT * FROM deal WHERE id = ?1").get(deal.id)!))); | |
| 650 | } | |
| 651 | ||
| 652 | /** The day a deal fell through, by the app's own calendar. */ | |
| 653 | export const fellOn = (deal: { fell_at: string | null }): string => today(new Date(deal.fell_at ?? Date.now())); | |
| 654 | ||
| 605 | 655 | export const deleteDeal = ( |
| 606 | 656 | db: Database, |
| 607 | 657 | who: Who, |
src/fell.test.ts added +167
| @@ -0,0 +1,167 @@ | ||
| 1 | /** A deal that fell through leaves the clock and is listed apart; reopening puts it back as it was. */ | |
| 2 | import { afterAll, beforeEach, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { addDays, today } from "./dates.ts"; | |
| 7 | import { openDb } from "./db.ts"; | |
| 8 | import { createDeal, getDeal, listDeals, markFellThrough, REASON_MAX, type DealView } from "./deals.ts"; | |
| 9 | import { agent } from "./env.ts"; | |
| 10 | import { book } from "./ics.ts"; | |
| 11 | import { people } from "./people.ts"; | |
| 12 | import { drafts, statusDrafts, todaysDrafts, waitingCount } from "./reminders.ts"; | |
| 13 | import { forgetRationing } from "./security.ts"; | |
| 14 | import { serve } from "./server.ts"; | |
| 15 | import { terminal } from "./terminal.ts"; | |
| 16 | import { sign } from "./who.ts"; | |
| 17 | import type { Who } from "./who.ts"; | |
| 18 | import { thisWeek } from "./week.ts"; | |
| 19 | ||
| 20 | const db = openDb(mkdtempSync(join(tmpdir(), "app-fell-"))); | |
| 21 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 22 | const anon: Who = { role: "anon" }; | |
| 23 | const made = createDeal(db, alice, { | |
| 24 | openAnyway: true, | |
| 25 | address: "5 Fallen Fir Ln", | |
| 26 | buyer: "Ann Buyer", | |
| 27 | seller: "Sam Seller", | |
| 28 | contractDate: addDays(today(), -10), | |
| 29 | closingDate: addDays(today(), 3), | |
| 30 | }); | |
| 31 | if (!made.ok) throw new Error(made.message); | |
| 32 | db.query("UPDATE party SET contact = '[email protected]' WHERE deal_id = ?1").run(made.value.id); | |
| 33 | const deal = (): DealView => { | |
| 34 | const got = getDeal(db, alice, { id: made.value.id }); | |
| 35 | if (!got.ok) throw new Error(got.message); | |
| 36 | return got.value; | |
| 37 | }; | |
| 38 | const all = (): DealView[] => { | |
| 39 | const listed = listDeals(db, alice); | |
| 40 | if (!listed.ok) throw new Error(listed.message); | |
| 41 | return listed.value; | |
| 42 | }; | |
| 43 | const here = (d: DealView[]) => d.filter((x) => x.id === made.value.id); | |
| 44 | ||
| 45 | test("a refused reason, an empty one and a visitor change nothing", () => { | |
| 46 | for (const reason of [undefined, "", " ", "x".repeat(REASON_MAX + 1), 7]) { | |
| 47 | const r = markFellThrough(db, alice, { id: made.value.id, fell: true, reason }); | |
| 48 | expect(r.ok).toBe(false); | |
| 49 | } | |
| 50 | expect(markFellThrough(db, anon, { id: made.value.id, fell: true, reason: "Financing" })).toMatchObject({ ok: false, error: "signin_required" }); | |
| 51 | expect(markFellThrough(db, alice, { id: made.value.id, fell: false })).toMatchObject({ ok: false, error: "invalid" }); | |
| 52 | expect(deal().fell_through).toBe(false); | |
| 53 | }); | |
| 54 | ||
| 55 | test("it leaves the clock, the week, people, reminders and the calendar, and drafts nothing", () => { | |
| 56 | const day = today(); | |
| 57 | expect(here(thisWeek(all(), day).map((i) => i.deal)).length).toBeGreaterThan(0); | |
| 58 | expect(drafts(deal(), agent).length).toBeGreaterThan(0); | |
| 59 | expect(statusDrafts(deal(), agent).length).toBeGreaterThan(0); | |
| 60 | expect(waitingCount(here(all()), day, agent)).toBeGreaterThan(0); | |
| 61 | expect(book(all(), "h")).toContain("5 Fallen Fir Ln"); | |
| 62 | ||
| 63 | const fell = markFellThrough(db, alice, { id: made.value.id, fell: true, reason: " Buyer's loan\nfell through " }); | |
| 64 | expect(fell.ok).toBe(true); | |
| 65 | expect(deal()).toMatchObject({ fell_through: true, closed: false, fell_reason: "Buyer's loan fell through", fell_by: "alice" }); | |
| 66 | expect(deal().fell_at).toStartWith(day); | |
| 67 | expect(markFellThrough(db, alice, { id: made.value.id, fell: true, reason: "again" }).ok).toBe(false); | |
| 68 | ||
| 69 | expect(here(thisWeek(all(), day).map((i) => i.deal))).toEqual([]); | |
| 70 | expect(drafts(deal(), agent)).toEqual([]); | |
| 71 | expect(statusDrafts(deal(), agent)).toEqual([]); | |
| 72 | expect(todaysDrafts(all(), day, agent).filter((d) => d.deal_id === made.value.id)).toEqual([]); | |
| 73 | expect(waitingCount(here(all()), day, agent)).toBe(0); | |
| 74 | expect(people(all(), day).flatMap((p) => p.owes).filter((o) => o.deal_id === made.value.id)).toEqual([]); | |
| 75 | expect(book(all(), "h")).not.toContain("5 Fallen Fir Ln"); | |
| 76 | }); | |
| 77 | ||
| 78 | test("reopening puts it back with its dates as they were", () => { | |
| 79 | const dates = deal().deadlines.map((d) => [d.id, d.due, d.done_at]); | |
| 80 | expect(markFellThrough(db, alice, { id: made.value.id, fell: false }).ok).toBe(true); | |
| 81 | expect(deal()).toMatchObject({ fell_through: false, fell_at: null, fell_reason: null, fell_by: null }); | |
| 82 | expect(deal().deadlines.map((d) => [d.id, d.due, d.done_at])).toEqual(dates); | |
| 83 | expect(drafts(deal(), agent).length).toBeGreaterThan(0); | |
| 84 | expect(book(all(), "h")).toContain("5 Fallen Fir Ln"); | |
| 85 | }); | |
| 86 | ||
| 87 | test("F in the terminal asks a reason, then F and yes brings it back", () => { | |
| 88 | const t = terminal(db, alice, { title: "deal-clock", where: "test", agent }); | |
| 89 | const plain = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, ""); | |
| 90 | const press = (...keys: string[]) => keys.forEach((k) => t.press(k)); | |
| 91 | press("/", "Fallen Fir", "\r", "o", "F", ..."Loan denied", "\r"); | |
| 92 | expect(deal().fell_reason).toBe("Loan denied"); | |
| 93 | press("F", ..."yes", "\r"); | |
| 94 | expect(deal().fell_through).toBe(false); | |
| 95 | expect(t.screen().map(plain).join("\n")).not.toContain("Fell through ("); | |
| 96 | }); | |
| 97 | ||
| 98 | const KEY = "a-key-only-the-forge-holds"; | |
| 99 | const server = serve({ port: 0, dataDir: mkdtempSync(join(tmpdir(), "app-fell-http-")), identityKey: KEY }); | |
| 100 | const base = `http://${server.hostname}:${server.port}`; | |
| 101 | afterAll(() => server.stop(true)); | |
| 102 | beforeEach(forgetRationing); | |
| 103 | const identity = `u9|nora|user|${Date.now() + 600_000}`; | |
| 104 | const me = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 105 | const json = { ...me, "content-type": "application/json" }; | |
| 106 | ||
| 107 | test("the page, the API, MCP, the Closed list and Activity agree", async () => { | |
| 108 | const created = await fetch(base + "/api/deals", { | |
| 109 | method: "POST", | |
| 110 | headers: json, | |
| 111 | body: JSON.stringify({ address: "8 Door Dr", open_anyway: true, buyer: "A", seller: "B", contract_date: "2026-10-01", closing_date: "2026-12-01" }), | |
| 112 | }); | |
| 113 | const d = (await created.json()) as DealView; | |
| 114 | expect(d.fell_through).toBe(false); | |
| 115 | const post = (path: string, body?: unknown, headers = json) => | |
Drop the |
||
| 116 | fetch(base + path, { method: "POST", headers, ...(body === undefined ? {} : { body: JSON.stringify(body) }) }); | |
| 117 | ||
| 118 | expect((await post(`/api/deals/${d.id}/fell`, { reason: "" })).status).toBe(422); | |
| 119 | // A visitor with no identity is refused at the door, and the deal is untouched. | |
| 120 | const stranger = { "content-type": "application/json" }; | |
| 121 | expect((await post(`/api/deals/${d.id}/fell`, { reason: "Seller took another offer" }, stranger)).status).toBe(401); | |
| 122 | expect((await post(`/api/deals/${d.id}/reopen`, undefined, stranger)).status).toBe(401); | |
| 123 | const mcpStranger = await post("/mcp", { jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "fell_through", arguments: { id: d.id, reason: "Nope" } } }, stranger); | |
| 124 | expect(JSON.stringify(await mcpStranger.json())).not.toContain("fell_through\":true"); | |
| 125 | expect(((await (await fetch(`${base}/api/deals/${d.id}`, { headers: me })).json()) as DealView).fell_through).toBe(false); | |
| 126 | expect((await post(`/api/deals/${d.id}/reopen`)).status).toBe(422); | |
| 127 | const fell = await post(`/api/deals/${d.id}/fell`, { reason: "Seller took another offer" }); | |
| 128 | expect(fell.status).toBe(200); | |
| 129 | expect(await fell.json()).toMatchObject({ fell_through: true, closed: false, fell_reason: "Seller took another offer", fell_by: "nora" }); | |
| 130 | const list = (await (await fetch(base + "/api/deals", { headers: me })).json()) as DealView[]; | |
| 131 | expect(list.find((x) => x.id === d.id)!.fell_through).toBe(true); | |
| 132 | ||
| 133 | const clock = await (await fetch(base + "/", { headers: me })).text(); | |
| 134 | const closed = clock.slice(clock.indexOf('id="closed"')); | |
| 135 | expect(closed).toContain("8 Door Dr"); | |
| 136 | expect(closed).toContain("Fell through"); | |
| 137 | expect(closed).toContain("Seller took another offer"); | |
| 138 | expect(clock.slice(0, clock.indexOf('id="closed"'))).not.toContain("8 Door Dr"); | |
| 139 | const page = await (await fetch(`${base}/deals/${d.id}`, { headers: me })).text(); | |
| 140 | expect(page).toContain("Fell through on"); | |
| 141 | expect(page).toContain("Reopen"); | |
| 142 | expect(await (await fetch(base + "/deals/ics", { headers: me })).text()).not.toContain("8 Door Dr"); | |
| 143 | expect(await (await fetch(base + "/deals/csv", { headers: me })).text()).not.toContain("8 Door Dr"); | |
| 144 | ||
| 145 | // MCP: reopen, then fall through again. | |
| 146 | const call = async (name: string, args: object) => | |
| 147 | JSON.stringify(await (await post("/mcp", { jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } })).json()); | |
| 148 | expect(await call("reopen_deal", { id: d.id })).toContain('\\"fell_through\\":false'); | |
| 149 | expect(await call("fell_through", { id: d.id, reason: "Appraisal gap" })).toContain("Appraisal gap"); | |
| 150 | const asked = (await (await post("/mcp", { jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "list_deals", arguments: {} } })).text()); | |
| 151 | expect(asked).toContain("fell_through"); | |
| 152 | ||
| 153 | // The page forms: marking asks once; reopening is one tap. | |
| 154 | const form = (path: string, fields: Record<string, string>) => | |
| 155 | fetch(base + path, { method: "POST", body: new URLSearchParams(fields), headers: { ...me, "content-type": "application/x-www-form-urlencoded" }, redirect: "manual" }); | |
| 156 | expect((await form(`/deals/${d.id}/reopen`, {})).status).toBe(303); | |
| 157 | expect((await form(`/deals/${d.id}/fell`, { reason: "Cold feet" })).status).toBe(422); | |
| 158 | expect((await form(`/deals/${d.id}/fell`, { reason: "Cold feet", confirm: "yes" })).status).toBe(303); | |
| 159 | const after = (await (await fetch(`${base}/api/deals/${d.id}`, { headers: me })).json()) as DealView; | |
| 160 | expect(after.fell_reason).toBe("Cold feet"); | |
| 161 | ||
| 162 | const root = `u1|root|admin|${Date.now() + 600_000}`; | |
| 163 | const admin = { "x-forge-identity": root, "x-forge-signature": sign(root, KEY) }; | |
| 164 | const activity = await (await fetch(`${base}/activity`, { headers: admin })).text(); | |
| 165 | expect(activity).toContain("marked a deal fell through"); | |
| 166 | expect(activity).toContain("brought a deal that fell through back onto the clock"); | |
| 167 | }); |
src/ics.ts +1 −1
| @@ -99,7 +99,7 @@ export function book(deals: readonly DealView[], host: string, stamp = new Date( | ||
| 99 | 99 | const now = stampOf(stamp); |
| 100 | 100 | return wrap( |
| 101 | 101 | "Deal clock", |
| 102 | deals.filter((deal) => !deal.closed).flatMap((deal) => events(deal, host, now, true)), | |
| 102 | deals.filter((deal) => !deal.closed && !deal.fell_through).flatMap((deal) => events(deal, host, now, true)), | |
| 103 | 103 | agent, |
| 104 | 104 | ); |
| 105 | 105 | } |
src/mcp.ts +13
| @@ -23,6 +23,7 @@ import { | ||
| 23 | 23 | listDeals, |
| 24 | 24 | markDone, |
| 25 | 25 | waiveDate, |
| 26 | markFellThrough, | |
| 26 | 27 | amendDeal, |
| 27 | 28 | moveDeadline, |
| 28 | 29 | removeDeadline, |
| @@ -144,6 +145,18 @@ export const tools: readonly Tool[] = [ | ||
| 144 | 145 | }, |
| 145 | 146 | run: (db, who, args) => setOwes(db, who, { id: Number(args.id), owes: args.owes }), |
| 146 | 147 | }, |
| 148 | { | |
| 149 | name: "fell_through", | |
| 150 | description: "Mark a deal that fell through, with a reason (1 to 200 characters): it leaves the clock, drafts no reminders and is listed apart from closed deals. Bring it back with reopen_deal.", | |
| 151 | inputSchema: { type: "object", properties: { id: { type: "integer" }, reason: { type: "string", minLength: 1, maxLength: 200 } }, required: ["id", "reason"] }, | |
| 152 | run: (db, who, args) => markFellThrough(db, who, { id: Number(args.id), fell: true, reason: args.reason }), | |
| 153 | }, | |
| 154 | { | |
| 155 | name: "reopen_deal", | |
| 156 | description: "Put a deal that fell through back on the clock with its dates as they were.", | |
| 157 | inputSchema: { type: "object", properties: { id: { type: "integer" } }, required: ["id"] }, | |
| 158 | run: (db, who, args) => markFellThrough(db, who, { id: Number(args.id), fell: false }), | |
| 159 | }, | |
| 147 | 160 | { |
| 148 | 161 | name: "waive_date", |
| 149 | 162 | description: "Waive a date the buyer gave up on purpose (not Closing): it stays on the deal, settled, with no more reminders. Un-waive it with waived: false.", |
src/pages/clock.ts +11 −8
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import { addDays, daysBetween, isWeekend, short, spoken, urgency, urgencyOf } from "../dates.ts"; |
| 9 | import type { DealView } from "../deals.ts"; | |
| 9 | import { fellOn, type DealView } from "../deals.ts"; | |
| 10 | 10 | import { isOpen, KIND } from "../deadlines.ts"; |
| 11 | 11 | import { escape } from "../http.ts"; |
| 12 | 12 | import { agent as defaults } from "../env.ts"; |
| @@ -164,10 +164,13 @@ function render(input: { | ||
| 164 | 164 | input.open === undefined ? undefined : input.deals.flatMap((d) => d.sent).find((m) => m.id === input.open); |
| 165 | 165 | const q = input.q ?? ""; |
| 166 | 166 | const found = find(input.deals, q); |
| 167 | const deals = found.filter((deal) => !deal.closed); | |
| 167 | const deals = found.filter((deal) => !deal.closed && !deal.fell_through); | |
| 168 | 168 | const closed = found |
| 169 | .filter((deal) => deal.closed) | |
| 169 | .filter((deal) => deal.closed && !deal.fell_through) | |
| 170 | 170 | .sort((a, b) => b.closing.localeCompare(a.closing) || b.id - a.id); |
| 171 | const fell = found | |
| 172 | .filter((deal) => deal.fell_through) | |
| 173 | .sort((a, b) => (b.fell_at ?? "").localeCompare(a.fell_at ?? "") || b.id - a.id); | |
| 171 | 174 | const open = deals.flatMap((deal) => deal.deadlines.filter(isOpen)); |
| 172 | 175 | const overdue = open.filter((d) => urgency(d.due, on, false) === "overdue").length; |
| 173 | 176 | const soon = open.filter((d) => urgency(d.due, on, false) === "soon").length; |
| @@ -181,10 +184,10 @@ function render(input: { | ||
| 181 | 184 | ? `<p class="empty">No deal matches “${escape(q)}”. <a href="/">Clear</a></p>` |
| 182 | 185 | : `<p class="empty">No deals yet. <a href="/deals/new">Open the first one</a>.</p>`; |
| 183 | 186 | const timeline = |
| 184 | deals.length === 0 && closed.length === 0 | |
| 187 | deals.length === 0 && closed.length === 0 && fell.length === 0 | |
| 185 | 188 | ? nothing |
| 186 | 189 | : deals.length === 0 |
| 187 | ? `<p class="empty">Every deal is closed.</p>` | |
| 190 | ? `<p class="empty">Every deal is closed or fell through.</p>` | |
| 188 | 191 | : `<div class="clock-scroll" tabindex="0" role="region" aria-label="Timeline of every deal's dates"> |
| 189 | 192 | <div class="clock-inner" style="width:${at(days)}"> |
| 190 | 193 | <div class="axis" aria-hidden="true">${axis(start, days, on)}</div> |
| @@ -192,12 +195,12 @@ function render(input: { | ||
| 192 | 195 | </div> |
| 193 | 196 | </div>`; |
| 194 | 197 | const closedCard = |
| 195 | closed.length === 0 | |
| 198 | closed.length === 0 && fell.length === 0 | |
| 196 | 199 | ? "" |
| 197 | 200 | : `<section id="closed" class="card" data-live="deal.changed"> |
| 198 | <h2>Closed <span class="muted">· ${closed.length}</span></h2> | |
| 201 | <h2>Closed <span class="muted">· ${closed.length + fell.length}</span></h2> | |
| 199 | 202 | ${who.role === "anon" ? "" : `<p class="small"><a href="/deals/csv?closed=1" download>Download spreadsheet with closed deals</a></p>`} |
| 200 | <ul class="closed">${closed.map((deal) => `<li><a href="/deals/${deal.id}">${escape(deal.address)}</a> <span class="muted">closed ${escape(spoken(deal.closing))}</span></li>`).join("")}</ul> | |
| 203 | <ul class="closed">${closed.map((deal) => `<li><a href="/deals/${deal.id}">${escape(deal.address)}</a> <span class="muted">closed ${escape(spoken(deal.closing))}</span></li>`).join("")}${fell.map((deal) => `<li><a href="/deals/${deal.id}">${escape(deal.address)}</a> <b>Fell through</b> <span class="muted">${escape(spoken(fellOn(deal)))} · ${escape(deal.fell_reason ?? "")}</span></li>`).join("")}</ul> | |
| 201 | 204 | </section>`; |
| 202 | 205 | const restoreCard = |
| 203 | 206 | !mayRestore(who).ok |
src/pages/deal.ts +23 −3
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { addDays, relative, short, spoken } from "../dates.ts"; |
| 8 | import { canChange, canRemove, closedOn, type DealView, type Party, type Plan, type PlannedDate, type Sent } from "../deals.ts"; | |
| 8 | import { canChange, canRemove, closedOn, fellOn, REASON_MAX, type DealView, type Party, type Plan, type PlannedDate, type Sent } from "../deals.ts"; | |
| 9 | 9 | import { agent as defaults, standard } from "../env.ts"; |
| 10 | 10 | import { DEFAULT_LENGTHS, PARTY_LABEL, PARTY_ROLES } from "../deadlines.ts"; |
| 11 | 11 | import { escape } from "../http.ts"; |
| @@ -50,7 +50,9 @@ const partyControls = (p: Party, to: string): string => `<div class="party-contr | ||
| 50 | 50 | function statusCard(deal: DealView, agent: Agent, on: string, may: boolean, to: string): string { |
| 51 | 51 | if (!may) return ""; |
| 52 | 52 | const these = statusDrafts(deal, agent); |
| 53 | const body = deal.closed | |
| 53 | const body = deal.fell_through | |
| 54 | ? `<p class="muted">This deal fell through, so there is nothing to update.</p>` | |
| 55 | : deal.closed | |
| 54 | 56 | ? `<p class="muted">This deal is closed, so there is nothing to update.</p>` |
| 55 | 57 | : these.length === 0 |
| 56 | 58 | ? `<p class="muted">Add a buyer or seller with an email or phone to draft a status update.</p>` |
| @@ -163,7 +165,7 @@ export function dealPage(input: { | ||
| 163 | 165 | <section class="intro"> |
| 164 | 166 | <div> |
| 165 | 167 | <h1>${escape(deal.address)}</h1> |
| 166 | <p class="summary">${deal.closed ? `<b>Closed on ${escape(spoken(closedOn(deal)))}</b> · ` : ""}Under contract ${escape(spoken(deal.contract_date))} · closes ${escape(spoken(deal.closing))}, ${escape(relative(deal.closing, on))}${deal.business_days ? " · business days" : ""}</p> | |
| 168 | <p class="summary">${deal.fell_through ? `<b>Fell through on ${escape(spoken(fellOn(deal)))}</b> · ${escape(deal.fell_reason ?? "")}${deal.fell_by ? ` (${escape(deal.fell_by)})` : ""} · ` : ""}${deal.closed ? `<b>Closed on ${escape(spoken(closedOn(deal)))}</b> · ` : ""}Under contract ${escape(spoken(deal.contract_date))} · closes ${escape(spoken(deal.closing))}, ${escape(relative(deal.closing, on))}${deal.business_days ? " · business days" : ""}</p> | |
| 167 | 169 | ${editAddress} |
| 168 | 170 | </div> |
| 169 | 171 | <div class="row"><a class="button button-quiet" href="${path}/timeline">Client timeline</a> <a class="button button-quiet" href="${path}/ics" download>Add to calendar</a></div> |
| @@ -187,6 +189,7 @@ export function dealPage(input: { | ||
| 187 | 189 | ${deal.deadlines.map((d) => sheet(deal, d, who, on, path, agent)).join("")} |
| 188 | 190 | </div> |
| 189 | 191 | ${forms} |
| 192 | ${fellForm(deal, path, may)} | |
| 190 | 193 | ${ |
| 191 | 194 | canRemove(who, deal) |
| 192 | 195 | ? confirmRemove({ |
| @@ -298,3 +301,20 @@ export function newDealPage(input: { | ||
| 298 | 301 | ${state?.preview === undefined ? "" : previewList(state.preview)}`; |
| 299 | 302 | return layout({ body, who, agent, waiting: input.waiting, path: "/deals/new", heading: "New deal", notice }); |
| 300 | 303 | } |
| 304 | ||
| 305 | /** Mark the deal fell through (asks once, takes the reason), or bring it back. */ | |
| 306 | function fellForm(deal: DealView, path: string, may: boolean): string { | |
| 307 | if (!may) return ""; | |
| 308 | if (deal.fell_through) | |
| 309 | return `<form method="post" action="${path}/reopen"><button class="button button-quiet">Reopen</button></form>`; | |
| 310 | if (deal.closed) return ""; | |
| 311 | return `<details class="confirm"> | |
| 312 | <summary class="button button-quiet">Fell through…</summary> | |
| 313 | <p>The deal leaves the clock and drafts no more reminders; its dates are kept, and Reopen brings it back.</p> | |
| 314 | <form class="stack" method="post" action="${path}/fell"> | |
| 315 | <input type="hidden" name="confirm" value="yes"> | |
| 316 | <label>Why it fell through<input class="input" name="reason" required maxlength="${REASON_MAX}"></label> | |
| 317 | <button class="button" aria-label="Yes, ${escape(deal.address)} fell through">Yes, it fell through</button> | |
| 318 | </form> | |
| 319 | </details>`; | |
| 320 | } |
src/people.ts +1 −1
| @@ -46,7 +46,7 @@ export function people(deals: readonly DealView[], on: string): Person[] { | ||
| 46 | 46 | } |
| 47 | 47 | one.roles.add(party.role); |
| 48 | 48 | // A closed deal is off the clock: nothing on it is chased. |
| 49 | if (deal.closed) continue; | |
| 49 | if (deal.closed || deal.fell_through) continue; | |
| 50 | 50 | for (const d of deal.deadlines) { |
| 51 | 51 | if (d.owes !== party.role || !isOpen(d)) continue; |
| 52 | 52 | one.owes.set(d.id, { |
src/reminders.ts +2 −2
| @@ -174,7 +174,7 @@ function words( | ||
| 174 | 174 | |
| 175 | 175 | /** Every reminder an open date of this deal has, sent or not; a closed deal reminds no one. */ |
| 176 | 176 | export function drafts(deal: DealView, agent: Agent): readonly Draft[] { |
| 177 | if (deal.closed) return []; | |
| 177 | if (deal.closed || deal.fell_through) return []; | |
| 178 | 178 | return remembered(`${JSON.stringify(agent)}|${print(deal)}`, () => |
| 179 | 179 | deal.deadlines |
| 180 | 180 | .filter(isOpen) |
| @@ -277,7 +277,7 @@ function statusWords(deal: DealView, party: Party, agent: Agent): { subject: str | ||
| 277 | 277 | */ |
| 278 | 278 | export function statusDrafts(deal: DealView, agent: Agent): Draft[] { |
| 279 | 279 | const closing = deal.deadlines.find((d) => d.kind === "closing"); |
| 280 | if (deal.closed || closing === undefined) return []; | |
| 280 | if (deal.closed || deal.fell_through || closing === undefined) return []; | |
| 281 | 281 | const stand = standing(deal); |
| 282 | 282 | return deal.parties |
| 283 | 283 | .filter((p) => (p.role === "buyer" || p.role === "seller") && p.contact !== "") |
src/routes.ts +51 −1
| @@ -46,6 +46,7 @@ import { | ||
| 46 | 46 | editParty, |
| 47 | 47 | createDeal, |
| 48 | 48 | deleteDeal, |
| 49 | markFellThrough, | |
| 49 | 50 | getDeal, |
| 50 | 51 | withConflicts, |
| 51 | 52 | listDeals, |
| @@ -133,7 +134,7 @@ function spreadsheet(c: Context): Result<Response> { | ||
| 133 | 134 | const withClosed = c.url.searchParams.get("closed") === "1"; |
| 134 | 135 | return { |
| 135 | 136 | ok: true, |
| 136 | value: new Response(sheet(deals.value.filter((deal) => withClosed || !deal.closed)), { | |
| 137 | value: new Response(sheet(deals.value.filter((deal) => withClosed || !(deal.closed || deal.fell_through))), { | |
| 137 | 138 | headers: { |
| 138 | 139 | "content-type": "text/csv; charset=utf-8", |
| 139 | 140 | "content-disposition": `attachment; filename="deal-clock-${today()}.csv"`, |
| @@ -215,6 +216,10 @@ export const DEAL: Schema = { | ||
| 215 | 216 | contract_date: DATE, |
| 216 | 217 | closing: DATE, |
| 217 | 218 | closed: { type: "boolean", description: "The Closing date is marked done; the deal is off the clock." }, |
| 219 | fell_through: { type: "boolean", description: "The deal was marked Fell through: it is off the clock and drafts nothing, and is listed apart from the deals that closed." }, | |
| 220 | fell_at: { type: ["string", "null"], format: "date-time", description: "When it was marked fell through; null while it stands." }, | |
| 221 | fell_reason: { type: ["string", "null"], description: "The reason given, 1 to 200 characters." }, | |
| 222 | fell_by: { type: ["string", "null"], description: "Who marked it." }, | |
| 218 | 223 | business_days: { enum: [0, 1] }, |
| 219 | 224 | author_id: { type: "string" }, |
| 220 | 225 | author_name: { type: "string" }, |
| @@ -907,6 +912,27 @@ export const routes: readonly Route[] = [ | ||
| 907 | 912 | : back(removed.message, backTo(`/deals/${id}`)); |
| 908 | 913 | }, |
| 909 | 914 | }, |
| 915 | { | |
| 916 | method: "POST", | |
| 917 | path: "/deals/:id/fell", | |
| 918 | handle: async (c) => { | |
| 919 | const f = await form(c); | |
| 920 | if (f instanceof Response) return f; | |
| 921 | const id = Number(c.params.id); | |
| 922 | if (f.confirm !== "yes") return unconfirmed(c, `/deals/${id}`); | |
| 923 | return done(markFellThrough(c.db, c.who, { id, fell: true, reason: f.reason }), `/deals/${id}`, "Marked fell through; it has left the clock."); | |
| 924 | }, | |
| 925 | }, | |
| 926 | { | |
| 927 | method: "POST", | |
| 928 | path: "/deals/:id/reopen", | |
| 929 | handle: async (c) => { | |
| 930 | const f = await form(c); | |
| 931 | if (f instanceof Response) return f; | |
| 932 | const id = Number(c.params.id); | |
| 933 | return done(markFellThrough(c.db, c.who, { id, fell: false }), `/deals/${id}`, "Back on the clock with its dates as they were."); | |
| 934 | }, | |
| 935 | }, | |
| 910 | 936 | { |
| 911 | 937 | method: "POST", |
| 912 | 938 | path: "/deadlines/:id/done", |
| @@ -1397,6 +1423,30 @@ export const routes: readonly Route[] = [ | ||
| 1397 | 1423 | answer: { type: "object", properties: { id: { type: "integer" } }, required: ["id"] }, |
| 1398 | 1424 | }, |
| 1399 | 1425 | }, |
| 1426 | { | |
| 1427 | method: "POST", | |
| 1428 | path: "/api/deals/:id/fell", | |
| 1429 | handle: async (c) => { | |
| 1430 | const body = await readJson(c.request); | |
| 1431 | if (body === undefined) return json({ error: "too_large" }, 413); | |
| 1432 | return answer(markFellThrough(c.db, c.who, { id: Number(c.params.id), fell: true, reason: fields(body).reason })); | |
| 1433 | }, | |
| 1434 | api: { | |
| 1435 | summary: | |
| 1436 | "Mark a deal Fell through with a reason (1 to 200 characters; 422 otherwise, and for a deal already fell through or closed). It leaves the clock, This week, People and reminders, drafts no reminder or status update, is left out of the calendar file, and is listed apart from closed deals. The day and who are kept.", | |
| 1437 | body: { type: "object", properties: { reason: { type: "string", minLength: 1, maxLength: 200 } }, required: ["reason"] }, | |
| 1438 | answer: DEAL, | |
| 1439 | }, | |
| 1440 | }, | |
| 1441 | { | |
| 1442 | method: "POST", | |
| 1443 | path: "/api/deals/:id/reopen", | |
| 1444 | handle: (c) => answer(markFellThrough(c.db, c.who, { id: Number(c.params.id), fell: false })), | |
| 1445 | api: { | |
| 1446 | summary: "Bring a deal that fell through back onto the clock with its dates as they were. Refused (422) when it has not fallen through.", | |
| 1447 | answer: DEAL, | |
| 1448 | }, | |
| 1449 | }, | |
| 1400 | 1450 | { |
| 1401 | 1451 | method: "DELETE", |
| 1402 | 1452 | path: "/api/deals/:id", |
src/security.ts +1 −1
| @@ -99,7 +99,7 @@ export function doorOf( | ||
| 99 | 99 | return verb === undefined ? undefined : { resource, verb }; |
| 100 | 100 | } |
| 101 | 101 | // `<api>/:id/<children>/:child` (or `…/:child/waive`, or `/:action`, posted): a change inside the row is an update of it. |
| 102 | if (rest !== undefined && (/^[^/]+\/[^/]+\/[^/]+(\/waive)?$/.test(rest) || (method === "POST" && /^[^/]+\/amend$/.test(rest)))) | |
| 102 | if (rest !== undefined && (/^[^/]+\/[^/]+\/[^/]+(\/waive)?$/.test(rest) || (method === "POST" && /^[^/]+\/(?:amend|fell|reopen)$/.test(rest)))) | |
| 103 | 103 | return method === "PATCH" || method === "DELETE" || method === "POST" ? { resource, verb: "update" } : undefined; |
| 104 | 104 | } |
| 105 | 105 | return undefined; |
src/server.test.ts +2
| @@ -179,6 +179,8 @@ test("the MCP server lists the tools and calls them as the caller", async () => | ||
| 179 | 179 | "list_people", |
| 180 | 180 | "mark_done", |
| 181 | 181 | "set_owes", |
| 182 | "fell_through", | |
| 183 | "reopen_deal", | |
| 182 | 184 | "waive_date", |
| 183 | 185 | "move_date", |
| 184 | 186 | "set_reminder_lead", |
src/terminal.ts +27 −7
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | * timeline a deal's dates as a client would get them (t on a deal) |
| 11 | 11 | * activity who approved, declined or changed what, newest first (L, admins); m only messages, o older, y newer |
| 12 | 12 | * profile your name, brokerage, phone and email (I); e edits them, U goes back to the defaults |
| 13 | * deal one deal's dates and parties; a adds a date, p a party, A corrects the address, M amends the contract, S drafts a status update for the party under the cursor, D removes it | |
| 13 | * deal one deal's dates and parties; a adds a date, p a party, A corrects the address, M amends the contract, S drafts a status update for the party under the cursor, D removes it, F marks it fell through (or brings it back) | |
| 14 | 14 | * |
| 15 | 15 | * On a date (dates or deal view): x marks it done, W waives it (the buyer gave it up), u reopens it or un-waives it, m moves it |
| 16 | 16 | * (a new date, then the reason), N moves one flagged "not a business day" to the next business day, l sets how many days before it reminds. o or enter opens its deal; n opens a new |
| @@ -36,6 +36,8 @@ import { | ||
| 36 | 36 | listDeals, |
| 37 | 37 | markDone, |
| 38 | 38 | waiveDate, |
| 39 | markFellThrough, | |
| 40 | fellOn, | |
| 39 | 41 | mayOpen, |
| 40 | 42 | planFor, |
| 41 | 43 | plannedDates, |
| @@ -143,7 +145,7 @@ export function terminal( | ||
| 143 | 145 | if (view === "week") return thisWeek(deals(), on()).map(({ deal, deadline }) => ({ deal, date: deadline })); |
| 144 | 146 | return deals() |
| 145 | 147 | .flatMap((deal) => |
| 146 | (deal.closed && !showDone ? [] : deal.deadlines.filter((d) => showDone || isOpen(d))).map((date) => ({ deal, date })), | |
| 148 | ((deal.closed || deal.fell_through) && !showDone ? [] : deal.deadlines.filter((d) => showDone || isOpen(d))).map((date) => ({ deal, date })), | |
| 147 | 149 | ) |
| 148 | 150 | .sort((a, b) => a.date.due.localeCompare(b.date.due) || a.date.id - b.date.id); |
| 149 | 151 | }; |
| @@ -228,7 +230,7 @@ export function terminal( | ||
| 228 | 230 | |
| 229 | 231 | function onDeal(key: string): boolean { |
| 230 | 232 | const deal = openDeal(); |
| 231 | if (deal === undefined || !["a", "p", "D", "e", "X", "A", "M", "S"].includes(key)) return false; | |
| 233 | if (deal === undefined || !["a", "p", "D", "e", "X", "A", "M", "S", "F"].includes(key)) return false; | |
| 232 | 234 | const party = deal.parties[cursor - deal.deadlines.length]; |
| 233 | 235 | const date = deal.deadlines[cursor]; |
| 234 | 236 | if (key === "S") { |
| @@ -236,7 +238,7 @@ export function terminal( | ||
| 236 | 238 | const mine = update.ok ? update.value.find((d) => d.party_id === party?.id) : undefined; |
| 237 | 239 | if (!update.ok) said = update.message; |
| 238 | 240 | else if (party === undefined) said = "Move to a buyer or seller to draft their status update."; |
| 239 | else if (mine === undefined) said = deal.closed ? "A closed deal has no status update." : `${party.name} gets no status update: only a buyer or seller with an email or phone does.`; | |
| 241 | else if (mine === undefined) said = deal.fell_through ? "A deal that fell through has no status update." : deal.closed ? "A closed deal has no status update." : `${party.name} gets no status update: only a buyer or seller with an email or phone does.`; | |
| 240 | 242 | else if (mine.sent_at !== null) said = `${party.name} has the latest status update; it is offered again when a date changes.`; |
| 241 | 243 | else if (mine.declined !== null) said = `Not sent to ${party.name}: ${mine.declined.reason}. It is offered again when a date changes.`; |
| 242 | 244 | else |
| @@ -248,6 +250,16 @@ export function terminal( | ||
| 248 | 250 | return "Not sent."; |
| 249 | 251 | }, REASON_MAX); |
| 250 | 252 | } |
| 253 | if (key === "F") { | |
| 254 | if (deal.fell_through) | |
| 255 | ask(deal, [`Type yes to put ${deal.address} back on the clock`], ([yes]) => | |
| 256 | yes === "yes" ? report(markFellThrough(db, me, { id: deal.id, fell: false }), `${deal.address} is back on the clock.`) : "Kept.", | |
| 257 | ); | |
| 258 | else | |
| 259 | ask(deal, [`Why ${deal.address} fell through (a short reason)`], ([reason]) => | |
| 260 | report(markFellThrough(db, me, { id: deal.id, fell: true, reason }), `${deal.address} fell through; it has left the clock.`), | |
| 261 | REASON_MAX); | |
| 262 | } | |
| 251 | 263 | if (key === "A") |
| 252 | 264 | ask(deal, [`Address (now ${deal.address})`], ([address]) => |
| 253 | 265 | report(updateDeal(db, me, { id: deal.id, address }), `Address is now ${address!.trim()}.`), |
| @@ -611,7 +623,7 @@ export function terminal( | ||
| 611 | 623 | const when = tint(u, `${spoken(date.due).padEnd(12)} ${(isOpen(date) ? relative(date.due, on()) : date.waived_at !== null ? "waived" : "done").padEnd(12)}`); |
| 612 | 624 | const who = owner(deal, date); |
| 613 | 625 | const label = view === "week" ? `${date.label} — ${deal.address} · ${who.name ?? who.role}` : withDeal ? `${date.label} — ${deal.address}` : date.label; |
| 614 | const text = label + (deal.closed && withDeal ? dim(" (closed)") : ""); | |
| 626 | const text = label + (deal.fell_through && withDeal ? dim(" (fell through)") : deal.closed && withDeal ? dim(" (closed)") : ""); | |
| 615 | 627 | const moved = date.moves.length > 0 ? dim(` moved: ${date.moves.at(-1)!.reason}`) : ""; |
| 616 | 628 | const flag = date.nonBusinessDay === null ? "" : dim(` not a business day · next is ${spoken(date.nonBusinessDay.next)} (N moves it)`); |
| 617 | 629 | const lead = isOpen(date) ? dim(` reminds ${date.lead_days}d before`) : ""; |
| @@ -635,7 +647,7 @@ export function terminal( | ||
| 635 | 647 | if (!listed.ok) lines.push(listed.message); |
| 636 | 648 | else if (rows.length === 0) lines.push(dim("Nothing open. Every date is done.")); |
| 637 | 649 | rows.forEach((row, i) => lines.push(dateLine(row, i, true))); |
| 638 | const closed = all.filter((d) => d.closed); | |
| 650 | const closed = all.filter((d) => d.closed && !d.fell_through); | |
| 639 | 651 | if (closed.length > 0) |
| 640 | 652 | lines.push( |
| 641 | 653 | "", |
| @@ -643,6 +655,14 @@ export function terminal( | ||
| 643 | 655 | ...closed.map((d) => dim(` ${d.address} — closed ${spoken(closedOn(d))}`)), |
| 644 | 656 | dim(showDone ? " Their dates are listed above; u on the Closing date reopens the deal." : " h shows their dates; u on the Closing date reopens the deal."), |
| 645 | 657 | ); |
| 658 | const fell = all.filter((d) => d.fell_through); | |
| 659 | if (fell.length > 0) | |
| 660 | lines.push( | |
| 661 | "", | |
| 662 | bold(`Fell through (${fell.length})`), | |
| 663 | ...fell.map((d) => dim(` ${d.address} — fell through ${spoken(fellOn(d))}: ${d.fell_reason ?? ""}`)), | |
| 664 | dim(" Open the deal and press F to put it back on the clock."), | |
| 665 | ); | |
| 646 | 666 | } else if (view === "week") { |
| 647 | 667 | const rows = dateRows(); |
| 648 | 668 | const listed = listDeals(db, me); |
| @@ -728,7 +748,7 @@ export function terminal( | ||
| 728 | 748 | const deal = openDeal(); |
| 729 | 749 | if (deal === undefined) lines.push(dim("That deal is gone.")); |
| 730 | 750 | else { |
| 731 | lines.push(bold(deal.address), ...(deal.closed ? [bold(`Closed on ${spoken(closedOn(deal))}`)] : []), dim(`under contract ${spoken(deal.contract_date)} · closes ${spoken(deal.closing)}, ${relative(deal.closing, day)}`), ""); | |
| 751 | lines.push(bold(deal.address), ...(deal.fell_through ? [bold(`Fell through on ${spoken(fellOn(deal))}: ${deal.fell_reason ?? ""}`)] : []), ...(deal.closed ? [bold(`Closed on ${spoken(closedOn(deal))}`)] : []), dim(`under contract ${spoken(deal.contract_date)} · closes ${spoken(deal.closing)}, ${relative(deal.closing, day)}`), ""); | |
| 732 | 752 | deal.deadlines.forEach((date, i) => lines.push(dateLine({ deal, date }, i, false))); |
| 733 | 753 | if (deal.conflicts.length > 0) lines.push("", ...deal.conflicts.map((c) => tint("overdue", `! ${conflictLine(c)}`))); |
| 734 | 754 | lines.push("", bold("Parties")); |
src/week.ts +3 −1
| @@ -13,6 +13,8 @@ export interface WeekDeal<D> { | ||
| 13 | 13 | readonly address: string; |
| 14 | 14 | /** A closed deal is off the clock, so none of its dates are chased. */ |
| 15 | 15 | readonly closed?: boolean; |
| 16 | /** A deal that fell through is off the clock too. */ | |
| 17 | readonly fell_through?: boolean; | |
| 16 | 18 | readonly deadlines: readonly D[]; |
| 17 | 19 | } |
| 18 | 20 | |
| @@ -27,7 +29,7 @@ export function thisWeek<D extends { due: string; done_at: string | null; waived | ||
| 27 | 29 | ): WeekItem<Deal, D>[] { |
| 28 | 30 | const last = addDays(on, WEEK); |
| 29 | 31 | return deals |
| 30 | .filter((deal) => deal.closed !== true) | |
| 32 | .filter((deal) => deal.closed !== true && deal.fell_through !== true) | |
| 31 | 33 | .flatMap((deal) => |
| 32 | 34 | deal.deadlines.filter((d) => isOpen(d) && d.due <= last).map((deadline) => ({ deal, deadline })), |
| 33 | 35 | ) |
new above
offline
Only anon is tried. Add a signed-in user who may not update this deal, over the action, API and MCP, and assert nothing changes.