Skip to content

API & Data Model

Controller: src/ai-chatbot/ai-chatbot.controller.ts
Base path: /ai-chatbot

  • POST /ai-chatbot

    • Creates the current user’s chatbot settings.
    • Body: CreateAiChatbotDto (src/ai-chatbot/dto/create-ai-chatbot.dto.ts)
  • GET /ai-chatbot

    • Returns current user’s chatbot settings including relations.
  • PATCH /ai-chatbot

    • Updates current user’s chatbot settings (and can mutate enhancement/schedules).
    • Body: UpdateAiChatbotDto (src/ai-chatbot/dto/update-ai-chatbot.dto.ts)
    • If no chatbot exists yet: BAD_REQUEST (“Chatbot must be created first”).
  • DELETE /ai-chatbot

    • Deletes current user’s chatbot settings (schedules cascade delete).

Schedule endpoints:

  • POST /ai-chatbot/schedule

    • Adds one or more schedules (batch).
    • Body: CreateAiOperatingScheduleDto[] (src/ai-chatbot/dto/create-operating-schedule.dto.ts)
  • GET /ai-chatbot/schedules

    • Lists schedules for current user.
  • GET /ai-chatbot/schedule/:id

    • Fetches one schedule by id (ownership enforced).
  • PATCH /ai-chatbot/schedule/:id

    • Updates one schedule by id.
    • Body: UpdateAiOperatingScheduleDto (src/ai-chatbot/dto/update-operating-schedule.dto.ts)
  • DELETE /ai-chatbot/schedule/:id

    • Deletes one schedule by id (ownership enforced).
  • CreateAiChatbotDto fields (all optional):

    • operatingSchedule?: UpdateAiOperatingScheduleDto[]
    • modelType?: ModelType
    • isChatbotOn?: boolean
    • isOperatingHour?: boolean
    • chatMaxMessages?: number
    • chatbotEnhancement?: CreateWorkflowEnhancementInput
  • UpdateAiChatbotDto

    • PartialType(CreateAiChatbotDto) (all fields optional)
  • CreateAiOperatingScheduleDto

    • day: number
    • startHour: string
    • endHour: string
    • timezone: string
  • UpdateAiOperatingScheduleDto

    • Partial schedule fields + id?: number

Validation rules (as implemented/intended)

Section titled “Validation rules (as implemented/intended)”

From DTOs and service logic:

  • DTOs use class-validator decorators (intended runtime validation).
    • Note: runtime enforcement depends on global ValidationPipe configuration.
  • Service-level schedule rules (validateTime(...)):
    • start and end must not be equal
    • end must be after start (after timezone conversion)
    • no overlaps on the same day (excluding the current schedule id on updates)
  • Auth: JwtAuthGuard on the controller (src/common/auth/jwt-auth.guard.ts).
  • User scoping: getUserId(req) (src/utils/shared.utils.ts).
  • Schedule authorization:
    • schedule reads/updates require schedule.ai_chatbot.user.id === userId
    • else FORBIDDEN

Service: src/ai-chatbot/ai-chatbot.service.ts

  • Settings

    • create(user: User, dto: CreateAiChatbotDto)
    • findByUser(userId: number)
    • update(userId: number, dto: UpdateAiChatbotDto)
    • remove(userId: number)
  • Schedules

    • addSchedulesToChatbot(userId: number, schedules: CreateAiOperatingScheduleDto[])
    • getSchedules(userId: number)
    • getScheduleById(userId: number, scheduleId: number)
    • updateSchedule(scheduleId: number, userId: number, dto: UpdateAiOperatingScheduleDto)
    • deleteScheduleFromChatbot(userId: number, scheduleId: number)

Cross-module dependencies used directly in the service:

  • GptPrompt repository (auto-link user prompt)
  • WorkflowEnhancement repository (enhancement upsert)

Entity: src/ai-chatbot/entities/ai-chatbot.entity.ts

  • Identifiers and constraints

    • id (PK)
    • unique per user: @Unique(['user'])
  • Key fields

    • modelType?: ModelType
    • isChatbotOn: boolean
    • isOperatingHour: boolean
    • chatMaxMessages?: number | null
    • optional WhatsApp linkage: whatsapp_account_id, whatsapp_account
  • Key relationships

    • user (1:1, required; CASCADE on user delete)
    • prompt (1:1 to GptPrompt, nullable; SET NULL on prompt delete)
    • chatbotEnhancement (1:1 to WorkflowEnhancement, cascade)
    • operatingSchedule (1:many to AiOperatingSchedule, cascade)

Table: ai_operating_schedule (AiOperatingSchedule)

Section titled “Table: ai_operating_schedule (AiOperatingSchedule)”

Entity: src/ai-chatbot/entities/ai-operating-schedule.entity.ts

  • Key fields

    • id (PK)
    • day (float column in entity)
    • startHour, endHour (varchar)
    • timezone (varchar)
  • Relationship

    • ai_chatbot (many-to-one; required; CASCADE on chatbot delete)

  • All endpoints are protected by JwtAuthGuard (src/common/auth/jwt-auth.guard.ts).
  • User scoping is done by getUserId(req) (src/utils/shared.utils.ts).
  • Schedule operations enforce ownership; cross-user access returns FORBIDDEN.

DTOs use class-validator, but runtime enforcement depends on whether a Nest ValidationPipe is enabled in main.ts.

Controller: src/ai-chatbot/ai-chatbot.controller.ts
Base path: /ai-chatbot

  • POST /ai-chatbot

    • Creates the current user’s settings.
    • Auto-links a user GptPrompt if present.
    • Optionally persists enhancement and schedules.
  • GET /ai-chatbot

    • Returns current user’s settings, including relations:
      • prompt, chatbotEnhancement, operatingSchedule
  • PATCH /ai-chatbot

    • Updates current user’s settings.
    • Can also update/create schedules and upsert enhancement.
    • Fails with BAD_REQUEST if settings do not exist yet.
  • DELETE /ai-chatbot

    • Deletes current user’s settings (schedules are cascade-deleted).
  • POST /ai-chatbot/schedule

    • Adds one or more schedules (batch).
    • Validates no overlaps against existing schedules.
  • GET /ai-chatbot/schedules

    • Lists all schedules for the current user.
  • GET /ai-chatbot/schedule/:id

    • Fetches a single schedule by id (ownership enforced).
  • PATCH /ai-chatbot/schedule/:id

    • Updates one schedule by id.
    • Validates overlap against the chatbot’s other schedules.
  • DELETE /ai-chatbot/schedule/:id

    • Deletes one schedule by id (scoped to the current user’s chatbot).

DTOs live under src/ai-chatbot/dto/:

  • CreateAiChatbotDto

    • operatingSchedule?: UpdateAiOperatingScheduleDto[]
    • modelType?: ModelType
    • isChatbotOn?: boolean
    • isOperatingHour?: boolean
    • chatMaxMessages?: number
    • chatbotEnhancement?: CreateWorkflowEnhancementInput
  • UpdateAiChatbotDto

    • PartialType(CreateAiChatbotDto) (all fields optional)
  • CreateAiOperatingScheduleDto

    • day: number
    • startHour: string
    • endHour: string
    • timezone: string
  • UpdateAiOperatingScheduleDto

    • Partial schedule fields + id?: number

Service: src/ai-chatbot/ai-chatbot.service.ts

  • Settings:
    • create(user: User, dto: CreateAiChatbotDto)
    • findByUser(userId: number)
    • update(userId: number, dto: UpdateAiChatbotDto)
    • remove(userId: number)
  • Schedules:
    • addSchedulesToChatbot(userId: number, schedules: CreateAiOperatingScheduleDto[])
    • getSchedules(userId: number)
    • getScheduleById(userId: number, scheduleId: number)
    • updateSchedule(scheduleId: number, userId: number, dto: UpdateAiOperatingScheduleDto)
    • deleteScheduleFromChatbot(userId: number, scheduleId: number)

