diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index b563a2c..761a220 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -75,10 +75,20 @@ app.delete("/history", async (req: Request, res: Response) => { res.status(200).json(await getHistoryByUser(Number(userID))); }); -app.post("/query", async (req: Request, res: Response) => { +app.post("/search", async (req: Request, res: Response) => { const { userID, historyID } = req.query; const data = req.body; + const convo = req.body; + const prompt = "Prompt from MML"; + const results: docs[] = []; + + const convoObj: conversation = { + prompt: prompt, + request: req.body, + docs: results, + }; + const driver = await createDriver(); const session = driver.session(); @@ -91,14 +101,18 @@ app.post("/query", async (req: Request, res: Response) => { if (!historyID) { const returnedVal = createHistory(Number(userID), data) - const hID = Number((await returnedVal).hID) - console.log(hID) - - const createdHistoryConversationRel = await session.run( - "MATCH (h:History) WHERE ID(h) = $hID CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", - { conversation: data, hID: Number(hID) } - ); - return res.json(createdHistoryConversationRel) + const response = Number((await returnedVal).response.statusCode) + + if (response == 200) { + const hID = Number((await returnedVal).hID) + console.log(hID) + + const createdHistoryConversationRel = await session.run( + "MATCH (h:History) WHERE ID(h) = $hID CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", + { conversation: data, hID: Number(hID) } + ); + return res.json(createdHistoryConversationRel) + } } else { const appendHistoryConversationRel = await session.run( "MATCH (h:History) WHERE ID(h) = $history CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", @@ -116,14 +130,35 @@ async function createHistory(userID: number, retrievedData: string) { const session = driver.session(); - const createdUserHistoryRel = await session.run( - "MATCH (u:User) WHERE ID(u) = $uID CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h) return ID(h) as nodeId", - { history: data, uID: userID } - ); - const historyID: number = createdUserHistoryRel.records[0].get("nodeId"); + try { + const createdUserHistoryRel = await session.run( + "MATCH (u:User) WHERE ID(u) = $uID CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h) return ID(h) as nodeId", + { history: data, uID: userID } + ); + const historyID: number = createdUserHistoryRel.records[0].get("nodeId"); + const response: createdResponse = { + statusCode: 200, + response: { + response: "Successfully created conversation", + } + } + const returnData = { "response": response, "hID": historyID } + return returnData + } catch (error) { + const response: createdResponse = { + statusCode: 500, + response: { + response: "Successfully created conversation", + } + } + const returnData = { "response": response, "hID": null} + return returnData + } + + + + - const returnData = { "response": createdUserHistoryRel, "hID": historyID } - return returnData } // app.post("/history", async (req: Request, res: Response) => { @@ -273,7 +308,7 @@ const addConversation = async ( ); closeConnection(driver, session); const response: createdResponse = { - statusCode: 500, + statusCode: 200, response: { response: "Successfully created conversation", },