Overview
Overview
Section titled “Overview”-
What the feature does
- Persists and exposes per-user AI Chatbot settings (
AiChatbot) and operating schedules (AiOperatingSchedule). - Provides authenticated REST endpoints to:
- create/read/update/delete the current user’s chatbot settings
- add/list/update/delete operating schedules
- Auto-attaches a user’s
GptPrompt(if one exists) to the chatbot settings record. - Stores optional “enhancement” configuration via
WorkflowEnhancementlinked 1:1 to the chatbot.
- Persists and exposes per-user AI Chatbot settings (
-
Why it exists (business purpose)
- Lets customers control AI automation behavior safely:
- turn the chatbot on/off
- restrict when it is allowed to run (operating hours)
- configure model choice and max message limits
- Centralizes configuration so downstream AI execution can read a single source of truth for a user.
- Lets customers control AI automation behavior safely:
-
Who uses it
- Product managers: define supported configuration knobs (toggles, scheduling behavior, limits).
- Developers: implement UI and backend integrations against stable endpoints/contracts.
- Software architects: reason about dependencies, lifecycle, and failure modes when changing AI runtime behavior.
Key Concepts
Section titled “Key Concepts”-
AiChatbot(settings record)- Entity:
src/ai-chatbot/entities/ai-chatbot.entity.ts - Constraint: one row per user enforced via
@Unique(['user']). - Key fields:
modelType?: ModelType(src/constants)isChatbotOn: booleanisOperatingHour: booleanchatMaxMessages?: number | null- optional relations:
prompt,chatbotEnhancement,operatingSchedule
- Entity:
-
AiOperatingSchedule(operating hours entries)- Entity:
src/ai-chatbot/entities/ai-operating-schedule.entity.ts - Key fields:
day: number(stored as float in entity; DTOs treat it as int)startHour: string,endHour: string(service expectsHH:mm)timezone: string(IANA timezone, e.g.America/New_York)
- Rule: no overlapping schedules for the same
day(validated in service).
- Entity:
-
Enhancement
- Entity:
WorkflowEnhancement(src/workflow-enhancements/entities/workflow-enhancement.entity.ts) - Input type used here:
CreateWorkflowEnhancementInputimported byCreateAiChatbotDto. - Special handling:
is_new_templateis treated like a relation id in the service ({ id: ... } as any).
- Entity:
-
Prompt
- Entity:
GptPrompt(src/gpt-prompt/entities/gpt-prompt.entity.ts) - Selection behavior: the service automatically attaches a prompt for the user if one exists.
- Entity:
System Context
Section titled “System Context”-
Module boundary
- Module:
src/ai-chatbot/ai-chatbot.module.ts - Controller:
src/ai-chatbot/ai-chatbot.controller.ts - Service:
src/ai-chatbot/ai-chatbot.service.ts
- Module:
-
Dependencies
- Auth:
JwtAuthGuard(src/common/auth/jwt-auth.guard.ts) andgetUserId(req)(src/utils/shared.utils.ts) - Persistence: TypeORM repositories for
AiChatbot,AiOperatingSchedule,WorkflowEnhancement,GptPrompt - Timezone operations:
date-fns-tzfunctionsformatInTimeZone,fromZonedTimein schedule validation
- Auth:
High-Level Flow
Section titled “High-Level Flow”- Client calls an endpoint under
AiChatbotController(/ai-chatbot/...). JwtAuthGuardauthenticates;getUserId(req)resolvesuserId.- Controller delegates to
AiChatbotService. - Service loads and mutates state in the DB:
- settings (
AiChatbot) - enhancement (
WorkflowEnhancement) if provided - schedules (
AiOperatingSchedule) with overlap validation - prompt link (
GptPrompt) if present
- settings (
- Response returns the updated settings or schedule data.
Business Flows
Section titled “Business Flows”Happy path
Section titled “Happy path”-
Create chatbot settings
- User saves initial settings with optional enhancement and schedules.
- Service saves settings, then schedules, and returns settings with relations via
findByUser.
-
Update chatbot settings
- User changes toggles/model/limits and optionally updates enhancement and schedules.
- Service validates schedule changes, saves updates, returns settings with relations.
-
Manage operating schedules
- User adds a batch of schedules, lists schedules, edits one schedule, or deletes one.
- Service enforces ownership and overlap rules and persists changes.
Alternate paths
Section titled “Alternate paths”-
No prompt exists
- Settings are still saved;
promptstays unset.
- Settings are still saved;
-
No enhancement
- Settings are saved without
chatbotEnhancement.
- Settings are saved without
-
No schedules
- Settings are saved with no schedules; schedule endpoints return an empty list (or
NOT_FOUNDif chatbot is missing).
- Settings are saved with no schedules; schedule endpoints return an empty list (or
Failure scenarios
Section titled “Failure scenarios”-
Update settings before creation
PATCH /ai-chatbotfails withBAD_REQUEST(“Chatbot must be created first”).
-
Schedule validation failures
- Start time equals end time →
BAD_REQUEST. - End time is not after start time (in timezone) →
BAD_REQUEST. - Overlaps another schedule on the same
day→BAD_REQUEST.
- Start time equals end time →
-
Unauthorized schedule access
- Fetching/updating a schedule not owned by the user →
FORBIDDEN.
- Fetching/updating a schedule not owned by the user →