diff --git a/checklist.txt b/checklist.txt index 0e4a2e8..75795e9 100644 --- a/checklist.txt +++ b/checklist.txt @@ -5,3 +5,7 @@ CREATE TABLE IF NOT EXISTS document_views ( user_role ENUM('Superadmin', 'Admin', 'Viewer', 'Spurrinadmin', 'AppUser') NOT NULL, viewed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); + + +ALTER TABLE interaction_logs +ADD COLUMN is_flagged BOOLEAN DEFAULT FALSE; diff --git a/src/config/initDatabase.js b/src/config/initDatabase.js index d7da11d..eaddc6e 100644 --- a/src/config/initDatabase.js +++ b/src/config/initDatabase.js @@ -217,6 +217,7 @@ async function initializeDatabase() { query TEXT NOT NULL, response TEXT NOT NULL, is_liked BOOLEAN DEFAULT FALSE, + is_flagged BOOLEAN DEFAULT FALSE, created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, hospital_code VARCHAR(12) NOT NULL, PRIMARY KEY (id), @@ -284,6 +285,14 @@ async function initializeDatabase() { created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )`, + `CREATE TABLE IF NOT EXISTS document_views ( + id INT NOT NULL AUTO_INCREMENT, + document_id INT NOT NULL, + user_id INT NOT NULL, + user_role ENUM('Superadmin', 'Admin', 'Viewer', 'Spurrinadmin', 'AppUser') NOT NULL, + viewed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )`, + // Audit logs table `CREATE TABLE IF NOT EXISTS audit_logs ( id INT NOT NULL AUTO_INCREMENT, diff --git a/src/controllers/appUserController.js b/src/controllers/appUserController.js index b27f37c..a124773 100644 --- a/src/controllers/appUserController.js +++ b/src/controllers/appUserController.js @@ -191,6 +191,56 @@ exports.hitlike = async (req, res) => { } }; +exports.hitFlag = async (req, res) => { + try { + // Extract user ID and session ID from the request + const { session_id, logid } = req.body; + const log_id = logid; + + const app_user_id = req.user.id; + // Check if both app_user_id and session_id are provided + if (!app_user_id || !session_id || !log_id) { + return res.status(400).json({ + status: 'error', + message: 'app user id and session id and log id are required', + }); + } + + // Query to toggle the is_liked value + const toggleQuery = ` + UPDATE interaction_logs +SET is_flagged = NOT is_flagged +WHERE app_user_id = ? AND session_id = ? AND id = ? + + `; + + // Execute the query + const result = await db.query(toggleQuery, [app_user_id, session_id,log_id]); + + // Check if any rows were affected + if (result.affectedRows > 0) { + return res.status(200).json({ + status: 'success', + message: 'Updated successfully', + data: { + app_user_id, + session_id + }, + }); + } else { + return res.status(404).json({ + status: 'error', + message: 'No matching record found to toggle', + }); + } + } catch (error) { + console.error('Error during like toggle:', error); + return res.status(500).json({ + status: 'error', + message: 'Internal server error', + }); + } +}; exports.signup = async (req, res) => { diff --git a/src/controllers/documentsController.js b/src/controllers/documentsController.js index ed0aa2e..2338c33 100644 --- a/src/controllers/documentsController.js +++ b/src/controllers/documentsController.js @@ -527,6 +527,86 @@ exports.logDocumentView = async (req, res) => { // GET /documents/:id/views // GET /documents/views +// exports.getDocumentsViewDetailsByHospital = async (req, res) => { +// try { +// const userId = req.user.id; +// const hospitalCode = req.user.hospital_code; + +// if (!userId || !hospitalCode) { +// return res.status(400).json({ error: 'User ID and hospital code are required' }); +// } + +// // Get hospital_id from hospital_users using hospital_code +// const hospitalUsers = await db.query( +// `SELECT hospital_id FROM hospital_users WHERE hospital_code = ?`, +// [hospitalCode] +// ); + +// if (!hospitalUsers.length) { +// return res.status(404).json({ error: 'Hospital not found for user' }); +// } + +// const hospitalId = hospitalUsers[0].hospital_id; + +// // Get all documents for this hospital +// const documents = await db.query( +// `SELECT id, file_name, file_url, processed_status, uploaded_by, uploaded_at FROM documents WHERE hospital_id = ?`, +// [hospitalId] +// ); + +// if (!documents.length) { +// return res.status(200).json({ total: 0, documents: [] }); +// } + +// const documentIds = documents.map(doc => doc.id); +// const placeholders = documentIds.map(() => '?').join(','); + +// // Get all views for these documents +// const views = await db.query( +// `SELECT document_id, user_id AS viewed_by, user_role, viewed_at +// FROM document_views +// WHERE document_id IN (${placeholders}) +// ORDER BY viewed_at DESC`, +// documentIds +// ); + +// // Group views by document_id +// const viewsByDocument = {}; +// views.forEach(view => { +// if (!viewsByDocument[view.document_id]) { +// viewsByDocument[view.document_id] = []; +// } +// viewsByDocument[view.document_id].push({ +// viewed_by: view.viewed_by, +// user_role: view.user_role, +// viewed_at: view.viewed_at, +// }); +// }); + +// // Combine documents with their views, totalViews, and whether current user has viewed +// const result = documents.map(doc => { +// const docViews = viewsByDocument[doc.id] || []; +// const userViewed = docViews.some(v => v.viewed_by === userId); + +// return { +// ...doc, +// totalViews: docViews.length, +// views: docViews, +// viewed: userViewed, +// }; +// }); + +// res.status(200).json({ +// total: result.length, +// documents: result, +// }); + +// } catch (error) { +// console.error('Error fetching documents with view details:', error.message); +// res.status(500).json({ message: 'Internal server error' }); +// } +// }; + exports.getDocumentsViewDetailsByHospital = async (req, res) => { try { const userId = req.user.id; @@ -561,12 +641,13 @@ exports.getDocumentsViewDetailsByHospital = async (req, res) => { const documentIds = documents.map(doc => doc.id); const placeholders = documentIds.map(() => '?').join(','); - // Get all views for these documents + // Get all views for these documents along with usernames const views = await db.query( - `SELECT document_id, user_id AS viewed_by, user_role, viewed_at - FROM document_views - WHERE document_id IN (${placeholders}) - ORDER BY viewed_at DESC`, + `SELECT dv.document_id, dv.user_id AS viewed_by, au.username, dv.user_role, dv.viewed_at + FROM document_views dv + LEFT JOIN app_users au ON dv.user_id = au.id + WHERE dv.document_id IN (${placeholders}) + ORDER BY dv.viewed_at DESC`, documentIds ); @@ -578,6 +659,7 @@ exports.getDocumentsViewDetailsByHospital = async (req, res) => { } viewsByDocument[view.document_id].push({ viewed_by: view.viewed_by, + username: view.username, user_role: view.user_role, viewed_at: view.viewed_at, }); @@ -605,4 +687,4 @@ exports.getDocumentsViewDetailsByHospital = async (req, res) => { console.error('Error fetching documents with view details:', error.message); res.status(500).json({ message: 'Internal server error' }); } -}; +}; \ No newline at end of file diff --git a/src/routes/appUsers.js b/src/routes/appUsers.js index b0dd1ea..c5f9c24 100644 --- a/src/routes/appUsers.js +++ b/src/routes/appUsers.js @@ -160,5 +160,6 @@ router.post('/verify-pin', upload.none(), appUserController.checkPin); router.put('/update-settings', upload.none(), authMiddleware.authenticateToken, appUserController.updateSettings); router.put('/like', upload.none(), authMiddleware.authenticateToken, appUserController.hitlike); +router.put('/flag', upload.none(), authMiddleware.authenticateToken, appUserController.hitFlag); module.exports = router; \ No newline at end of file