-
Notifications
You must be signed in to change notification settings - Fork 0
medtech project is in progress #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,172 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import User from '../models/User.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import DoctorProfile from '../models/DoctorProfile.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import PatientProfile from '../models/PatientProfile.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import Appointment from '../models/Appointment.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import Payment from '../models/Payment.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import AuditLog from '../models/AuditLog.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import Notification from '../models/Notification.model.js'; | ||||||||||||||||||||||||||||||||||||||||||
| import { createAuditLog } from '../middleware/audit.middleware.js'; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ---------------- GET PLATFORM ANALYTICS ---------------- | ||||||||||||||||||||||||||||||||||||||||||
| export const getPlatformAnalytics = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const [ | ||||||||||||||||||||||||||||||||||||||||||
| totalPatients, | ||||||||||||||||||||||||||||||||||||||||||
| totalDoctors, | ||||||||||||||||||||||||||||||||||||||||||
| totalAppointments, | ||||||||||||||||||||||||||||||||||||||||||
| pendingVerifications, | ||||||||||||||||||||||||||||||||||||||||||
| completedPayments, | ||||||||||||||||||||||||||||||||||||||||||
| recentLogs | ||||||||||||||||||||||||||||||||||||||||||
| ] = await Promise.all([ | ||||||||||||||||||||||||||||||||||||||||||
| User.countDocuments({ role: 'patient' }), | ||||||||||||||||||||||||||||||||||||||||||
| User.countDocuments({ role: 'doctor' }), | ||||||||||||||||||||||||||||||||||||||||||
| Appointment.countDocuments(), | ||||||||||||||||||||||||||||||||||||||||||
| DoctorProfile.countDocuments({ verificationStatus: 'pending' }), | ||||||||||||||||||||||||||||||||||||||||||
| Payment.find({ status: 'completed' }), | ||||||||||||||||||||||||||||||||||||||||||
| AuditLog.find().populate('userId', 'name email role').sort('-createdAt').limit(15) | ||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const totalRevenue = completedPayments.reduce((acc, curr) => acc + (curr.amount || 0), 0); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| res.json({ | ||||||||||||||||||||||||||||||||||||||||||
| success: true, | ||||||||||||||||||||||||||||||||||||||||||
| analytics: { | ||||||||||||||||||||||||||||||||||||||||||
| totalPatients, | ||||||||||||||||||||||||||||||||||||||||||
| totalDoctors, | ||||||||||||||||||||||||||||||||||||||||||
| totalAppointments, | ||||||||||||||||||||||||||||||||||||||||||
| pendingVerifications, | ||||||||||||||||||||||||||||||||||||||||||
| totalRevenue, | ||||||||||||||||||||||||||||||||||||||||||
| recentLogs | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error('Get Platform Analytics Error:', error); | ||||||||||||||||||||||||||||||||||||||||||
| res.status(500).json({ success: false, message: error.message }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ---------------- VERIFY OR REJECT DOCTOR ---------------- | ||||||||||||||||||||||||||||||||||||||||||
| export const verifyDoctor = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const { profileId } = req.params; | ||||||||||||||||||||||||||||||||||||||||||
| const { status, rejectionReason } = req.body; // 'verified' or 'rejected' | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (!['verified', 'rejected', 'pending'].includes(status)) { | ||||||||||||||||||||||||||||||||||||||||||
| return res.status(400).json({ success: false, message: 'Invalid status. Use verified, rejected, or pending' }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const doctorProfile = await DoctorProfile.findById(profileId).populate('userId', 'name email status isVerified'); | ||||||||||||||||||||||||||||||||||||||||||
| if (!doctorProfile) { | ||||||||||||||||||||||||||||||||||||||||||
| return res.status(404).json({ success: false, message: 'Doctor profile not found' }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| doctorProfile.verificationStatus = status; | ||||||||||||||||||||||||||||||||||||||||||
| if (status === 'rejected') { | ||||||||||||||||||||||||||||||||||||||||||
| doctorProfile.rejectionReason = rejectionReason || 'License verification failed'; | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| doctorProfile.rejectionReason = ''; | ||||||||||||||||||||||||||||||||||||||||||
| if (doctorProfile.userId) { | ||||||||||||||||||||||||||||||||||||||||||
| doctorProfile.userId.isVerified = true; | ||||||||||||||||||||||||||||||||||||||||||
| await doctorProfile.userId.save(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+63
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
The 🐛 Proposed fix doctorProfile.verificationStatus = status;
if (status === 'rejected') {
doctorProfile.rejectionReason = rejectionReason || 'License verification failed';
- } else {
+ } else if (status === 'verified') {
doctorProfile.rejectionReason = '';
if (doctorProfile.userId) {
doctorProfile.userId.isVerified = true;
await doctorProfile.userId.save();
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| await doctorProfile.save(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Send notification | ||||||||||||||||||||||||||||||||||||||||||
| if (doctorProfile.userId) { | ||||||||||||||||||||||||||||||||||||||||||
| await Notification.create({ | ||||||||||||||||||||||||||||||||||||||||||
| userId: doctorProfile.userId._id, | ||||||||||||||||||||||||||||||||||||||||||
| title: `Doctor Verification ${status.toUpperCase()}`, | ||||||||||||||||||||||||||||||||||||||||||
| message: status === 'verified' | ||||||||||||||||||||||||||||||||||||||||||
| ? 'Congratulations! Your medical license has been verified by the administrator.' | ||||||||||||||||||||||||||||||||||||||||||
| : `Your doctor verification was rejected: ${rejectionReason || 'Incomplete documentation'}`, | ||||||||||||||||||||||||||||||||||||||||||
| type: 'system', | ||||||||||||||||||||||||||||||||||||||||||
| link: '/doctor-dashboard', | ||||||||||||||||||||||||||||||||||||||||||
| relatedId: doctorProfile._id | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| await createAuditLog(req.user._id, `Verified doctor profile ${doctorProfile.userId?.name} status -> ${status}`); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| res.json({ success: true, message: `Doctor profile status updated to ${status}`, doctorProfile }); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error('Verify Doctor Error:', error); | ||||||||||||||||||||||||||||||||||||||||||
| res.status(500).json({ success: false, message: error.message }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ---------------- GET ALL USERS ---------------- | ||||||||||||||||||||||||||||||||||||||||||
| export const getAllUsers = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const { role, status, search } = req.query; | ||||||||||||||||||||||||||||||||||||||||||
| const filter = {}; | ||||||||||||||||||||||||||||||||||||||||||
| if (role && role !== 'All') filter.role = role; | ||||||||||||||||||||||||||||||||||||||||||
| if (status && status !== 'All') filter.status = status; | ||||||||||||||||||||||||||||||||||||||||||
| if (search) { | ||||||||||||||||||||||||||||||||||||||||||
| filter.$or = [ | ||||||||||||||||||||||||||||||||||||||||||
| { name: { $regex: search, $options: 'i' } }, | ||||||||||||||||||||||||||||||||||||||||||
| { email: { $regex: search, $options: 'i' } } | ||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const users = await User.find(filter).select('-password').sort('-createdAt'); | ||||||||||||||||||||||||||||||||||||||||||
| res.json({ success: true, users }); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error('Get All Users Error:', error); | ||||||||||||||||||||||||||||||||||||||||||
| res.status(500).json({ success: false, message: error.message }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ---------------- UPDATE USER STATUS OR ROLE ---------------- | ||||||||||||||||||||||||||||||||||||||||||
| export const updateUserStatus = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const { userId } = req.params; | ||||||||||||||||||||||||||||||||||||||||||
| const { status, role } = req.body; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const user = await User.findById(userId).select('-password'); | ||||||||||||||||||||||||||||||||||||||||||
| if (!user) { | ||||||||||||||||||||||||||||||||||||||||||
| return res.status(404).json({ success: false, message: 'User not found' }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (status) user.status = status; | ||||||||||||||||||||||||||||||||||||||||||
| if (role) { | ||||||||||||||||||||||||||||||||||||||||||
| user.role = role; | ||||||||||||||||||||||||||||||||||||||||||
| // Ensure profile exists for the new role | ||||||||||||||||||||||||||||||||||||||||||
| if (role === 'doctor') { | ||||||||||||||||||||||||||||||||||||||||||
| const docExists = await DoctorProfile.findOne({ userId }); | ||||||||||||||||||||||||||||||||||||||||||
| if (!docExists) await DoctorProfile.create({ userId }); | ||||||||||||||||||||||||||||||||||||||||||
| } else if (role === 'patient') { | ||||||||||||||||||||||||||||||||||||||||||
| const patExists = await PatientProfile.findOne({ userId }); | ||||||||||||||||||||||||||||||||||||||||||
| if (!patExists) await PatientProfile.create({ userId }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| await user.save(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| await createAuditLog(req.user._id, `Updated user ${user.email} -> status: ${user.status}, role: ${user.role}`); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| res.json({ success: true, message: 'User updated successfully', user }); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error('Update User Status Error:', error); | ||||||||||||||||||||||||||||||||||||||||||
| res.status(500).json({ success: false, message: error.message }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ---------------- GET AUDIT LOGS ---------------- | ||||||||||||||||||||||||||||||||||||||||||
| export const getAuditLogs = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const { action, status, limit } = req.query; | ||||||||||||||||||||||||||||||||||||||||||
| const filter = {}; | ||||||||||||||||||||||||||||||||||||||||||
| if (action) filter.action = { $regex: action, $options: 'i' }; | ||||||||||||||||||||||||||||||||||||||||||
| if (status && status !== 'All') filter.status = status; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const logs = await AuditLog.find(filter) | ||||||||||||||||||||||||||||||||||||||||||
| .populate('userId', 'name email role') | ||||||||||||||||||||||||||||||||||||||||||
| .sort('-createdAt') | ||||||||||||||||||||||||||||||||||||||||||
| .limit(Number(limit) || 100); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| res.json({ success: true, logs }); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error('Get Audit Logs Error:', error); | ||||||||||||||||||||||||||||||||||||||||||
| res.status(500).json({ success: false, message: error.message }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🔴 Critical | ⚡ Quick win
Critical CORS misconfiguration: credentialed requests allowed from any origin.
With
credentials: trueand'*'in theoriginarray, thecorspackage reflects any request origin back inAccess-Control-Allow-Originwhile also sendingAccess-Control-Allow-Credentials: true. This allows any website to make credentialed cross-origin requests to the API, enabling CSRF-style attacks if cookies or other browser-attached credentials are ever used. The'*'entry also renders the specific localhost entries pointless.Remove
'*'and restrict origins to trusted origins only. For production, drive this via an environment variable.🔒 Proposed fix for CORS configuration
app.use(cors({ - origin: ['http://localhost:5173', 'http://127.0.0.1:5173', '*'], + origin: (process.env.CORS_ORIGINS || 'http://localhost:5173,http://127.0.0.1:5173').split(','), credentials: true }));📝 Committable suggestion
🤖 Prompt for AI Agents