2026-06-03 15:01:34 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-06-09 10:16:04 +07:00
|
|
|
import type { Request, Response } from "express";
|
2026-06-09 19:46:08 +07:00
|
|
|
import { asyncHandler } from "../../shared/middlewares/index.js";
|
2026-06-03 15:01:34 +07:00
|
|
|
import { mascotChatService } from "./mascot-chat.service.js";
|
|
|
|
|
|
|
|
|
|
const logger = createChildLogger("mascot-chat.controller");
|
|
|
|
|
|
2026-06-09 13:07:01 +07:00
|
|
|
interface AuthenticatedRequest extends Request {
|
|
|
|
|
userId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
export const handleMascotChat = asyncHandler(
|
|
|
|
|
async (req: Request, res: Response) => {
|
2026-07-02 03:54:44 +07:00
|
|
|
const { message, context } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!message || typeof message !== "string") {
|
2026-06-03 15:01:34 +07:00
|
|
|
return res.status(400).json({
|
|
|
|
|
error: "INVALID_INPUT",
|
2026-07-02 03:54:44 +07:00
|
|
|
message: "Message is required and must be a string",
|
2026-06-03 15:01:34 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get user ID from auth middleware (if available)
|
2026-06-09 13:07:01 +07:00
|
|
|
const userId = (req as AuthenticatedRequest).userId || "anonymous";
|
2026-06-03 15:01:34 +07:00
|
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
|
{ userId, messageLength: message.length, context },
|
2026-06-09 10:16:04 +07:00
|
|
|
"Received mascot chat message",
|
2026-06-03 15:01:34 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Process message & generate response
|
2026-06-09 10:16:04 +07:00
|
|
|
const response = await mascotChatService.processMessage(
|
|
|
|
|
message,
|
|
|
|
|
context,
|
|
|
|
|
userId,
|
|
|
|
|
);
|
2026-06-03 15:01:34 +07:00
|
|
|
|
|
|
|
|
// Save conversation to database
|
|
|
|
|
await mascotChatService.saveConversation({
|
|
|
|
|
userId,
|
|
|
|
|
userMessage: message,
|
|
|
|
|
mascotResponse: response,
|
|
|
|
|
context,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
logger.info({ userId }, "Mascot chat processed successfully");
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
response,
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
});
|
2026-06-09 19:46:08 +07:00
|
|
|
},
|
|
|
|
|
);
|
2026-06-03 15:01:34 +07:00
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
export const getMascotChatHistory = asyncHandler(
|
|
|
|
|
async (req: Request, res: Response) => {
|
2026-06-09 13:07:01 +07:00
|
|
|
const userId = (req as AuthenticatedRequest).userId || "anonymous";
|
2026-06-03 15:01:34 +07:00
|
|
|
const limit = Math.min(parseInt(req.query.limit as string) || 50, 100);
|
|
|
|
|
|
|
|
|
|
const history = await mascotChatService.getChatHistory(userId, limit);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
history,
|
|
|
|
|
total: history.length,
|
|
|
|
|
});
|
2026-06-09 19:46:08 +07:00
|
|
|
},
|
|
|
|
|
);
|
2026-06-03 15:01:34 +07:00
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
export const clearMascotChatHistory = asyncHandler(
|
|
|
|
|
async (req: Request, res: Response) => {
|
2026-06-09 13:07:01 +07:00
|
|
|
const userId = (req as AuthenticatedRequest).userId || "anonymous";
|
2026-06-03 15:01:34 +07:00
|
|
|
|
|
|
|
|
await mascotChatService.clearChatHistory(userId);
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
message: "Chat history cleared successfully",
|
|
|
|
|
});
|
2026-06-09 19:46:08 +07:00
|
|
|
},
|
|
|
|
|
);
|