Skip to content

Dispatch-AI-com/backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

143 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Backend Service - DispatchAI Platform

Edit Time: 9/11/2025

NestJS REST API handling business logic, integrations, and data management for the DispatchAI platform.

🎯 Overview

The Backend service is the core API layer that orchestrates all platform operations including authentication, telephony webhooks, payment processing, calendar integration, and data persistence.

πŸ—οΈ Architecture

Tech Stack

  • Framework: NestJS 11 (Node.js 18+)
  • Language: TypeScript 5.8
  • Database: MongoDB 7 with Mongoose ODM
  • Cache: Redis 7 for sessions & caching
  • Auth: JWT + Google OAuth 2.0
  • Payment: Stripe
  • Telephony: Twilio Voice API
  • Calendar: Google Calendar API + Microsoft Graph (Outlook)
  • Validation: class-validator + class-transformer
  • Logging: Winston
  • Testing: Jest + Supertest

Project Structure

apps/backend/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.ts                      # Application entry point
β”‚   β”œβ”€β”€ modules/                     # Feature modules
β”‚   β”‚   β”œβ”€β”€ auth/                   # Authentication & authorization
β”‚   β”‚   β”œβ”€β”€ user/                   # User management
β”‚   β”‚   β”œβ”€β”€ company/                # Company/business management
β”‚   β”‚   β”œβ”€β”€ plan/                   # Subscription plans
β”‚   β”‚   β”œβ”€β”€ subscription/           # User subscriptions
β”‚   β”‚   β”œβ”€β”€ telephony/              # Twilio voice integration
β”‚   β”‚   β”œβ”€β”€ calllog/                # Call logs & history
β”‚   β”‚   β”œβ”€β”€ transcript/             # Call transcripts
β”‚   β”‚   β”œβ”€β”€ transcript-chunk/       # Transcript chunks
β”‚   β”‚   β”œβ”€β”€ service/                # Services catalog
β”‚   β”‚   β”œβ”€β”€ service-booking/        # Service bookings/appointments
β”‚   β”‚   β”œβ”€β”€ service-form-field/     # Dynamic form fields
β”‚   β”‚   β”œβ”€β”€ service-location-mapping/ # Service locations
β”‚   β”‚   β”œβ”€β”€ location/               # Location management
β”‚   β”‚   β”œβ”€β”€ availability/           # Business hours/availability
β”‚   β”‚   β”œβ”€β”€ google-calendar/        # Google Calendar integration
β”‚   β”‚   β”œβ”€β”€ google-places/          # Google Places API
β”‚   β”‚   β”œβ”€β”€ stripe/                 # Stripe payment processing
β”‚   β”‚   β”œβ”€β”€ onboarding/             # User onboarding flow
β”‚   β”‚   β”œβ”€β”€ setting/                # User settings
β”‚   β”‚   β”œβ”€β”€ blog/                   # Blog content
β”‚   β”‚   └── health/                 # Health check
β”‚   β”œβ”€β”€ common/                     # Shared utilities
β”‚   β”‚   β”œβ”€β”€ constants/             # Constants
β”‚   β”‚   β”œβ”€β”€ decorators/            # Custom decorators
β”‚   β”‚   β”œβ”€β”€ filters/               # Exception filters
β”‚   β”‚   β”œβ”€β”€ guards/                # Auth guards
β”‚   β”‚   └── interfaces/            # TypeScript interfaces
β”‚   β”œβ”€β”€ lib/                       # External library wrappers
β”‚   β”‚   β”œβ”€β”€ ai/                    # AI service HTTP client
β”‚   β”‚   β”œβ”€β”€ redis/                 # Redis client
β”‚   β”‚   └── twilio/                # Twilio client
β”‚   β”œβ”€β”€ config/                    # Configuration
β”‚   β”‚   └── swagger.config.ts     # API documentation
β”‚   └── utils/                     # Utility functions
β”œβ”€β”€ test/                          # Test suite
β”‚   β”œβ”€β”€ fixtures/                 # Test data
β”‚   β”œβ”€β”€ helpers/                  # Test utilities
β”‚   β”œβ”€β”€ integration/              # Integration tests
β”‚   └── unit/                     # Unit tests
β”œβ”€β”€ scripts/                       # Utility scripts
β”‚   └── seeds/                    # Database seeding
β”œβ”€β”€ package.json                  # Dependencies & scripts
β”œβ”€β”€ tsconfig.json                 # TypeScript config
β”œβ”€β”€ jest.config.json              # Jest config
β”œβ”€β”€ nest-cli.json                 # NestJS CLI config
β”œβ”€β”€ Dockerfile.dev               # Development Docker image
└── Dockerfile.uat               # UAT Docker image

πŸ”Œ API Endpoints

Base URL

Development: http://localhost:4000/api
Swagger UI: http://localhost:4000/api

Core Modules & Endpoints

Authentication (/auth)

  • POST /auth/signup - User registration
  • POST /auth/login - Login with email/password
  • POST /auth/google - Google OAuth login
  • POST /auth/logout - Logout (clear cookie)
  • GET /auth/me - Get current user

