Public: anyone can read this; the crew can change it.
Sample deals look like today on every boot, not like the day the database was made #11
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/4-sample-deals-look-like-today-on-every-bo → main 1 commit fork
5 files
changed +196
−3
README.md +4 −1
| @@ -35,7 +35,10 @@ tick *Count business days* to skip weekends. | ||
| 35 | 35 | |
| 36 | 36 | Four sample deals are already in: one with an overdue inspection, one |
| 37 | 37 | closing this week, one whose appraisal was moved, and one just signed. |
| 38 | Their dates are set relative to the day the app first started. | |
| 38 | They stay current: each time the app starts, the samples nobody has | |
| 39 | touched move forward together by the days since they were last set, so the | |
| 40 | first screen always looks like today. A sample you changed (marked done, | |
| 41 | moved, added a date or party to, sent a reminder for) is left alone. | |
| 39 | 42 | |
| 40 | 43 | ### What is real, and what is not yet |
| 41 | 44 |
src/db.test.ts +32
| @@ -1,5 +1,7 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { mkdtempSync } from "node:fs"; |
| 3 | import { addDays, today } from "./dates.ts"; | |
| 4 | import { seed } from "./sample.ts"; | |
| 3 | 5 | import { tmpdir } from "node:os"; |
| 4 | 6 | import { join } from "node:path"; |
| 5 | 7 | import { check, migrate, migrations, openDb } from "./db.ts"; |
| @@ -30,3 +32,33 @@ test("the sample deals are written once, however many processes open a new file" | ||
| 30 | 32 | db.query<{ n: number }, []>("SELECT count(*) AS n FROM deal WHERE author_id = 'sample'").get()!.n; |
| 31 | 33 | expect([count(a), count(b)]).toEqual([4, 4]); |
| 32 | 34 | }); |
| 35 | ||
| 36 | test("sample deals boot looking like today, and a changed one is left alone", () => { | |
| 37 | const dir = mkdtempSync(join(tmpdir(), "app-refresh-")); | |
| 38 | const then = addDays(today(), -30); | |
| 39 | // A database seeded 30 days ago: open a new one, then replace its samples. | |
| 40 | const db = openDb(dir); | |
| 41 | db.exec("DELETE FROM deal; DELETE FROM app_meta"); | |
| 42 | seed(db, then); | |
| 43 | const row = (address: string, kind: string) => | |
| 44 | db.query<{ id: number; due: string; done_at: string | null }, [string, string]>( | |
| 45 | "SELECT d.id, d.due, d.done_at FROM deadline d JOIN deal ON deal.id = d.deal_id WHERE deal.address = ?1 AND d.kind = ?2", | |
| 46 | ).get(address, kind)!; | |
| 47 | expect(row("9 Orchard Row", "closing").due).toBe(addDays(then, 18)); | |
| 48 | // A person marks a date done on the Juniper Court sample. | |
| 49 | db.query("UPDATE deadline SET done_at = '2026-01-01T09:00:00.000Z' WHERE id = ?1").run(row("2210 Juniper Court", "inspection").id); | |
| 50 | const juniper = row("2210 Juniper Court", "closing").due; | |
| 51 | db.close(); | |
| 52 | const again = openDb(dir); | |
| 53 | const after = (address: string, kind: string) => | |
| 54 | again.query<{ due: string }, [string, string]>( | |
| 55 | "SELECT d.due FROM deadline d JOIN deal ON deal.id = d.deal_id WHERE deal.address = ?1 AND d.kind = ?2", | |
| 56 | ).get(address, kind)!.due; | |
| 57 | expect(after("1418 Alder Lane", "closing")).toBe(addDays(today(), 4)); | |
| 58 | expect(after("1418 Alder Lane", "inspection")).toBe(addDays(today(), -26 + 10)); | |
| 59 | expect(after("9 Orchard Row", "appraisal")).toBe(addDays(today(), 2)); | |
| 60 | expect(after("2210 Juniper Court", "closing")).toBe(juniper); | |
| 61 | const move = again.query<{ to_due: string }, []>("SELECT to_due FROM deadline_move").get()!; | |
| 62 | expect(move.to_due).toBe(addDays(today(), 2)); | |
| 63 | expect(again.query("SELECT value FROM app_meta WHERE key = 'sample_anchor'").get()).toEqual({ value: today() }); | |
| 64 | }); |
src/db.ts +6 −1
| @@ -12,7 +12,7 @@ import { Database } from "bun:sqlite"; | ||
| 12 | 12 | import { mkdirSync } from "node:fs"; |
| 13 | 13 | import { join } from "node:path"; |
| 14 | 14 | import { dataDir } from "./env.ts"; |
| 15 | import { seed } from "./sample.ts"; | |
| 15 | import { refresh, seed } from "./sample.ts"; | |
| 16 | 16 | |
| 17 | 17 | /** Append, never edit: a migration that has run is history. */ |
| 18 | 18 | export const migrations: string[] = [ |
| @@ -93,6 +93,10 @@ export const migrations: string[] = [ | ||
| 93 | 93 | `CREATE TABLE app_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)`, |
| 94 | 94 | // The template's notes scaffold is gone; nothing reads the table. |
| 95 | 95 | `DROP TABLE note`, |
| 96 | // The day the sample deals' dates are anchored to; src/sample.ts moves the | |
| 97 | // untouched ones forward from it on boot. A database seeded before this | |
| 98 | // anchored them to the day it was seeded. | |
| 99 | `INSERT OR IGNORE INTO app_meta (key, value) SELECT 'sample_anchor', value FROM app_meta WHERE key = 'seeded'`, | |
| 96 | 100 | ]; |
| 97 | 101 | |
| 98 | 102 | export function openDb(dir = dataDir): Database { |
| @@ -103,6 +107,7 @@ export function openDb(dir = dataDir): Database { | ||
| 103 | 107 | db.exec("PRAGMA busy_timeout = 5000"); |
| 104 | 108 | migrate(db); |
| 105 | 109 | seed(db); |
| 110 | refresh(db); | |
| 106 | 111 | return db; |
| 107 | 112 | } |
| 108 | 113 |
src/sample.test.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { isUntouched, shiftSample, type SampleDates } from "./sample.ts"; | |
| 3 | ||
| 4 | const dates: SampleDates = { | |
| 5 | contractDate: "2026-09-01", | |
| 6 | deadlines: [ | |
| 7 | { id: 1, due: "2026-09-11", doneAt: "2026-09-30T17:00:00.000Z" }, | |
| 8 | { id: 2, due: "2026-10-05", doneAt: null }, | |
| 9 | ], | |
| 10 | moves: [{ id: 7, fromDue: "2026-09-22", toDue: "2026-10-03" }], | |
| 11 | }; | |
| 12 | ||
| 13 | test("every date of a sample moves by the days since its anchor, together", () => { | |
| 14 | const a = shiftSample(dates, "2026-10-01", "2026-10-31"); | |
| 15 | expect(a.contractDate).toBe("2026-10-01"); | |
| 16 | expect(a.deadlines).toEqual([ | |
| 17 | { id: 1, due: "2026-10-11", doneAt: "2026-10-30T17:00:00.000Z" }, | |
| 18 | { id: 2, due: "2026-11-04", doneAt: null }, | |
| 19 | ]); | |
| 20 | expect(a.moves).toEqual([{ id: 7, fromDue: "2026-10-22", toDue: "2026-11-02" }]); | |
| 21 | // A month later again: the same gap between dates, a different shift. | |
| 22 | const b = shiftSample(dates, "2026-10-01", "2026-11-30"); | |
| 23 | expect(b.deadlines[1]!.due).toBe("2026-12-04"); | |
| 24 | expect(shiftSample(dates, "2026-10-01", "2026-10-01")).toBe(dates); | |
| 25 | expect(shiftSample(dates, "2026-10-01", "2026-09-01")).toBe(dates); | |
| 26 | }); | |
| 27 | ||
| 28 | test("a sample a person changed is not untouched", () => { | |
| 29 | const state = { | |
| 30 | address: "9 Orchard Row", | |
| 31 | parties: 3, // lender, title, other agent | |
| 32 | customs: 1, | |
| 33 | reminders: 0, | |
| 34 | doneAts: ["2026-09-30T17:00:00.000Z", "2026-09-30T17:00:00.000Z", null], | |
| 35 | moveAuthors: ["Sample data"], | |
| 36 | }; | |
| 37 | expect(isUntouched(state, "2026-10-01")).toBe(true); | |
| 38 | expect(isUntouched({ ...state, reminders: 1 }, "2026-10-01")).toBe(false); | |
| 39 | expect(isUntouched({ ...state, parties: 4 }, "2026-10-01")).toBe(false); | |
| 40 | expect(isUntouched({ ...state, moveAuthors: ["Sample data", "Ana"] }, "2026-10-01")).toBe(false); | |
| 41 | expect(isUntouched({ ...state, doneAts: ["2026-09-30T17:00:00.000Z", "2026-10-02T08:00:00.000Z", null] }, "2026-10-01")).toBe(false); | |
| 42 | expect(isUntouched({ ...state, address: "Mine" }, "2026-10-01")).toBe(false); | |
| 43 | }); |
src/sample.ts +111 −1
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| 9 | import { addDays, today } from "./dates.ts"; | |
| 9 | import { addDays, daysBetween, today } from "./dates.ts"; | |
| 10 | 10 | import { DEFAULT_LENGTHS, type PartyRole } from "./deadlines.ts"; |
| 11 | 11 | import { insertDeal } from "./deals.ts"; |
| 12 | 12 | |
| @@ -82,6 +82,115 @@ const SAMPLES: readonly Sample[] = [ | ||
| 82 | 82 | }, |
| 83 | 83 | ]; |
| 84 | 84 | |
| 85 | /** The time of day a sample's finished dates carry: what tells them from a person's click. */ | |
| 86 | const DONE_TIME = "T17:00:00.000Z"; | |
| 87 | ||
| 88 | /** The dates of one sample deal, as plain data. */ | |
| 89 | export interface SampleDates { | |
| 90 | readonly contractDate: string; | |
| 91 | readonly deadlines: readonly { readonly id: number; readonly due: string; readonly doneAt: string | null }[]; | |
| 92 | readonly moves: readonly { readonly id: number; readonly fromDue: string; readonly toDue: string }[]; | |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Every date of a sample deal moved forward from the day `anchor` to the day | |
| 97 | * `on`, together, so what was overdue stays overdue by as much and what closed | |
| 98 | * in four days still closes in four. The clock is an argument; a day not later | |
| 99 | * than the anchor moves nothing. | |
| 100 | */ | |
| 101 | export function shiftSample(dates: SampleDates, anchor: string, on: string): SampleDates { | |
| 102 | const days = daysBetween(anchor, on); | |
| 103 | if (days <= 0) return dates; | |
| 104 | return { | |
| 105 | contractDate: addDays(dates.contractDate, days), | |
| 106 | deadlines: dates.deadlines.map((d) => ({ | |
| 107 | id: d.id, | |
| 108 | due: addDays(d.due, days), | |
| 109 | doneAt: d.doneAt === null ? null : addDays(d.doneAt.slice(0, 10), days) + d.doneAt.slice(10), | |
| 110 | })), | |
| 111 | moves: dates.moves.map((m) => ({ id: m.id, fromDue: addDays(m.fromDue, days), toDue: addDays(m.toDue, days) })), | |
| 112 | }; | |
| 113 | } | |
| 114 | ||
| 115 | /** What a person could have changed in a sample deal, as plain data; `parties` leaves out the buyer and seller. */ | |
| 116 | export interface SampleState { | |
| 117 | readonly address: string; | |
| 118 | readonly parties: number; | |
| 119 | readonly customs: number; | |
| 120 | readonly reminders: number; | |
| 121 | readonly doneAts: readonly (string | null)[]; | |
| 122 | readonly moveAuthors: readonly string[]; | |
| 123 | } | |
| 124 | ||
| 125 | /** Is this deal still exactly as `seed` wrote it, anchored to `anchor`? */ | |
| 126 | export function isUntouched(state: SampleState, anchor: string): boolean { | |
| 127 | const s = SAMPLES.find((x) => x.address === state.address); | |
| 128 | if (s === undefined) return false; | |
| 129 | const marker = `${addDays(anchor, -1)}${DONE_TIME}`; | |
| 130 | return ( | |
| 131 | state.parties === s.parties.length && | |
| 132 | state.customs === (s.custom?.length ?? 0) && | |
| 133 | state.reminders === 0 && | |
| 134 | state.doneAts.every((d) => d === null || d === marker) && | |
| 135 | state.doneAts.filter((d) => d !== null).length === (s.done?.length ?? 0) && | |
| 136 | state.moveAuthors.length === (s.moved === undefined ? 0 : 1) && | |
| 137 | state.moveAuthors.every((a) => a === SAMPLE.name) | |
| 138 | ); | |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Bring the sample deals nobody has touched up to `on`, then remember `on` as | |
| 143 | * the day they are anchored to. A deal a person changed is left where it is. | |
| 144 | * (A deal with a sent reminder is a changed one, so no reminder row needs to | |
| 145 | * follow a date.) | |
| 146 | */ | |
| 147 | export function refresh(db: Database, on = today()): void { | |
| 148 | db.transaction(() => { | |
| 149 | const row = db.query<{ value: string }, []>("SELECT value FROM app_meta WHERE key = 'sample_anchor'").get(); | |
| 150 | if (row === null || daysBetween(row.value, on) <= 0) return; | |
| 151 | const anchor = row.value; | |
| 152 | const deals = db.query<{ id: number; address: string; contract_date: string }, [string]>( | |
| 153 | "SELECT id, address, contract_date FROM deal WHERE author_id = ?1", | |
| 154 | ).all(SAMPLE.id); | |
| 155 | for (const deal of deals) { | |
| 156 | const deadlines = db.query<{ id: number; kind: string; due: string; done_at: string | null }, [number]>( | |
| 157 | "SELECT id, kind, due, done_at FROM deadline WHERE deal_id = ?1", | |
| 158 | ).all(deal.id); | |
| 159 | const moves = db.query<{ id: number; from_due: string; to_due: string; by_name: string }, [number]>( | |
| 160 | "SELECT m.id, m.from_due, m.to_due, m.by_name FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id WHERE d.deal_id = ?1", | |
| 161 | ).all(deal.id); | |
| 162 | const parties = db.query<{ n: number }, [number]>( | |
| 163 | "SELECT count(*) AS n FROM party WHERE deal_id = ?1 AND role NOT IN ('buyer', 'seller')", | |
| 164 | ).get(deal.id)!.n; | |
| 165 | const reminders = db.query<{ n: number }, [number]>( | |
| 166 | "SELECT count(*) AS n FROM reminder r JOIN deadline d ON d.id = r.deadline_id WHERE d.deal_id = ?1", | |
| 167 | ).get(deal.id)!.n; | |
| 168 | const state: SampleState = { | |
| 169 | address: deal.address, | |
| 170 | parties, | |
| 171 | customs: deadlines.filter((d) => d.kind === "custom").length, | |
| 172 | reminders, | |
| 173 | doneAts: deadlines.map((d) => d.done_at), | |
| 174 | moveAuthors: moves.map((m) => m.by_name), | |
| 175 | }; | |
| 176 | if (!isUntouched(state, anchor)) continue; | |
| 177 | const shifted = shiftSample( | |
| 178 | { | |
| 179 | contractDate: deal.contract_date, | |
| 180 | deadlines: deadlines.map((d) => ({ id: d.id, due: d.due, doneAt: d.done_at })), | |
| 181 | moves: moves.map((m) => ({ id: m.id, fromDue: m.from_due, toDue: m.to_due })), | |
| 182 | }, | |
| 183 | anchor, | |
| 184 | on, | |
| 185 | ); | |
| 186 | db.query("UPDATE deal SET contract_date = ?1 WHERE id = ?2").run(shifted.contractDate, deal.id); | |
| 187 | for (const d of shifted.deadlines) db.query("UPDATE deadline SET due = ?1, done_at = ?2 WHERE id = ?3").run(d.due, d.doneAt, d.id); | |
| 188 | for (const m of shifted.moves) db.query("UPDATE deadline_move SET from_due = ?1, to_due = ?2 WHERE id = ?3").run(m.fromDue, m.toDue, m.id); | |
| 189 | } | |
| 190 | db.query("UPDATE app_meta SET value = ?1 WHERE key = 'sample_anchor'").run(on); | |
| 191 | }).immediate(); | |
| 192 | } | |
| 193 | ||
| 85 | 194 | /** Write the samples into an empty database, once; a database that has had them never gets them again. */ |
| 86 | 195 | export function seed(db: Database, on = today()): void { |
| 87 | 196 | // Asked and written in one write transaction, taken before the question: |
| @@ -113,5 +222,6 @@ export function seed(db: Database, on = today()): void { | ||
| 113 | 222 | } |
| 114 | 223 | } |
| 115 | 224 | db.query("INSERT INTO app_meta (key, value) VALUES ('seeded', ?1)").run(on); |
| 225 | db.query("INSERT INTO app_meta (key, value) VALUES ('sample_anchor', ?1)").run(on); | |
| 116 | 226 | }).immediate(); |
| 117 | 227 | } |
new above
offline