# OB_WORKFLOW: new_customer.crm_conversion
version: 0.3-draft (2026-10-11). product: OrangeBoard (OB) PSA, https://ob.standardcomputer.com
goal: org that responded to marketing outreach (Stage Prospect/MQL/Lead) -> Client (Onboarding).
actor: marketing/sales staff with CRM access. Convert to client is sales-all or admin.
## MODEL
- One organizations table. CRM side = Stage {Prospect, MQL (Marketing Qualified Lead), Lead, Lost}; also Vendor, Former Client (not convertible). Clients side = Relationship {Onboarding, Managed Client}. "Client" = any customer organization. Customer vs pre-customer is computed isCustomer. There is no /customers noun and no /api/v1/customers route.
- Crossing pre-customer -> customer is the audited UI action "Convert to client" only (sales-all or admin). Server action convertToClientAction -> convertOrgToClient. Optional QBO, contract-from-deal, and onboarding-ticket steps run inside that action. Not undone by a stage edit; people/notes/deals stay on record. No REST endpoint. PATCH /api/v1/clients/{id} that would cross pre-customer -> customer returns 422: "This stage change would make a pre-customer org a client. Use Convert to client (CRM)…".
- Convert dialog "Convert \"{org}\" to client": checkboxes "Create/link QBO customer" and "Open onboarding ticket" (both on by default). Admin defaults: /settings/crm "Conversion defaults" (QBO create/link, contract from deal MRR/term/renewal, onboarding ticket + template default "Plain onboarding ticket", Landing stage: Auto (onboarding → customer) / Managed Client / Onboarding, notify on conversion).
- Non-convertible: Vendor ("Not a sales stage"), Archived, Former Client. "Needs cleanup" flagged orgs require explicit human acknowledgement.
- UI label "Practice name*" = organization name (literal label; applies to any business).
- CRM module off: /crm pages and the CRM API are disabled (redirect to /console, including admins). "+ Client" / "+ Add client" stay for ADMIN. Header CRM pill stays. Ticket overflow "Convert to CRM lead…" can still show and fails at submit with "The CRM module is turned off (Admin → CRM)." This workflow needs the CRM module on.
- Until the Add-client stage fix ships: do not create a pre-customer with "+ Add client" Relationship Lead/MQL/Prospect (those save isCustomer true and skip Convert). Use "+ New lead", CSV import, or POST /api/v1/crm/leads.

## API (confirmed 2026-10-11)
- base: https://ob.standardcomputer.com/api/v1. Header: Authorization: Bearer, plus the key. Key string starts with the prefix obk_. Shown once at creation (Admin → Integrations → API keys); stored hashed. Content-Type: application/json. Never print a real key.
- Write implies read within an area. 13 areas: tickets:read/write, clients:read/write (contacts ride on clients — no contacts scope), sites:read/write, devices:read/write, users:read (no users:write), mentions:read/write, webhooks:read/write, reports:read, projects:read/write, crm_leads:read/write (label "CRM: Leads (intake)"), crm_prospects:read/write, kb:read/write, kb_secrets:read. No quotes scope.
- Error body is {"error":"..."}: 400 malformed JSON or schema ("Invalid request: field: message"); 401 missing/invalid/revoked/expired key; 403 missing scope (names it); 422 business rule; 429 + Retry-After on rate-limited CRM keys; 503 API kill switch off, or CRM module/intake/researcher API off on CRM routes.
- Phone: omit or "" = no number; JSON null = 400.
- POST /api/v1/clients (clients:write) creates customer-side orgs only. Required name. Optional phone, website, relationship, industry, notes, address_line_1, address_line_2, city, county, postcode, country, main_site_name. Use county, not state (a state key is dropped silently). Pre-customer relationship here is 422. GET /clients, /contacts, and /sites return customer-side rows unless ?include_pre_customer=true is sent with crm_prospects:read (otherwise 403).
- POST /api/v1/crm/leads (crm_leads:write): required practice_name (alias name). Optional contact_name, contact_email, contact_phone, website, address_line_1, address_line_2, city, state, postcode, org_npi, notes, source, source_detail, research_notes, confidence 0–1, external_id, idempotency_key. No country. contact_email not validated and contact_phone not shape-checked at intake. 201, or 200 created:false on idempotency replay. Response may omit address/notes. dedupe_state: clean | customer_match | precustomer_match | fuzzy. status: pending | promoting | approved | merged | rejected. Trusted key clearing confidence + dedupe_state clean can auto-approve (auto_approved:true, client_id). 503 when CRM module, intake queue, or researcher API is off.
- POST /api/v1/sites (sites:write): required client_id, name. Optional phone, address_line_1, address_line_2, city, state, postcode, country. 201 does not echo address. Unknown client → 422 {"error":"Unknown client."} (not 404). Pre-customer parent without crm_prospects:write → 422 (not 403). Phone "" ok, null 400.
- POST /api/v1/contacts (clients:write): required client_id, name. Optional site_id, email (RFC-validated), phone, mobile, job_title, role, is_primary. 201 legacy names (emailaddress, phonenumber); site_id not echoed. Same 422 traps, plus 422 if the site is not under the client.
- Quote accept does not convert (quote-only handler; no quotes scope). See the quote-conversion file.
- CSV wizard headers are camelCase. This leads API is snake_case. Do not conflate them.

