Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
MigiKTHZ committed Oct 29, 2023
1 parent 7ff1a3b commit 978ea52
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions backend/src/controllers/index.ts
Expand Up @@ -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();
Expand All @@ -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)",
Expand All @@ -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) => {
Expand Down Expand Up @@ -273,7 +308,7 @@ const addConversation = async (
);
closeConnection(driver, session);
const response: createdResponse = {
statusCode: 500,
statusCode: 200,
response: {
response: "Successfully created conversation",
},
Expand Down

0 comments on commit 978ea52

Please sign in to comment.