@blackcode_sa/metaestetics-api 1.7.24 → 1.7.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -987,14 +987,19 @@ var ClinicAggregationService = class {
987
987
  );
988
988
  for (const practitionerId of practitionerIds) {
989
989
  const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
990
- batch.update(practitionerRef, {
991
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
992
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
993
- });
994
- batch.update(practitionerRef, {
995
- clinicsInfo: admin3.firestore.FieldValue.arrayUnion(clinicInfo),
996
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
997
- });
990
+ const practitionerDoc = await practitionerRef.get();
991
+ if (practitionerDoc.exists) {
992
+ const practitionerData = practitionerDoc.data();
993
+ const currentClinicsInfo = (practitionerData == null ? void 0 : practitionerData.clinicsInfo) || [];
994
+ const filteredClinicsInfo = currentClinicsInfo.filter(
995
+ (clinic) => clinic.id !== clinicId
996
+ );
997
+ const updatedClinicsInfo = [...filteredClinicsInfo, clinicInfo];
998
+ batch.update(practitionerRef, {
999
+ clinicsInfo: updatedClinicsInfo,
1000
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1001
+ });
1002
+ }
998
1003
  }
999
1004
  try {
1000
1005
  await batch.commit();
@@ -1058,28 +1063,35 @@ var ClinicAggregationService = class {
1058
1063
  );
1059
1064
  return;
1060
1065
  }
1061
- const batch = this.db.batch();
1062
1066
  const clinicId = clinicInfo.id;
1063
1067
  const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
