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

The agent brings their deals back from a downloaded deal-clock file on the Profile page, so the download is a real way to move or recover their book #181

Merged opened this

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

Merged

mason/deal-clock:mason/179-the-agent-brings-their-deals-back-from-a → main 2 commits fork 2 threads · 2 unresolved

  • Merged. mason/179-the-agent-brings-their-deals-back-from-a was merged into main as ebad1ba.

Review

Sign in to review this change.
10 files changed +670 −4
README.md +9
@@ -154,6 +154,15 @@ with every deal you can read: addresses, figures, dates, parties, notes, every
154154 move with its reason, closed and fell-through deals, and the exact words of
155155 every message you sent. In the terminal, press `E` to save the same file. If you ever stop using the app, you keep them.
156156
157 **Bring deals back.** Switching laptops or accounts? On the Profile, under
158 **Bring deals back from a file**, choose the file you downloaded and press the
159 button. Each deal returns as yours, with its dates (done, waived, moved), parties,
160 notes, side, cash flag, closing time and place; closed and fell-through deals stay
161 so. A deal already in your book with the same address and contract date is
162 skipped, so doing it twice adds nothing. The record of messages sent or declined
163 is not brought back. A file with a mistake in any deal brings nothing, and says
164 which deal and field. In the terminal, press `E` to write your file, and `O` to bring deals back from a file in the exports folder (enter alone takes the newest).
165
157166 **Put the samples back.** After the tour, an admin taps **Sample deals** below
158167 the clock, then **Restore the sample deals** and confirms: the seven samples
159168 return as they were, dated from today, and the deals you opened are left alone.
contract/openapi.json +69
@@ -783,6 +783,75 @@
783783 }
784784 }
785785 },
786 "/api/deals/import": {
787 "post": {
788 "summary": "Bring deals back from a file Download all my deals gave you (the same JSON as the body, up to 5 MB): version must be 1, at most 2000 deals. Each is created as yours with its side, cash flag, figures, closing time and place, parties, dates (done, waived, notes, moves) and state (open, closed, fell through). A deal already in your book with the same address and contract date is skipped. Any bad deal refuses the whole file with a 422 naming its place and field, and nothing is written. Sent and declined messages are not re-created.",
789 "operationId": "postApiDealsImport",
790 "requestBody": {
791 "required": true,
792 "content": {
793 "application/json": {
794 "schema": {
795 "type": "object",
796 "required": [
797 "version",
798 "deals"
799 ],
800 "properties": {
801 "version": {
802 "type": "integer"
803 },
804 "deals": {
805 "type": "array",
806 "items": {
807 "type": "object"
808 }
809 }
810 }
811 }
812 }
813 }
814 },
815 "responses": {
816 "201": {
817 "description": "Bring deals back from a file Download all my deals gave you (the same JSON as the body, up to 5 MB): version must be 1, at most 2000 deals. Each is created as yours with its side, cash flag, figures, closing time and place, parties, dates (done, waived, notes, moves) and state (open, closed, fell through). A deal already in your book with the same address and contract date is skipped. Any bad deal refuses the whole file with a 422 naming its place and field, and nothing is written. Sent and declined messages are not re-created.",
818 "content": {
819 "application/json": {
820 "schema": {
821 "type": "object",
822 "properties": {
823 "brought": {
824 "type": "integer"
825 },
826 "skipped": {
827 "type": "integer"
828 },
829 "messages": {
830 "type": "string"
831 }
832 }
833 }
834 }
835 }
836 },
837 "default": {
838 "description": "{ error, message }: why not",
839 "content": {
840 "application/json": {
841 "schema": {
842 "$ref": "#/components/schemas/Error"
843 }
844 }
845 }
846 }
847 },
848 "x-contract": {
849 "resource": "deal",
850 "verb": "create",
851 "least": "user"
852 }
853 }
854 },
786855 "/api/deals/export": {
787856 "get": {
788857 "summary": "All the deals you may read as one JSON file: version, exported_at, and per deal its address, side, figures, closing time and place, state (open, closed or fell_through with reason), parties with phone and email, each date with due, done, waived, owed_by, note and moves, and the sent messages with the exact approved words. Never the audit log.",
docs/TECHNICAL.md +2
@@ -177,6 +177,8 @@ A deal is **closed** when its Closing date is marked done (derived, no column; `
177177
178178 **The export.** `GET /deals/export.json` (Profile's "Download all my deals") and `GET /api/deals/export`, the MCP tool `export_deals` and `E` in the terminal (which writes the same file under `FORGE_APP_DATA/exports/`), answer every deal the person may read (the `deal` list cell; a stranger gets 401), built from `listDeals` so contacts are blanked exactly as the deal page blanks them. The audit log is never in it. The file is `application/json` with `Content-Disposition: attachment; filename="deal-clock-YYYY-MM-DD.json"`, `Cache-Control: no-store` and the security headers. Shape (`src/export.ts`): `{ version: 1, exported_at, truncated, deals: [...] }`, at most 2000 deals (`truncated` is true past that). Each deal: `id, address, side, contract_date, closing, closing_time, closing_place, business_days, sale_price, earnest_amount, commission_rate, opened_by, created_at, state` (`open`, `closed` or `fell_through`), `reason` (null unless fell through, which also has `fell_at` and `by`), `parties[]` (`role, name, phone, email`), `dates[]` (`label, kind, due, done, waived, waived_by, owed_by, note, moves[]` of `from, to, reason, by, at`) and `messages[]` (`date, to, to_role, moment, due, by, sent_at, subject, body`: the words as approved).
179179
180 **The import.** `POST /deals/import` (Profile's "Bring deals back from a file": a multipart form with a `file` field) and `POST /api/deals/import` (the file as the JSON body) read a file `src/export.ts` wrote, through `src/import.ts`: `version` must be 1, at most 2000 deals, at most 5 MB (413 past it, counted by the bytes read). Every deal passes `plan` (the checks opening a deal uses) and every party `partyText`; any refusal is a 422 naming the deal's place (`Deal 3 of the file: …`) and nothing is written (one transaction). Each deal is created as the signed-in agent's own whatever author the file names, asking `permit` for `deal` create; a deal of theirs (or a sample) with the same address and contract date is skipped. Sent and declined messages are never re-created. Audited as `deal.import`, one row per deal, written inside the same transaction as the deals. The terminal's `O` reads a bare `*.json` file name from `FORGE_APP_DATA/exports/` (the newest when none is typed, never a path) through `importText` and the same `importDeals`, gated by `mayOpen`; it refuses a file over 5 MB. There is no MCP tool: `/mcp` reads at most 64 KB, and widening that door's cap for one tool was not worth it.
181
180182 **The spreadsheet.** `GET /deals/csv` and `GET /api/deals.csv` answer `text/csv` with `Content-Disposition: attachment; filename="deal-clock-YYYY-MM-DD.csv"`: one row per date of every open deal the person may read (the `deal` list cell, like the clock); `?closed=1` adds closed deals. A signed-out visitor gets the sign-in page (401), the API a JSON 401. Columns, in order: `address`, `buyer`, `seller` (party names, empty if none), `date` (label), `due` (YYYY-MM-DD), `done` (the day it was marked done, empty while open), `owed_by` (role label), `party` (that role's name, empty if none), `moved` (`yes` if the date was ever moved, else `no`). Cells with a comma, quote or line break are quoted; a cell starting with `=`, `+`, `-`, `@`, tab or carriage return is prefixed with `'` so it is never a formula (`src/csv.ts`). Lines end CRLF.
181183
182184 `POST /tick` answers the reminders that go out today and are not yet
src/contract.test.ts +21
@@ -260,3 +260,24 @@ test("adding a date, at POST /api/deals/:id/deadlines, the page and add_date, an
260260 }
261261 expect(addDeadline(db, { role: "anon" }, { dealId: dealBy("u1"), label: "X", due: "2026-11-10", owes: "buyer" }).ok).toBe(false);
262262 });
263
264 test("bringing deals back, at POST /api/deals/import and POST /deals/import, answers as deal create says, as every identity", async () => {
265 const empty = JSON.stringify({ version: 1, deals: [] });
266 for (const path of ["/api/deals/import", "/deals/import"]) {
267 const route = routes.find((r) => r.method === "POST" && r.path === path)!;
268 expect(route).toBeDefined();
269 for (const identity of identities(contract)) {
270 const who = whoIs(identity);
271 const said = `${path} as ${identity}`;
272 const url = new URL(`http://app.test${path}`);
273 let request: Request;
274 if (path === "/deals/import") {
275 const form = new FormData();
276 form.set("file", new File([empty], "deals.json"));
277 request = new Request(url, { method: "POST", body: form });
278 } else request = new Request(url, { method: "POST", headers: { "content-type": "application/json" }, body: empty });
279 const res = await route.handle({ request, url, db, who, params: {} });
280 expect([said, res.status < 300]).toEqual([said, permit(who, "deal", "create") !== "no"]);
281 }
282 }
283 });
src/deals.ts +1 −1
@@ -1066,7 +1066,7 @@ export function addDeadline(
10661066 * email or phone only when neither is given, and is sorted into whichever it
10671067 * looks like.
10681068 */
1069 function partyText(input: { name: unknown; email?: unknown; phone?: unknown; contact?: unknown }): Result<{ name: string; email: string; phone: string; contact: string }> {
1069 export function partyText(input: { name: unknown; email?: unknown; phone?: unknown; contact?: unknown }): Result<{ name: string; email: string; phone: string; contact: string }> {
10701070 const name = line(input.name, 1, 100);
10711071 if (name === undefined) return fail("invalid", "A name is 1 to 100 characters.");
10721072 const given = (v: unknown): v is string => v !== undefined && v !== null && v !== "";
src/import.test.ts added +202
@@ -0,0 +1,202 @@
1 /** Bringing deals back: export then import gives the same deals, twice adds nothing, any bad deal writes nothing, and nobody reaches another's book. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtempSync, writeFileSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { openDb } from "./db.ts";
7 import { getDeal, insertDeal, listDeals, markFellThrough, moveDeadline, plan, sendReminder, setNote, type NewDeal } from "./deals.ts";
8 import { exportDeals } from "./export.ts";
9 import { forgetRationing } from "./security.ts";
10 import { serve } from "./server.ts";
11 import { importDeals } from "./import.ts";
12 import { terminal } from "./terminal.ts";
13 import { sign, type Who } from "./who.ts";
14
15 const KEY = "a-key-only-the-forge-holds";
16 const dir = mkdtempSync(join(tmpdir(), "app-import-"));
17 const alice: Who = { role: "user", id: "u1", name: "alice" };
18 const bob: Who = { role: "user", id: "u2", name: "bob" };
19 const must = <T>(r: { ok: true; value: T } | { ok: false; message: string }): T => {
20 if (!r.ok) throw new Error(r.message);
21 return r.value;
22 };
23 const open = (db: ReturnType<typeof openDb>, who: Who & { id: string; name: string }, input: NewDeal): number => {
24 const p = plan(input);
25 if (!p.ok) throw new Error(p.message);
26 return insertDeal(db, { id: who.id, name: who.name }, p.value).id;
27 };
28 const base0 = { buyer: "Bea Buyer", seller: "Sam Seller", contractDate: "2026-10-01", closingDate: "2026-11-20" };
29
30 let server: ReturnType<typeof serve>;
31 let base = "";
32 let file: string;
33 const asUser = (id: string, name: string): Record<string, string> => {
34 const identity = `${id}|${name}|user|${Date.now() + 600_000}`;
35 return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
36 };
37 const upload = (text: string, headers: Record<string, string>, extra: Record<string, string> = {}) => {
38 const form = new FormData();
39 form.set("file", new File([text], "deals.json", { type: "application/json" }));
40 return fetch(`${base}/deals/import`, { method: "POST", body: form, headers: { ...headers, ...extra } });
41 };
42 const shape = (db: ReturnType<typeof openDb>, who: Who, authors: string[] = []): { address: string }[] =>
43 must(listDeals(db, who))
44 .filter((d) => authors.length === 0 || authors.includes(d.author_id))
45 .map((d) => ({
46 address: d.address, side: d.side, cash: d.cash, contract: d.contract_date, price: d.sale_price, time: d.closing_time, place: d.closing_place,
47 fell: d.fell_reason, closed: d.closed,
48 parties: d.parties.map((p) => [p.role, p.name, p.phone, p.email]),
49 dates: d.deadlines.map((x) => [x.kind, x.label, x.due, x.note, x.done_at, x.waived_at, x.moves.map((m) => [m.from_due, m.to_due, m.reason])]),
50 }))
51 .sort((a, b) => a.address.localeCompare(b.address));
52 const filed = (): { version: number; deals: Record<string, unknown>[] } => JSON.parse(file);
53
54 beforeAll(() => {
55 const db = openDb(dir);
56 const a = open(db, alice as never, { ...base0, address: "9 Import Way", salePrice: 425000, side: "listing" });
57 open(db, alice as never, { ...base0, address: "5 Cash Row", cash: true });
58 const lost = open(db, alice as never, { ...base0, address: "3 Fell Court" });
59 const done = open(db, alice as never, { ...base0, address: "1 Closed Lane" });
60 db.query("UPDATE deal SET closing_time = '14:00', closing_place = 'Title office' WHERE id = ?1").run(a);
61 db.query("UPDATE party SET phone = '555 0100', email = '[email protected]' WHERE deal_id = ?1 AND role = 'buyer'").run(a);
62 const view = must(getDeal(db, alice, { id: a }));
63 const insp = view.deadlines.find((d) => d.kind === "inspection")!;
64 must(setNote(db, alice, { dealId: a, id: insp.id, note: "Call the inspector" }));
65 must(moveDeadline(db, alice, { dealId: a, id: insp.id, due: "2026-10-20", reason: "Seller asked" }));
66 must(sendReminder(db, alice, { deadlineId: insp.id, partyId: view.parties[0]!.id, moment: "before", subject: "Soon", body: "Hi there" }));
67 must(markFellThrough(db, alice, { id: lost, fell: true, reason: "Financing fell apart" }));
68 db.query("UPDATE deadline SET done_at = '2026-11-20T15:00:00.000Z' WHERE deal_id = ?1 AND kind = 'closing'").run(done);
69 open(db, bob as never, { ...base0, address: "77 Bob Road" });
70 file = JSON.stringify(exportDeals(must(listDeals(db, alice)), new Date()));
71 db.close();
72 server = serve({ port: 0, dataDir: dir, identityKey: KEY });
73 base = `http://${server.hostname}:${server.port}`;
74 });
75 afterAll(() => server.stop(true));
76
77 test("export then import into an empty agent gives the same deals; twice adds nothing", async () => {
78 const carol = asUser("u3", "carol");
79 forgetRationing();
80 const res = await upload(file, carol);
81 expect(res.status).toBe(200);
82 const text = await res.text();
83 expect(text).toContain("Brought back 5 deals, skipped 8 already here");
84 expect(text).toContain("not brought back");
85 const db = openDb(dir);
86 const carol3 = { role: "user", id: "u3", name: "carol" } as const;
87 const mine = shape(db, carol3, ["u3"]);
88 const hers = must(listDeals(db, carol3)).filter((d) => d.author_id === "u3");
89 expect(hers.length).toBe(5);
90 expect(hers.every((d) => d.author_id === "u3" && d.author_name === "carol")).toBe(true);
91 expect(hers.every((d) => d.sent.length === 0 && d.declined.length === 0)).toBe(true);
92 const original = shape(db, alice, ["u1", "u2"]);
93 db.close();
94 expect(mine).toEqual(original);
95 const again = await (await upload(file, carol)).text();
96 expect(again).toContain("Brought back 0 deals, skipped 13 already here");
97 const db2 = openDb(dir);
98 expect(must(listDeals(db2, { role: "user", id: "u3", name: "carol" })).filter((d) => d.author_id === "u3").length).toBe(5);
99 db2.close();
100 });
101
102 test("a wrong version, a bad date in deal 3, and an oversized body are refused with nothing written", async () => {
103 const dan = asUser("u4", "dan");
104 const count = (): number => {
105 const db = openDb(dir);
106 const n = must(listDeals(db, { role: "user", id: "u4", name: "dan" })).filter((d) => d.author_id === "u4").length;
107 db.close();
108 return n;
109 };
110 forgetRationing();
111 const wrong = await upload(JSON.stringify({ ...filed(), version: 2 }), dan);
112 expect(wrong.status).toBe(422);
113 const bad = filed();
114 (bad.deals[2]!.dates as { due: string }[])[0]!.due = "2026-13-45";
115 const res = await upload(JSON.stringify(bad), dan);
116 expect(res.status).toBe(422);
117 const text = await res.text();
118 expect(text).toContain("Deal 3 of the file");
119 expect(text).toContain("Nothing was brought back");
120 const api = await fetch(`${base}/api/deals/import`, { method: "POST", headers: { ...dan, "content-type": "application/json" }, body: JSON.stringify(bad) });
121 expect(api.status).toBe(422);
122 expect(JSON.stringify(await api.json())).toContain("Deal 3 of the file");
123 const noAddress = filed();
124 noAddress.deals[1]!.address = "";
125 expect((await upload(JSON.stringify(noAddress), dan)).status).toBe(422);
126 const huge = await fetch(`${base}/api/deals/import`, { method: "POST", headers: { ...dan, "content-type": "application/json" }, body: " ".repeat(5 * 1024 * 1024 + 10) });
127 expect(huge.status).toBe(413);
128 expect(count()).toBe(0);
129 });
130
131 test("the author is who imports, whatever the file says, and another agent's import never touches the first agent's deals", async () => {
132 const eve = asUser("u5", "eve");
133 const forged = filed();
134 forged.deals = forged.deals.map((d) => ({ ...d, author_id: "u1", author: "alice", opened_by: "alice" }));
135 forgetRationing();
136 const res = await fetch(`${base}/api/deals/import`, { method: "POST", headers: { ...eve, "content-type": "application/json" }, body: JSON.stringify(forged) });
137 expect(res.status).toBe(201);
138 expect(((await res.json()) as { brought: number }).brought).toBe(5);
139 const db = openDb(dir);
140 const aliceBefore = shape(db, alice);
141 expect(must(listDeals(db, alice)).filter((d) => d.author_id === "u1").length).toBe(4);
142 expect(must(listDeals(db, alice)).filter((d) => d.author_id === "u5").length).toBe(5);
143 expect(must(listDeals(db, alice)).some((d) => d.author_id === "u1" && d.author_name !== "alice")).toBe(false);
144 expect(must(listDeals(db, bob)).filter((d) => d.author_id === "u2").length).toBe(1);
145 db.close();
146 expect(aliceBefore).toEqual(shape(openDb(dir), alice));
147 });
148
149 test("a stranger is asked to sign in, a cross-site post is refused, and the Profile offers the form", async () => {
150 forgetRationing();
151 expect((await upload(file, {})).status).toBe(401);
152 const cross = await upload(file, asUser("u6", "fay"), { origin: "https://evil.example", "sec-fetch-site": "cross-site" });
153 expect(cross.status).toBe(403);
154 const profile = await (await fetch(`${base}/profile`, { headers: asUser("u6", "fay") })).text();
155 expect(profile).toContain("Bring deals back from a file");
156 expect(profile).toContain('enctype="multipart/form-data"');
157 const spec = (await (await fetch(`${base}/openapi.json`, { headers: asUser("u6", "fay") })).json()) as { paths: Record<string, unknown> };
158 expect(Object.keys(spec.paths)).toContain("/api/deals/import");
159 });
160
161 test("a deal with no buyer or seller party comes back with exactly the parties the file holds", () => {
162 const db = openDb(dir);
163 const grace = { role: "user", id: "u7", name: "grace" } as const;
164 const one = filed().deals.find((d) => d.address === "9 Import Way")!;
165 const lone = { ...one, address: "12 Lone Party Rd", parties: [{ role: "lender", name: "First Bank", phone: "", email: "[email protected]" }] };
166 const got = importDeals(db, grace, { version: 1, deals: [lone] });
167 expect(got.ok && got.value.brought).toBe(1);
168 const mine = must(listDeals(db, grace)).find((d) => d.address === "12 Lone Party Rd")!;
169 expect(mine.parties.map((p) => [p.role, p.name, p.email])).toEqual([["lender", "First Bank", "[email protected]"]]);
170 const audited = db.query<{ n: number }, []>("SELECT count(*) AS n FROM audit WHERE what = 'deal.import' AND outcome = 'ok'").get()!.n;
171 expect(audited).toBeGreaterThan(0);
172 db.close();
173 });
174
175 test("the terminal's O brings deals back from the exports folder, gated like the page", () => {
176 const out = mkdtempSync(join(tmpdir(), "app-import-term-"));
177 writeFileSync(join(out, "deal-clock-2026-11-09.json"), file);
178 writeFileSync(join(out, "bad.json"), JSON.stringify({ ...filed(), version: 9 }));
179 const db = openDb(dir);
180 const context = { title: "deal-clock", where: "test", exportDir: out };
181 const hal: Who = { role: "user", id: "u8", name: "hal" };
182 const t = terminal(db, hal, context);
183 const say = (): string => t.screen().join(" ").replace(/\x1b\[[0-9;]*m/g, "");
184 t.press("O");
185 t.press("\r");
186 expect(say()).toContain("Brought back 5 deals, skipped 8 already here");
187 expect(must(listDeals(db, hal)).filter((d) => d.author_id === "u8").length).toBe(5);
188 t.press("O");
189 "bad.json".split("").forEach((ch) => t.press(ch));
190 t.press("\r");
191 expect(say()).toContain("Nothing was brought back");
192 t.press("O");
193 "../../etc/passwd".split("").forEach((ch) => t.press(ch));
194 t.press("\r");
195 expect(say()).toContain("No such file");
196 const guest = terminal(db, { role: "anon" }, context);
197 guest.press("O");
198 guest.press("\r");
199 expect(guest.screen().join(" ")).toContain("Sign in");
200 expect(must(listDeals(db, hal)).filter((d) => d.author_id === "").length).toBe(0);
201 db.close();
202 });
src/import.ts added +260
@@ -0,0 +1,260 @@
1 /**
2 * Bringing deals back from a downloaded deal-clock file (`export.ts`): the
3 * file is checked whole at the boundary, then written in one transaction as
4 * the signed-in agent's own. Every deal goes through `plan`, the check opening
5 * a deal uses, and every party through `partyText`, so a second set of rules
6 * never exists. The message record (sent and declined reminders) is never
7 * re-created: nothing here says a message was sent from this account.
8 */
9
10 import type { Database } from "bun:sqlite";
11 import { permit } from "./contract.ts";
12 import { isDate } from "./dates.ts";
13 import { KIND, KINDS, isPartyRole, leadOf, leadOfLabel, type Kind, type PartyRole } from "./deadlines.ts";
14 import { closingPlace, closingTime } from "./closing.ts";
15 import { NOTE_MAX, REASON_MAX, insertDeal, line, partyText, plan, sameAddress, type NewDeal, type Plan } from "./deals.ts";
16 import { EXPORT_MAX, EXPORT_VERSION } from "./export.ts";
17 import { emit } from "./live.ts";
18 import { audited, fail, ok, refuse as denied, type Result } from "./result.ts";
19 import type { Who } from "./who.ts";
20
21 /** The most bytes of a file the import reads. */
22 export const IMPORT_BYTES = 5 * 1024 * 1024;
23 const MOVES_MAX = 100;
24 const DATES_MAX = 100;
25 const PARTIES_MAX = 50;
26
27 interface Move {
28 from: string;
29 to: string;
30 reason: string;
31 by: string;
32 at: string | null;
33 }
34 interface DateIn {
35 label: string;
36 kind: Kind;
37 due: string;
38 owes: PartyRole;
39 note: string;
40 done: string | null;
41 waived: string | null;
42 waivedBy: string | null;
43 moves: Move[];
44 }
45 interface PartyIn {
46 role: PartyRole;
47 name: string;
48 email: string;
49 phone: string;
50 contact: string;
51 }
52 /** One deal of the file, checked and ready to write. */
53 export interface DealIn {
54 plan: Plan;
55 closingTime: string | null;
56 closingPlace: string | null;
57 fell: { at: string; reason: string; by: string } | null;
58 parties: PartyIn[];
59 dates: DateIn[];
60 }
61
62 const obj = (v: unknown): Record<string, unknown> =>
63 typeof v === "object" && v !== null && !Array.isArray(v) ? (v as Record<string, unknown>) : {};
64
65 const stamp = (v: unknown): string | null | undefined =>
66 v === null || v === undefined || v === ""
67 ? null
68 : typeof v === "string" && v.length <= 40 && /^\d{4}-\d{2}-\d{2}T[\d:.]+(Z|[+-]\d{2}:\d{2})$/.test(v) && !Number.isNaN(Date.parse(v))
69 ? v
70 : undefined;
71
72 /** A refusal that names the deal's place in the file ("Deal 3") and the field. */
73 const refuse = (n: number, message: string, field: string): Result<never> =>
74 fail("invalid", `Deal ${n} of the file: ${message}`, `deals[${n - 1}].${field}`);
75
76 function readDeal(raw: unknown, n: number): Result<DealIn> {
77 const d = obj(raw);
78 const list = (v: unknown, max: number, field: string): unknown[] | Result<never> =>
79 v === undefined || v === null ? [] : Array.isArray(v) && v.length <= max ? v : refuse(n, `${field} is a list of at most ${max}.`, field);
80 const partiesRaw = list(d.parties, PARTIES_MAX, "parties");
81 if (!Array.isArray(partiesRaw)) return partiesRaw;
82 const datesRaw = list(d.dates, DATES_MAX, "dates");
83 if (!Array.isArray(datesRaw)) return datesRaw;
84
85 const parties: PartyIn[] = [];
86 for (const [i, p] of partiesRaw.entries()) {
87 const o = obj(p);
88 if (!isPartyRole(o.role)) return refuse(n, `party ${i + 1} has no valid role.`, `parties[${i}].role`);
89 const text = partyText(o);
90 if (!text.ok) return refuse(n, `party ${i + 1}: ${text.message}`, `parties[${i}]`);
91 parties.push({ role: o.role, ...text.value });
92 }
93
94 const dates: DateIn[] = [];
95 for (const [i, x] of datesRaw.entries()) {
96 const o = obj(x);
97 const at = `dates[${i}]`;
98 const label = line(o.label, 1, 100);
99 if (label === undefined) return refuse(n, `date ${i + 1} needs a label (up to 100 characters).`, `${at}.label`);
100 if (!KINDS.includes(o.kind as Kind)) return refuse(n, `date ${i + 1} has an unknown kind.`, `${at}.kind`);
101 if (!isDate(o.due)) return refuse(n, `date ${i + 1} (${label}) is not a date (YYYY-MM-DD).`, `${at}.due`);
102 const owes = o.owed_by;
103 if (!isPartyRole(owes)) return refuse(n, `date ${i + 1} (${label}) says no valid party owes it.`, `${at}.owed_by`);
104 const note = o.note === undefined || o.note === null || o.note === "" ? "" : line(o.note, 1, NOTE_MAX);
105 if (note === undefined) return refuse(n, `the note on date ${i + 1} is up to ${NOTE_MAX} characters.`, `${at}.note`);
106 const done = stamp(o.done);
107 const waived = stamp(o.waived);
108 if (done === undefined) return refuse(n, `date ${i + 1} (${label}) has a bad done time.`, `${at}.done`);
109 if (waived === undefined) return refuse(n, `date ${i + 1} (${label}) has a bad waived time.`, `${at}.waived`);
110 if (done !== null && waived !== null) return refuse(n, `date ${i + 1} (${label}) is both done and waived.`, `${at}.waived`);
111 const waivedBy = waived === null ? null : (line(o.waived_by, 1, 100) ?? "");
112 const movesRaw = list(o.moves, MOVES_MAX, `${at}.moves`);
113 if (!Array.isArray(movesRaw)) return movesRaw;
114 const moves: Move[] = [];
115 for (const [j, mv] of movesRaw.entries()) {
116 const m = obj(mv);
117 const reason = line(m.reason, 1, REASON_MAX);
118 const by = line(m.by, 1, 100);
119 const when = stamp(m.at);
120 if (!isDate(m.from) || !isDate(m.to) || reason === undefined || by === undefined || when === undefined)
121 return refuse(n, `move ${j + 1} of date ${i + 1} (${label}) is not a from and to date, a reason and who moved it.`, `${at}.moves[${j}]`);
122 moves.push({ from: m.from, to: m.to, reason, by, at: when });
123 }
124 dates.push({ label, kind: o.kind as Kind, due: o.due, owes, note, done, waived, waivedBy, moves });
125 }
126
127 const closing = dates.find((x) => x.kind === "closing");
128 if (dates.filter((x) => x.kind === "closing").length !== 1 || closing === undefined)
129 return refuse(n, "a deal has exactly one Closing date.", "dates");
130 const buyer = parties.find((p) => p.role === "buyer")?.name ?? "Buyer";
131 const seller = parties.find((p) => p.role === "seller")?.name ?? "Seller";
132 const input: NewDeal = {
133 address: d.address,
134 buyer,
135 seller,
136 contractDate: d.contract_date,
137 closingDate: closing.due,
138 businessDays: d.business_days === true,
139 cash: d.cash === undefined ? false : d.cash,
140 salePrice: d.sale_price,
141 earnestAmount: d.earnest_amount,
142 commissionRate: d.commission_rate,
143 side: d.side,
144 };
145 const planned = plan(input);
146 if (!planned.ok) return refuse(n, planned.message, planned.field ?? "address");
147 const time = closingTime(d.closing_time);
148 if (!time.ok) return refuse(n, time.message, "closing_time");
149 const place = closingPlace(d.closing_place);
150 if (!place.ok) return refuse(n, place.message, "closing_place");
151
152 let fell: DealIn["fell"] = null;
153 if (d.state === "fell_through") {
154 const at = stamp(d.fell_at);
155 const reason = line(d.reason, 1, REASON_MAX);
156 if (at === null || at === undefined || reason === undefined)
157 return refuse(n, "a deal that fell through needs the time and a reason (up to 200 characters).", "reason");
158 fell = { at, reason, by: line(d.by, 1, 100) ?? "" };
159 } else if (d.state !== undefined && d.state !== "open" && d.state !== "closed") {
160 return refuse(n, "state is open, closed or fell_through.", "state");
161 }
162 return ok({ plan: planned.value, closingTime: time.value, closingPlace: place.value, fell, parties, dates });
163 }
164
165 /** The whole file, checked: the deals ready to write, or the one refusal that names where. Nothing is read from a database here. */
166 export function readImport(file: unknown): Result<DealIn[]> {
167 const f = obj(file);
168 if (f.version !== EXPORT_VERSION)
169 return fail("invalid", `This is not a deal-clock file this app reads: its version should be ${EXPORT_VERSION}.`, "version");
170 if (!Array.isArray(f.deals)) return fail("invalid", "The file holds no list of deals.", "deals");
171 if (f.deals.length > EXPORT_MAX) return fail("invalid", `A file holds at most ${EXPORT_MAX} deals.`, "deals");
172 const deals: DealIn[] = [];
173 for (const [i, raw] of f.deals.entries()) {
174 const one = readDeal(raw, i + 1);
175 if (!one.ok) return one;
176 deals.push(one.value);
177 }
178 return ok(deals);
179 }
180
181 export interface Imported {
182 readonly id: number;
183 readonly brought: number;
184 readonly skipped: number;
185 }
186
187 function write(db: Database, who: Who & { role: "user" | "admin" }, deals: readonly DealIn[]): { brought: number[]; skipped: number } {
188 const author = { id: who.id, name: who.name };
189 const have = new Set(
190 db
191 .query<{ address: string; contract_date: string }, [string]>("SELECT address, contract_date FROM deal WHERE author_id IN (?1, 'sample')")
192 .all(author.id)
193 .map((r) => `${sameAddress(r.address)}|${r.contract_date}`),
194 );
195 const brought: number[] = [];
196 let skipped = 0;
197 for (const d of deals) {
198 const key = `${sameAddress(d.plan.address)}|${d.plan.contractDate}`;
199 if (have.has(key)) {
200 skipped++;
201 continue;
202 }
203 have.add(key);
204 const deal = insertDeal(db, author, d.plan);
205 // The file's own dates and parties stand in place of the standard ones.
206 db.query("DELETE FROM deadline WHERE deal_id = ?1").run(deal.id);
207 db.query("DELETE FROM party WHERE deal_id = ?1").run(deal.id);
208 db.query("UPDATE deal SET closing_time = ?1, closing_place = ?2, fell_at = ?3, fell_reason = ?4, fell_by = ?5 WHERE id = ?6").run(
209 d.closingTime, d.closingPlace, d.fell?.at ?? null, d.fell?.reason ?? null, d.fell?.by ?? null, deal.id,
210 );
211 for (const p of d.parties)
212 db.query("INSERT INTO party (deal_id, role, name, contact, email, phone) VALUES (?1, ?2, ?3, ?4, ?5, ?6)").run(deal.id, p.role, p.name, p.contact, p.email, p.phone);
213 for (const x of d.dates) {
214 const lead = x.kind === "custom" ? leadOfLabel(x.label) : leadOf(x.kind);
215 const what = x.kind === "custom" ? "" : KIND[x.kind].what;
216 const row = db
217 .query<{ id: number }, [number, string, string, string, string, string, string, string | null, string | null, string | null, number]>(
218 "INSERT INTO deadline (deal_id, kind, label, due, owes, what, note, done_at, waived_at, waived_by, lead_days) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11) RETURNING id",
219 )
220 .get(deal.id, x.kind, x.label, x.due, x.owes, what, x.note, x.done, x.waived, x.waivedBy, lead)!;
221 for (const m of x.moves)
222 db.query("INSERT INTO deadline_move (deadline_id, from_due, to_due, reason, by_name, at) VALUES (?1, ?2, ?3, ?4, ?5, COALESCE(?6, strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))").run(row.id, m.from, m.to, m.reason, m.by, m.at);
223 }
224 brought.push(deal.id);
225 }
226 return { brought, skipped };
227 }
228
229 /**
230 * Bring a file's deals into `who`'s book: all of them or, on any refusal,
231 * none. The table is asked as opening a deal asks it; the author is `who`,
232 * whatever the file says. A deal already in the book (same address and contract
233 * date, the sample deals included, as the file carries them) is skipped.
234 */
235 export function importDeals(db: Database, who: Who, file: unknown): Result<Imported> {
236 const refused = (r: Result<never>): Result<Imported> => audited(db, who, "deal", "create", undefined, r as Result<{ id: number }>, "deal.import") as Result<Imported>;
237 if (who.role === "anon" || permit(who, "deal", "create") !== "yes")
238 return refused(denied(who, "Sign in to bring deals back.", "You may not open a deal."));
239 const read = readImport(file);
240 if (!read.ok) return refused(read);
241 // The audit rows go in with the deals: a crash leaves both or neither.
242 const done = db.transaction(() => {
243 const written = write(db, who, read.value);
outdated

The audit rows are written after the transaction commits, so a failure between the two leaves deals with no deal.import row. Move this loop inside the db.transaction.

244 for (const id of written.brought) audited(db, who, "deal", "create", id, ok({ id }), "deal.import");
245 return written;
246 })();
247 for (const id of done.brought) emit("deal.changed", { id });
248 return ok({ id: done.brought[0] ?? 0, brought: done.brought.length, skipped: done.skipped });
249 }
250
251 /** A file's text, as the terminal reads it from disk: not JSON is a refusal, nothing written. */
252 export function importText(db: Database, who: Who, text: string): Result<Imported> {
253 let file: unknown;
254 try {
255 file = JSON.parse(text);
256 } catch {
257 return fail("invalid", "That is not a deal-clock file (it is not JSON).");
258 }
259 return importDeals(db, who, file);
260 }
src/pages/profile.ts +19
@@ -67,12 +67,31 @@ export function profilePage(input: {
6767 <div><dt class="muted small">A line of your own</dt><dd>${escape(agent.line ?? "") || "None"}</dd></div>
6868 </dl>
6969 <p class="muted small">Only an admin can change these.</p>`;
70 const bringBack = who.role === "anon" ? "" : `<form class="stack" method="post" action="/deals/import" enctype="multipart/form-data">
71 <h2>Bring deals back from a file</h2>
72 <p class="muted small">Choose a file you downloaded above, to move to another laptop or account or to recover your book. Each deal comes back as yours, with its dates, parties and notes; one already here (same address and contract date) is skipped. Sent and declined messages are not brought back: this account never claims to have sent them. A file with a mistake in it brings nothing.</p>
73 <label>Your deal-clock file<input class="input" type="file" name="file" accept="application/json,.json" required></label>
74 <div class="actions"><button class="button">Bring deals back</button></div>
75 </form>`;
7076 const body = `<p class="crumb"><a href="/">← The clock</a></p>
7177 <section class="card narrow">
7278 <h1>Profile</h1>
7379 <p class="muted">${profile.saved ? "Saved by you." : "These are the defaults the app was started with."} The header, the client timeline and the sign-off of every unsent reminder carry them. Reminders already sent keep the words that went out. Your time zone decides which day is today.</p>
7480 ${mayChange ? form : readOnly}
7581 <p class="small"><a href="/deals/export.json" download>Download all my deals</a> <span class="muted">One file with every deal you can read: dates, parties, notes, moves and the words of every message sent.</span></p>
82 ${bringBack}
7683 </section>`;
7784 return layout({ body, who, agent, path: "/profile", heading: "Profile", notice, waiting: input.waiting, live: false });
7885 }
86
87 /** The answer to a file brought back: what came, what was already here, and what is never re-created. */
88 export function broughtBackPage(input: { who: Who; waiting?: number | undefined; agent?: Agent | undefined; brought: number; skipped: number }): string {
89 const n = (count: number): string => `${count} deal${count === 1 ? "" : "s"}`;
90 const body = `<section class="card narrow">
91 <h1>Bring deals back from a file</h1>
92 <p role="status">Brought back ${n(input.brought)}, skipped ${input.skipped} already here.</p>
93 <p class="muted">The record of messages sent or declined is not brought back, so nothing here says a message went out from this account.</p>
94 <p><a class="button" href="/">Go to the clock</a></p>
95 </section>`;
96 return layout({ body, who: input.who, agent: input.agent, path: "/profile", heading: "Bring deals back from a file", waiting: input.waiting, live: false });
97 }
src/routes.ts +55 −1
@@ -29,6 +29,7 @@ import {
2929 html,
3030 json,
3131 isForm,
32 readBody,
3233 readForm,
3334 readJson,
3435 readNotice,
@@ -84,9 +85,10 @@ import {
8485 } from "./deals.ts";
8586 import { activitySheet, sheet } from "./csv.ts";
8687 import { exportDeals, exportName } from "./export.ts";
88 import { IMPORT_BYTES, importDeals } from "./import.ts";
8789 import { book, calendar } from "./ics.ts";
8890 import { agentOf, getProfile, setProfile, useDefaults, type Profile } from "./profile.ts";
89 import { profilePage } from "./pages/profile.ts";
91 import { broughtBackPage, profilePage } from "./pages/profile.ts";
9092 import { permit } from "./contract.ts";
9193 import type { AuditRow } from "./audit.ts";
9294 import { rpc } from "./mcp.ts";
@@ -175,6 +177,22 @@ function everything(c: Context): Result<Response> {
175177 };
176178 }
177179
180 /** The JSON of an uploaded file field, capped at the import's size: undefined when too large, null when it is not JSON. */
181 async function uploaded(request: Request): Promise<unknown> {
182 const type = request.headers.get("content-type") ?? "";
183 if (!/^multipart\/form-data\b/i.test(type)) return null;
184 const body = await readBody(request, IMPORT_BYTES);
185 if (body === undefined) return undefined;
186 try {
187 const data = await new Request(request.url, { method: "POST", headers: { "content-type": type }, body: new Uint8Array(body) }).formData();
188 const file = data.get("file");
189 if (file === null || typeof file === "string") return null;
190 return JSON.parse(await file.text());
191 } catch {
192 return null;
193 }
194 }
195
178196 export interface Route {
179197 readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
180198 readonly path: string;
@@ -1017,6 +1035,19 @@ export const routes: readonly Route[] = [
10171035 return html(profilePage({ who: c.who, waiting: waitingFor(c), profile: profile.value, mayChange: true, state: { values: f, problem: saved } }), 422);
10181036 },
10191037 },
1038 {
1039 method: "POST",
1040 path: "/deals/import",
1041 handle: async (c) => {
1042 const file = await uploaded(c.request);
1043 const heading = "Bring deals back from a file";
1044 if (file === undefined) return html(refusedPage({ who: c.who, agent: agentFor(c), waiting: waitingFor(c), path: "/profile", heading, error: "invalid", message: "That file is over 5 MB; nothing was brought back." }), 413);
1045 if (file === null) return html(refusedPage({ who: c.who, agent: agentFor(c), waiting: waitingFor(c), path: "/profile", heading, error: "invalid", message: "Choose a deal-clock file (the one Download all my deals gave you); nothing was brought back." }), 422);
1046 const got = importDeals(c.db, c.who, file);
1047 if (!got.ok) return html(refusedPage({ who: c.who, agent: agentFor(c), waiting: waitingFor(c), path: "/profile", heading, error: got.error, message: `${got.message} Nothing was brought back.` }), STATUS[got.error]);
1048 return html(broughtBackPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), brought: got.value.brought, skipped: got.value.skipped }));
1049 },
1050 },
10201051 {
10211052 method: "POST",
10221053 path: "/profile/defaults",
@@ -1489,6 +1520,29 @@ export const routes: readonly Route[] = [
14891520 conflict: CONFLICT,
14901521 },
14911522 },
1523 {

This new door has no row in src/contract.test.ts. Add one for deal create, least user. The terminal and MCP also have no import, though both have the export.

1524 method: "POST",
1525 path: "/api/deals/import",
1526 handle: async (c) => {
1527 const body = await readBody(c.request, IMPORT_BYTES);
1528 if (body === undefined) return json({ error: "too_large" }, 413);
1529 let file: unknown;
1530 try {
1531 file = JSON.parse(new TextDecoder().decode(body));
1532 } catch {
1533 return answer({ ok: false, error: "invalid", message: "The body is not JSON." });
1534 }
1535 const got = importDeals(c.db, c.who, file);
1536 return answer(got.ok ? { ok: true, value: { brought: got.value.brought, skipped: got.value.skipped, messages: "not re-created" } } : got, 201);
1537 },
1538 api: {
1539 summary: "Bring deals back from a file Download all my deals gave you (the same JSON as the body, up to 5 MB): version must be 1, at most 2000 deals. Each is created as yours with its side, cash flag, figures, closing time and place, parties, dates (done, waived, notes, moves) and state (open, closed, fell through). A deal already in your book with the same address and contract date is skipped. Any bad deal refuses the whole file with a 422 naming its place and field, and nothing is written. Sent and declined messages are not re-created.",
1540 body: { type: "object", required: ["version", "deals"], properties: { version: { type: "integer" }, deals: { type: "array", items: { type: "object" } } } },
1541 answer: { type: "object", properties: { brought: { type: "integer" }, skipped: { type: "integer" }, messages: { type: "string" } } },
1542 status: 201,
1543 cell: { resource: "deal", verb: "create" },
1544 },
1545 },
14921546 {
14931547 method: "GET",
14941548 path: "/api/deals/export",
src/terminal.ts +32 −2
@@ -73,7 +73,8 @@ import { isOpen, PARTY_LABEL, PARTY_ROLES, type PartyRole } from "./deadlines.ts
7373 import { agentOf, getProfile, setProfile, useDefaults, zoneOf } from "./profile.ts";
7474 import { permit } from "./contract.ts";
7575 import { exportDeals, exportName } from "./export.ts";
76 import { mkdirSync, writeFileSync } from "node:fs";
76 import { IMPORT_BYTES, importText } from "./import.ts";
77 import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
7778 import { join } from "node:path";
7879 import { coming, comingLine } from "./coming.ts";
7980 import { drafts, heldFrom, inClockOrder, momentLabel, pending, remindsLine, sentLabel, unsent, type Agent, type Draft } from "./reminders.ts";
@@ -598,6 +599,14 @@ export function terminal(
598599 cursor = 0;
599600 } else if (key === "E") {
600601 said = exportAll();
602 } else if (key === "O" && (view === "dates" || view === "week")) {
603 prompt = {
604 steps: ["File in the exports folder to bring deals back from (enter alone: the newest)"],
605 answers: [],
606 text: "",
607 max: 100,
608 finish: ([name]) => bringBack(name ?? ""),
609 };
601610 } else if (key === "I") {
602611 view = "profile";
603612 cursor = 0;
@@ -708,6 +717,27 @@ export function terminal(
708717 return `Wrote ${listed.value.length} deals to ${path}.`;
709718 }
710719
720 /** O: the page's Bring deals back from a file, read from the exports folder (where E writes); the same `importDeals`, the same table. */
721 function bringBack(name: string): string {
722 if (context.exportDir === undefined || !existsSync(context.exportDir)) return "Bringing deals back is not available here: no exports folder.";
723 const may = mayOpen(me);
724 if (!may.ok) return may.message;
725 const files = readdirSync(context.exportDir).filter((f) => /^[\w.-]+\.json$/.test(f)).sort();
726 const file = name === "" ? files[files.length - 1] : name;
727 // A bare file name only: no path leaves the exports folder.
728 if (file === undefined || !/^[\w.-]+\.json$/.test(file) || !files.includes(file)) return `No such file in ${context.exportDir}${files.length === 0 ? " (press E to write one)" : `: ${files.join(", ")}`}.`;
729 const path = join(context.exportDir, file);
730 try {
731 if (statSync(path).size > IMPORT_BYTES) return "That file is over 5 MB; nothing was brought back.";
732 const got = importText(db, me, readFileSync(path, "utf8"));
733 return got.ok
734 ? `Brought back ${got.value.brought} deals, skipped ${got.value.skipped} already here. Sent and declined messages are not brought back.`
735 : `${got.message} Nothing was brought back.`;
736 } catch {
737 return "Could not read that file.";
738 }
739 }
740
711741 const dateLine = (row: Row, i: number, withDeal: boolean): string => {
712742 const { date, deal } = row;
713743 const u = urgencyOf(date, on());
@@ -917,7 +947,7 @@ export function terminal(
917947 ? "b back to the deal · d dates · q quit"
918948 : view === "deal"
919949 ? "j/k move · t client timeline · f broker file · s messages · x done · W waive · u reopen/un-waive · m move · N next business day · l remind lead · T note · a add date · p add party · e edit party · A edit address · C closing time and place · M amend the contract · S status update to the party · X remove date/party · D remove deal · b back · q quit"
920 : `j/k move · x done · W waive · u reopen · m move · N next business day · l lead · T note · o open deal · n new deal · c check dates · / find · R restore samples · h ${showDone ? "hide" : "show"} done · w this week · r reminders · P people · I profile · E export · L activity · q quit`;
950 : `j/k move · x done · W waive · u reopen · m move · N next business day · l lead · T note · o open deal · n new deal · c check dates · / find · R restore samples · O bring deals back · h ${showDone ? "hide" : "show"} done · w this week · r reminders · P people · I profile · E export · L activity · q quit`;
921951 lines.push(dim(me.role === "anon" ? `${keys} · sign in with a key the forge knows to change anything` : keys));
922952 }
923953 return lines;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs