API & Data Model
API / Interfaces
Section titled “API / Interfaces”Endpoints
Section titled “Endpoints”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).
Request/response structures
Section titled “Request/response structures”Settings DTOs
Section titled “Settings DTOs”-
CreateAiChatbotDtofields (all optional):operatingSchedule?: UpdateAiOperatingScheduleDto[]modelType?: ModelTypeisChatbotOn?: booleanisOperatingHour?: booleanchatMaxMessages?: numberchatbotEnhancement?: CreateWorkflowEnhancementInput
-
UpdateAiChatbotDtoPartialType(CreateAiChatbotDto)(all fields optional)
Schedule DTOs
Section titled “Schedule DTOs”-
CreateAiOperatingScheduleDtoday: numberstartHour: stringendHour: stringtimezone: string
-
UpdateAiOperatingScheduleDto- Partial schedule fields +
id?: number
- Partial schedule fields +
Validation rules (as implemented/intended)
Section titled “Validation rules (as implemented/intended)”From DTOs and service logic:
- DTOs use
class-validatordecorators (intended runtime validation).- Note: runtime enforcement depends on global
ValidationPipeconfiguration.
- Note: runtime enforcement depends on global
- 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)
Authentication/authorization
Section titled “Authentication/authorization”- Auth:
JwtAuthGuardon 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
- schedule reads/updates require
Internal Interfaces
Section titled “Internal Interfaces”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:
GptPromptrepository (auto-link user prompt)WorkflowEnhancementrepository (enhancement upsert)
Data Model
Section titled “Data Model”Table: ai_chatbot (AiChatbot)
Section titled “Table: ai_chatbot (AiChatbot)”Entity: src/ai-chatbot/entities/ai-chatbot.entity.ts
-
Identifiers and constraints
id(PK)- unique per user:
@Unique(['user'])
-
Key fields
modelType?: ModelTypeisChatbotOn: booleanisOperatingHour: booleanchatMaxMessages?: number | null- optional WhatsApp linkage:
whatsapp_account_id,whatsapp_account
-
Key relationships
user(1:1, required; CASCADE on user delete)prompt(1:1 toGptPrompt, nullable; SET NULL on prompt delete)chatbotEnhancement(1:1 toWorkflowEnhancement, cascade)operatingSchedule(1:many toAiOperatingSchedule, 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)
title: API & Data Model sidebar: order: 3
Section titled “title: API & Data Model sidebar: order: 3”API / Interfaces
Section titled “API / Interfaces”Authentication/authorization
Section titled “Authentication/authorization”- 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 NestValidationPipeis enabled inmain.ts.
REST endpoints
Section titled “REST endpoints”Controller: src/ai-chatbot/ai-chatbot.controller.ts
Base path: /ai-chatbot
Settings
Section titled “Settings”-
POST /ai-chatbot- Creates the current user’s settings.
- Auto-links a user
GptPromptif present. - Optionally persists enhancement and schedules.
-
GET /ai-chatbot- Returns current user’s settings, including relations:
prompt,chatbotEnhancement,operatingSchedule
- Returns current user’s settings, including relations:
-
PATCH /ai-chatbot- Updates current user’s settings.
- Can also update/create schedules and upsert enhancement.
- Fails with
BAD_REQUESTif settings do not exist yet.
-
DELETE /ai-chatbot- Deletes current user’s settings (schedules are cascade-deleted).
Operating schedules
Section titled “Operating schedules”-
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).
Request structures (DTOs)
Section titled “Request structures (DTOs)”DTOs live under src/ai-chatbot/dto/:
-
CreateAiChatbotDtooperatingSchedule?: UpdateAiOperatingScheduleDto[]modelType?: ModelTypeisChatbotOn?: booleanisOperatingHour?: booleanchatMaxMessages?: numberchatbotEnhancement?: CreateWorkflowEnhancementInput
-
UpdateAiChatbotDtoPartialType(CreateAiChatbotDto)(all fields optional)
-
CreateAiOperatingScheduleDtoday: numberstartHour: stringendHour: stringtimezone: string
-
UpdateAiOperatingScheduleDto- Partial schedule fields +
id?: number
- Partial schedule fields +
Internal Interfaces
Section titled “Internal Interfaces”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+CreateWorkflowEnhancementInputGptPrompt
Data Model
Section titled “Data Model”ai_chatbot (AiChatbot)
Section titled “ai_chatbot (AiChatbot)”Entity: src/ai-chatbot/entities/ai-chatbot.entity.ts
id(PK)user(1:1, required, unique)modelType?: ModelType(enum)isChatbotOn: booleanisOperatingHour: booleanchatMaxMessages?: number | nullprompt?: 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: stringendHour: stringtimezone: stringai_chatbot: AiChatbot(many-to-one, required; CASCADE delete)
Relationships
Section titled “Relationships”User (1) -> (1) AiChatbot(unique)AiChatbot (1) -> (many) AiOperatingScheduleAiChatbot (1) -> (0..1) GptPromptAiChatbot (1) -> (0..1) WorkflowEnhancement
title: API & Data Model
Section titled “title: API & Data Model”API / Interfaces
Section titled “API / Interfaces”Authentication/authorization
Section titled “Authentication/authorization”- Auth guard:
JwtAuthGuardis applied to the entire controller.- File:
src/ai-chatbot/ai-chatbot.controller.ts
- File:
- User scoping: all reads and writes are scoped to the authenticated user via:
getUserId(req)fromsrc/utils/shared.utils.ts
- Ownership enforcement
- Schedule reads/updates enforce that the schedule belongs to the current user.
- Error:
FORBIDDENwhen trying to access another user’s schedule.
DTOs use
class-validator, but runtime enforcement depends on whether a NestValidationPipeis enabled inmain.ts.
Endpoints
Section titled “Endpoints”Controller: src/ai-chatbot/ai-chatbot.controller.ts
Base path: /ai-chatbot
POST /ai-chatbot — Create settings
Section titled “POST /ai-chatbot — Create settings”- Request body:
CreateAiChatbotDto(src/ai-chatbot/dto/create-ai-chatbot.dto.ts)operatingSchedule?: UpdateAiOperatingScheduleDto[]modelType?: ModelTypeisChatbotOn?: booleanisOperatingHour?: booleanchatMaxMessages?: numberchatbotEnhancement?: CreateWorkflowEnhancementInput
- Behavior
- Creates
AiChatbotfor the current user. - Auto-links
GptPromptfor the user if present. - Optionally creates
WorkflowEnhancement. - If schedules are provided, saves schedule entries; on schedule-save failure, deletes the chatbot record (manual rollback).
- Creates
GET /ai-chatbot — Get current user settings
Section titled “GET /ai-chatbot — Get current user settings”- Response
- Returns
AiChatbotincluding relations:promptchatbotEnhancementoperatingSchedule
- Returns
- Failure
NOT_FOUNDif 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).
- A
- Behavior
- Updates scalar fields on the chatbot settings.
- Auto-links
GptPromptfor the user if present. - Upserts
WorkflowEnhancementif provided. - If
operatingScheduleis provided:- items with
idupdate existing schedules (ownership + overlap validation) - items without
idcreate new schedules (overlap validation)
- items with
- Failure
BAD_REQUESTif the chatbot record does not exist yet.FORBIDDENif schedule ownership validation fails.BAD_REQUESTfor invalid schedule timing (overlap, end <= start, etc.).
DELETE /ai-chatbot — Delete settings
Section titled “DELETE /ai-chatbot — Delete settings”- Behavior
- Removes the user’s
AiChatbotrow. - Schedules are deleted by DB cascade.
- Removes the user’s
Operating schedule endpoints
Section titled “Operating schedule endpoints”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: numberstartHour: stringendHour: stringtimezone: 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.
- Enforces ownership by checking
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
- Partial schedule fields + optional
- 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.
Internal Interfaces
Section titled “Internal Interfaces”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)
Data Model
Section titled “Data Model”ai_chatbot (AiChatbot)
Section titled “ai_chatbot (AiChatbot)”Entity: src/ai-chatbot/entities/ai-chatbot.entity.ts
id(PK)user(1:1, required, unique; cascades on user delete)modelType?: ModelTypeisChatbotOn: booleanisOperatingHour: booleanchatMaxMessages?: number | nullprompt?: 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: stringendHour: stringtimezone: stringai_chatbot: AiChatbot(many-to-one; required; CASCADE delete)
Relationships
Section titled “Relationships”User (1) -> (1) AiChatbot(unique)AiChatbot (1) -> (many) AiOperatingScheduleAiChatbot (1) -> (0..1) GptPromptAiChatbot (1) -> (0..1) WorkflowEnhancementAiChatbot (0..1) -> (0..1) WhatsAppAccount