Onsidian Help
Lead System

Database Schema

Tables, columns, and relationships in the lead system

leads

The primary table. One row per prospect.

Core fields

ColumnTypeRequiredDefaultNotes
iduuidyesgen_random_uuid()PK
created_attimestamptzyesNOW()
first_nametextyes
last_nametextno
phonetextyes
emailtextno
addresstextno
citytextno
statetextnoUS state abbreviation
ziptextno
languagetextno'en'
sourcetextyes'onsidian' or 'manual'
typetextyesFK to lead_types.id
statustextyes'open''open' or 'resolved'
stagetextno'new'Workflow position — see State Machine

Ownership

ColumnTypeNotes
agency_iduuidFK to agencies.id
agent_iduuidFK to profiles.id — the assigned agent
referral_lead_iduuidSelf-referencing FK — the parent lead that generated this referral
export_iduuidFK to lead_exports.id, ON DELETE SET NULL
external_iduuidID from the source system (for deduplication on import)
is_trainingbooleanTraining leads don't count toward real metrics
last_activity_attimestamptzSet by trigger on every new activity

Demographics — primary insured

ColumnTypeNotes
primary_sextext
primary_agesmallint
primary_incomeinteger
primary_work_coverageintegerEmployer-provided coverage amount
primary_term_coverageinteger
primary_whole_coverageinteger
primary_health_dqbooleanDisqualified for health insurance
primary_life_dqbooleanDisqualified for life insurance
primary_nicotineboolean

Demographics — secondary insured

Same pattern as primary: secondary_first_name, secondary_last_name, secondary_sex, secondary_age, secondary_income, secondary_work_coverage, secondary_term_coverage, secondary_whole_coverage, secondary_health_dq, secondary_life_dq, secondary_nicotine. Plus has_secondary boolean (default false).

Demographics — family

ColumnTypeDefault
num_childrensmallint0
num_grandchildrensmallint0
has_mortgageboolean
has_bank_accountboolean

Outcome / sale fields

These are written by submit_lead_presentation and represent the aggregate outcome.

ColumnTypeNotes
is_salebooleanDefault false
sold_attimestamptzSet atomically by presentation submission
primary_is_trialbooleanTrial sale (free-look period)
primary_alpnumericAnnual Life Premium
primary_ahpnumericAnnual Health Premium
secondary_is_trialboolean
secondary_alpnumeric
secondary_ahpnumeric

RLS policies

  • SELECT/INSERT/UPDATE: agency_id = get_user_agency() OR is_super_user()
  • DELETE: No policy — leads cannot be deleted

Indexes

agent_id, agency_id, status, stage, is_sale, sold_at, external_id, export_id, last_activity_at


lead_activity

Every action taken on a lead is an activity row. This is the audit trail and the trigger source for all automatic state transitions.

ColumnTypeRequiredDefaultNotes
idbigintyesidentityPK, auto-increment
created_attimestamptzyesNOW()
lead_iduuidyesFK to leads.id
agent_iduuidnoFK to profiles.id
typetextyesActivity type (see below)
metadatajsonbnoFlexible payload — replaced the old note column

Activity types

TypeWhen loggedKey metadata
createdLead created
contact_attemptedAgent contacts leadmethod, direction, result, note
callback_requestedLead wants a callbackscheduled_for?, note
appointment_scheduledAppointment bookedappointment_type, is_instant, note
appointment_cancelAppointment cancellednote
appointment_no_showLead didn't shownote
appointment_completedAppointment finishednote
presentation_startedPresentation opened
presentation_pausedPresentation paused
presentation_resumedPresentation resumed
presentation_endedPresentation submitted
saleSale recorded
no_saleNo sale recorded
refusedLead refusednote
wrong_numberWrong numbernote
bad_numberBad/disconnected numbernote
unresponsiveLead stopped respondingnote
duplicateDuplicate leadnote
dncDo Not Contactnote
lead_updatedField changed on leadchanges: [{field, old, new}]
sale_updatedSale detail changedaction, product, insured, premium
noteManual note addednote
assignedLead reassigned

