Skip to main content

🗄️ PostgreSQL Data Models

Kloyst strongly leverages PostgreSQL managed entirely behind Prisma ORM. By sticking aggressively to a Relational Schema, we guarantee strict isolation and transactional safety.


1. Core Tenant Architecture

Vendor Model

The root environment node housing the billing configurations.

  • id: UUID @id
  • name: String
  • planType: Enum (SHARED, DEDICATED)
  • settings: JSON (Stores dynamic configs rather than creating multiple sparse columns).
  • Relationships: One-to-Many with Users, Wallets, Campaigns.

User Model

The authentication node granting access to a dashboard scope.

  • id: UUID @id
  • vendorId: UUID (Strict mapping to Vendor)
  • email: String (Unique)
  • passwordHash: String
  • role: Enum (ADMIN, STAFF)
  • Indexing: @@index([vendorId])

2. Infrastructure Models

WabaAccount Model

Configuration keys specifically tracking WhatsApp states.

  • id: UUID
  • vendorId: UUID
  • wabaId: String (Extracted Meta WABA ID)
  • systemUserToken: String (Encrypted token if Dedicated model)
  • phoneNumberId: String

Contact Model (Phonebook)

Client CRM mappings representing destination targets.

  • id: UUID
  • vendorId: UUID
  • mobile: String (Cleaned standardized E.164 without '+' symbols)
  • attributes: JSON (Dynamic mail-merge tags: e.g., name, age, city).

3. Financial Mechanics

Wallet Model (Prepaid Mechanism)

Tracks exact integer floating balance metrics for Shared instances.

  • id: UUID
  • vendorId: UUID (Unique, ensuring exactly 1 wallet per vendor)
  • balance: Decimal (Precision 10, Scale 4)
  • updatedAt: DateTime

Transaction Model (Ledger)

Append-only log containing every credit and debit payload for strict accounting trails.

  • id: UUID
  • walletId: UUID
  • type: Enum (CREDIT_RECHARGE, DEBIT_MESSAGE, REFUND)
  • amount: Decimal
  • reference: String (Razorpay ID / Target Campaign ID)

4. Communication States

Campaign Model

Group orchestrator for bulk deployments.

  • id: UUID
  • vendorId: UUID
  • name: String
  • status: Enum (DRAFT, QUEUED, PROCESSING, COMPLETED, FAILED)

MessageLog Model (High-Throughput Table)

Tracks the absolute granular state of individual WA transmissions.

  • id: UUID
  • vendorId: UUID
  • campaignId: UUID (Nullable if transaction sent via API)
  • waMessageId: String (Unique Meta return hash e.g., wamid.XXX)
  • status: Enum (SENT, DELIVERED, READ, FAILED)
  • direction: Enum (INBOUND, OUTBOUND)