Operion Database Documentation
Opsnexa Database Documentation
| Database | opsnexa (PostgreSQL 18, schema public) |
| Tables | 34 (33 business tables + 1 EF Core migration table) |
| Extensions | pgcrypto |
| Stored routines | 2 functions, 2 procedures (one procedure has two overloads) |
| Source | pg_dump taken 2026-10-02 |
1. Overview
Opsnexa is a multi-tenant business-operations application. Each tenant is an organization; users, employees, customers, services, income, expenses, accounts and payments all belong to one organization. Behaviour that varies per business (dropdown values, roles, departments and so on) is driven by reference tables that mix system-defined rows (shared by every organization) with organization-defined rows.
The database has seven functional areas:
| S.No | Module | Tables |
|---|---|---|
| 1 | Tenancy, Users & Access | organizations, users, user_security, user_sessions, user_preferences, roles, user_roles, permissions, role_permissions, organization_invitations, pending_signups |
| 2 | Master & Reference Data | master_categories, master_values, departments, designations, work_locations |
| 3 | HR | employees, employee_photos, employee_documents, attendance, wages |
| 4 | Customers, Services & Income | customers, services, income |
| 5 | Expenses | ExpenseCategories, ExpensePayees, Expense |
| 6 | Accounts & Payments | organizationaccount, bankaccountdetails, debitcarddetails, creditcarddetails, walletdetails, payment |
| 7 | System | __EFMigrationsHistory |
2. Design Conventions
- Multi-tenancy. Business tables carry
organization_id(uuid). Many also store a denormalizedorg_code; where a composite foreign key(organization_id, org_code) → organizations(id, code)exists, it guarantees the two always match (used oncustomers,employees,designations,services,master_categories). - System vs. organization rows.
departments,designations,roles,master_categories,master_valuesandwork_locationshold shared rows (is_system_defined = true,organization_idNULL) and organization rows. Ondepartmentsanddesignations, check constraints enforce this scoping, and partial unique indexes keep codes and names unique within each scope. - Lookup values. Dropdown-style columns (gender, currency, payment mode, tax and so on) are stored as
*_idforeign keys tomaster_values. Eachmaster_valuesrow belongs to amaster_categoriesrow. Section 6 lists which category each column uses. - Soft delete. Tables use
deleted_at(timestamp) oris_deleted(boolean) instead of removing rows. Unique indexes are usually partial (WHERE deleted_at IS NULL) so deleted rows do not block re-use of codes or emails. - Audit columns. Most tables have
created_at,updated_at,created_by,updated_by. The*_bycolumns hold ausers.id(bigint); only some tables enforce this with a foreign key (see Section 9). - Status columns. Status values are restricted by CHECK constraints (Section 7).
- Encrypted data. Account-detail tables store sensitive values in
*encryptedcolumns, with a matching*hashcolumn for lookups. Card numbers are stored as a token plus a hash and the last four digits only. - Identifiers. Core business tables use
uuidprimary keys (gen_random_uuid()). Reference and security tables (users,roles,departments,master_*,user_sessionsand so on) usebigintkeys generated from sequences. - Naming. The EF-created expense tables use PascalCase (
Expense,ExpenseCategories,ExpensePayees). The account/payment tables use lowercase without underscores (organizationaccount,payment). All other tables use snake_case. - Binary files. Employee photos and documents are stored in the database as
bytea.
3. Entity-Relationship Diagrams
The diagrams are written in Mermaid. Foreign keys to users that only record who created/updated/deleted a row are left out to keep the diagrams readable, and foreign keys to master_values are drawn as one line per table, labelled with the number of lookup columns (e.g. lookups(6)). Section 5 lists every column.
Legend: ||--o{ one-to-many, ||--o| one-to-one, |o--o{ optional parent. PK primary key, FK foreign key, UK unique column. Tables drawn with only a primary key belong to another module and are shown for context.
Overview (all tables, relationships only)
erDiagram
organizations |o--o{ ExpenseCategories : "OrganizationId"
organizations ||--o{ ExpensePayees : "OrganizationId"
organizationaccount |o--o{ Expense : "AccountId"
ExpenseCategories ||--o{ Expense : "CategoryId"
organizations ||--o{ Expense : "OrganizationId"
ExpensePayees ||--o{ Expense : "VendorPayeeId"
employees ||--o{ attendance : "employee_id"
organizations ||--o{ attendance : "organization_id"
organizations ||--o{ customers : "organization_id"
organizations |o--o{ designations : "organization_id"
employees ||--o{ employee_documents : "employee_id"
employees ||--o| employee_photos : "employee_id"
departments |o--o{ employees : "department_id"
designations ||--o{ employees : "designation_id"
organizations ||--o{ employees : "organization_id"
employees |o--o{ employees : "reporting_manager_id"
work_locations |o--o{ employees : "work_location_id"
organizationaccount |o--o{ income : "account_id"
customers ||--o{ income : "customer_id"
organizations ||--o{ income : "organization_id"
services |o--o{ income : "service_id"
users ||--o{ organization_invitations : "invited_by"
organizations ||--o{ organization_invitations : "organization_id"
roles |o--o{ organization_invitations : "role_id"
organizations |o--o{ master_categories : "organization_id"
master_categories ||--o{ master_values : "master_category_id"
organization_invitations |o--o{ pending_signups : "invitation_id"
organizations |o--o{ pending_signups : "organization_id"
roles |o--o{ pending_signups : "role_id"
permissions ||--o{ role_permissions : "permission_id"
roles ||--o{ role_permissions : "role_id"
organizations |o--o{ roles : "organization_id"
organizations ||--o{ services : "organization_id"
organizations ||--o{ user_preferences : "organization_id"
users ||--o| user_preferences : "user_id"
roles ||--o{ user_roles : "role_id"
users ||--o{ user_roles : "user_id"
users ||--o| user_security : "user_id"
users ||--o{ user_sessions : "user_id"
organizations ||--o{ users : "organization_id"
organizationaccount ||--o| bankaccountdetails : "organizationaccountid"
organizationaccount ||--o| creditcarddetails : "organizationaccountid"
organizationaccount ||--o| debitcarddetails : "organizationaccountid"
organizations ||--o{ organizationaccount : "organizationid"
organizations ||--o{ payment : "organizationid"
organizationaccount ||--o| walletdetails : "organizationaccountid"
master_values ||--o{ Expense : "lookups(3)"
master_values ||--o{ customers : "lookups(5)"
master_values ||--o{ employees : "lookups(6)"
master_values ||--o{ income : "lookups(4)"
master_values ||--o{ services : "lookups(7)"
1. Tenancy, Users & Access
erDiagram
Expense {
uuid Id PK
}
ExpenseCategories {
uuid Id PK
}
ExpensePayees {
uuid Id PK
}
attendance {
uuid id PK
}
customers {
uuid id PK
}
designations {
bigint id PK
}
employees {
uuid id PK
}
income {
uuid id PK
}
master_categories {
bigint id PK
}
organization_invitations {
uuid id PK
uuid organization_id FK
bigint invited_by FK
varchar token_hash UK
bigint role_id FK
}
organizationaccount {
uuid id PK
}
organizations {
uuid id PK
varchar code UK
}
payment {
uuid id PK
}
pending_signups {
uuid id PK
uuid invitation_id FK
uuid organization_id FK
bigint role_id FK
}
permissions {
bigint id PK
varchar name UK
}
role_permissions {
bigint role_id PK,FK
bigint permission_id PK,FK
}
roles {
bigint id PK
uuid organization_id FK
}
services {
uuid id PK
}
user_preferences {
uuid id PK
bigint user_id FK,UK
uuid organization_id FK
}
user_roles {
bigint id PK
bigint user_id FK
bigint role_id FK
}
user_security {
bigint user_id PK,FK
}
user_sessions {
bigint id PK
bigint user_id FK
varchar refresh_token_hash UK
}
users {
bigint id PK
uuid organization_id FK
}
organizations |o--o{ ExpenseCategories : "OrganizationId"
organizations ||--o{ ExpensePayees : "OrganizationId"
organizations ||--o{ Expense : "OrganizationId"
organizations ||--o{ attendance : "organization_id"
organizations ||--o{ customers : "organization_id"
organizations |o--o{ designations : "organization_id"
organizations ||--o{ employees : "organization_id"
organizations ||--o{ income : "organization_id"
users ||--o{ organization_invitations : "invited_by"
organizations ||--o{ organization_invitations : "organization_id"
roles |o--o{ organization_invitations : "role_id"
organizations |o--o{ master_categories : "organization_id"
organization_invitations |o--o{ pending_signups : "invitation_id"
organizations |o--o{ pending_signups : "organization_id"
roles |o--o{ pending_signups : "role_id"
permissions ||--o{ role_permissions : "permission_id"
roles ||--o{ role_permissions : "role_id"
organizations |o--o{ roles : "organization_id"
organizations ||--o{ services : "organization_id"
organizations ||--o{ user_preferences : "organization_id"
users ||--o| user_preferences : "user_id"
roles ||--o{ user_roles : "role_id"
users ||--o{ user_roles : "user_id"
users ||--o| user_security : "user_id"
users ||--o{ user_sessions : "user_id"
organizations ||--o{ users : "organization_id"
organizations ||--o{ organizationaccount : "organizationid"
organizations ||--o{ payment : "organizationid"
2. Master & Reference Data
erDiagram
departments {
bigint id PK
}
designations {
bigint id PK
uuid organization_id FK
varchar org_code FK
}
employees {
uuid id PK
}
master_categories {
bigint id PK
uuid organization_id FK
varchar org_code FK
}
master_values {
bigint id PK
bigint master_category_id FK
}
organizations {
uuid id PK
}
work_locations {
bigint id PK
}
organizations |o--o{ designations : "organization_id"
departments |o--o{ employees : "department_id"
designations ||--o{ employees : "designation_id"
work_locations |o--o{ employees : "work_location_id"
organizations |o--o{ master_categories : "organization_id"
master_categories ||--o{ master_values : "master_category_id"
3. HR
erDiagram
attendance {
uuid id PK
uuid organization_id FK
uuid employee_id FK
}
departments {
bigint id PK
}
designations {
bigint id PK
}
employee_documents {
uuid id PK
uuid employee_id FK
}
employee_photos {
uuid employee_id PK,FK
}
employees {
uuid id PK
uuid organization_id FK
varchar org_code FK
bigint gender_id FK
bigint marital_status_id FK
bigint blood_group_id FK
bigint department_id FK
bigint designation_id FK
bigint employment_type_id FK
uuid reporting_manager_id FK
bigint work_location_id FK
bigint pay_type_id FK
bigint shift_type_id FK
}
master_values {
bigint id PK
}
organizations {
uuid id PK
}
wages {
uuid id PK
}
work_locations {
bigint id PK
}
employees ||--o{ attendance : "employee_id"
organizations ||--o{ attendance : "organization_id"
employees ||--o{ employee_documents : "employee_id"
employees ||--o| employee_photos : "employee_id"
departments |o--o{ employees : "department_id"
designations ||--o{ employees : "designation_id"
organizations ||--o{ employees : "organization_id"
employees |o--o{ employees : "reporting_manager_id"
work_locations |o--o{ employees : "work_location_id"
master_values ||--o{ employees : "lookups(6)"
4. Customers, Services & Income
erDiagram
customers {
uuid id PK
uuid organization_id FK
varchar org_code FK
bigint customer_type_id FK
bigint industry_id FK
bigint customer_group_id FK
bigint payment_terms_id FK
bigint payment_method_id FK
}
income {
uuid id PK
uuid organization_id FK
uuid customer_id FK
uuid service_id FK
bigint category_id FK
bigint currency_id FK
bigint payment_mode_id FK
uuid account_id FK
bigint payment_status_id FK
}
master_values {
bigint id PK
}
organizationaccount {
uuid id PK
}
organizations {
uuid id PK
}
services {
uuid id PK
uuid organization_id FK
varchar org_code FK
bigint category_id FK
bigint sub_category_id FK
bigint pricing_type_id FK
bigint unit_of_measurement_id FK
bigint tax_id FK
bigint applicable_for_id FK
bigint discount_type_id FK
}
organizations ||--o{ customers : "organization_id"
organizationaccount |o--o{ income : "account_id"
customers ||--o{ income : "customer_id"
organizations ||--o{ income : "organization_id"
services |o--o{ income : "service_id"
organizations ||--o{ services : "organization_id"
master_values ||--o{ customers : "lookups(5)"
master_values ||--o{ income : "lookups(4)"
master_values ||--o{ services : "lookups(7)"
5. Expenses
erDiagram
Expense {
uuid Id PK
uuid OrganizationId FK
uuid CategoryId FK
uuid VendorPayeeId FK
bigint CurrencyId FK
bigint PaymentModeId FK
uuid AccountId FK
bigint PaymentStatusId FK
}
ExpenseCategories {
uuid Id PK
uuid OrganizationId FK
}
ExpensePayees {
uuid Id PK
uuid OrganizationId FK
}
master_values {
bigint id PK
}
organizationaccount {
uuid id PK
}
organizations {
uuid id PK
}
organizations |o--o{ ExpenseCategories : "OrganizationId"
organizations ||--o{ ExpensePayees : "OrganizationId"
organizationaccount |o--o{ Expense : "AccountId"
ExpenseCategories ||--o{ Expense : "CategoryId"
organizations ||--o{ Expense : "OrganizationId"
ExpensePayees ||--o{ Expense : "VendorPayeeId"
master_values ||--o{ Expense : "lookups(3)"
6. Accounts & Payments
erDiagram
Expense {
uuid Id PK
}
bankaccountdetails {
uuid id PK
uuid organizationaccountid FK,UK
}
creditcarddetails {
uuid id PK
uuid organizationaccountid FK,UK
}
debitcarddetails {
uuid id PK
uuid organizationaccountid FK,UK
}
income {
uuid id PK
}
organizationaccount {
uuid id PK
uuid organizationid FK
}
organizations {
uuid id PK
}
payment {
uuid id PK
uuid organizationid FK
}
walletdetails {
uuid id PK
uuid organizationaccountid FK,UK
}
organizationaccount |o--o{ Expense : "AccountId"
organizationaccount |o--o{ income : "account_id"
organizationaccount ||--o| bankaccountdetails : "organizationaccountid"
organizationaccount ||--o| creditcarddetails : "organizationaccountid"
organizationaccount ||--o| debitcarddetails : "organizationaccountid"
organizations ||--o{ organizationaccount : "organizationid"
organizations ||--o{ payment : "organizationid"
organizationaccount ||--o| walletdetails : "organizationaccountid"
An interactive version of all diagrams is in opsnexa_er_diagrams.html.
4. Table Index
| Table | Module | Purpose |
|---|---|---|
organizations | Tenancy, Users & Access | The tenant (company). Holds identity, contact/address, GSTIN/PAN, base currency, fiscal-year start, time zone and invoice prefix. code is unique and is copied into org_code on many child tables. |
users | Tenancy, Users & Access | Application login accounts. Each user belongs to one organization; email is unique among non-deleted users. |
user_security | Tenancy, Users & Access | One-to-one security state for a user: failed-login lockout, password expiry/reset, and email-verification tokens (hashed). |
user_sessions | Tenancy, Users & Access | Refresh-token sessions per user/device, with expiry, last activity and revocation time. |
user_preferences | Tenancy, Users & Access | Per-user UI preferences (currently page size) scoped by organization and preference type. |
roles | Tenancy, Users & Access | Roles used for access control. System roles (organization NULL) are shared by all tenants; organizations may define their own. |
user_roles | Tenancy, Users & Access | Assigns roles to users, with assigner and optional expiry. |
permissions | Tenancy, Users & Access | Catalog of named permissions. |
role_permissions | Tenancy, Users & Access | Many-to-many mapping between roles and permissions. |
organization_invitations | Tenancy, Users & Access | Email invitations to join an organization, with hashed token, role to assign, expiry and accepted/revoked timestamps. |
pending_signups | Tenancy, Users & Access | Staging table for self-signup and invited signup until the email OTP is verified. Holds the OTP hash and attempt counter. |
master_categories | Master & Reference Data | Groups of dropdown/lookup values (e.g. GENDER, PAYMENT_MODE, CURRENCY). System-defined or organization-defined. |
master_values | Master & Reference Data | The individual lookup values belonging to a master category. Referenced by many business tables through *_id columns. |
departments | Master & Reference Data | HR departments. System-defined (shared) or organization-specific. |
designations | Master & Reference Data | Job titles/designations. System-defined (shared) or organization-specific. |
work_locations | Master & Reference Data | Work sites/offices used by employees and wages. |
employees | HR | Employee master record: personal, contact, job, pay and emergency details. Supports a reporting-manager hierarchy through a self-reference. |
employee_photos | HR | Employee photo stored as binary data (one per employee). |
employee_documents | HR | Uploaded employee documents stored as binary data (one per document type per employee). |
attendance | HR | Daily attendance per employee (status, in/out time, break minutes). One active row per employee per day. |
wages | HR | Wage/payroll records per employee and pay period: base wage, overtime, allowances, deductions, net payable, and payment details. |
customers | Customers, Services & Income | Customer master: contact details, billing/service addresses, GSTIN/PAN, payment terms and credit limit. |
services | Customers, Services & Income | Service catalog: pricing type, unit, default rate, cost, minimum charge, tax and discount rules. |
income | Customers, Services & Income | Income (sales/invoice) records for customers, with tax breakup, payment status/mode and optional service and account. |
ExpenseCategories | Expenses | Expense categories, system-defined or organization-defined. |
ExpensePayees | Expenses | Vendors/payees that expenses are paid to. |
Expense | Expenses | Expense records with tax breakup, payment status/mode, due/payment dates and reimbursable/deductible/recurring flags. |
organizationaccount | Accounts & Payments | Parent record for every money account of an organization. accounttype (BANK, DEBIT_CARD, CREDIT_CARD, WALLET) decides which detail table holds its data. |
bankaccountdetails | Accounts & Payments | Bank account details for an account of type BANK. Sensitive fields are stored encrypted with searchable hashes. |
debitcarddetails | Accounts & Payments | Debit card details for an account of type DEBIT_CARD (token, hash, last four digits, expiry, network). |
creditcarddetails | Accounts & Payments | Credit card details for an account of type CREDIT_CARD, including encrypted limits. |
walletdetails | Accounts & Payments | Wallet details for an account of type WALLET. Sensitive fields are stored encrypted. |
payment | Accounts & Payments | Payment ledger entries (received/paid) with party, reference, amount, mode and optional attachment. |
__EFMigrationsHistory | System | Entity Framework Core migration tracking table. Managed by the framework; not a business table. |
5. Data Dictionary
1. Tenancy, Users & Access
organizations
The tenant (company). Holds identity, contact/address, GSTIN/PAN, base currency, fiscal-year start, time zone and invoice prefix. code is unique and is copied into org_code on many child tables.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK; UNIQUE with code |
name | varchar(200) | NO | ||
code | varchar(50) | NO | UNIQUE; UNIQUE with id | |
description | text | YES | ||
email | varchar(255) | YES | ||
phone | varchar(30) | YES | ||
website | varchar(255) | YES | ||
address_line1 | varchar(255) | YES | ||
address_line2 | varchar(255) | YES | ||
city | varchar(100) | YES | ||
state | varchar(100) | YES | ||
country | varchar(100) | YES | ||
postal_code | varchar(20) | YES | ||
logo_url | text | YES | ||
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE, SUSPENDED |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
updated_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
deleted_at | timestamptz | YES | ||
gstin | varchar(20) | YES | ||
pan | varchar(20) | YES | ||
tax_registration_type | varchar(50) | YES | ||
base_currency | varchar(50) | YES | ||
fiscal_year_start | varchar(20) | YES | ||
time_zone | varchar(50) | YES | ||
invoice_prefix | varchar(20) | YES |
Table constraints
- UNIQUE (id, code)
Referenced by: ExpenseCategories (OrganizationId), ExpensePayees (OrganizationId), Expense (OrganizationId), attendance (organization_id), customers (organization_id), designations (organization_id), employees (organization_id), income (organization_id), organization_invitations (organization_id), master_categories (organization_id), pending_signups (organization_id), roles (organization_id), services (organization_id), user_preferences (organization_id), users (organization_id), organizationaccount (organizationid), payment (organizationid)
users
Application login accounts. Each user belongs to one organization; email is unique among non-deleted users.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
organization_id | uuid | NO | FK → organizations.id (ON DELETE RESTRICT) | |
full_name | varchar(200) | NO | ||
email | varchar(255) | NO | ||
password_hash | varchar(255) | NO | ||
display_name | varchar(200) | YES | ||
phone_number | varchar(30) | YES | ||
profile_image_url | text | YES | ||
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE, PENDING, SUSPENDED |
email_verified | boolean | NO | false | |
phone_number_verified | boolean | NO | false | |
last_login_at | timestamptz | YES | ||
password_changed_at | timestamptz | YES | ||
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
updated_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
deleted_at | timestamptz | YES | ||
org_code | varchar(50) | NO |
Referenced by: organization_invitations (invited_by), user_preferences (user_id), user_roles (user_id), user_security (user_id), user_sessions (user_id)
Indexes
idx_users_last_login(last_login_at)idx_users_org_code(org_code)idx_users_organization(organization_id)idx_users_status(status)uq_users_org_email(organization_id, lower((email)::text)) UNIQUE WHERE (deleted_at IS NULL)ux_users_active_email(lower((email)::text)) UNIQUE WHERE ((deleted_at IS NULL) AND ((status) = 'ACTIVE'))ux_users_email_active(lower((email)::text)) UNIQUE WHERE (deleted_at IS NULL)
user_security
One-to-one security state for a user: failed-login lockout, password expiry/reset, and email-verification tokens (hashed).
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
user_id | bigint | NO | PK; FK → users.id (ON DELETE CASCADE) | |
failed_login_attempts | integer | NO | 0 | |
locked_until | timestamptz | YES | ||
password_changed_at | timestamptz | YES | ||
password_expires_at | timestamptz | YES | ||
last_password_reset_at | timestamptz | YES | ||
email_verification_code_hash | varchar(255) | YES | ||
email_verification_expires_at | timestamptz | YES | ||
email_verification_attempts | integer | NO | 0 | |
email_verified_at | timestamptz | YES | ||
password_reset_token_hash | varchar(255) | YES | ||
password_reset_expires_at | timestamptz | YES | ||
email_verification_sent_at | timestamptz | YES |
Table constraints
- CHECK
chk_failed_login_attempts:(failed_login_attempts >= 0)
user_sessions
Refresh-token sessions per user/device, with expiry, last activity and revocation time.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
user_id | bigint | NO | FK → users.id (ON DELETE CASCADE) | |
refresh_token_hash | varchar(255) | NO | UNIQUE | |
last_activity_at | timestamptz | NO | CURRENT_TIMESTAMP | |
expires_at | timestamptz | NO | ||
revoked_at | timestamptz | YES | ||
device_id | varchar(255) | YES | ||
created_at | timestamptz | NO | CURRENT_TIMESTAMP |
Indexes
idx_user_sessions_active(user_id, expires_at) WHERE (revoked_at IS NULL)idx_user_sessions_expires(expires_at)idx_user_sessions_user(user_id)
user_preferences
Per-user UI preferences (currently page size) scoped by organization and preference type.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
user_id | bigint | NO | FK → users.id (ON DELETE CASCADE); UNIQUE | |
page_size | integer | NO | 10 | |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
organization_id | uuid | NO | FK → organizations.id (ON DELETE CASCADE) | |
preference_type | varchar(50) | YES |
Table constraints
- CHECK
user_preferences_page_size_check:((page_size >= 1) AND (page_size <= 200))
Indexes
ux_user_preferences_org_user_type(organization_id, user_id, preference_type) UNIQUE
roles
Roles used for access control. System roles (organization NULL) are shared by all tenants; organizations may define their own.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
organization_id | uuid | YES | FK → organizations.id (ON DELETE CASCADE); UNIQUE with role_name | |
role_name | varchar(100) | NO | UNIQUE with organization_id | |
description | text | YES | ||
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
org_code | varchar(50) | YES | ||
is_system_defined | boolean | NO | false |
Table constraints
- UNIQUE (organization_id, role_name)
Referenced by: organization_invitations (role_id), pending_signups (role_id), role_permissions (role_id), user_roles (role_id)
Indexes
idx_roles_is_system_defined(is_system_defined)idx_roles_org_code(org_code)idx_roles_organization(organization_id)idx_roles_status(status)uq_roles_system_role_name(role_name) UNIQUE WHERE (is_system_defined = true)
user_roles
Assigns roles to users, with assigner and optional expiry.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
user_id | bigint | NO | FK → users.id (ON DELETE CASCADE); UNIQUE with role_id | |
role_id | bigint | NO | FK → roles.id (ON DELETE CASCADE); UNIQUE with user_id | |
assigned_at | timestamptz | NO | CURRENT_TIMESTAMP | |
assigned_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
expires_at | timestamptz | YES | ||
organization_id | uuid | YES | ||
org_code | varchar(50) | YES |
Table constraints
- UNIQUE (user_id, role_id)
Indexes
idx_user_roles_expires(expires_at)idx_user_roles_org_code(org_code)idx_user_roles_organization(organization_id)idx_user_roles_role(role_id)idx_user_roles_user(user_id)
permissions
Catalog of named permissions.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
name | varchar(150) | NO | UNIQUE | |
description | text | YES |
Referenced by: role_permissions (permission_id)
role_permissions
Many-to-many mapping between roles and permissions.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
role_id | bigint | NO | PK (composite); FK → roles.id (ON DELETE CASCADE) | |
permission_id | bigint | NO | PK (composite); FK → permissions.id (ON DELETE CASCADE) |
organization_invitations
Email invitations to join an organization, with hashed token, role to assign, expiry and accepted/revoked timestamps.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | FK → organizations.id (ON DELETE CASCADE) | |
org_code | varchar(50) | NO | ||
email | varchar(255) | NO | ||
invited_by | bigint | NO | FK → users.id (ON DELETE RESTRICT) | |
token_hash | varchar(255) | NO | UNIQUE | |
expires_at | timestamptz | NO | ||
accepted_at | timestamptz | YES | ||
revoked_at | timestamptz | YES | ||
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
role | varchar(50) | NO | 'Employee' | |
role_id | bigint | YES | FK → roles.id (ON DELETE RESTRICT) | |
is_deleted | boolean | NO | false |
Referenced by: pending_signups (invitation_id)
Indexes
ix_org_invitations_email(lower((email)::text))ix_org_invitations_expires_at(expires_at)ix_org_invitations_invited_by(invited_by)ix_org_invitations_organization(organization_id)ix_org_invitations_pending_email(organization_id, lower((email)::text)) WHERE ((accepted_at IS NULL) AND (revoked_at IS NULL))
pending_signups
Staging table for self-signup and invited signup until the email OTP is verified. Holds the OTP hash and attempt counter.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
full_name | varchar(200) | NO | ||
email | varchar(255) | NO | ||
organization_name | varchar(200) | NO | ||
password_hash | varchar(255) | NO | ||
email_verification_code_hash | varchar(255) | NO | ||
email_verification_sent_at | timestamptz | NO | CURRENT_TIMESTAMP | |
email_verification_expires_at | timestamptz | NO | ||
email_verification_attempts | integer | NO | 0 | |
status | varchar(30) | NO | 'PENDING' | Allowed: PENDING, COMPLETED, CANCELLED |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
signup_type | varchar(20) | NO | 'OPEN' | |
invitation_id | uuid | YES | FK → organization_invitations.id (ON DELETE CASCADE) | |
organization_id | uuid | YES | FK → organizations.id (ON DELETE CASCADE) | |
org_code | varchar(50) | YES | ||
role_id | bigint | YES | FK → roles.id (ON DELETE RESTRICT) |
Table constraints
- CHECK
chk_pending_signup_attempts:(email_verification_attempts >= 0)
2. Master & Reference Data
master_categories
Groups of dropdown/lookup values (e.g. GENDER, PAYMENT_MODE, CURRENCY). System-defined or organization-defined.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
organization_id | uuid | YES | FK (composite organization_id, org_code) → organizations(id, code); UNIQUE with code; UNIQUE with name | |
org_code | varchar(50) | YES | FK (composite organization_id, org_code) → organizations(id, code) | |
code | varchar(100) | NO | UNIQUE with organization_id | |
name | varchar(150) | NO | UNIQUE with organization_id | |
description | text | YES | ||
status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
is_system_defined | boolean | NO | false | |
OrganizationId | uuid | YES | ||
OrgCode | text | YES |
Table constraints
- UNIQUE (organization_id, code)
- UNIQUE (organization_id, name)
Referenced by: master_values (master_category_id)
Indexes
idx_master_categories_org_code(org_code)idx_master_categories_organization(organization_id)idx_master_categories_status(status)uq_master_categories_system_code(code) UNIQUE WHERE (is_system_defined = true)uq_master_categories_system_name(name) UNIQUE WHERE (is_system_defined = true)
master_values
The individual lookup values belonging to a master category. Referenced by many business tables through *_id columns.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
master_category_id | bigint | NO | FK → master_categories.id (ON DELETE CASCADE) | |
code | varchar(100) | NO | ||
name | varchar(150) | NO | ||
description | text | YES | ||
display_order | integer | NO | 0 | |
is_default | boolean | NO | false | |
is_system_defined | boolean | NO | false | |
status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
organization_id | uuid | YES | ||
org_code | varchar(50) | YES | ||
OrganizationId | uuid | YES | ||
OrgCode | text | YES | ||
deleted_at | timestamp | YES | ||
deleted_by | bigint | YES |
Table constraints
- CHECK
chk_master_value_display_order:(display_order >= 0)
Referenced by: Expense (CurrencyId, PaymentModeId, PaymentStatusId), customers (customer_group_id, customer_type_id, industry_id, payment_method_id, payment_terms_id), employees (blood_group_id, employment_type_id, gender_id, marital_status_id, pay_type_id, shift_type_id), income (category_id, currency_id, payment_mode_id, payment_status_id), services (applicable_for_id, category_id, discount_type_id, pricing_type_id, sub_category_id, tax_id, unit_of_measurement_id)
Indexes
idx_master_values_category(master_category_id)idx_master_values_display_order(master_category_id, display_order)idx_master_values_status(status)uq_master_value_org_category_code(organization_id, org_code, master_category_id, code) UNIQUE WHERE ((is_system_defined = false) AND (deleted_at IS NULL))uq_master_value_org_category_name(organization_id, org_code, master_category_id, name) UNIQUE WHERE ((is_system_defined = false) AND (deleted_at IS NULL))uq_master_value_system_category_code(master_category_id, code) UNIQUE WHERE (is_system_defined = true)uq_master_value_system_category_name(master_category_id, name) UNIQUE WHERE (is_system_defined = true)
departments
HR departments. System-defined (shared) or organization-specific.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
organization_id | uuid | YES | ||
org_code | varchar(50) | YES | ||
code | varchar(50) | NO | ||
name | varchar(150) | NO | ||
description | text | YES | ||
status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
is_system_defined | boolean | NO | false |
Table constraints
- CHECK
chk_departments_scope:(((is_system_defined = true) AND (organization_id IS NULL) AND (org_code IS NULL)) OR ((is_system_defined = false) AND (organization_id IS NOT NULL) AND (org_code IS NOT NULL)))
Referenced by: employees (department_id)
Indexes
idx_departments_is_system_defined(is_system_defined)idx_departments_organization(organization_id)idx_departments_status(status)uq_departments_org_code(organization_id, code) UNIQUE WHERE (is_system_defined = false)uq_departments_org_name(organization_id, name) UNIQUE WHERE (is_system_defined = false)uq_departments_system_code(code) UNIQUE WHERE (is_system_defined = true)uq_departments_system_name(name) UNIQUE WHERE (is_system_defined = true)
designations
Job titles/designations. System-defined (shared) or organization-specific.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
organization_id | uuid | YES | FK (composite organization_id, org_code) → organizations(id, code) | |
org_code | varchar(50) | YES | FK (composite organization_id, org_code) → organizations(id, code) | |
code | varchar(50) | NO | ||
name | varchar(150) | NO | ||
description | text | YES | ||
status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
is_system_defined | boolean | NO | false |
Table constraints
- CHECK
chk_designations_scope:(((is_system_defined = true) AND (organization_id IS NULL) AND (org_code IS NULL)) OR ((is_system_defined = false) AND (organization_id IS NOT NULL) AND (org_code IS NOT NULL)))
Referenced by: employees (designation_id)
Indexes
idx_designations_is_system_defined(is_system_defined)idx_designations_organization(organization_id)idx_designations_status(status)uq_designations_org_code(organization_id, code) UNIQUE WHERE (is_system_defined = false)uq_designations_org_name(organization_id, name) UNIQUE WHERE (is_system_defined = false)uq_designations_system_code(code) UNIQUE WHERE (is_system_defined = true)uq_designations_system_name(name) UNIQUE WHERE (is_system_defined = true)
work_locations
Work sites/offices used by employees and wages.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | bigint | NO | PK | |
organization_id | uuid | YES | ||
org_code | varchar(50) | YES | ||
code | varchar(50) | NO | ||
name | varchar(150) | NO | ||
description | text | YES | ||
address | text | YES | ||
status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
system_defined | boolean | NO | false |
Referenced by: employees (work_location_id)
Indexes
idx_work_locations_organization(organization_id)idx_work_locations_status(status)uq_work_locations_org_code(organization_id, code) UNIQUE WHERE (organization_id IS NOT NULL)uq_work_locations_org_name(organization_id, name) UNIQUE WHERE (organization_id IS NOT NULL)
3. HR
employees
Employee master record: personal, contact, job, pay and emergency details. Supports a reporting-manager hierarchy through a self-reference.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | FK (composite organization_id, org_code) → organizations(id, code); UNIQUE with employee_code | |
org_code | varchar(50) | NO | FK (composite organization_id, org_code) → organizations(id, code) | |
employee_code | varchar(50) | NO | UNIQUE with organization_id | |
full_name | varchar(200) | NO | ||
date_of_birth | date | YES | ||
gender_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category GENDER | |
father_guardian_name | varchar(200) | YES | ||
marital_status_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category MARITAL_STATUS | |
blood_group_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category BLOOD_GROUP | |
nationality | varchar(100) | YES | ||
phone_number | varchar(30) | NO | ||
alternate_phone | varchar(30) | YES | ||
email | varchar(255) | YES | ||
whatsapp_number | varchar(30) | YES | ||
current_address | text | YES | ||
permanent_address | text | YES | ||
department_id | bigint | YES | FK → departments.id (ON DELETE RESTRICT) | |
designation_id | bigint | NO | FK → designations.id (ON DELETE RESTRICT) | |
date_of_joining | date | NO | ||
employment_type_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category EMPLOYMENT_TYPE | |
reporting_manager_id | uuid | YES | FK → employees.id (ON DELETE SET NULL) | |
work_location_id | bigint | YES | FK → work_locations.id (ON DELETE RESTRICT) | |
pay_type_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category PAY_TYPE | |
salary_amount | numeric(18,2) | NO | ||
government_id | varchar(50) | YES | ||
pan_number | varchar(20) | YES | ||
bank_name | varchar(150) | YES | ||
bank_account_number | varchar(50) | YES | ||
ifsc_code | varchar(20) | YES | ||
account_holder_name | varchar(200) | YES | ||
emergency_contact_name | varchar(200) | YES | ||
emergency_contact_number | varchar(30) | YES | ||
profile_image_url | text | YES | ||
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE, ON_LEAVE, TERMINATED |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
updated_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
deleted_at | timestamptz | YES | ||
shift_type_id | bigint | YES | FK → master_values.id (ON DELETE NO ACTION); category SHIFT_TYPE |
Table constraints
- UNIQUE (organization_id, employee_code)
- CHECK
chk_employee_salary:(salary_amount >= 0)
Referenced by: attendance (employee_id), employee_documents (employee_id), employee_photos (employee_id), employees (reporting_manager_id)
Indexes
idx_employees_date_of_joining(date_of_joining)idx_employees_department(department_id)idx_employees_designation(designation_id)idx_employees_email(email)idx_employees_full_name(full_name)idx_employees_org_code(org_code)idx_employees_organization(organization_id)idx_employees_phone(phone_number)idx_employees_reporting_manager(reporting_manager_id)idx_employees_status(status)idx_employees_work_location(work_location_id)
employee_photos
Employee photo stored as binary data (one per employee).
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
employee_id | uuid | NO | PK; FK → employees.id (ON DELETE CASCADE) | |
file_name | text | NO | ||
content_type | text | NO | ||
file_size | bigint | NO | ||
file_data | bytea | NO | ||
created_at | timestamptz | NO | ||
updated_at | timestamptz | NO |
Table constraints
- CHECK
ck_employee_photos_file_size:(file_size > 0)
employee_documents
Uploaded employee documents stored as binary data (one per document type per employee).
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
employee_id | uuid | NO | FK → employees.id (ON DELETE CASCADE); UNIQUE with document_type | |
document_type | text | NO | UNIQUE with employee_id | |
file_name | text | NO | ||
content_type | text | NO | ||
file_size | bigint | NO | ||
file_data | bytea | NO | ||
created_at | timestamptz | NO | ||
updated_at | timestamptz | NO |
Table constraints
- UNIQUE (employee_id, document_type)
- CHECK
ck_employee_documents_file_size:(file_size > 0)
Indexes
ix_employee_documents_employee_id(employee_id)
attendance
Daily attendance per employee (status, in/out time, break minutes). One active row per employee per day.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | FK → organizations.id (ON DELETE CASCADE) | |
org_code | text | YES | ||
employee_id | uuid | NO | FK → employees.id (ON DELETE CASCADE) | |
attendance_date | date | NO | ||
shift_type_id | bigint | YES | ||
in_time | text | YES | ||
out_time | text | YES | ||
break_minutes | integer | NO | 0 | |
status | text | NO | 'PRESENT' | |
remarks | text | YES | ||
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
deleted_at | timestamptz | YES |
Indexes
idx_attendance_organization_employee_date(organization_id, employee_id, attendance_date)ux_attendance_org_employee_date(organization_id, employee_id, attendance_date) UNIQUE WHERE (deleted_at IS NULL)
wages
Wage/payroll records per employee and pay period: base wage, overtime, allowances, deductions, net payable, and payment details.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | ||
org_code | varchar(50) | YES | ||
employee_id | uuid | NO | ||
start_date | date | NO | ||
end_date | date | NO | ||
wage_type_id | bigint | NO | ||
work_location_id | bigint | YES | ||
payment_status_id | bigint | NO | ||
working_days | numeric(18,2) | YES | ||
paid_units | numeric(18,2) | YES | ||
overtime | numeric(18,2) | YES | ||
overtime_rate | numeric(18,2) | YES | ||
overtime_amount | numeric(18,2) | YES | ||
base_wage | numeric(18,2) | NO | ||
allowances | numeric(18,2) | YES | ||
advance_recovery | numeric(18,2) | YES | ||
other_deductions | numeric(18,2) | YES | ||
basic_wage | numeric(18,2) | YES | ||
gross_earnings | numeric(18,2) | YES | ||
total_deductions | numeric(18,2) | YES | ||
net_payable | numeric(18,2) | YES | ||
payment_date | date | NO | ||
payment_method_id | bigint | NO | ||
payment_account_id | uuid | YES | ||
transaction_number | varchar(255) | YES | ||
remarks | text | YES | ||
payslip_file_name | varchar(255) | YES | ||
status | varchar(20) | NO | 'ACTIVE' | |
created_at | timestamptz | NO | now() | |
updated_at | timestamptz | NO | now() | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
deleted_at | timestamptz | YES |
Indexes
ix_wages_employee_id(employee_id)ix_wages_organization_id(organization_id)
4. Customers, Services & Income
customers
Customer master: contact details, billing/service addresses, GSTIN/PAN, payment terms and credit limit.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | FK (composite organization_id, org_code) → organizations(id, code); UNIQUE with customer_code | |
org_code | varchar(50) | NO | FK (composite organization_id, org_code) → organizations(id, code) | |
customer_code | varchar(50) | NO | UNIQUE with organization_id | |
name | varchar(200) | NO | ||
customer_type_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category CUSTOMER_TYPE | |
industry_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category INDUSTRY | |
customer_group_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category CUSTOMER_GROUP | |
customer_since | date | YES | ||
primary_contact_person | varchar(200) | YES | ||
email | varchar(255) | NO | ||
phone_number | varchar(30) | NO | ||
alternate_phone | varchar(30) | YES | ||
website | varchar(255) | YES | ||
billing_address | text | YES | ||
service_site_address | text | YES | ||
gstin | varchar(30) | YES | ||
pan | varchar(20) | YES | ||
payment_terms_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category PAYMENT_TERMS | |
credit_limit | numeric(18,2) | YES | ||
currency_code | varchar(10) | YES | ||
payment_method_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category PAYMENT_METHOD | |
landmark | varchar(200) | YES | ||
city | varchar(100) | YES | ||
state | varchar(100) | YES | ||
country | varchar(100) | YES | ||
pincode | varchar(20) | YES | ||
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE, SUSPENDED |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
updated_by | bigint | YES | FK → users.id (ON DELETE SET NULL) | |
deleted_at | timestamptz | YES | ||
display_trading_name | varchar(255) | YES | ||
billing_city | varchar(120) | YES | ||
billing_state | varchar(120) | YES | ||
billing_pincode | varchar(20) | YES | ||
billing_country | varchar(120) | YES | ||
same_as_billing | boolean | NO | false | |
service_city | varchar(120) | YES | ||
service_state | varchar(120) | YES | ||
service_pincode | varchar(20) | YES | ||
service_country | varchar(120) | YES | ||
preferred_contact_mode | varchar(255) | YES | ||
notes | text | YES |
Table constraints
- UNIQUE (organization_id, customer_code)
- CHECK
chk_customers_credit_limit:((credit_limit IS NULL) OR (credit_limit >= 0))
Referenced by: income (customer_id)
services
Service catalog: pricing type, unit, default rate, cost, minimum charge, tax and discount rules.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | FK (composite organization_id, org_code) → organizations(id, code); UNIQUE with code; UNIQUE with name | |
org_code | varchar(50) | NO | FK (composite organization_id, org_code) → organizations(id, code) | |
code | varchar(50) | NO | UNIQUE with organization_id | |
name | varchar(150) | NO | UNIQUE with organization_id | |
category_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category SERVICE_CATEGORY | |
sub_category_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category SERVICE_SUB_CATEGORY | |
pricing_type_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category PRICING_TYPE | |
unit_of_measurement_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category UNIT_OF_MEASUREMENT | |
discount_allowed | boolean | NO | false | |
default_rate | numeric(18,2) | NO | 0 | |
cost | numeric(18,2) | YES | ||
minimum_charge | numeric(18,2) | NO | 0 | |
tax_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category TAX_GST | |
description | text | YES | ||
estimated_duration | varchar(100) | YES | ||
applicable_for_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category APPLICABLE_FOR | |
status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES | ||
is_deleted | boolean | NO | false | |
deleted_at | timestamptz | YES | ||
deleted_by | bigint | YES | ||
discount_type_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category DISCOUNT_TYPE |
Table constraints
- UNIQUE (organization_id, code)
- UNIQUE (organization_id, name)
- CHECK
chk_services_cost:((cost IS NULL) OR (cost >= 0)) - CHECK
chk_services_default_rate:(default_rate >= 0) - CHECK
chk_services_minimum_charge:(minimum_charge >= 0)
Referenced by: income (service_id)
Indexes
idx_services_category(category_id)idx_services_is_deleted(is_deleted)idx_services_org_code(org_code)idx_services_organization(organization_id)idx_services_pricing_type(pricing_type_id)idx_services_status(status)idx_services_sub_category(sub_category_id)idx_services_unit(unit_of_measurement_id)
income
Income (sales/invoice) records for customers, with tax breakup, payment status/mode and optional service and account.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organization_id | uuid | NO | FK → organizations.id (ON DELETE RESTRICT); UNIQUE with income_number | |
income_number | varchar(50) | NO | UNIQUE with organization_id | |
income_date | date | NO | ||
customer_id | uuid | NO | FK → customers.id (ON DELETE RESTRICT) | |
service_id | uuid | YES | FK → services.id (ON DELETE RESTRICT) | |
category_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category INCOME_CATEGORY | |
currency_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category CURRENCY | |
amount_before_tax | numeric(18,2) | NO | 0 | |
tax_percentage | numeric(5,2) | NO | 0 | |
tax_amount | numeric(18,2) | NO | 0 | |
total_amount | numeric(18,2) | NO | 0 | |
payment_mode_id | bigint | YES | FK → master_values.id (ON DELETE RESTRICT); category PAYMENT_MODE | |
account_id | uuid | YES | FK → organizationaccount.id (ON DELETE RESTRICT) | |
payment_status_id | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category PAYMENT_STATUS | |
payment_date | date | YES | ||
transaction_reference | varchar(255) | YES | ||
invoice_reference | varchar(255) | YES | ||
payment_reference | varchar(255) | YES | ||
description | text | YES | ||
status | varchar(20) | NO | 'ACTIVE' | |
created_at | timestamptz | NO | CURRENT_TIMESTAMP | |
updated_at | timestamptz | NO | CURRENT_TIMESTAMP | |
created_by | bigint | YES | ||
updated_by | bigint | YES |
Table constraints
- UNIQUE (organization_id, income_number)
- CHECK
chk_income_amount_before_tax:(amount_before_tax >= 0) - CHECK
chk_income_tax_amount:(tax_amount >= 0) - CHECK
chk_income_tax_percentage:((tax_percentage >= 0) AND (tax_percentage <= 100)) - CHECK
chk_income_total_amount:(total_amount >= 0)
Indexes
idx_income_account(account_id)idx_income_category(category_id)idx_income_customer(customer_id)idx_income_income_date(income_date)idx_income_organization(organization_id)idx_income_payment_date(payment_date)idx_income_payment_mode(payment_mode_id)idx_income_payment_status(payment_status_id)idx_income_service(service_id)idx_income_status(status)
5. Expenses
ExpenseCategories
Expense categories, system-defined or organization-defined.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
Id | uuid | NO | gen_random_uuid() | PK |
OrganizationId | uuid | YES | FK → organizations.id (ON DELETE CASCADE) | |
Code | varchar(100) | NO | ||
Name | varchar(150) | NO | ||
Description | text | YES | ||
IsSystemDefined | boolean | NO | false | |
Status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
CreatedAt | timestamptz | NO | CURRENT_TIMESTAMP | |
UpdatedAt | timestamptz | NO | CURRENT_TIMESTAMP | |
CreatedBy | bigint | YES | ||
UpdatedBy | bigint | YES | ||
DeletedAt | timestamptz | YES |
Referenced by: Expense (CategoryId)
Indexes
"ExpenseCategoriesOrganizationCodeUnique"(OrganizationId, Code) UNIQUE WHERE (("IsSystemDefined" = false) AND ("DeletedAt" IS NULL))"ExpenseCategoriesOrganizationIndex"(OrganizationId)"ExpenseCategoriesOrganizationNameUnique"(OrganizationId, Name) UNIQUE WHERE (("IsSystemDefined" = false) AND ("DeletedAt" IS NULL))"ExpenseCategoriesStatusIndex"(Status)"ExpenseCategoriesSystemCodeUnique"(Code) UNIQUE WHERE ("IsSystemDefined" = true)"ExpenseCategoriesSystemDefinedIndex"(IsSystemDefined)"ExpenseCategoriesSystemNameUnique"(Name) UNIQUE WHERE ("IsSystemDefined" = true)
ExpensePayees
Vendors/payees that expenses are paid to.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
Id | uuid | NO | gen_random_uuid() | PK |
OrganizationId | uuid | NO | FK → organizations.id (ON DELETE CASCADE) | |
Name | varchar(200) | NO | ||
Code | varchar(100) | YES | ||
Email | varchar(255) | YES | ||
Phone | varchar(50) | YES | ||
Address | text | YES | ||
Description | text | YES | ||
Status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE |
CreatedAt | timestamptz | NO | CURRENT_TIMESTAMP | |
UpdatedAt | timestamptz | NO | CURRENT_TIMESTAMP | |
CreatedBy | bigint | YES | ||
UpdatedBy | bigint | YES |
Referenced by: Expense (VendorPayeeId)
Indexes
"ExpensePayeesOrganizationCodeUnique"(OrganizationId, Code) UNIQUE WHERE ("Code" IS NOT NULL)"ExpensePayeesOrganizationIndex"(OrganizationId)"ExpensePayeesOrganizationNameUnique"(OrganizationId, Name) UNIQUE"ExpensePayeesStatusIndex"(Status)
Expense
Expense records with tax breakup, payment status/mode, due/payment dates and reimbursable/deductible/recurring flags.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
Id | uuid | NO | gen_random_uuid() | PK |
OrganizationId | uuid | NO | FK → organizations.id (ON DELETE RESTRICT); UNIQUE with ExpenseNumber | |
ExpenseNumber | varchar(50) | NO | UNIQUE with OrganizationId | |
ExpenseDate | date | NO | ||
CategoryId | uuid | NO | FK → ExpenseCategories.Id (ON DELETE RESTRICT) | |
VendorPayeeId | uuid | NO | FK → ExpensePayees.Id (ON DELETE RESTRICT) | |
CurrencyId | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category CURRENCY | |
AmountBeforeTax | numeric(18,2) | NO | 0 | |
TaxPercentage | numeric(5,2) | NO | 0 | |
TaxAmount | numeric(18,2) | NO | 0 | |
TotalAmount | numeric(18,2) | NO | 0 | |
PaymentModeId | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category PAYMENT_MODE | |
AccountId | uuid | YES | FK → organizationaccount.id (ON DELETE RESTRICT) | |
TransactionReference | varchar(255) | YES | ||
PaymentStatusId | bigint | NO | FK → master_values.id (ON DELETE RESTRICT); category PAYMENT_STATUS | |
PaymentDate | date | YES | ||
DueDate | date | YES | ||
ServiceJobWorkOrder | varchar(255) | YES | ||
InvoiceReference | varchar(255) | YES | ||
Description | text | YES | ||
IsReimbursable | boolean | NO | false | |
IsTaxDeductible | boolean | NO | false | |
IsRecurring | boolean | NO | false | |
Status | varchar(20) | NO | 'ACTIVE' | Allowed: ACTIVE, CANCELLED, INACTIVE |
CreatedAt | timestamptz | NO | CURRENT_TIMESTAMP | |
UpdatedAt | timestamptz | NO | CURRENT_TIMESTAMP | |
CreatedBy | bigint | YES | ||
UpdatedBy | bigint | YES |
Table constraints
- UNIQUE (OrganizationId, ExpenseNumber)
- CHECK
ChkExpenseAmountBeforeTax:("AmountBeforeTax" >= 0) - CHECK
ChkExpenseTaxAmount:("TaxAmount" >= 0) - CHECK
ChkExpenseTaxPercentage:(("TaxPercentage" >= 0) AND ("TaxPercentage" <= 100)) - CHECK
ChkExpenseTotalAmount:("TotalAmount" >= 0)
Indexes
"IdxExpenseAccount"(AccountId)"IdxExpenseCategory"(CategoryId)"IdxExpenseCurrency"(CurrencyId)"IdxExpenseDueDate"(DueDate)"IdxExpenseExpenseDate"(ExpenseDate)"IdxExpenseOrganization"(OrganizationId)"IdxExpensePaymentDate"(PaymentDate)"IdxExpensePaymentMode"(PaymentModeId)"IdxExpensePaymentStatus"(PaymentStatusId)"IdxExpenseStatus"(Status)"IdxExpenseVendorPayee"(VendorPayeeId)
6. Accounts & Payments
organizationaccount
Parent record for every money account of an organization. accounttype (BANK, DEBIT_CARD, CREDIT_CARD, WALLET) decides which detail table holds its data.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organizationid | uuid | NO | FK → organizations.id (ON DELETE CASCADE) | |
accountname | text | NO | ||
accounttype | varchar(30) | NO | Allowed: BANK, DEBIT_CARD, CREDIT_CARD, WALLET | |
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE, DELETED |
createdat | timestamptz | NO | CURRENT_TIMESTAMP | |
updatedat | timestamptz | NO | CURRENT_TIMESTAMP | |
createdby | bigint | YES | ||
updatedby | bigint | YES | ||
is_deleted | boolean | NO | false | |
deleted_at | timestamptz | YES | ||
deleted_by | bigint | YES | FK → users.id (ON DELETE RESTRICT) |
Referenced by: Expense (AccountId), income (account_id), bankaccountdetails (organizationaccountid), creditcarddetails (organizationaccountid), debitcarddetails (organizationaccountid), walletdetails (organizationaccountid)
Indexes
idx_organizationaccount_is_deleted(is_deleted)ixorganizationaccountorganization(organizationid)ixorganizationaccountstatus(status)ixorganizationaccounttype(accounttype)
bankaccountdetails
Bank account details for an account of type BANK. Sensitive fields are stored encrypted with searchable hashes.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organizationaccountid | uuid | NO | FK → organizationaccount.id (ON DELETE CASCADE); UNIQUE | |
banknameencrypted | text | NO | ||
banknamehash | varchar(64) | YES | ||
accountnumberencrypted | text | NO | ||
accountnumberhash | varchar(64) | YES | ||
branchcodeencrypted | text | YES | ||
branchcodehash | varchar(64) | YES | ||
ifsccodeencrypted | text | NO | ||
ifsccodehash | varchar(64) | YES | ||
branchnameencrypted | text | YES | ||
initialamountencrypted | text | YES | ||
accounttype | varchar(50) | YES | ||
createdat | timestamptz | NO | CURRENT_TIMESTAMP | |
updatedat | timestamptz | NO | CURRENT_TIMESTAMP | |
is_primary | boolean | NO | false |
debitcarddetails
Debit card details for an account of type DEBIT_CARD (token, hash, last four digits, expiry, network).
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organizationaccountid | uuid | NO | FK → organizationaccount.id (ON DELETE CASCADE); UNIQUE | |
cardholdernameencrypted | text | NO | ||
cardnumbertoken | text | YES | ||
cardnumberhash | varchar(64) | YES | ||
lastfourdigits | varchar(4) | YES | ||
expirymonth | smallint | YES | ||
expiryyear | smallint | YES | ||
banknameencrypted | text | YES | ||
cardnetwork | varchar(30) | YES | ||
createdat | timestamptz | NO | CURRENT_TIMESTAMP | |
updatedat | timestamptz | NO | CURRENT_TIMESTAMP |
Table constraints
- CHECK
ckdebitcardlastfour:((lastfourdigits IS NULL) OR ((lastfourdigits) ~ '^[0-9]{4}$'))
creditcarddetails
Credit card details for an account of type CREDIT_CARD, including encrypted limits.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organizationaccountid | uuid | NO | FK → organizationaccount.id (ON DELETE CASCADE); UNIQUE | |
cardholdernameencrypted | text | NO | ||
cardnumbertoken | text | YES | ||
cardnumberhash | varchar(64) | YES | ||
lastfourdigits | varchar(4) | YES | ||
expirymonth | smallint | YES | ||
expiryyear | smallint | YES | ||
banknameencrypted | text | YES | ||
cardnetwork | varchar(30) | YES | ||
creditlimitencrypted | text | YES | ||
availablelimitencrypted | text | YES | ||
createdat | timestamptz | NO | CURRENT_TIMESTAMP | |
updatedat | timestamptz | NO | CURRENT_TIMESTAMP |
Table constraints
- CHECK
ckcreditcardlastfour:((lastfourdigits IS NULL) OR ((lastfourdigits) ~ '^[0-9]{4}$'))
walletdetails
Wallet details for an account of type WALLET. Sensitive fields are stored encrypted.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organizationaccountid | uuid | NO | FK → organizationaccount.id (ON DELETE CASCADE); UNIQUE | |
walletproviderencrypted | text | NO | ||
walletidentifierencrypted | text | YES | ||
walletidentifierhash | varchar(64) | YES | ||
holdernameencrypted | text | YES | ||
initialamountencrypted | text | YES | ||
createdat | timestamptz | NO | CURRENT_TIMESTAMP | |
updatedat | timestamptz | NO | CURRENT_TIMESTAMP |
payment
Payment ledger entries (received/paid) with party, reference, amount, mode and optional attachment.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
id | uuid | NO | gen_random_uuid() | PK |
organizationid | uuid | NO | FK → organizations.id (ON DELETE CASCADE) | |
paymentnumber | text | NO | ||
paymentdate | date | NO | ||
paymenttype | varchar(30) | NO | ||
party | text | NO | ||
referencetype | varchar(30) | NO | ||
referencenumber | text | YES | ||
amount | numeric(14,2) | NO | ||
paymentmode | text | NO | ||
transactionreference | text | YES | ||
notes | text | YES | ||
attachmentname | text | YES | ||
attachmentpath | text | YES | ||
status | varchar(30) | NO | 'ACTIVE' | Allowed: ACTIVE, INACTIVE, DELETED |
createdat | timestamptz | NO | CURRENT_TIMESTAMP | |
updatedat | timestamptz | NO | CURRENT_TIMESTAMP | |
createdby | bigint | YES | ||
updatedby | bigint | YES | ||
is_deleted | boolean | NO | false | |
deleted_at | timestamptz | YES | ||
deleted_by | bigint | YES | FK → users.id (ON DELETE RESTRICT) |
Table constraints
- CHECK
ckpaymentamountpositive:(amount > 0)
Indexes
idx_payment_is_deleted(is_deleted)ixpaymentdate(paymentdate)ixpaymentorganization(organizationid)ixpaymentstatus(status)ixpaymenttype(paymenttype)uqpaymentnumberperorg(organizationid, paymentnumber) UNIQUE
7. System
__EFMigrationsHistory
Entity Framework Core migration tracking table. Managed by the framework; not a business table.
| Column | Type | Null | Default | Notes |
|---|---|---|---|---|
MigrationId | varchar(150) | NO | PK | |
ProductVersion | varchar(32) | NO |
6. Master Data Catalog
master_categories defines the dropdown groups and master_values holds the values. The Used by column was derived from the foreign-key columns and the data they reference.
| ID | Category code | Name | System values | Org values | Used by |
|---|---|---|---|---|---|
| 1 | CUSTOMER_TYPE | Customer Type | 3 | 23 | customers.customer_type_id |
| 2 | CUSTOMER_GROUP | Customer Group / Segment | 4 | 2 | customers.customer_group_id |
| 3 | INDUSTRY | Industry / Category | 7 | 32 | customers.industry_id |
| 4 | PAYMENT_TERMS | Payment Terms | 5 | 2 | customers.payment_terms_id |
| 5 | PAYMENT_METHOD | Preferred Payment Method | 5 | 1 | customers.payment_method_id |
| 6 | CONTACT_MODE | Preferred Contact Mode | 4 | 0 | — |
| 7 | GENDER | Gender | 4 | 0 | employees.gender_id |
| 8 | MARITAL_STATUS | Marital Status | 4 | 0 | employees.marital_status_id |
| 9 | BLOOD_GROUP | Blood Group | 8 | 0 | employees.blood_group_id |
| 10 | EMPLOYMENT_TYPE | Employment Type | 4 | 0 | employees.employment_type_id |
| 11 | PAY_TYPE | Pay Type | 3 | 0 | employees.pay_type_id |
| 13 | SERVICE_CATEGORY | Service Category | 3 | 5 | services.category_id |
| 14 | SERVICE_SUB_CATEGORY | Service Sub Category | 3 | 3 | services.sub_category_id |
| 15 | PRICING_TYPE | Pricing Type | 3 | 3 | services.pricing_type_id |
| 16 | UNIT_OF_MEASUREMENT | Unit of Measurement | 3 | 4 | services.unit_of_measurement_id |
| 17 | APPLICABLE_FOR | Applicable For | 3 | 6 | services.applicable_for_id |
| 18 | INCOME_CATEGORY | Income Category | 6 | 23 | income.category_id |
| 19 | PAYMENT_MODE | Payment Mode | 8 | 35 | Expense.PaymentModeId, income.payment_mode_id |
| 20 | PAYMENT_STATUS | Payment Status | 2 | 4 | Expense.PaymentStatusId, income.payment_status_id |
| 21 | CURRENCY | Currency | 1 | 44 | Expense.CurrencyId, income.currency_id |
| 22 | SHIFT_TYPE | Shift Type | 4 | 0 | employees.shift_type_id |
| 23 | TAX_GST | Tax / GST | 6 | 0 | services.tax_id |
| 24 | DISCOUNT_TYPE | Discount Type | 2 | 0 | services.discount_type_id |
| 28 | EXPENSE_CATEGORY | Expense Category | 10 | 0 | — |
| 29 | EXPENSE_VENDOR | Vendor / Payee | 4 | 0 | — |
| 30 | EXPENSE_ACCOUNT_TYPE | Account Type | 5 | 0 | — |
| 31 | WAGE_TYPE | Wage Type | 4 | 0 | — |
| 33 | PAYMENT_TYPE | Payment Type | 2 | 9 | — |
| 34 | PARTY | Party | 4 | 3 | — |
| 35 | REFERENCE_TYPE | Reference Type | 4 | 4 | — |
Categories with no entry under Used by (CONTACT_MODE, EXPENSE_CATEGORY, EXPENSE_VENDOR, EXPENSE_ACCOUNT_TYPE, WAGE_TYPE, PAYMENT_TYPE, PARTY, REFERENCE_TYPE) are not linked through a foreign key. They are consumed by application logic or stored as text (for example the payment table).
7. Status and Enumerated Values
| Table.column | Allowed values |
|---|---|
Expense.Status | ACTIVE, CANCELLED, INACTIVE |
ExpenseCategories.Status | ACTIVE, INACTIVE |
ExpensePayees.Status | ACTIVE, INACTIVE |
customers.status | ACTIVE, INACTIVE, SUSPENDED |
departments.status | ACTIVE, INACTIVE |
designations.status | ACTIVE, INACTIVE |
employees.status | ACTIVE, INACTIVE, ON_LEAVE, TERMINATED |
master_categories.status | ACTIVE, INACTIVE |
master_values.status | ACTIVE, INACTIVE |
organizationaccount.status | ACTIVE, INACTIVE, DELETED |
organizationaccount.accounttype | BANK, DEBIT_CARD, CREDIT_CARD, WALLET |
organizations.status | ACTIVE, INACTIVE, SUSPENDED |
payment.status | ACTIVE, INACTIVE, DELETED |
pending_signups.status | PENDING, COMPLETED, CANCELLED |
roles.status | ACTIVE, INACTIVE |
services.status | ACTIVE, INACTIVE |
users.status | ACTIVE, INACTIVE, PENDING, SUSPENDED |
work_locations.status | ACTIVE, INACTIVE |
Other fixed value set: pending_signups.signup_type (OPEN, INVITED).
8. Stored Routines
| Routine | Type | Description |
|---|---|---|
fn_get_organization_master_data(p_organization_id uuid) | Function → jsonb | Validates that the organization is active and not deleted, then returns the roles and master values (system-defined plus that organization's own) as one JSON document for the frontend. |
sp_get_departments(p_organization_id uuid) | Function → table | Returns active departments: all system-defined ones plus those of the given organization, ordered by name. |
sp_create_pending_signup(...) | Procedure | Starts a signup. Validates name, email and signup type (OPEN or INVITED), rejects emails that already belong to an active user, and creates or refreshes a PENDING row in pending_signups with a new OTP hash and expiry. For INVITED signups it also validates the invitation and role. Returns p_success and p_message. Two overloads exist: an older one (open signup only) and a newer one with signup type, organization, invitation and role parameters. |
sp_verify_and_complete_signup(p_email, p_otp_hash, p_max_attempts, ...) | Procedure | Completes a signup after OTP entry. Checks that the pending signup exists, is not expired, is under the attempt limit and that the OTP hash matches (a wrong code increments email_verification_attempts). For OPEN signups it creates a new organization with an auto-generated code (first three letters of the name plus four random digits); for INVITED signups it re-validates the invitation and joins the existing organization. Returns p_success and p_message. |
9. Planned Changes
Items below are known gaps and improvements to be handled later. They do not affect current behaviour.
| # | Area | Current state | Proposed change |
|---|---|---|---|
| 1 | Organization foreign keys | departments, work_locations and master_values have organization_id but no foreign key to organizations (designations has a composite one). | Add foreign keys (and the composite (organization_id, org_code) key where org_code exists) to match designations, customers and employees. |
| 2 | Duplicate columns | master_categories and master_values contain both organization_id/org_code and "OrganizationId"/"OrgCode", apparently left over from an EF migration. master_values.deleted_at is timestamp while other tables use timestamptz. | Remove the duplicate PascalCase columns after confirming nothing reads them. Align deleted_at to timestamptz. |
| 3 | payment lookups stored as text | paymenttype, party, referencetype and paymentmode are free text, although PAYMENT_TYPE, PARTY, REFERENCE_TYPE and PAYMENT_MODE exist as master categories. referencenumber has no foreign key to income or Expense. | Store lookup IDs referencing master_values and consider typed references to the source record. |
| 4 | attendance shift and times | shift_type_id has no foreign key (unlike employees.shift_type_id), and in_time/out_time are text. | Add the foreign key to master_values and change the time columns to time or timestamptz. |
| 5 | Plaintext employee financial data | employees.bank_account_number, ifsc_code, pan_number and government_id are stored unencrypted, while the account-detail tables encrypt equivalent data. | Encrypt these columns, or move them to an encrypted structure like bankaccountdetails. |
| 6 | user_preferences constraints | Unique on user_id alone conflicts with the unique on (organization_id, user_id, preference_type), so a user can effectively have one row only. Two identical foreign keys exist on user_id. | Drop the single-column unique (if multiple preference rows are intended) and one duplicate foreign key. |
| 7 | Redundant email indexes on users | uq_users_org_email, ux_users_active_email and ux_users_email_active overlap. | Keep the indexes that match the intended rule (per-organization or global email uniqueness) and drop the rest. |
| 8 | Audit-column foreign keys | created_by/updated_by have foreign keys to users only on users, organizations, employees and customers. | Add them consistently, or document that they are informational. |
| 9 | Session retention | user_sessions keeps expired and revoked sessions indefinitely. | Add a periodic cleanup job based on expires_at / revoked_at. |
| 10 | Permissions not populated | permissions and role_permissions have no rows, so access control currently relies on role names. | Populate permissions, or remove the tables if they will not be used. |
| 11 | Naming consistency | Three naming styles are in use (snake_case, PascalCase, run-together lowercase). | Standardize on snake_case in a future migration. |
Appendix: Viewing the ER Diagrams
- Interactive: open
opsnexa_er_diagrams.htmlin a browser (needs internet access to load Mermaid). Each diagram can be downloaded as SVG. - Online editor: paste any Mermaid block from Section 3 into Mermaid Live Editor to view and export PNG/SVG.
- Confluence: use a Mermaid macro app, or insert the exported SVG/PNG.
- GitHub/GitLab and VS Code (Markdown preview): render Mermaid blocks natively.