-- Document Requests database schema -- Source: https://personalsoftwarefactory.com/blueprints/document-requests (data model, section 5 of the blueprint) -- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC. -- License: MIT. Free to copy and adapt. -- The single settings row. id is fixed at 1 so the table can never hold a second row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), firm_name TEXT NOT NULL, from_email TEXT NOT NULL, retention_days INTEGER NOT NULL DEFAULT 90 CHECK (retention_days >= 1), -- files of completed or cancelled requests are deleted this many days later reminder_first_after_days INTEGER NOT NULL DEFAULT 3 CHECK (reminder_first_after_days >= 1), -- first reminder, in days after sending reminder_every_days INTEGER NOT NULL DEFAULT 4 CHECK (reminder_every_days >= 1), -- later reminders, counted from the first reminder_before_due_days INTEGER NOT NULL DEFAULT 2 CHECK (reminder_before_due_days >= 0), -- one extra reminder this many days before the due date max_reminders INTEGER NOT NULL DEFAULT 6 CHECK (max_reminders >= 0) -- never more reminders than this per request ); -- A person at the firm who signs in: an admin or a staff member. CREATE TABLE staff ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, -- sign-in identity role TEXT NOT NULL CHECK (role IN ('admin', 'staff')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- A reusable list of things to ask a client for. Only admins create and edit templates. -- App rule (R9): editing a template never changes requests already created from it. CREATE TABLE templates ( id TEXT PRIMARY KEY, name TEXT NOT NULL ); -- One item of a template, in order. Stores the template items list. CREATE TABLE template_items ( template_id TEXT NOT NULL REFERENCES templates(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position >= 1), label TEXT NOT NULL, help_text TEXT, kind TEXT NOT NULL CHECK (kind IN ('files', 'text', 'long_text', 'choice', 'date')), options TEXT, -- choice items only: the choices, one per line required BOOLEAN NOT NULL, PRIMARY KEY (template_id, position), CHECK ((kind = 'choice') = (options IS NOT NULL)) ); -- A client business and the one contact who receives the link and the access codes. CREATE TABLE clients ( id TEXT PRIMARY KEY, name TEXT NOT NULL, contact_name TEXT NOT NULL, contact_email TEXT NOT NULL ); -- One request for documents and answers sent to one client. -- App rule (section 7): status moves only by Send, the first client answer, Submit, accepting every required item, Request changes and Cancel. -- App rule (section 6): Submit needs every required item done; the request is completed when every required item is accepted. -- App rule (R4): reminders go only while sent or in progress, at most one a day and never more than max_reminders. -- App rule: a new request takes the highest existing number plus one. CREATE TABLE requests ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 2) = 'R-'), -- e.g. R-0012 client_id TEXT NOT NULL REFERENCES clients(id), title TEXT NOT NULL, owner_id TEXT NOT NULL REFERENCES staff(id), due_on DATE NOT NULL, -- a UTC day status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'sent', 'in_progress', 'submitted', 'completed', 'cancelled')), sent_at TIMESTAMP, submitted_at TIMESTAMP, -- the last time the client pressed Submit completed_at TIMESTAMP, cancelled_at TIMESTAMP, -- retention counts from here for cancelled requests token TEXT NOT NULL UNIQUE, -- random and long; the client link is /r/. Resend link replaces it. token_revoked BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE when this token no longer opens the request reminders_sent INTEGER NOT NULL DEFAULT 0 CHECK (reminders_sent >= 0), last_reminded_on DATE, -- the UTC day of the last reminder, so no day gets two CHECK (status <> 'draft' OR sent_at IS NULL), CHECK (status NOT IN ('sent', 'in_progress', 'submitted', 'completed') OR sent_at IS NOT NULL), CHECK (status <> 'submitted' OR submitted_at IS NOT NULL), CHECK (status <> 'completed' OR completed_at IS NOT NULL), CHECK (status <> 'cancelled' OR cancelled_at IS NOT NULL) ); CREATE INDEX requests_owner ON requests (owner_id, status); CREATE INDEX requests_client ON requests (client_id); -- A staff member watching a request, who can then see and review it. Stores the request watcher_ids list. -- App rule (section 4): only the owner or an admin adds watchers. CREATE TABLE request_watchers ( request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE, staff_id TEXT NOT NULL REFERENCES staff(id), PRIMARY KEY (request_id, staff_id) ); -- One item on one request, copied from the template when the request is created. -- App rule (section 6): an item is done for the client when it has an answer or at least one file, and not done again after changes are requested until the client answers or uploads. -- App rule (R5): only the owner, watchers and admins review. CREATE TABLE request_items ( id TEXT PRIMARY KEY, request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position >= 1), label TEXT NOT NULL, help_text TEXT, kind TEXT NOT NULL CHECK (kind IN ('files', 'text', 'long_text', 'choice', 'date')), options TEXT, -- choice items only: the choices, one per line required BOOLEAN NOT NULL, answer_text TEXT, -- the answer for text, long_text, choice (one of the options) and date (YYYY-MM-DD) items review TEXT NOT NULL DEFAULT 'empty' CHECK (review IN ('empty', 'answered', 'accepted', 'changes_requested')), review_comment TEXT, -- required when changes are requested updated_at TIMESTAMP, UNIQUE (request_id, position), CHECK ((kind = 'choice') = (options IS NOT NULL)), CHECK (kind <> 'files' OR answer_text IS NULL), CHECK (review <> 'changes_requested' OR review_comment IS NOT NULL) ); -- One uploaded file on a files item. -- App rule (section 3): PDF, JPG, PNG, HEIC, CSV, XLSX or DOCX only. -- App rule (R7): retention removes the stored file and sets deleted_at; the row stays, so the item still counts as provided. CREATE TABLE files ( id TEXT PRIMARY KEY, item_id TEXT NOT NULL REFERENCES request_items(id) ON DELETE CASCADE, name TEXT NOT NULL, -- the name the client uploaded size INTEGER NOT NULL CHECK (size >= 0 AND size <= 26214400), -- bytes; at most 25 MB (25 x 1024 x 1024) type TEXT NOT NULL, -- MIME type, e.g. application/pdf storage_key TEXT NOT NULL UNIQUE, -- where the bytes live in private storage uploaded_at TIMESTAMP NOT NULL, deleted_at TIMESTAMP -- set when retention deletes the stored file ); CREATE INDEX files_item ON files (item_id); -- A 6-digit code emailed to the client contact to open the link in a new browser. Stored only as a hash. -- App rule (R1): valid 10 minutes and once; after 5 wrong tries a new code is needed. CREATE TABLE access_codes ( id TEXT PRIMARY KEY, -- added: the spec lists no id, and two codes can share a hash request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE, code_hash TEXT NOT NULL, expires_at TIMESTAMP NOT NULL, used_at TIMESTAMP, attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 5) -- wrong tries so far ); CREATE INDEX access_codes_request ON access_codes (request_id); -- A client browser that entered a valid code. The timeline labels client changes by session. CREATE TABLE client_sessions ( browser_token_hash TEXT PRIMARY KEY, -- hash of the random token kept in that browser request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE, created_at TIMESTAMP NOT NULL ); -- One change, written by every action. Append-only. -- App rule (R10): events are never edited or deleted; every review is kept here. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT NOT NULL, -- a staff id, a client browser session, or the daily job type TEXT NOT NULL, -- e.g. item_accepted, reminder_sent, retention_warning data TEXT -- details of the change as JSON text ); -- One email in the outbox. Every email is written here first, then sent. -- App rule (section 10): a failed email never blocks the action that caused it. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, -- which trigger in section 10, e.g. sent, access_code, reminder to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged when no email provider is configured error TEXT );