Telephony (/telephony)

  • POST /telephony/voice - Handle incoming call (TwiML)
  • POST /telephony/gather - Handle speech input (TwiML)
  • POST /telephony/status - Handle call status callbacks

Call Logs (/calllog)

  • GET /calllog - List call logs (paginated, filtered)
  • GET /calllog/:id - Get single call log
  • GET /calllog/metrics - Get call metrics/stats
  • POST /calllog - Create call log
  • PATCH /calllog/:id - Update call log

Transcripts (/transcript)

  • GET /transcript/call/:callSid - Get transcript by call SID
  • POST /transcript - Create transcript
  • PATCH /transcript/:id - Update transcript

Service Bookings (/service-booking)

  • GET /service-booking - List bookings
  • POST /service-booking - Create booking
  • PATCH /service-booking/:id - Update booking
  • DELETE /service-booking/:id - Cancel booking

Calendar (/google-calendar)

  • GET /google-calendar/auth-url - Get OAuth URL
  • GET /google-calendar/callback - OAuth callback
  • POST /google-calendar/token - Store access token
  • GET /google-calendar/token - Get stored token

Stripe (/stripe)

  • POST /stripe/webhook - Handle Stripe webhooks
  • POST /stripe/create-checkout - Create checkout session

Services (/service)

  • GET /service - List services
  • POST /service - Create service
  • PATCH /service/:id - Update service
  • DELETE /service/:id - Soft delete service

πŸ“Š Data Models

Core Schemas

User (user.schema.ts)

{
  firstName: string
  lastName: string
  email: string (unique, required)
  password?: string
  twilioPhoneNumber?: string
  fullPhoneNumber?: string
  role: 'admin' | 'user'
  status: 'active' | 'inactive' | 'suspended'
  address?: {
    unitAptPOBox?: string
    streetAddress: string
    suburb: string
    state: string
    postcode: string
  }
  greeting: {
    message: string
    isCustom: boolean
  }
  createdAt: Date
  updatedAt: Date
}

Company (company.schema.ts)

{
  businessName: string (required)
  abn: string (unique, required)
  user: ObjectId (ref: User)
  calendar_access_token?: string
  createdAt: Date
  updatedAt: Date
}

Service (service.schema.ts)

{
  name: string
  description: string
  price: number
  duration: number (minutes)
  userId: string
  isDeleted?: boolean
  createdAt: Date
  updatedAt: Date
}

ServiceBooking (service-booking.schema.ts)

{
  serviceId: string
  client: {
    name: string
    phoneNumber: string
    address: string
  }
  serviceFormValues: {
    serviceFieldId: string
    answer: string
  }[]
  status: 'Cancelled' | 'Confirmed' | 'Done'
  note: string
  bookingTime: Date
  userId: string
  callSid?: string
  createdAt: Date
  updatedAt: Date
}

CallLog (calllog.schema.ts)

{
  userId: string
  callerNumber: string
  duration: number (seconds)
  status: 'completed' | 'no-answer' | 'busy' | 'failed'
  serviceBookedId?: string
  recordingUrl?: string
  transcriptionUrl?: string
  startAt: Date
  createdAt: Date
  updatedAt: Date
}

Transcript (transcript.schema.ts)

{
  callSid: string
  userId: string
  fullTranscript: string
  summary?: string
  keyPoints?: string[]
  createdAt: Date
  updatedAt: Date
}

Plan (plan.schema.ts)

{
  name: string (unique, required)
  tier: 'FREE' | 'BASIC' | 'PRO'
  pricing: {
    rrule: string
    price: number
    stripePriceId: string
  }[]
  features: {
    callMinutes: string
    support: string
  }
  isActive: boolean
}

Subscription (subscription.schema.ts)

{
  userId: ObjectId (ref: User)
  planId: ObjectId (ref: Plan)
  subscriptionId?: string
  stripeCustomerId?: string
  chargeId?: string
  startAt: Date
  endAt: Date
  status: 'active' | 'failed' | 'cancelled'
  createdAt: Date
  updatedAt: Date
}

πŸ§ͺ Testing

Test Structure

test/
β”œβ”€β”€ fixtures/           # Test data & factories
β”œβ”€β”€ helpers/           # Test utilities (DB helper)
β”œβ”€β”€ integration/       # Integration tests
β”‚   β”œβ”€β”€ calendar/
β”‚   β”œβ”€β”€ calllog/
β”‚   β”œβ”€β”€ plan/
β”‚   β”œβ”€β”€ subscription/
β”‚   └── transcript/
└── unit/              # Unit tests
    β”œβ”€β”€ calendar/
    β”œβ”€β”€ calllog/
    β”œβ”€β”€ common/
    β”œβ”€β”€ plan/
    β”œβ”€β”€ subscription/
    └── transcript/

Running Tests

cd apps/backend

# Run all tests
pnpm test

# Run unit tests only
pnpm test:unit

# Run integration tests only
pnpm test:integration

# Watch mode
pnpm test:watch

