/** * chatbot.controller.ts * Endpoint handlers untuk chatbot API. */ import { Response, NextFunction } from 'express'; import { ChatbotService } from '../services/chatbot.service'; import { sukses, gagal } from '../utils/response'; import { AuthenticatedRequest } from '../middlewares/auth.middleware'; export class ChatbotController { /** * POST /api/chatbot/pesan * Kirim pesan ke chatbot dan dapatkan jawaban. */ static async kirimPesan(req: AuthenticatedRequest, res: Response, next: NextFunction) { try { const { pesan } = req.body; if (!pesan || typeof pesan !== 'string' || pesan.trim().length === 0) { return gagal(res, 400, 'Pesan tidak boleh kosong'); } if (pesan.length > 1000) { return gagal(res, 400, 'Pesan terlalu panjang (maks 1000 karakter)'); } const user = req.user; const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); const userAgent = (req.headers['user-agent'] || 'unknown') as string; const result = await ChatbotService.kirimPesan( { id: user.id, nama: user.nama, role: user.role }, pesan.trim(), ip, userAgent ); return sukses(res, result, 'Pesan berhasil diproses'); } catch (error) { next(error); } } /** * POST /api/chatbot/konfirmasi * Konfirmasi atau batalkan aksi pending. */ static async konfirmasiAksi(req: AuthenticatedRequest, res: Response, next: NextFunction) { try { const { aksiId, setuju } = req.body; if (!aksiId || typeof aksiId !== 'string') { return gagal(res, 400, 'ID aksi tidak valid'); } if (typeof setuju !== 'boolean') { return gagal(res, 400, 'Parameter "setuju" harus boolean (true/false)'); } const user = req.user; const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); const userAgent = (req.headers['user-agent'] || 'unknown') as string; const result = await ChatbotService.konfirmasiAksi( aksiId, setuju, { id: user.id, nama: user.nama, role: user.role }, ip, userAgent ); return sukses(res, result, setuju ? 'Aksi berhasil dikonfirmasi' : 'Aksi dibatalkan'); } catch (error) { next(error); } } /** * DELETE /api/chatbot/riwayat * Hapus riwayat chat session user. */ static async hapusRiwayat(req: AuthenticatedRequest, res: Response, next: NextFunction) { try { const user = req.user; ChatbotService.hapusRiwayat(user.id); return sukses(res, null, 'Riwayat chat berhasil dihapus'); } catch (error) { next(error); } } }