File size: 2,711 Bytes
48013ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * 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);
    }
  }
}