LeadSmart API uses PostgreSQL as the database and Sequelize as the ORM (Object-Relational Mapping) tool. This document outlines the database structure, models, and relationships.Database Configuration#
The database configuration is managed through Sequelize and defined in the .sequelizerc
file and src/config/sequelize.config.ts
.Entities and Relationships#
Core Entities#
User#
The User
model represents system users.email
: User's email address (unique)
password
: Hashed password
role
: User role (admin, user)
active
: User account status
One-to-many with Member
(user can be a member of multiple companies)
One-to-many with RefreshToken
(user can have multiple refresh tokens)
One-to-many with PendingUser
(for invitations)
Company#
The Company
model represents organizations in the system.description
: Company description
One-to-many with Member
(company can have multiple members)
One-to-many with Agenda
(company can have multiple agendas)
One-to-many with Client
(company can have multiple clients)
One-to-many with CompanyRole
(company can define multiple roles)
Member#
The Member
model represents a user's membership in a company.userId
: Reference to User
companyId
: Reference to Company
companyRoleId
: Reference to CompanyRole
active
: Membership status
Many-to-one with User
(member belongs to a user)
Many-to-one with Company
(member belongs to a company)
Many-to-one with CompanyRole
(member has a role in the company)
One-to-many with Appointment
(member can have multiple appointments)
Client#
The Client
model represents clients of a company.phone
: Client phone number
companyId
: Reference to Company
Many-to-one with Company
(client belongs to a company)
One-to-many with ClientThread
(client can have multiple conversation threads)
One-to-many with Appointment
(client can have multiple appointments)
Appointment Management#
Agenda#
The Agenda
model represents appointment calendars.description
: Agenda description
companyId
: Reference to Company
googleCalendarId
: Optional Google Calendar ID
color
: Calendar color code
Many-to-one with Company
(agenda belongs to a company)
One-to-many with Appointment
(agenda can have multiple appointments)
Appointment#
The Appointment
model represents scheduled meetings.description
: Appointment description
startDate
: Start date and time
endDate
: End date and time
status
: Appointment status (scheduled, completed, cancelled)
agendaId
: Reference to Agenda
clientId
: Reference to Client
googleEventId
: Optional Google Calendar event ID
Many-to-one with Agenda
(appointment belongs to an agenda)
Many-to-one with Client
(appointment is with a client)
Many-to-many with Member
through AppointmentMember
(appointment has multiple members)
Messaging#
MessagingInstance#
The MessagingInstance
model represents WhatsApp messaging instances.companyId
: Reference to Company
status
: Connection status
Many-to-one with Company
(messaging instance belongs to a company)
ClientThread#
The ClientThread
model represents messaging threads with clients.clientId
: Reference to Client
messagingInstanceId
: Reference to MessagingInstance
lastMessage
: Last message content
lastMessageDate
: Last message timestamp
unreadCount
: Number of unread messages
Many-to-one with Client
(thread is with a client)
Many-to-one with MessagingInstance
(thread uses a messaging instance)
Plans and Subscriptions#
PlanType#
The PlanType
model represents subscription plan types.description
: Plan description
One-to-many with PlanDuration
(plan type can have multiple durations)
Many-to-many with Company
(companies can subscribe to plans)
PlanDuration#
The PlanDuration
model represents subscription duration options.planTypeId
: Reference to PlanType
months
: Duration in months
discount
: Discount percentage
Many-to-one with PlanType
(duration belongs to a plan type)
Integration Models#
IntegrationToken#
The IntegrationToken
model stores tokens for external service integrations.companyId
: Reference to Company
service
: Service name (google, chatwoot, etc.)
accessToken
: Access token
refreshToken
: Refresh token
expiry
: Token expiration date
Many-to-one with Company
(token belongs to a company)
GoogleWebhook#
The GoogleWebhook
model tracks Google Calendar webhook subscriptions.companyId
: Reference to Company
calendarId
: Google Calendar ID
resourceId
: Google resource ID
channelId
: Google channel ID
expiration
: Webhook expiration date
Many-to-one with Company
(webhook belongs to a company)
OpenAICred#
The OpenAICred
model stores OpenAI API credentials.companyId
: Reference to Company
organization
: OpenAI organization ID
active
: Credential status
Many-to-one with Company
(credentials belong to a company)
One-to-many with OpenAIBots
(credentials can be used by multiple bots)
OpenAIBots#
The OpenAIBots
model represents OpenAI-powered chatbot configurations.companyId
: Reference to Company
openaiCredId
: Reference to OpenAICred
temperature
: Model temperature setting
systemPrompt
: System prompt for the bot
Many-to-one with Company
(bot belongs to a company)
Many-to-one with OpenAICred
(bot uses OpenAI credentials)
Database Schema Diagram#
The database schema follows a relational design with normalized tables and proper foreign key relationships. The main entity clusters are:1.
User Management: User, Member, CompanyRole
2.
Company Management: Company, Client
3.
Appointment Management: Agenda, Appointment, AppointmentMember
4.
Messaging: MessagingInstance, ClientThread
5.
Plans and Billing: PlanType, PlanDuration
6.
Integrations: IntegrationToken, GoogleWebhook, OpenAICred, OpenAIBots
Modificado em 2025-04-24 22:32:03