Cross-module dependencies:

  • WorkflowEnhancement + CreateWorkflowEnhancementInput
  • GptPrompt

Entity: src/ai-chatbot/entities/ai-chatbot.entity.ts

  • id (PK)
  • user (1:1, required, unique)
  • modelType?: ModelType (enum)
  • isChatbotOn: boolean
  • isOperatingHour: boolean
  • chatMaxMessages?: number | null
  • prompt?: GptPrompt (nullable)
  • chatbotEnhancement?: WorkflowEnhancement (1:1)
  • operatingSchedule: AiOperatingSchedule[] (1:many)
  • optional WhatsApp linkage: whatsapp_account_id, whatsapp_account
  • timestamps: createdAt, updatedAt

ai_operating_schedule (AiOperatingSchedule)

Section titled “ai_operating_schedule (AiOperatingSchedule)”

Entity: src/ai-chatbot/entities/ai-operating-schedule.entity.ts

  • id (PK)
  • day: number (stored as float in the entity)
  • startHour: string
  • endHour: string
  • timezone: string
  • ai_chatbot: AiChatbot (many-to-one, required; CASCADE delete)
  • User (1) -> (1) AiChatbot (unique)
  • AiChatbot (1) -> (many) AiOperatingSchedule
  • AiChatbot (1) -> (0..1) GptPrompt
  • AiChatbot (1) -> (0..1) WorkflowEnhancement

  • Auth guard: JwtAuthGuard is applied to the entire controller.
    • File: src/ai-chatbot/ai-chatbot.controller.ts
  • User scoping: all reads and writes are scoped to the authenticated user via:
    • getUserId(req) from src/utils/shared.utils.ts
  • Ownership enforcement
    • Schedule reads/updates enforce that the schedule belongs to the current user.
    • Error: FORBIDDEN when trying to access another user’s schedule.

DTOs use class-validator, but runtime enforcement depends on whether a Nest ValidationPipe is enabled in main.ts.

Controller: src/ai-chatbot/ai-chatbot.controller.ts
Base path: /ai-chatbot

  • Request body: CreateAiChatbotDto (src/ai-chatbot/dto/create-ai-chatbot.dto.ts)
    • operatingSchedule?: UpdateAiOperatingScheduleDto[]
    • modelType?: ModelType
    • isChatbotOn?: boolean
    • isOperatingHour?: boolean
    • chatMaxMessages?: number
    • chatbotEnhancement?: CreateWorkflowEnhancementInput
  • Behavior
    • Creates AiChatbot for the current user.
    • Auto-links GptPrompt for the user if present.
    • Optionally creates WorkflowEnhancement.
    • If schedules are provided, saves schedule entries; on schedule-save failure, deletes the chatbot record (manual rollback).

GET /ai-chatbot — Get current user settings

Section titled “GET /ai-chatbot — Get current user settings”
  • Response
    • Returns AiChatbot including relations:
      • prompt
      • chatbotEnhancement
      • operatingSchedule
  • Failure
    • NOT_FOUND if no chatbot settings exist for the user.

PATCH /ai-chatbot — Update settings (and optional enhancement/schedules)

Section titled “PATCH /ai-chatbot — Update settings (and optional enhancement/schedules)”
  • Request body: UpdateAiChatbotDto (src/ai-chatbot/dto/update-ai-chatbot.dto.ts)
    • A PartialType(CreateAiChatbotDto) (all fields optional).
  • Behavior
    • Updates scalar fields on the chatbot settings.
    • Auto-links GptPrompt for the user if present.
    • Upserts WorkflowEnhancement if provided.
    • If operatingSchedule is provided:
      • items with id update existing schedules (ownership + overlap validation)
      • items without id create new schedules (overlap validation)
  • Failure
    • BAD_REQUEST if the chatbot record does not exist yet.
    • FORBIDDEN if schedule ownership validation fails.
    • BAD_REQUEST for invalid schedule timing (overlap, end <= start, etc.).
  • Behavior
    • Removes the user’s AiChatbot row.
    • Schedules are deleted by DB cascade.

