File size: 2,735 Bytes
5306da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
const axios = require("axios");

const getChats = async (req, res) => {
  try {
    const response = await axios.get(`http://localhost:8082/get-chats`);
    console.log(response.data);
    if (!response.data) {
      return res.status(200).json({
        success: false,
        message: "failed to get chats",
      });
    }
    const chats = response.data;
    const all_chats = chats.sort(
      (a, b) => new Date(b.lastUpdatedAt) - new Date(a.lastUpdatedAt)
    );
    return res.status(200).json({
      success: true,
      message: "All chats",
      chats: all_chats,
    });
  } catch (error) {
    console.log(error);
    return res
      .status(500)
      .json({ success: false, message: "Failed to get chat" });
  }
};
const renameChats = async (req, res) => {
  try {
    const { sessionId, title } = req.body;
    const response = await axios.post(`http://localhost:8082/rename-chat`, {
      sessionId,
      title,
    });
    console.log(response.data);
    if (!response.data) {
      return res.status(200).json({
        success: false,
        message: "failed to rename chats",
      });
    }
    return res.status(200).json({
      success: true,
      message: "chat renamed!",
      rename: { id: sessionId, title: title },
    });
  } catch (error) {
    console.log(error);
    return res
      .status(500)
      .json({ success: false, message: "Failed to get chat" });
  }
};

const createChat = async (req, res) => {
  try {
    const { sessionId } = req.body;
    console.log(sessionId);
    const response = await axios.post(
      `http://localhost:8082/create-chat/${sessionId}`
    );
    if (!response.data) {
      return res
        .status(500)
        .json({ success: false, message: "Failed to create chat" });
    }
    return res.status(200).json({
      success: true,
      message: "Chat created successfully",
      sessionId: sessionId,
    });
  } catch (error) {
    console.log(error);
    return res
      .status(500)
      .json({ success: false, message: "Failed to create chat" });
  }
};

const deleteChat = async (req, res) => {
  try {
    const { sessionId } = req.body;
    const response = await axios.post(`http://localhost:8082/delete-chat`, {
      sessionId,
    });
    if (!response.data) {
      return res
        .status(500)
        .json({ success: false, message: "Failed to delete chat" });
    }
    return res.status(200).json({
      success: true,
      message: "Chat deleted successfully",
      sessionId: sessionId,
    });
  } catch (error) {
    console.log(error);
    return res
      .status(500)
      .json({ success: false, message: "Failed to delete chat" });
  }
};

module.exports = { createChat, getChats, renameChats, deleteChat };