Skip to content

Commit 19c812b

Browse files
feat: Rename 'notes' to 'additional_notes' in appointments and update related service logic
1 parent d1d5ddc commit 19c812b

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

src/config/schema.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ CREATE TABLE IF NOT EXISTS appointments (
115115
appointment_time TIME NOT NULL,
116116
duration_minutes INTEGER DEFAULT 60,
117117
status VARCHAR(20) DEFAULT 'scheduled' CHECK (status IN ('scheduled', 'completed', 'cancelled', 'no_show')),
118-
notes TEXT,
118+
additional_notes TEXT,
119119

120120
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
121121
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
@@ -251,4 +251,4 @@ VALUES (
251251
'Chiropractic adjustment and physical therapy',
252252
'Reduce pain to level 2-3, restore full mobility, return to normal activities'
253253
);
254-
*/
254+
*/

src/controllers/chat.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class ChatController {
201201
throw new ErrorResponse('Invalid role parameter. Must be one of: doctor, staff, admin', 400, '4001');
202202
}
203203

204-
const users = await ChatService.getUsersByRole(role, req.query);
204+
const users = await ChatService.getUsersByRole(role, req.query, req.user);
205205

206206
const meta = {
207207
roles: [role],

src/repositories/doctor.repository.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ class DoctorRepository extends BaseRepository {
486486
'end_time', a.end_time,
487487
'status', a.status,
488488
'appointment_type', a.appointment_type,
489-
'notes', a.notes,
489+
'notes', a.additional_notes,
490490
'created_at', a.created_at
491491
)
492492
) FILTER (WHERE a.id IS NOT NULL) as appointments

src/services/appointment.service.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class AppointmentService {
1616
* @returns {Object} Created appointment
1717
*/
1818
static async createAppointment(appointmentData, req) {
19+
// Normalize note field from clients that send `notes`
20+
if (appointmentData && appointmentData.notes && !appointmentData.additional_notes) {
21+
appointmentData.additional_notes = appointmentData.notes;
22+
delete appointmentData.notes;
23+
}
24+
1925
const {
2026
doctor_id, patient_id, appointment_date, appointment_time,
2127
location, reason_for_visit, additional_notes, status
@@ -134,16 +140,18 @@ class AppointmentService {
134140
if (user) {
135141
if (user.role === 'doctor') {
136142
// Doctors can only see appointments assigned to them
143+
// Support both cases:
144+
// 1) Appointments linked to doctor table id
145+
// 2) Appointments linked to the doctor's user_id (fallback used during creation)
137146
const doctorRepo = getDoctorRepository();
138147
const doctor = await doctorRepo.findByUserId(user.id);
148+
149+
// Always include the user's id as a fallback to catch records created
150+
// when no doctor profile existed yet
139151
if (doctor) {
140-
conditions.doctor_id = doctor.id;
152+
conditions.doctor_id = [doctor.id, user.id];
141153
} else {
142-
// If user is doctor but no doctor record found, return empty
143-
return {
144-
appointments: [],
145-
pagination: { page, limit, total: 0, pages: 0 }
146-
};
154+
conditions.doctor_id = [user.id];
147155
}
148156
} else if (user.role === 'patient') {
149157
// Patients can only see their own appointments
@@ -339,6 +347,12 @@ class AppointmentService {
339347
*/
340348
static async updateAppointment(appointmentId, updateData) {
341349
try {
350+
// Normalize note field: accept `notes` from clients, map to `additional_notes`
351+
if (updateData && updateData.notes && !updateData.additional_notes) {
352+
updateData.additional_notes = updateData.notes;
353+
delete updateData.notes;
354+
}
355+
342356
const existingAppointment = await AppointmentService.findAppointmentById(appointmentId);
343357
if (!existingAppointment) {
344358
throw new NotFoundError('Appointment not found', '4048');

src/services/chat.service.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ class ChatService {
881881
* @param {Object} query - Query parameters
882882
* @returns {Array} Users with the specified role
883883
*/
884-
static async getUsersByRole(role, query = {}) {
884+
static async getUsersByRole(role, query = {}, currentUser) {
885885
try {
886886
const chatRepo = getChatRepository();
887887
const { per_page = 100, search_term = '' } = query;
@@ -893,9 +893,12 @@ class ChatService {
893893
});
894894

895895
// Get users by role from repository
896+
// Note: We must pass the currentUser so role-specific filtering (e.g.,
897+
// doctor seeing their patients) can be applied correctly.
896898
const users = await chatRepo.getUsersByRole(role, {
897899
limit: parseInt(per_page),
898-
search: search_term.trim()
900+
search: search_term.trim(),
901+
currentUser,
899902
});
900903

901904
// Format users consistently

0 commit comments

Comments
 (0)