POST /ai-chatbot/schedule — Add schedules (batch)

Section titled “POST /ai-chatbot/schedule — Add schedules (batch)”
  • Request body: CreateAiOperatingScheduleDto[] (src/ai-chatbot/dto/create-operating-schedule.dto.ts)
    • day: number
    • startHour: string
    • endHour: string
    • timezone: string
  • Behavior
    • Validates each new schedule against existing schedules (no overlaps).
    • Creates new schedules linked to the user’s chatbot.

GET /ai-chatbot/schedules — List schedules

Section titled “GET /ai-chatbot/schedules — List schedules”
  • Response: AiOperatingSchedule[]

GET /ai-chatbot/schedule/:id — Get one schedule

Section titled “GET /ai-chatbot/schedule/:id — Get one schedule”
  • Behavior
    • Enforces ownership by checking schedule.ai_chatbot.user.id.

PATCH /ai-chatbot/schedule/:id — Update one schedule

Section titled “PATCH /ai-chatbot/schedule/:id — Update one schedule”
  • Request body: UpdateAiOperatingScheduleDto (src/ai-chatbot/dto/update-operating-schedule.dto.ts)
    • Partial schedule fields + optional id?: number
  • Behavior
    • Computes effective day/time/timezone from DTO or existing schedule.
    • Validates overlap (excluding the schedule itself).
    • Saves updates.

DELETE /ai-chatbot/schedule/:id — Delete one schedule

Section titled “DELETE /ai-chatbot/schedule/:id — Delete one schedule”
  • Behavior
    • Deletes only if the schedule belongs to the user’s chatbot.

Service file: src/ai-chatbot/ai-chatbot.service.ts

  • create(user: User, dto: CreateAiChatbotDto)
  • findByUser(userId: number)
  • update(userId: number, dto: UpdateAiChatbotDto)
  • remove(userId: number)
  • Schedules:
    • addSchedulesToChatbot(userId: number, schedules: CreateAiOperatingScheduleDto[])
    • getSchedules(userId: number)
    • getScheduleById(userId: number, scheduleId: number)
    • updateSchedule(scheduleId: number, userId: number, dto: UpdateAiOperatingScheduleDto)
    • deleteScheduleFromChatbot(userId: number, scheduleId: number)

Cross-module dependencies:

  • WorkflowEnhancement + CreateWorkflowEnhancementInput (enhancement configuration)
  • GptPrompt (auto-linked user prompt)

Entity: src/ai-chatbot/entities/ai-chatbot.entity.ts

  • id (PK)
  • user (1:1, required, unique; cascades on user delete)
  • modelType?: ModelType
  • isChatbotOn: boolean
  • isOperatingHour: boolean
  • chatMaxMessages?: number | null
  • prompt?: GptPrompt (nullable, SET NULL on prompt delete)
  • chatbotEnhancement?: WorkflowEnhancement (1:1, cascade)
  • operatingSchedule: AiOperatingSchedule[] (1:many, cascade)
  • whatsapp_account_id?: number + whatsapp_account?: WhatsAppAccount (nullable, SET NULL on delete)
  • createdAt, updatedAt

ai_operating_schedule (AiOperatingSchedule)

Section titled “ai_operating_schedule (AiOperatingSchedule)”

Entity: src/ai-chatbot/entities/ai-operating-schedule.entity.ts

  • id (PK)
  • day: number (entity stores as float)
  • startHour: string
  • endHour: string
  • timezone: string
  • ai_chatbot: AiChatbot (many-to-one; required; CASCADE delete)
  • User (1) -> (1) AiChatbot (unique)
  • AiChatbot (1) -> (many) AiOperatingSchedule
  • AiChatbot (1) -> (0..1) GptPrompt
  • AiChatbot (1) -> (0..1) WorkflowEnhancement
  • AiChatbot (0..1) -> (0..1) WhatsAppAccount