LiamKhoaLe commited on
Commit
2591af6
·
1 Parent(s): d1c32da

Upd doctor saver

Browse files
Files changed (1) hide show
  1. static/js/app.js +51 -31
static/js/app.js CHANGED
@@ -1019,6 +1019,13 @@ How can I assist you today?`;
1019
  _id: result.doctor_id
1020
  });
1021
  this.saveDoctors();
 
 
 
 
 
 
 
1022
  }
1023
  }
1024
  await this.populateDoctorSelect();
@@ -1038,52 +1045,65 @@ How can I assist you today?`;
1038
  return;
1039
  }
1040
 
1041
- // Update or add doctor to the doctors list
1042
  const existingDoctorIndex = this.doctors.findIndex(d => d.name === name);
1043
- if (existingDoctorIndex >= 0) {
1044
- // Update existing doctor
1045
- this.doctors[existingDoctorIndex] = {
1046
- ...this.doctors[existingDoctorIndex],
1047
- role: role,
1048
- specialty: specialty
1049
- };
1050
- } else {
1051
- // Add new doctor
1052
  this.doctors.unshift({
1053
  name,
1054
  role,
1055
  specialty
1056
  });
 
 
 
 
 
 
 
1057
  }
1058
  this.saveDoctors();
1059
 
 
1060
  this.currentUser.name = name;
1061
  this.currentUser.role = role;
1062
  this.currentUser.specialty = specialty;
1063
-
1064
  this.saveUser();
1065
  this.updateUserDisplay();
1066
 
1067
- // Persist to backend (MongoDB) as well
1068
- const payload = {
1069
- user_id: this.currentUser.id,
1070
- name: this.currentUser.name,
1071
- role: this.currentUser.role,
1072
- specialty: this.currentUser.specialty || null,
1073
- medical_roles: [this.currentUser.role]
1074
- };
1075
- fetch('/users', {
1076
- method: 'POST',
1077
- headers: { 'Content-Type': 'application/json' },
1078
- body: JSON.stringify(payload)
1079
- }).then(resp => {
1080
- if (!resp.ok) throw new Error('Failed to save user to backend');
1081
- return resp.json();
1082
- }).then(() => {
1083
- console.log('[User] persisted to backend');
1084
- }).catch(err => {
1085
- console.warn('[User] failed to persist to backend:', err);
1086
- });
 
 
 
 
 
 
 
 
 
 
1087
 
1088
  this.hideModal('userModal');
1089
  }
 
1019
  _id: result.doctor_id
1020
  });
1021
  this.saveDoctors();
1022
+
1023
+ // Update current user profile
1024
+ this.currentUser.name = name;
1025
+ this.currentUser.role = role;
1026
+ this.currentUser.specialty = specialty;
1027
+ this.saveUser();
1028
+ this.updateUserDisplay();
1029
  }
1030
  }
1031
  await this.populateDoctorSelect();
 
1045
  return;
1046
  }
1047
 
1048
+ // Check if this is a new doctor creation (not in our local list)
1049
  const existingDoctorIndex = this.doctors.findIndex(d => d.name === name);
1050
+ const isNewDoctor = existingDoctorIndex === -1;
1051
+
1052
+ // Update local doctors list
1053
+ if (isNewDoctor) {
1054
+ // Add new doctor to local list
 
 
 
 
1055
  this.doctors.unshift({
1056
  name,
1057
  role,
1058
  specialty
1059
  });
1060
+ } else {
1061
+ // Update existing doctor in local list
1062
+ this.doctors[existingDoctorIndex] = {
1063
+ ...this.doctors[existingDoctorIndex],
1064
+ role: role,
1065
+ specialty: specialty
1066
+ };
1067
  }
1068
  this.saveDoctors();
1069
 
1070
+ // Update current user profile
1071
  this.currentUser.name = name;
1072
  this.currentUser.role = role;
1073
  this.currentUser.specialty = specialty;
 
1074
  this.saveUser();
1075
  this.updateUserDisplay();
1076
 
1077
+ // Only create new doctor in MongoDB if it's actually a new doctor
1078
+ if (isNewDoctor) {
1079
+ const doctorPayload = {
1080
+ name: name,
1081
+ role: role,
1082
+ specialty: specialty || null,
1083
+ medical_roles: [role]
1084
+ };
1085
+
1086
+ fetch('/doctors', {
1087
+ method: 'POST',
1088
+ headers: { 'Content-Type': 'application/json' },
1089
+ body: JSON.stringify(doctorPayload)
1090
+ }).then(resp => {
1091
+ if (!resp.ok) throw new Error('Failed to create doctor in backend');
1092
+ return resp.json();
1093
+ }).then((data) => {
1094
+ console.log('[Doctor] Created new doctor in backend:', data);
1095
+ // Update local doctor with the ID from backend
1096
+ const localDoctor = this.doctors.find(d => d.name === name);
1097
+ if (localDoctor) {
1098
+ localDoctor._id = data.doctor_id;
1099
+ this.saveDoctors();
1100
+ }
1101
+ }).catch(err => {
1102
+ console.warn('[Doctor] failed to create doctor in backend:', err);
1103
+ });
1104
+ } else {
1105
+ console.log('[Doctor] Updated existing doctor profile locally (no backend call needed)');
1106
+ }
1107
 
1108
  this.hideModal('userModal');
1109
  }