# With coverage
pnpm test -- --coverage

# Specific test file
pnpm test -- src/modules/auth/auth.service.spec.ts

Database Seeding

# Run all seeds
pnpm seed

# Seed telephony test data
pnpm seed:telephony

# Seed call logs
pnpm seed:calllog

πŸ”§ Configuration

Environment Variables

Required:

# MongoDB
MONGODB_URI=mongodb://mongo:27017/dispatchai

# Redis
REDIS_HOST=redis
REDIS_PORT=6379

# JWT
JWT_SECRET=your_jwt_secret
JWT_EXPIRES_IN=7d

# Twilio
TWILIO_ACCOUNT_SID=ACxxx...
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_PHONE_NUMBER=+1234567890

Optional:

# Port
PORT=4000

# CORS
CORS_ORIGIN=http://localhost:3000

# Google OAuth
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REDIRECT_URI=http://localhost:4000/api/google-calendar/callback

# Stripe
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...

# AI Service
AI_SERVICE_URL=http://localhost:8000/api

Configuration Files

.eslintrc.js: ESLint configuration
tsconfig.json: TypeScript compiler options
jest.config.json: Jest test configuration
nest-cli.json: NestJS CLI settings

πŸš€ Development

Local Development

  1. Install dependencies:

    cd apps/backend
    pnpm install

    Note: This project uses pnpm as the package manager. If you don't have pnpm installed:

    npm install -g pnpm
  2. Set up environment:

    # Create .env.local
    cp .env.example .env.local
    # Edit with your values
  3. Start MongoDB & Redis:

    docker compose up mongo redis
  4. Run in watch mode:

    pnpm dev
  5. Access services:

Code Quality

# Lint code
pnpm lint

# Lint source only
pnpm lint:src

# Lint tests only
pnpm lint:test

# Type checking
pnpm type-check

Docker Development

# Build image
docker build -f Dockerfile.dev -t dispatchai-backend:dev .

# Run container
docker run -p 4000:4000 dispatchai-backend:dev

# Or use docker-compose
docker compose up api

πŸ”— Key Integrations

Twilio Voice API

Webhooks:

  • /telephony/voice - Initial call
  • /telephony/gather - Speech input
  • /telephony/status - Call status updates

Features:

  • Call routing & handling
  • Speech recognition
  • Text-to-speech
  • Recording

References:

  • src/modules/telephony/
  • src/lib/twilio/

Stripe Payment

Features:

  • Subscription management
  • Webhook handling
  • Customer portal

References:

  • src/modules/stripe/
  • src/modules/plan/
  • src/modules/subscription/

Google Calendar API

Features:

  • OAuth 2.0 flow
  • Token storage
  • Event creation
  • Calendar sync

References:

  • src/modules/google-calendar/

AI Service Integration

Usage:

import { AiHttpModule } from '@/lib/ai/ai-http.module';

@Module({
  imports: [AiHttpModule],
})
export class MyModule {}

References:

  • src/lib/ai/

Redis Usage

Features:

  • Session storage
  • CallSkeleton caching
  • Rate limiting

References:

  • src/lib/redis/

πŸ“š Module Deep Dive

Authentication Module

Strategies:

  • JWT strategy (Passport)
  • Google OAuth strategy

Guards:

  • JWT auth guard
  • CSRF guard

Files:

  • src/modules/auth/auth.module.ts
  • src/modules/auth/auth.service.ts
  • src/modules/auth/strategies/jwt.strategy.ts
  • src/modules/auth/strategies/google.strategy.ts

Telephony Module

Process Flow:

  1. Incoming call β†’ /telephony/voice
  2. Gather speech β†’ /telephony/gather
  3. Send to AI service
  4. Generate TwiML response
  5. Play AI response to customer

Files:

  • src/modules/telephony/telephony.module.ts
  • src/modules/telephony/telephony.service.ts
  • src/modules/telephony/services/call-processor.service.ts

Transcript Module

Features:

  • Store full transcripts
  • Generate summaries
  • Extract key points
  • Chunk management

Files:

  • src/modules/transcript/transcript.service.ts
  • src/modules/transcript-chunk/transcript-chunk.service.ts

πŸ› Common Issues

Issue: MongoDB Connection Failed

Fix: Ensure MongoDB is running and MONGODB_URI is correct

Issue: Redis Connection Failed

Fix: Ensure Redis is running and accessible

Issue: JWT Token Invalid

Fix: Check JWT_SECRET matches between services

Issue: Twilio Webhook Fails

Fix: Verify webhook URL in Twilio console, check authentication

Issue: Stripe Webhook Signature Invalid

Fix: Ensure STRIPE_WEBHOOK_SECRET matches Stripe dashboard

πŸ“– Additional Resources

🀝 Contributing

When adding new modules:

  1. Generate module with Nest CLI: nest g module module-name
  2. Create schema in schema/
  3. Add service in module root
  4. Add controller in module root
  5. Create DTOs in dto/
  6. Write tests in test/unit/
  7. Update app.module.ts
  8. Document in Swagger

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Contributors