Leads
Save the pipeline to your database: leads and their stage history, winning a lead in one step on the server, pipeline settings, CSV import and leads suggested from email.
What the demo does today
LeadsProvider (src/components/providers/leads-provider.tsx) starts from the sample LEADS in src/lib/data/leads.ts and keeps the leads you create, edit and remove in memory for the session. Its actions are createLead, updateLead, moveStage, markLost, markWon, reopen, removeLeads and restoreLeads. Every stage change is added to the lead's history with when and who.
The tables and the fields of a lead are on the Database page, under Leads data model.
Load and save leads
Load the workspace's leads with their contacts and stage history, and pass them to the provider in place of LEADS.
Make each action call the server, then update the provider's state with what comes back. Keep the toasts and Undo: Undo is the same update with the old values.
A database function (move_lead_stage here) can update the lead, set last_activity_at and add the row to lead_stage_changes together, so the history can't miss a change.
Win a lead on the server
Winning happens in ConvertLeadDialog (src/components/leads/convert-lead-dialog.tsx). Its finish function does the whole conversion in the browser today:
- Creates the customer, unless the lead is more business from an existing one
- Adds the chosen people as contacts; on an existing customer, people already there are matched by email
- Creates a draft contract from the plan, seats, billing and start date
- Moves the lead's tasks, notes and meetings to the new customer
- Marks the lead won with the new
customerIdandcontractId
With a database, send the same choices to one Server Action that does all five steps in one transaction and returns the new ids. If any step fails, nothing is saved, and the dialog can show the error and stay open.
Tasks, notes and meetings on a lead that isn't a customer yet use customerId: -1 (NO_CUSTOMER) and point at the lead. In your tables, a nullable customer_id plus a lead_id does the same job.
Pipeline settings
Stage names, colours and chances, lost reasons and source names are workspace settings (leadStages, lostReasons and sourceLabels in AdminState), saved with the rest of the workspace settings in the browser today. Save them per workspace on the server, for example as a JSON column on your workspaces table, and load them before the app renders so pills don't change colour after the page appears. Customization › Pipeline covers the defaults.
Import and suggested leads
- CSV import and export run in the browser. For large files, upload the CSV and import it on the server, then reload the leads.
- Suggested leads from email. Emails › Suggested records lists companies writing in that aren't customers yet. In the demo the list is sample data (
SEEDED_SUGGESTIONS); adding one as a lead first checksfindByDomainso the same company isn't added twice. With a real mailbox, build the list as mail syncs: senders whose domain matches no customer or lead (see Email). - Website forms. Create leads from your site with a Route Handler that checks a secret, saves the lead with
source: "website"and runs the "Lead is created" workflows (see Workflows).
Numbers and the forecast
Pipeline value, weighted value, win rate, days to close and the funnel are worked out in the browser from all leads by pipelineStats and stageReach in src/lib/data/leads.ts. That's fine for a few thousand leads. Beyond that, work them out in SQL and pass the totals in; Charts and metrics shows the pattern.