1064
1068
  console.log(
1065
1069
  `[ClinicAggregationService] Starting update of ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
1066
1070
  );
1067
- batch.update(groupRef, {
1068
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
1069
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1070
- });
1071
- batch.update(groupRef, {
1072
- clinicsInfo: admin3.firestore.FieldValue.arrayUnion(clinicInfo),
1073
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1074
- });
1075
1071
  try {
1076
- await batch.commit();
1077
- console.log(
1078
- `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
1079
- );
1072
+ const clinicGroupDoc = await groupRef.get();
1073
+ if (clinicGroupDoc.exists) {
1074
+ const clinicGroupData = clinicGroupDoc.data();
1075
+ const currentClinicsInfo = (clinicGroupData == null ? void 0 : clinicGroupData.clinicsInfo) || [];
1076
+ const filteredClinicsInfo = currentClinicsInfo.filter(
1077
+ (clinic) => clinic.id !== clinicId
1078
+ );
1079
+ const updatedClinicsInfo = [...filteredClinicsInfo, clinicInfo];
1080
+ await groupRef.update({
1081
+ clinicsInfo: updatedClinicsInfo,
1082
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1083
+ });
1084
+ console.log(
1085
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
1086
+ );
1087
+ } else {
1088
+ console.warn(
1089
+ `[ClinicAggregationService] Clinic Group ${clinicGroupId} not found. Cannot update clinic info.`
1090
+ );
1091
+ }
1080
1092
  } catch (error) {
1081
1093
  console.error(
1082
- `[ClinicAggregationService] Error committing update for ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
1094
+ `[ClinicAggregationService] Error updating ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
1083
1095
  error
1084
1096
  );
1085
1097
  throw error;
@@ -1223,12 +1235,23 @@ var ClinicAggregationService = class {
1223
1235
  );
1224
1236
  for (const practitionerId of practitionerIds) {
1225
1237
  const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
1226
- batch.update(practitionerRef, {
1227
- clinicIds: admin3.firestore.FieldValue.arrayRemove(clinicId),
1228
- // Remove all clinic info objects where id matches the clinic ID
1229
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
1230
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1231
- });
1238
+ const practitionerDoc = await practitionerRef.get();
1239
+ if (practitionerDoc.exists) {
1240
+ const practitionerData = practitionerDoc.data();
1241
+ const currentClinicIds = (practitionerData == null ? void 0 : practitionerData.clinics) || [];
1242
+ const currentClinicsInfo = (practitionerData == null ? void 0 : practitionerData.clinicsInfo) || [];
1243
+ const filteredClinicIds = currentClinicIds.filter(
1244
+ (id) => id !== clinicId
1245
+ );
1246
+ const filteredClinicsInfo = currentClinicsInfo.filter(
1247
+ (clinic) => clinic.id !== clinicId
1248
+ );
1249
+ batch.update(practitionerRef, {
1250
+ clinics: filteredClinicIds,
1251
+ clinicsInfo: filteredClinicsInfo,
1252
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1253
+ });
1254
+ }
1232
1255
  }
1233
1256
  try {
1234
1257
  await batch.commit();
@@ -1295,15 +1318,30 @@ var ClinicAggregationService = class {
1295
1318
  `[ClinicAggregationService] Removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
1296
1319
  );
1297
1320
  try {
1298
- await groupRef.update({
1299
- clinicIds: admin3.firestore.FieldValue.arrayRemove(clinicId),
1300
- // Remove all clinic info objects where id matches the clinic ID
1301
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
1302
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1303
- });
1304
- console.log(
1305
- `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
1306
- );
1321
+ const clinicGroupDoc = await groupRef.get();
1322
+ if (clinicGroupDoc.exists) {
1323
+ const clinicGroupData = clinicGroupDoc.data();
1324
+ const currentClinicIds = (clinicGroupData == null ? void 0 : clinicGroupData.clinics) || [];
1325
+ const currentClinicsInfo = (clinicGroupData == null ? void 0 : clinicGroupData.clinicsInfo) || [];
1326
+ const filteredClinicIds = currentClinicIds.filter(
1327
+ (id) => id !== clinicId
1328
+ );
1329
+ const filteredClinicsInfo = currentClinicsInfo.filter(
1330
+ (clinic) => clinic.id !== clinicId
1331
+ );
1332
+ await groupRef.update({
1333
+ clinics: filteredClinicIds,
1334
+ clinicsInfo: filteredClinicsInfo,
1335
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1336
+ });
1337
+ console.log(
1338
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
1339
+ );
1340
+ } else {
1341
+ console.warn(
1342
+ `[ClinicAggregationService] Clinic Group ${clinicGroupId} not found. Cannot remove clinic.`
1343
+ );
1344
+ }
1307
1345
  } catch (error) {
1308
1346
  console.error(
1309
1347
  `[ClinicAggregationService] Error removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}:`,
@@ -930,14 +930,19 @@ var ClinicAggregationService = class {
930
930
  );
931
931
  for (const practitionerId of practitionerIds) {
932
932
  const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
933
- batch.update(practitionerRef, {
934
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
935
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
936
- });
937
- batch.update(practitionerRef, {
938
- clinicsInfo: admin3.firestore.FieldValue.arrayUnion(clinicInfo),
939
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
940
- });
933
+ const practitionerDoc = await practitionerRef.get();
934
+ if (practitionerDoc.exists) {
935
+ const practitionerData = practitionerDoc.data();
936
+ const currentClinicsInfo = (practitionerData == null ? void 0 : practitionerData.clinicsInfo) || [];
937
+ const filteredClinicsInfo = currentClinicsInfo.filter(
938
+ (clinic) => clinic.id !== clinicId
939
+ );
940
+ const updatedClinicsInfo = [...filteredClinicsInfo, clinicInfo];
941
+ batch.update(practitionerRef, {
942
+ clinicsInfo: updatedClinicsInfo,
943
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
944
+ });
945
+ }
941
946
  }
942
947
  try {
943
948
  await batch.commit();
@@ -1001,28 +1006,35 @@ var ClinicAggregationService = class {
1001
1006
  );
1002
1007
  return;
1003
1008
  }
1004
- const batch = this.db.batch();
1005
1009
  const clinicId = clinicInfo.id;
1006
1010
  const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
1007
1011
  console.log(
1008
1012
  `[ClinicAggregationService] Starting update of ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
1009
1013
  );
1010
- batch.update(groupRef, {
1011
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
1012
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1013
- });
1014
- batch.update(groupRef, {
1015
- clinicsInfo: admin3.firestore.FieldValue.arrayUnion(clinicInfo),
1016
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1017
- });
1018
1014
  try {
1019
- await batch.commit();
1020
- console.log(
1021
- `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
1022
- );
1015
+ const clinicGroupDoc = await groupRef.get();
1016
+ if (clinicGroupDoc.exists) {
1017
+ const clinicGroupData = clinicGroupDoc.data();
1018
+ const currentClinicsInfo = (clinicGroupData == null ? void 0 : clinicGroupData.clinicsInfo) || [];
1019
+ const filteredClinicsInfo = currentClinicsInfo.filter(
1020
+ (clinic) => clinic.id !== clinicId
1021
+ );
1022
+ const updatedClinicsInfo = [...filteredClinicsInfo, clinicInfo];
1023
+ await groupRef.update({
1024
+ clinicsInfo: updatedClinicsInfo,
1025
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1026
+ });
1027
+ console.log(
1028
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
1029
+ );
1030
+ } else {
1031
+ console.warn(
1032
+ `[ClinicAggregationService] Clinic Group ${clinicGroupId} not found. Cannot update clinic info.`
1033
+ );
1034
+ }
1023
1035
  } catch (error) {
1024
1036
  console.error(
1025
- `[ClinicAggregationService] Error committing update for ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
1037
+ `[ClinicAggregationService] Error updating ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
1026
1038
  error
1027
1039
  );
1028
1040
  throw error;
@@ -1166,12 +1178,23 @@ var ClinicAggregationService = class {
1166
1178
  );
1167
1179
  for (const practitionerId of practitionerIds) {
1168
1180
  const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
1169
- batch.update(practitionerRef, {
1170
- clinicIds: admin3.firestore.FieldValue.arrayRemove(clinicId),
1171
- // Remove all clinic info objects where id matches the clinic ID
1172
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
1173
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1174
- });
1181
+ const practitionerDoc = await practitionerRef.get();
1182
+ if (practitionerDoc.exists) {
1183
+ const practitionerData = practitionerDoc.data();
1184
+ const currentClinicIds = (practitionerData == null ? void 0 : practitionerData.clinics) || [];
1185
+ const currentClinicsInfo = (practitionerData == null ? void 0 : practitionerData.clinicsInfo) || [];
1186
+ const filteredClinicIds = currentClinicIds.filter(
1187
+ (id) => id !== clinicId
1188
+ );
1189
+ const filteredClinicsInfo = currentClinicsInfo.filter(
1190
+ (clinic) => clinic.id !== clinicId
1191
+ );
1192
+ batch.update(practitionerRef, {
1193
+ clinics: filteredClinicIds,
1194
+ clinicsInfo: filteredClinicsInfo,
1195
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1196
+ });
1197
+ }
1175
1198
  }
1176
1199
  try {
1177
1200
  await batch.commit();
@@ -1238,15 +1261,30 @@ var ClinicAggregationService = class {
1238
1261
  `[ClinicAggregationService] Removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
1239
1262
  );
1240
1263
  try {
1241
- await groupRef.update({
1242
- clinicIds: admin3.firestore.FieldValue.arrayRemove(clinicId),
1243
- // Remove all clinic info objects where id matches the clinic ID
1244
- clinicsInfo: admin3.firestore.FieldValue.arrayRemove({ id: clinicId }),
1245
- updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1246
- });
1247
- console.log(
1248
- `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
1249
- );
1264
+ const clinicGroupDoc = await groupRef.get();
1265
+ if (clinicGroupDoc.exists) {
1266
+ const clinicGroupData = clinicGroupDoc.data();
1267
+ const currentClinicIds = (clinicGroupData == null ? void 0 : clinicGroupData.clinics) || [];
1268
+ const currentClinicsInfo = (clinicGroupData == null ? void 0 : clinicGroupData.clinicsInfo) || [];
1269
+ const filteredClinicIds = currentClinicIds.filter(
1270
+ (id) => id !== clinicId
1271
+ );
1272
+ const filteredClinicsInfo = currentClinicsInfo.filter(
1273
+ (clinic) => clinic.id !== clinicId
1274
+ );
1275
+ await groupRef.update({
1276
+ clinics: filteredClinicIds,
1277
+ clinicsInfo: filteredClinicsInfo,
1278
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1279
+ });
1280
+ console.log(
1281
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
1282
+ );
1283
+ } else {
1284
+ console.warn(
1285
+ `[ClinicAggregationService] Clinic Group ${clinicGroupId} not found. Cannot remove clinic.`
1286
+ );
1287
+ }
1250
1288
  } catch (error) {
1251
1289
  console.error(
1252
1290
  `[ClinicAggregationService] Error removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}:`,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.7.24",
4
+ "version": "1.7.25",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
@@ -1,8 +1,11 @@
1
1
  import * as admin from "firebase-admin";
2
2
  import { ClinicInfo } from "../../../types/profile"; // Corrected path
3
- import { PRACTITIONERS_COLLECTION } from "../../../types/practitioner"; // Corrected path
3
+ import {
4
+ PRACTITIONERS_COLLECTION,
5
+ Practitioner,
6
+ } from "../../../types/practitioner"; // Corrected path
4
7
  import { PROCEDURES_COLLECTION } from "../../../types/procedure"; // Added procedure collection
5
- import { CLINIC_GROUPS_COLLECTION } from "../../../types/clinic"; // Added clinic group collection
8
+ import { CLINIC_GROUPS_COLLECTION, ClinicGroup } from "../../../types/clinic"; // Added clinic group collection
6
9
  import { ClinicLocation } from "../../../types/clinic"; // Added ClinicLocation type
7
10
  import { CALENDAR_COLLECTION } from "../../../types/calendar"; // Added calendar collection
8
11
  import { PATIENTS_COLLECTION } from "../../../types/patient"; // Added patient collection
@@ -103,16 +106,27 @@ export class ClinicAggregationService {
103
106
  .collection(PRACTITIONERS_COLLECTION)
104
107
  .doc(practitionerId);
105
108
 
106
- // Remove old clinic info based on ID matcher
107
- batch.update(practitionerRef, {
108
- clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
109
- updatedAt: admin.firestore.FieldValue.serverTimestamp(),
110
- });
111
- // Add updated clinic info
112
- batch.update(practitionerRef, {
113
- clinicsInfo: admin.firestore.FieldValue.arrayUnion(clinicInfo),
114
- updatedAt: admin.firestore.FieldValue.serverTimestamp(),
115
- });
109
+ // Remove old clinic info based on ID matcher and add updated info
110
+ // First, get the current practitioner data to filter clinicsInfo manually
111
+ const practitionerDoc = await practitionerRef.get();
112
+ if (practitionerDoc.exists) {
113
+ const practitionerData = practitionerDoc.data() as Practitioner;
114
+ const currentClinicsInfo = practitionerData?.clinicsInfo || [];
115
+
116
+ // Filter out the clinic info with matching ID
117
+ const filteredClinicsInfo = currentClinicsInfo.filter(
118
+ (clinic: any) => clinic.id !== clinicId
119
+ );
120
+
121
+ // Add the updated clinic info to the filtered array
122
+ const updatedClinicsInfo = [...filteredClinicsInfo, clinicInfo];
123
+
124
+ // Update with the complete new array
125
+ batch.update(practitionerRef, {
126
+ clinicsInfo: updatedClinicsInfo,
127
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
128
+ });
129
+ }
116
130
  }
117
131
 
118
132
  try {
@@ -194,7 +208,6 @@ export class ClinicAggregationService {
194
208
  return;
195
209
  }
196
210
 
197
- const batch = this.db.batch();
198
211
  const clinicId = clinicInfo.id;
199
212
  const groupRef = this.db
200
213
  .collection(CLINIC_GROUPS_COLLECTION)
@@ -204,25 +217,38 @@ export class ClinicAggregationService {
204
217
  `[ClinicAggregationService] Starting update of ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
205
218
  );
206
219
 
207
- // Remove old clinic info based on ID matcher
208
- batch.update(groupRef, {
209
- clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
210
- updatedAt: admin.firestore.FieldValue.serverTimestamp(),
211
- });
212
- // Add updated clinic info
213
- batch.update(groupRef, {
214
- clinicsInfo: admin.firestore.FieldValue.arrayUnion(clinicInfo),
215
- updatedAt: admin.firestore.FieldValue.serverTimestamp(),
216
- });
217
-
218
220
  try {
219
- await batch.commit();
220
- console.log(
221
- `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
222
- );
221
+ // Get current clinic group data to filter clinicsInfo manually
222
+ const clinicGroupDoc = await groupRef.get();
223
+ if (clinicGroupDoc.exists) {
224
+ const clinicGroupData = clinicGroupDoc.data() as ClinicGroup;
225
+ const currentClinicsInfo = clinicGroupData?.clinicsInfo || [];
226
+
227
+ // Filter out the clinic info with matching ID
228
+ const filteredClinicsInfo = currentClinicsInfo.filter(
229
+ (clinic: any) => clinic.id !== clinicId
230
+ );
231
+
232
+ // Add the updated clinic info to the filtered array
233
+ const updatedClinicsInfo = [...filteredClinicsInfo, clinicInfo];
234
+
235
+ // Update with the complete new array
236
+ await groupRef.update({
237
+ clinicsInfo: updatedClinicsInfo,
238
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
239
+ });
240
+
241
+ console.log(
242
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
243
+ );
244
+ } else {
245
+ console.warn(
246
+ `[ClinicAggregationService] Clinic Group ${clinicGroupId} not found. Cannot update clinic info.`
247
+ );
248
+ }
223
249
  } catch (error) {
224
250
  console.error(
225
- `[ClinicAggregationService] Error committing update for ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
251
+ `[ClinicAggregationService] Error updating ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
226
252
  error
227
253
  );
228
254
  throw error;
@@ -427,13 +453,28 @@ export class ClinicAggregationService {
427
453
  .collection(PRACTITIONERS_COLLECTION)
428
454
  .doc(practitionerId);
429
455
 
430
- // Remove clinic ID from clinicIds array
431
- batch.update(practitionerRef, {
432
- clinicIds: admin.firestore.FieldValue.arrayRemove(clinicId),
433
- // Remove all clinic info objects where id matches the clinic ID
434
- clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
435
- updatedAt: admin.firestore.FieldValue.serverTimestamp(),
436
- });
456
+ // Get current practitioner data to filter manually
457
+ const practitionerDoc = await practitionerRef.get();
458
+ if (practitionerDoc.exists) {
459
+ const practitionerData = practitionerDoc.data() as Practitioner;
460
+ const currentClinicIds = practitionerData?.clinics || [];
461
+ const currentClinicsInfo = practitionerData?.clinicsInfo || [];
462
+
463
+ // Filter out the clinic ID and clinic info with matching ID
464
+ const filteredClinicIds = currentClinicIds.filter(
465
+ (id: string) => id !== clinicId
466
+ );
467
+ const filteredClinicsInfo = currentClinicsInfo.filter(
468
+ (clinic: any) => clinic.id !== clinicId
469
+ );
470
+
471
+ // Update with the filtered arrays
472
+ batch.update(practitionerRef, {
473
+ clinics: filteredClinicIds,
474
+ clinicsInfo: filteredClinicsInfo,
475
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
476
+ });
477
+ }
437
478
  }
438
479
 
439
480
  try {
@@ -518,16 +559,36 @@ export class ClinicAggregationService {
518
559
  );
519
560
 
520
561
  try {
521
- await groupRef.update({
522
- clinicIds: admin.firestore.FieldValue.arrayRemove(clinicId),
523
- // Remove all clinic info objects where id matches the clinic ID
524
- clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
525
- updatedAt: admin.firestore.FieldValue.serverTimestamp(),
526
- });
562
+ // Get current clinic group data to filter manually
563
+ const clinicGroupDoc = await groupRef.get();
564
+ if (clinicGroupDoc.exists) {
565
+ const clinicGroupData = clinicGroupDoc.data() as ClinicGroup;
566
+ const currentClinicIds = clinicGroupData?.clinics || [];
567
+ const currentClinicsInfo = clinicGroupData?.clinicsInfo || [];
568
+
569
+ // Filter out the clinic ID and clinic info with matching ID
570
+ const filteredClinicIds = currentClinicIds.filter(
571
+ (id: string) => id !== clinicId
572
+ );
573
+ const filteredClinicsInfo = currentClinicsInfo.filter(
574
+ (clinic: any) => clinic.id !== clinicId
575
+ );
527
576
 
528
- console.log(
529
- `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
530
- );
577
+ // Update with the filtered arrays
578
+ await groupRef.update({
579
+ clinics: filteredClinicIds,
580
+ clinicsInfo: filteredClinicsInfo,
581
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
582
+ });
583
+
584
+ console.log(
585
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
586
+ );
587
+ } else {
588
+ console.warn(
589
+ `[ClinicAggregationService] Clinic Group ${clinicGroupId} not found. Cannot remove clinic.`
590
+ );
591
+ }
531
592
  } catch (error) {
532
593
  console.error(
533
594
  `[ClinicAggregationService] Error removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}:`,