## RULES
- Search /crm/orgs and /clients for duplicates before creating or converting. On possible match: stop, ask human.
- Never convert a Needs cleanup org without explicit human approval.
- Show the human every create/convert/send action before executing; conversions are not reversible by you.
- In UI dialogs, nothing happens until the submit button; if only observing, press Cancel.
- Never output client names, contacts, PHI, or API keys. Use fictional examples (Example Dental, front-desk@example.com).
- Report partial results verbatim: converted / skipped / failed / "converted but QBO errored — fix from the client page".
- Leave Convert to client and Log touch to a human in the UI. Log touch has no confirmed REST endpoint.
- Replies from known contacts are captured by helpdesk email intake as inbound email on the client record; use Log touch for calls and anything intake didn't capture.

## UI_STEPS
1. Outreach: /crm/lists/{id} -> "✉ Outreach" drawer. Channel Email (SMS/Letter/Postcard marked soon). Send as (shared mailbox), Recipients per client ("Primary contacts only" / "Every contact with an email"), "Only members with a contact with email", Subject (tokens {{client}}, {{contact}}), Message. Buttons Close / Preview recipients / Send. Do-not-contact excluded automatically. Sending requires human approval. The send stores no thread key (random sourceRef), so a later reply cannot be correlated to this outreach email.
2. Log the reply. Suggested wording to follow: "Replies from known contacts are captured by helpdesk email intake as inbound email on the client record; use Log touch for calls and anything intake didn't capture."
   Automatic capture is incidental, not outreach-aware. It happens only when the reply lands in an intake-enabled support mailbox AND the sender resolves (known contact email, or an explicit trusted-sender entry — bare domain matching is not used). Then helpdesk email-intake logs an inbound EMAIL communication on the org, Last activity shows "Email in", and it appears in the communication log (/clients/{id}/communications). It does NOT appear in the recent-touches strip (manual touches only). It never attaches to a deal or campaign. Queue-stage (unpromoted) leads never receive one. Side effect: the same pass opens or appends a support ticket (intake has no sales-reply notion). If the outreach From mailbox is not intake-enabled, the reply is not captured.
   Manual: /clients/{id} Sales card "Log touch" -> "Log touch — {org}": TYPE Call/Email/Meeting/Note, WHEN (default now), CONTACT, NOTES. Buttons Log touch / Cancel. Updates LAST ACTIVITY. Log touch is strictly manual.
3. Org missing: /crm/orgs "+ New lead" -> "New lead": Practice name*, Contact name, Contact email, Contact phone, Website, City, State, Source, Source detail, Notes. Buttons Cancel / Add lead. No Owner field.
   Bulk CSV: /crm/intake/import. No downloadable template file. 3-step column-mapping wizard: upload any comma-delimited CSV with a header row, map headers onto 17 canonical fields, commit. Only practiceName is required. Nothing is saved until commit. Every row lands PENDING — import never bulk-promotes.
   Canonical header line:
   practiceName,contactName,contactEmail,contactPhone,website,addressLine1,addressLine2,city,state,postcode,orgNpi,notes,source,sourceDetail,researchNotes,externalId,confidence
   Rules: comma-delimited only (no semicolon/tab autodetect); RFC-4180 quoted fields; row 1 headers; max 5,000 data rows; confidence is the only typed field (number 0–1); unmapped columns ignored; country cannot be imported (and cannot be set via the leads API). Auto-mapping is case/underscore/hyphen-insensitive and uses synonyms. addressLine2 and sourceDetail do not auto-suggest — map them by hand. UI "mapping template" = a saved header-to-field map, not a CSV file. CSV names are camelCase; POST /api/v1/crm/leads is snake_case.
4. /crm/orgs default "Pre-customer organizations". Stage chips Prospect, MQL, Lead, Lost. Sortable headers. Bulk bar: "Change stage to", "Add to list", "Assign owner", "Export".
5a. Single: row "Convert to client" -> dialog (see MODEL) -> "Convert to client". No Convert on the org card. Human only.
5b. Bulk (2 or more ticked): "Convert selected…" -> "Convert N selected orgs to clients, landing on Onboarding". Already-client skipped and reported; flagged refused and named.
6. Result banner: converted / skipped / failed / "converted but QBO errored — fix from the client page"; non-clean => "Partial".
7. Verify: /clients left rail "Onboarding".

## API_STEPS
- Create a pre-customer: POST /api/v1/crm/leads with crm_leads:write and the snake_case body in MODEL. Confirm with a human first. Do not POST /api/v1/clients for a lead.
- Sites and contacts under a pre-customer also need crm_prospects:write or the API returns 422.
- Log touch, outreach send, and Convert to client: no REST endpoint. Convert is a deliberate UI gate. A crossing PATCH returns 422.
