const { User } = require('../models'); // Patients Controller exports.list = async (req, res, next) => { try { // Only return patients created by the authenticated user const patients = await User.findAll({ where: { role: 'patient', createdBy: req.user.id, }, }); res.json(patients); } catch (err) { next(err); } }; exports.create = async (req, res, next) => { try { // Only caregivers can add patients (enforced by route middleware) const requiredFields = [ 'firstName', 'lastName', 'email', 'phoneNumber', 'emergencyNumber', 'callFrequency', 'callTime', 'retryInterval', 'maxRetry', 'contactType' ]; for (const field of requiredFields) { if (!req.body[field]) { return res.status(400).json({ error: `${field} is required` }); } } // Unique email/phone check const existingEmail = await User.findOne({ where: { email: req.body.email } }); if (existingEmail) return res.status(409).json({ error: 'Email already in use' }); const existingPhone = await User.findOne({ where: { phoneNumber: req.body.phoneNumber } }); if (existingPhone) return res.status(409).json({ error: 'Phone number already in use' }); // Create patient const patient = await User.create({ firstName: req.body.firstName, lastName: req.body.lastName, email: req.body.email, phoneNumber: req.body.phoneNumber, emergencyNumber: req.body.emergencyNumber, callFrequency: req.body.callFrequency, callTime: req.body.callTime, retryInterval: req.body.retryInterval, maxRetry: req.body.maxRetry, contactType: req.body.contactType, timeZone: req.body.timeZone, scripts: req.body.scripts, genderVoiceCall: req.body.genderVoiceCall, voiceStyleCall: req.body.voiceStyleCall, role: 'patient', createdBy: req.user.id, }); res.status(201).json({ id: patient.id, firstName: patient.firstName, lastName: patient.lastName, email: patient.email, phoneNumber: patient.phoneNumber, emergencyNumber: patient.emergencyNumber, callFrequency: patient.callFrequency, callTime: patient.callTime, retryInterval: patient.retryInterval, maxRetry: patient.maxRetry, contactType: patient.contactType, timeZone: patient.timeZone, scripts: patient.scripts, genderVoiceCall: patient.genderVoiceCall, voiceStyleCall: patient.voiceStyleCall, role: patient.role, createdBy: patient.createdBy, }); } catch (err) { next(err); } }; exports.get = async (req, res, next) => { // TODO: Get patient by ID res.json({}); }; exports.update = async (req, res, next) => { // TODO: Update patient by ID res.json({}); }; exports.remove = async (req, res, next) => { // TODO: Delete patient by ID res.status(204).send(); }; exports.total = async (req, res, next) => { // TODO: Return total patients count res.json({ total: 0 }); };