RLS

SELECT/INSERT: lead's agency_id must match the user's agency.


lead_sale_details

Individual policy details for a sale. Multiple rows per lead (one per product sold).

ColumnTypeNotes
idbigintPK, identity
created_attimestamptz
lead_iduuidFK to leads.id, ON DELETE CASCADE
insuredtext'primary', 'secondary', 'child_0', 'child_1', etc.
producttextFK to products.id
amountintegerCoverage amount
premiumnumeric
waiver_of_premiumbooleanDefault false

All rows are deleted and re-inserted atomically by submit_lead_presentation to handle re-submissions cleanly.


calendar_events

Stores all calendar events including lead appointments. Replaced the old appointments table.

ColumnTypeNotes
iduuidPK
agency_iduuidFK to agencies.id
owner_iduuidFK to profiles.id — the agent who owns this event
event_typetext'lead_appointment', 'training', 'meeting', 'other', 'recruit_interview', 'group_interview'
titletext
descriptiontext
starts_attimestamptz
ends_attimestamptz
locationtext
statustext'scheduled', 'completed', 'cancelled', 'no_show', 'rescheduled'
lead_iduuidFK to leads.id, only set for lead_appointment type
rruletextRFC 5545 recurrence rule (null = one-time)
rrule_untiltimestamptzPrecomputed end date for range queries
rrule_exceptionstimestamptz[]Dates to skip
recurring_event_iduuidSelf-referencing FK for exception instances

RLS

  • SELECT: owner or same agency
  • INSERT/UPDATE/DELETE: owner only

lead_exports

Tracks CSV export history for auditing and re-download.

ColumnTypeNotes
iduuidPK
created_attimestamptz
user_iduuidFK to profiles.id — who exported
agency_iduuidFK to agencies.id
lead_countinteger
filtersjsonbThe filter criteria used
filenametext

lead_metrics

One row per lead, auto-maintained by triggers. Used for reporting and analytics.

ColumnTypeNotes
iduuidPK, FK to leads.id ON DELETE CASCADE
contact_attemptsintegerDefault 0
first_contact_attimestamptz
time_to_first_contactinterval
appointments_scheduledintegerDefault 0
appointments_no_showintegerDefault 0
first_appointment_attimestamptz
time_to_first_appointmentinterval
presentations_givenintegerDefault 0
first_presentation_attimestamptz
time_to_first_presentationinterval
outcometextMirrors the terminal activity type
outcome_attimestamptz
time_to_outcomeinterval
time_contact_to_outcomeinterval
time_appointment_to_outcomeinterval

lead_reports / lead_report_metrics

Daily activity roll-ups per agent. lead_reports has one row per (agent, date). lead_report_metrics breaks it down further by (report, lead_type, state).

lead_reports

ColumnTypeNotes
idbigintPK
agent_iduuidFK to profiles.id
datedateUnique with agent_id
contacts_madeintegerAuto-tracked
leads_workedintegerAuto-tracked
leads_reachedintegerAuto-tracked
appts_scheduledintegerAuto-tracked
appts_instantintegerAuto-tracked
recruitsintegerAuto-tracked
manual_contacts_madeintegerAgent self-reported
manual_leads_workedintegerAgent self-reported
manual_leads_reachedintegerAgent self-reported
manual_appts_scheduledintegerAgent self-reported
manual_appts_instantintegerAgent self-reported

lead_report_metrics

ColumnTypeNotes
report_idbigintFK to lead_reports.id
lead_typetextFK to lead_types.id
statetextUS state
presentationsinteger
salesinteger
trial_salesinteger
referralsinteger
alpnumeric
trial_alpnumeric
ahpnumeric
trial_ahpnumeric

Unique constraint: (report_id, lead_type, state).


Reference tables

lead_types

Defines the valid lead types. Current values: fwk, csk, br, union, ncr, pos, dcard, fe, globe.

products

Defines the insurance products that can be sold. Current values: fe, ip, adb, t4, t20, t65, t100, lpu65, dt15, dt30, c20, gio, a71, cnm, cr.

On this page