--- id: crm title: Small CRM web app (two phases) kind: create expected_rounds: 60-110 --- # Small CRM web app (two phases) Existing task of docs/e2e-run.md (jobs 945*), prompt text unchanged from `scripts/e2e/codex/prompt-crm.txt` (sha256 2ab4d92db393334f1ac953086116cefb9744b522b98bfd4ea6efd7e02e319af9 of the file). Empty seed. Acceptance: `accept.py` = `crm_acceptance.py` (21 checks, REST API plus the app's own unittest suite) run on a copy of the workspace. ## Prompt Build a small CRM web application in the current directory using ONLY Python 3 standard library modules (http.server, sqlite3, json, unittest, urllib): there is no network and no package can be installed. Layout and commands (fixed, an acceptance script will rely on them): - server.py: running `python3 server.py` starts an HTTP server on the port in the environment variable PORT (default 8000), bound to 127.0.0.1, with data stored in the SQLite file given by the environment variable CRM_DB (default crm.db). It creates the tables on first start. - tests/: a unittest suite; `python3 -m unittest discover -s tests` must run offline and pass. Cover every endpoint. - README.md: how to run, the API, and the data model. - static/index.html (served at GET /): a single-page front-end (plain HTML + JavaScript, no libraries) with a companies list, a contacts list, a deals board with one column per stage, a search box that filters the lists, and forms to create, edit and delete records and to add notes to a deal. REST API (JSON bodies and responses, Content-Type application/json): - Resources: /api/companies (name required, website, industry), /api/contacts (name required, email, phone, company_id optional), /api/deals (title required, amount number default 0, stage, company_id optional, contact_id optional), notes: GET and POST /api/deals/{id}/notes (body required). - For each of companies, contacts, deals: GET /api/ lists all records (supports ?q=text, a case-insensitive substring filter on the name/title/email fields, and for deals also ?stage=); POST /api/ creates and returns 201 with the record including its integer id; GET /api//{id}; PUT /api//{id} replaces the editable fields and returns 200 with the record; DELETE /api//{id} returns 204. Unknown ids return 404 with {"error": "..."}; invalid input returns 400 with {"error": "..."}. - Deal stages are exactly: lead, qualified, proposal, won, lost (default lead). PUT with another stage value returns 400. Deleting a company or contact must not delete deals; it clears the reference. - GET /api/deals?stage=won&q=... combines both filters. GET /api/pipeline returns {"lead": {"count": n, "amount": total}, ...} for every stage. Work step by step, run the tests and fix failures. When all tests pass and the server starts and answers the API, reply with a short summary. Phase 2 (start only after phase 1 passes; keep every phase-1 behaviour and endpoint unchanged, the acceptance script re-runs them). This phase is a long refactor-and-extend job, work through it item by item and re-run the whole test suite after every item. Before editing a file read all of it; before you change behaviour read the tests that cover it. 1. Split server.py into modules (for example db.py, models.py, handlers.py, router.py) while `python3 server.py` keeps working with the same environment variables. 2. Pagination: every list endpoint accepts ?limit=N&offset=M (defaults unchanged when absent) and returns the total count in an X-Total-Count header. 3. CSV export: GET /api/companies.csv, /api/contacts.csv, /api/deals.csv with a header row and proper quoting. 4. An activity log table written on every create, update and delete, and GET /api/activity?limit=N returning the newest entries first. 5. Input validation helpers shared by all handlers, with unit tests for every rule (types, lengths up to 200 characters for names, email shape, amount >= 0). 6. Front-end: add a deal detail panel showing notes and the activity of that deal, and a keyboard shortcut / to focus the search box. 7. Write CODE_REVIEW.md: read every source and test file once more, list the weaknesses you find, fix the three most important ones, and document them. When everything passes, reply with a short summary.