forked from rohit/spurrin-backend
views, docs
This commit is contained in:
parent
150276ecb4
commit
b7650d4729
@ -329,6 +329,8 @@ exports.login = async (req, res) => {
|
||||
const user = result[0];
|
||||
remember_me = user.remember_me
|
||||
|
||||
console.log("user-data----------------------",user);
|
||||
|
||||
const hospitalData = await db.query("SELECT * FROM hospitals WHERE hospital_code = ?", [user.hospital_code]);
|
||||
// if (hospitalData.publicSignupEnabled) {
|
||||
// throw new Error("Hospital not found");
|
||||
@ -422,6 +424,7 @@ exports.login = async (req, res) => {
|
||||
pin: user.pin_number,
|
||||
pin_enabled: user.pin_enabled,
|
||||
hospital_code: user.hospital_code,
|
||||
hospital_id : hospitalData[0].id,
|
||||
status: user.status,
|
||||
actualStatus: actual_status,
|
||||
hospital_name: result_hospital[0].name_hospital,
|
||||
@ -438,6 +441,8 @@ exports.login = async (req, res) => {
|
||||
},
|
||||
accessToken,
|
||||
});
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error during login:", error.message);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
|
||||
@ -319,6 +319,22 @@ exports.getDocumentsByHospital = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
exports.getDocumentsByHospitalappUser = async (req, res) => {
|
||||
try {
|
||||
const { hospital_id } = req.params;
|
||||
|
||||
|
||||
// Fetch documents
|
||||
const documents = await db.query('SELECT * FROM documents WHERE hospital_id = ?', [hospital_id]);
|
||||
|
||||
res.status(200).json({ documents });
|
||||
} catch (error) {
|
||||
// console.error('Error fetching documents:', error.message);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
};
|
||||
|
||||
exports.updateDocumentStatus = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
@ -447,54 +463,3 @@ exports.deleteDocument = async (req, res) => {
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
};
|
||||
|
||||
// Update document views
|
||||
exports.updateDocumentViews = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { views } = req.body;
|
||||
|
||||
if (!id) {
|
||||
return res.status(400).json({ error: 'Document ID is required' });
|
||||
}
|
||||
|
||||
// Fetch the document to validate ownership
|
||||
const documentQuery = 'SELECT * FROM documents WHERE id = ?';
|
||||
const documentResult = await db.query(documentQuery, [id]);
|
||||
|
||||
if (documentResult.length === 0) {
|
||||
return res.status(404).json({ error: 'Document not found' });
|
||||
}
|
||||
|
||||
const document = documentResult[0];
|
||||
|
||||
// Authorization check (reuse delete logic)
|
||||
if (!['Admin', 'Superadmin', 8, 7].includes(req.user.role)) {
|
||||
return res.status(403).json({ error: 'You are not authorized to update document views' });
|
||||
}
|
||||
|
||||
if (req.user.hospital_id !== document.hospital_id) {
|
||||
return res.status(403).json({ error: 'You are not authorized to update documents for this hospital' });
|
||||
}
|
||||
|
||||
// Update views (set to provided value or increment by 1 if not provided)
|
||||
let updateQuery, updateParams;
|
||||
if (typeof views === 'number') {
|
||||
updateQuery = 'UPDATE documents SET views = ? WHERE id = ?';
|
||||
updateParams = [views, id];
|
||||
} else {
|
||||
updateQuery = 'UPDATE documents SET views = views + 1 WHERE id = ?';
|
||||
updateParams = [id];
|
||||
}
|
||||
const result = await db.query(updateQuery, updateParams);
|
||||
|
||||
if (result.affectedRows === 0) {
|
||||
return res.status(404).json({ message: 'Document not found or no changes made' });
|
||||
}
|
||||
|
||||
res.status(200).json({ message: 'Document views updated successfully!' });
|
||||
} catch (error) {
|
||||
console.error('Error updating document views:', error.message);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
};
|
||||
@ -51,6 +51,12 @@ router.get(
|
||||
documentController.getDocumentsByHospital
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/app_user/hospital/:hospital_id',
|
||||
documentController.getDocumentsByHospitalappUser
|
||||
);
|
||||
|
||||
|
||||
router.put(
|
||||
'/update-status/:id',
|
||||
authMiddleware.authenticateToken,
|
||||
@ -58,13 +64,6 @@ router.put(
|
||||
documentController.updateDocumentStatus
|
||||
);
|
||||
|
||||
router.put(
|
||||
'/update-views/:id',
|
||||
authMiddleware.authenticateToken,
|
||||
roleMiddleware.authorizeRoles(['Superadmin', 'Admin',8,7]),
|
||||
documentController.updateDocumentViews
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/delete/:id',
|
||||
authMiddleware.authenticateToken,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user