@blackcode_sa/metaestetics-api 1.7.24 → 1.7.26

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/dist/index.js CHANGED
@@ -523,7 +523,7 @@ var appointmentMediaItemSchema = import_zod2.z.object({
523
523
  url: import_zod2.z.string().url("Media URL must be a valid URL"),
524
524
  fileName: import_zod2.z.string().optional(),
525
525
  uploadedAt: import_zod2.z.any().refine(
526
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
526
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
527
527
  "uploadedAt must be a valid timestamp or Date object"
528
528
  ),
529
529
  uploadedBy: import_zod2.z.string().min(MIN_STRING_LENGTH, "Uploaded by user ID is required"),
@@ -557,11 +557,11 @@ var linkedFormInfoSchema = import_zod2.z.object({
557
557
  status: filledDocumentStatusSchema,
558
558
  path: import_zod2.z.string().min(MIN_STRING_LENGTH, "Form path is required"),
559
559
  submittedAt: import_zod2.z.any().refine(
560
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
560
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
561
561
  "submittedAt must be a valid timestamp or Date object"
562
562
  ).optional(),
563
563
  completedAt: import_zod2.z.any().refine(
564
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
564
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
565
565
  "completedAt must be a valid timestamp or Date object"
566
566
  ).optional()
567
567
  });
@@ -570,14 +570,14 @@ var patientReviewInfoSchema = import_zod2.z.object({
570
570
  rating: import_zod2.z.number().min(1).max(5, "Rating must be between 1 and 5"),
571
571
  comment: import_zod2.z.string().max(MAX_STRING_LENGTH_LONG, "Comment too long").optional(),
572
572
  reviewedAt: import_zod2.z.any().refine(
573
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
573
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
574
574
  "reviewedAt must be a valid timestamp or Date object"
575
575
  )
576
576
  });
577
577
  var finalizedDetailsSchema = import_zod2.z.object({
578
578
  by: import_zod2.z.string().min(MIN_STRING_LENGTH, "Finalized by user ID is required"),
579
579
  at: import_zod2.z.any().refine(
580
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
580
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
581
581
  "Finalized at must be a valid timestamp or Date object"
582
582
  ),
583
583
  notes: import_zod2.z.string().max(MAX_STRING_LENGTH_LONG, "Finalization notes too long").optional()
@@ -588,11 +588,11 @@ var createAppointmentSchema = import_zod2.z.object({
588
588
  patientId: import_zod2.z.string().min(MIN_STRING_LENGTH, "Patient ID is required"),
589
589
  procedureId: import_zod2.z.string().min(MIN_STRING_LENGTH, "Procedure ID is required"),
590
590
  appointmentStartTime: import_zod2.z.any().refine(
591
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
591
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
592
592
  "Appointment start time must be a valid timestamp or Date object"
593
593
  ),
594
594
  appointmentEndTime: import_zod2.z.any().refine(
595
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
595
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
596
596
  "Appointment end time must be a valid timestamp or Date object"
597
597
  ),
598
598
  cost: import_zod2.z.number().min(0, "Cost must be a non-negative number"),
@@ -622,11 +622,11 @@ var updateAppointmentSchema = import_zod2.z.object({
622
622
  linkedFormIds: import_zod2.z.union([import_zod2.z.array(import_zod2.z.string()), import_zod2.z.any()]).optional(),
623
623
  pendingUserFormsIds: import_zod2.z.union([import_zod2.z.array(import_zod2.z.string()), import_zod2.z.any()]).optional(),
624
624
  appointmentStartTime: import_zod2.z.any().refine(
625
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
625
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
626
626
  "Appointment start time must be a valid timestamp or Date object"
627
627
  ).optional(),
628
628
  appointmentEndTime: import_zod2.z.any().refine(
629
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
629
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
630
630
  "Appointment end time must be a valid timestamp or Date object"
631
631
  ).optional(),
632
632
  calendarEventId: import_zod2.z.string().min(MIN_STRING_LENGTH).optional(),
@@ -670,11 +670,11 @@ var searchAppointmentsSchema = import_zod2.z.object({
670
670
  practitionerId: import_zod2.z.string().optional(),
671
671
  clinicBranchId: import_zod2.z.string().optional(),
672
672
  startDate: import_zod2.z.any().refine(
673
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
673
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
674
674
  "Start date must be a valid timestamp or Date object"
675
675
  ).optional(),
676
676
  endDate: import_zod2.z.any().refine(
677
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
677
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
678
678
  "End date must be a valid timestamp or Date object"
679
679
  ).optional(),
680
680
  status: import_zod2.z.union([
package/dist/index.mjs CHANGED
@@ -281,7 +281,7 @@ var appointmentMediaItemSchema = z2.object({
281
281
  url: z2.string().url("Media URL must be a valid URL"),
282
282
  fileName: z2.string().optional(),
283
283
  uploadedAt: z2.any().refine(
284
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
284
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
285
285
  "uploadedAt must be a valid timestamp or Date object"
286
286
  ),
287
287
  uploadedBy: z2.string().min(MIN_STRING_LENGTH, "Uploaded by user ID is required"),
@@ -315,11 +315,11 @@ var linkedFormInfoSchema = z2.object({
315
315
  status: filledDocumentStatusSchema,
316
316
  path: z2.string().min(MIN_STRING_LENGTH, "Form path is required"),
317
317
  submittedAt: z2.any().refine(
318
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
318
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
319
319
  "submittedAt must be a valid timestamp or Date object"
320
320
  ).optional(),
321
321
  completedAt: z2.any().refine(
322
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
322
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
323
323
  "completedAt must be a valid timestamp or Date object"
324
324
  ).optional()
325
325
  });
@@ -328,14 +328,14 @@ var patientReviewInfoSchema = z2.object({
328
328
  rating: z2.number().min(1).max(5, "Rating must be between 1 and 5"),
329
329
  comment: z2.string().max(MAX_STRING_LENGTH_LONG, "Comment too long").optional(),
330
330
  reviewedAt: z2.any().refine(
331
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
331
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
332
332
  "reviewedAt must be a valid timestamp or Date object"
333
333
  )
334
334
  });
335
335
  var finalizedDetailsSchema = z2.object({
336
336
  by: z2.string().min(MIN_STRING_LENGTH, "Finalized by user ID is required"),
337
337
  at: z2.any().refine(
338
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
338
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
339
339
  "Finalized at must be a valid timestamp or Date object"
340
340
  ),
341
341
  notes: z2.string().max(MAX_STRING_LENGTH_LONG, "Finalization notes too long").optional()
@@ -346,11 +346,11 @@ var createAppointmentSchema = z2.object({
346
346
  patientId: z2.string().min(MIN_STRING_LENGTH, "Patient ID is required"),
347
347
  procedureId: z2.string().min(MIN_STRING_LENGTH, "Procedure ID is required"),
348
348
  appointmentStartTime: z2.any().refine(
349
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
349
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
350
350
  "Appointment start time must be a valid timestamp or Date object"
351
351
  ),
352
352
  appointmentEndTime: z2.any().refine(
353
- (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
353
+ (val) => val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
354
354
  "Appointment end time must be a valid timestamp or Date object"
355
355
  ),
356
356
  cost: z2.number().min(0, "Cost must be a non-negative number"),
@@ -380,11 +380,11 @@ var updateAppointmentSchema = z2.object({
380
380
  linkedFormIds: z2.union([z2.array(z2.string()), z2.any()]).optional(),
381
381
  pendingUserFormsIds: z2.union([z2.array(z2.string()), z2.any()]).optional(),
382
382
  appointmentStartTime: z2.any().refine(
383
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
383
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
384
384
  "Appointment start time must be a valid timestamp or Date object"
385
385
  ).optional(),
386
386
  appointmentEndTime: z2.any().refine(
387
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
387
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
388
388
  "Appointment end time must be a valid timestamp or Date object"
389
389
  ).optional(),
390
390
  calendarEventId: z2.string().min(MIN_STRING_LENGTH).optional(),
@@ -428,11 +428,11 @@ var searchAppointmentsSchema = z2.object({
428
428
  practitionerId: z2.string().optional(),
429
429
  clinicBranchId: z2.string().optional(),
430
430
  startDate: z2.any().refine(
431
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
431
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
432
432
  "Start date must be a valid timestamp or Date object"
433
433
  ).optional(),
434
434
  endDate: z2.any().refine(
435
- (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || typeof val === "number",
435
+ (val) => val === void 0 || val instanceof Date || (val == null ? void 0 : val._seconds) !== void 0 || (val == null ? void 0 : val.seconds) !== void 0 || typeof val === "number" || typeof val === "string" || val && typeof val.toMillis === "function",
436
436
  "End date must be a valid timestamp or Date object"
437
437
  ).optional(),
438
438
  status: z2.union([
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.26",
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}:`,
@@ -30,7 +30,10 @@ export const appointmentMediaItemSchema = z.object({
30
30
  (val) =>
31
31
  val instanceof Date ||
32
32
  val?._seconds !== undefined ||
33
- typeof val === "number",
33
+ val?.seconds !== undefined ||
34
+ typeof val === "number" ||
35
+ typeof val === "string" ||
36
+ (val && typeof val.toMillis === "function"),
34
37
  "uploadedAt must be a valid timestamp or Date object"
35
38
  ),
36
39
  uploadedBy: z
@@ -80,7 +83,10 @@ export const linkedFormInfoSchema = z.object({
80
83
  val === undefined ||
81
84
  val instanceof Date ||
82
85
  val?._seconds !== undefined ||
83
- typeof val === "number",
86
+ val?.seconds !== undefined ||
87
+ typeof val === "number" ||
88
+ typeof val === "string" ||
89
+ (val && typeof val.toMillis === "function"),
84
90
  "submittedAt must be a valid timestamp or Date object"
85
91
  )
86
92
  .optional(),
@@ -91,7 +97,10 @@ export const linkedFormInfoSchema = z.object({
91
97
  val === undefined ||
92
98
  val instanceof Date ||
93
99
  val?._seconds !== undefined ||
94
- typeof val === "number",
100
+ val?.seconds !== undefined ||
101
+ typeof val === "number" ||
102
+ typeof val === "string" ||
103
+ (val && typeof val.toMillis === "function"),
95
104
  "completedAt must be a valid timestamp or Date object"
96
105
  )
97
106
  .optional(),
@@ -110,7 +119,10 @@ export const patientReviewInfoSchema = z.object({
110
119
  (val) =>
111
120
  val instanceof Date ||
112
121
  val?._seconds !== undefined ||
113
- typeof val === "number",
122
+ val?.seconds !== undefined ||
123
+ typeof val === "number" ||
124
+ typeof val === "string" ||
125
+ (val && typeof val.toMillis === "function"),
114
126
  "reviewedAt must be a valid timestamp or Date object"
115
127
  ),
116
128
  });
@@ -123,7 +135,10 @@ export const finalizedDetailsSchema = z.object({
123
135
  (val) =>
124
136
  val instanceof Date ||
125
137
  val?._seconds !== undefined ||
126
- typeof val === "number",
138
+ val?.seconds !== undefined ||
139
+ typeof val === "number" ||
140
+ typeof val === "string" ||
141
+ (val && typeof val.toMillis === "function"),
127
142
  "Finalized at must be a valid timestamp or Date object"
128
143
  ),
129
144
  notes: z
@@ -153,7 +168,10 @@ export const createAppointmentSchema = z
153
168
  (val) =>
154
169
  val instanceof Date ||
155
170
  val?._seconds !== undefined ||
156
- typeof val === "number",
171
+ val?.seconds !== undefined ||
172
+ typeof val === "number" ||
173
+ typeof val === "string" ||
174
+ (val && typeof val.toMillis === "function"),
157
175
  "Appointment start time must be a valid timestamp or Date object"
158
176
  ),
159
177
  appointmentEndTime: z
@@ -162,7 +180,10 @@ export const createAppointmentSchema = z
162
180
  (val) =>
163
181
  val instanceof Date ||
164
182
  val?._seconds !== undefined ||
165
- typeof val === "number",
183
+ val?.seconds !== undefined ||
184
+ typeof val === "number" ||
185
+ typeof val === "string" ||
186
+ (val && typeof val.toMillis === "function"),
166
187
  "Appointment end time must be a valid timestamp or Date object"
167
188
  ),
168
189
  cost: z.number().min(0, "Cost must be a non-negative number"),
@@ -228,7 +249,10 @@ export const updateAppointmentSchema = z
228
249
  val === undefined ||
229
250
  val instanceof Date ||
230
251
  val?._seconds !== undefined ||
231
- typeof val === "number",
252
+ val?.seconds !== undefined ||
253
+ typeof val === "number" ||
254
+ typeof val === "string" ||
255
+ (val && typeof val.toMillis === "function"),
232
256
  "Appointment start time must be a valid timestamp or Date object"
233
257
  )
234
258
  .optional(),
@@ -239,7 +263,10 @@ export const updateAppointmentSchema = z
239
263
  val === undefined ||
240
264
  val instanceof Date ||
241
265
  val?._seconds !== undefined ||
242
- typeof val === "number",
266
+ val?.seconds !== undefined ||
267
+ typeof val === "number" ||
268
+ typeof val === "string" ||
269
+ (val && typeof val.toMillis === "function"),
243
270
  "Appointment end time must be a valid timestamp or Date object"
244
271
  )
245
272
  .optional(),
@@ -311,7 +338,10 @@ export const searchAppointmentsSchema = z
311
338
  val === undefined ||
312
339
  val instanceof Date ||
313
340
  val?._seconds !== undefined ||
314
- typeof val === "number",
341
+ val?.seconds !== undefined ||
342
+ typeof val === "number" ||
343
+ typeof val === "string" ||
344
+ (val && typeof val.toMillis === "function"),
315
345
  "Start date must be a valid timestamp or Date object"
316
346
  )
317
347
  .optional(),
@@ -322,7 +352,10 @@ export const searchAppointmentsSchema = z
322
352
  val === undefined ||
323
353
  val instanceof Date ||
324
354
  val?._seconds !== undefined ||
325
- typeof val === "number",
355
+ val?.seconds !== undefined ||
356
+ typeof val === "number" ||
357
+ typeof val === "string" ||
358
+ (val && typeof val.toMillis === "function"),
326
359
  "End date must be a valid timestamp or Date object"
327
360
  )
328
361
  .optional(),