From 56955d1be229f480d00594c2b0c286b489089e22 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 1 Aug 2025 14:16:02 +0530 Subject: [PATCH] popular topic --- src/controllers/appUserController.js | 66 ++++++++++++++++++++++++++++ src/routes/appUsers.js | 3 ++ 2 files changed, 69 insertions(+) diff --git a/src/controllers/appUserController.js b/src/controllers/appUserController.js index c97f2b4..3b1d86f 100644 --- a/src/controllers/appUserController.js +++ b/src/controllers/appUserController.js @@ -1824,4 +1824,70 @@ exports.forgotPin = async (req, res) => { console.error("Error in forgot pin:", error); res.status(500).json({ error: "Internal server error" }); } +}; + +// Insert interaction log +exports.insertInteractionLog = async (req, res) => { + try { + const { session_id, query, response } = req.body; + const app_user_id = req.user.id; + + // Validate required fields + if (!session_id || !query || !response) { + return res.status(400).json({ + error: "session_id, query, and response are required" + }); + } + + // Get hospital_code from app_user + const userQuery = "SELECT hospital_code FROM app_users WHERE id = ?"; + const userResult = await db.query(userQuery, [app_user_id]); + + if (userResult.length === 0) { + return res.status(404).json({ error: "User not found" }); + } + + const hospital_code = userResult[0].hospital_code; + + // Insert into interaction_logs + const insertQuery = ` + INSERT INTO interaction_logs ( + session_id, + app_user_id, + query, + response, + hospital_code, + status, + session_title + ) VALUES (?, ?, ?, ?, ?, 'Active', ?) + `; + + const sessionTitle = query.length > 50 ? query.substring(0, 50) + '...' : query; + + const result = await db.query(insertQuery, [ + session_id, + app_user_id, + query, + response, + hospital_code, + sessionTitle + ]); + + res.status(201).json({ + message: "Interaction log created successfully", + data: { + id: result.insertId, + session_id, + app_user_id, + query, + response, + hospital_code, + created_at: new Date() + } + }); + + } catch (error) { + console.error("Error inserting interaction log:", error.message); + res.status(500).json({ error: "Internal server error" }); + } }; \ No newline at end of file diff --git a/src/routes/appUsers.js b/src/routes/appUsers.js index 67e4e32..9810f72 100644 --- a/src/routes/appUsers.js +++ b/src/routes/appUsers.js @@ -166,4 +166,7 @@ router.post('/logs/add-report', authMiddleware.authenticateToken, appUserControl router.put('/acknowledge/:id',authMiddleware.authenticateToken, upload.none(), appUserController.updateChecked); +// Insert interaction log +router.post('/interaction-log', authMiddleware.authenticateToken, upload.none(), appUserController.insertInteractionLog); + module.exports = router; \ No newline at end of file