forked from rohit/spurrin-backend
flags
This commit is contained in:
parent
a18fccc427
commit
643a131d56
@ -5,3 +5,7 @@ CREATE TABLE IF NOT EXISTS document_views (
|
|||||||
user_role ENUM('Superadmin', 'Admin', 'Viewer', 'Spurrinadmin', 'AppUser') NOT NULL,
|
user_role ENUM('Superadmin', 'Admin', 'Viewer', 'Spurrinadmin', 'AppUser') NOT NULL,
|
||||||
viewed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
viewed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
ALTER TABLE interaction_logs
|
||||||
|
ADD COLUMN is_flagged BOOLEAN DEFAULT FALSE;
|
||||||
|
|||||||
@ -217,6 +217,7 @@ async function initializeDatabase() {
|
|||||||
query TEXT NOT NULL,
|
query TEXT NOT NULL,
|
||||||
response TEXT NOT NULL,
|
response TEXT NOT NULL,
|
||||||
is_liked BOOLEAN DEFAULT FALSE,
|
is_liked BOOLEAN DEFAULT FALSE,
|
||||||
|
is_flagged BOOLEAN DEFAULT FALSE,
|
||||||
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
hospital_code VARCHAR(12) NOT NULL,
|
hospital_code VARCHAR(12) NOT NULL,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
@ -284,6 +285,14 @@ async function initializeDatabase() {
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
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
|
// Audit logs table
|
||||||
`CREATE TABLE IF NOT EXISTS audit_logs (
|
`CREATE TABLE IF NOT EXISTS audit_logs (
|
||||||
id INT NOT NULL AUTO_INCREMENT,
|
id INT NOT NULL AUTO_INCREMENT,
|
||||||
|
|||||||
@ -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) => {
|
exports.signup = async (req, res) => {
|
||||||
|
|||||||
@ -527,6 +527,86 @@ exports.logDocumentView = async (req, res) => {
|
|||||||
|
|
||||||
// GET /documents/:id/views
|
// GET /documents/:id/views
|
||||||
// GET /documents/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) => {
|
exports.getDocumentsViewDetailsByHospital = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
@ -561,12 +641,13 @@ exports.getDocumentsViewDetailsByHospital = async (req, res) => {
|
|||||||
const documentIds = documents.map(doc => doc.id);
|
const documentIds = documents.map(doc => doc.id);
|
||||||
const placeholders = documentIds.map(() => '?').join(',');
|
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(
|
const views = await db.query(
|
||||||
`SELECT document_id, user_id AS viewed_by, user_role, viewed_at
|
`SELECT dv.document_id, dv.user_id AS viewed_by, au.username, dv.user_role, dv.viewed_at
|
||||||
FROM document_views
|
FROM document_views dv
|
||||||
WHERE document_id IN (${placeholders})
|
LEFT JOIN app_users au ON dv.user_id = au.id
|
||||||
ORDER BY viewed_at DESC`,
|
WHERE dv.document_id IN (${placeholders})
|
||||||
|
ORDER BY dv.viewed_at DESC`,
|
||||||
documentIds
|
documentIds
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -578,6 +659,7 @@ exports.getDocumentsViewDetailsByHospital = async (req, res) => {
|
|||||||
}
|
}
|
||||||
viewsByDocument[view.document_id].push({
|
viewsByDocument[view.document_id].push({
|
||||||
viewed_by: view.viewed_by,
|
viewed_by: view.viewed_by,
|
||||||
|
username: view.username,
|
||||||
user_role: view.user_role,
|
user_role: view.user_role,
|
||||||
viewed_at: view.viewed_at,
|
viewed_at: view.viewed_at,
|
||||||
});
|
});
|
||||||
@ -605,4 +687,4 @@ exports.getDocumentsViewDetailsByHospital = async (req, res) => {
|
|||||||
console.error('Error fetching documents with view details:', error.message);
|
console.error('Error fetching documents with view details:', error.message);
|
||||||
res.status(500).json({ message: 'Internal server error' });
|
res.status(500).json({ message: 'Internal server error' });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -160,5 +160,6 @@ router.post('/verify-pin', upload.none(), appUserController.checkPin);
|
|||||||
router.put('/update-settings', upload.none(), authMiddleware.authenticateToken, appUserController.updateSettings);
|
router.put('/update-settings', upload.none(), authMiddleware.authenticateToken, appUserController.updateSettings);
|
||||||
router.put('/like', upload.none(), authMiddleware.authenticateToken, appUserController.hitlike);
|
router.put('/like', upload.none(), authMiddleware.authenticateToken, appUserController.hitlike);
|
||||||
|
|
||||||
|
router.put('/flag', upload.none(), authMiddleware.authenticateToken, appUserController.hitFlag);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
Loading…
Reference in New Issue
Block a user