@blackcode_sa/metaestetics-api 1.5.16 → 1.5.18

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.5.16",
4
+ "version": "1.5.18",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
package/src/index.ts CHANGED
@@ -96,6 +96,8 @@ export type {
96
96
  UpdateMedicationData,
97
97
  Allergy,
98
98
  PatientProfileComplete,
99
+ SearchPatientsParams,
100
+ RequesterInfo,
99
101
  } from "./types/patient";
100
102
  export {
101
103
  Gender,
@@ -208,6 +210,9 @@ export {
208
210
  CalendarEventStatus,
209
211
  CalendarEventType,
210
212
  CalendarSyncStatus,
213
+ SearchLocationEnum,
214
+ DateRange,
215
+ SearchCalendarEventsParams,
211
216
  CALENDAR_COLLECTION,
212
217
  } from "./types/calendar";
213
218
 
@@ -16,6 +16,9 @@ import {
16
16
  TimeSlot,
17
17
  CreateAppointmentParams,
18
18
  UpdateAppointmentParams,
19
+ SearchCalendarEventsParams,
20
+ SearchLocationEnum,
21
+ DateRange,
19
22
  } from "../../types/calendar";
20
23
  import {
21
24
  PRACTITIONERS_COLLECTION,
@@ -42,6 +45,9 @@ import {
42
45
  getDocs,
43
46
  setDoc,
44
47
  updateDoc,
48
+ QueryConstraint,
49
+ CollectionReference,
50
+ DocumentData,
45
51
  } from "firebase/firestore";
46
52
  import {
47
53
  createAppointmentSchema,
@@ -54,6 +60,7 @@ import {
54
60
  updateAppointmentUtil,
55
61
  deleteAppointmentUtil,
56
62
  } from "./utils/appointment.utils";
63
+ import { searchCalendarEventsUtil } from "./utils/calendar-event.utils";
57
64
  import { SyncedCalendarsService } from "./synced-calendars.service";
58
65
  import { Timestamp as FirestoreTimestamp } from "firebase-admin/firestore";
59
66
 
@@ -776,6 +783,146 @@ export class CalendarServiceV2 extends BaseService {
776
783
  */
777
784
  }
778
785
 
786
+ /**
787
+ * Searches for calendar events based on specified criteria.
788
+ *
789
+ * @param {SearchCalendarEventsParams} params - The search parameters.
790
+ * @param {SearchLocationEnum} params.searchLocation - The primary location to search (practitioner, patient, or clinic).
791
+ * @param {string} params.entityId - The ID of the entity (practitioner, patient, or clinic) to search within/for.
792
+ * @param {string} [params.clinicId] - Optional clinic ID to filter by.
793
+ * @param {string} [params.practitionerId] - Optional practitioner ID to filter by.
794
+ * @param {string} [params.patientId] - Optional patient ID to filter by.
795
+ * @param {string} [params.procedureId] - Optional procedure ID to filter by.
796
+ * @param {DateRange} [params.dateRange] - Optional date range to filter by (event start time).
797
+ * @param {CalendarEventStatus} [params.eventStatus] - Optional event status to filter by.
798
+ * @param {CalendarEventType} [params.eventType] - Optional event type to filter by.
799
+ * @returns {Promise<CalendarEvent[]>} A promise that resolves to an array of matching calendar events.
800
+ * @throws {Error} If the search location requires an entity ID that is not provided.
801
+ */
802
+ async searchCalendarEvents(
803
+ params: SearchCalendarEventsParams
804
+ ): Promise<CalendarEvent[]> {
805
+ // Use the utility function to perform the search
806
+ return searchCalendarEventsUtil(this.db, params);
807
+ }
808
+
809
+ /**
810
+ * Gets a doctor's upcoming appointments for a specific date range
811
+ *
812
+ * @param {string} doctorId - ID of the practitioner
813
+ * @param {Date} startDate - Start date of the range
814
+ * @param {Date} endDate - End date of the range
815
+ * @param {CalendarEventStatus} [status] - Optional status filter (defaults to CONFIRMED)
816
+ * @returns {Promise<CalendarEvent[]>} A promise that resolves to an array of appointments
817
+ */
818
+ async getPractitionerUpcomingAppointments(
819
+ doctorId: string,
820
+ startDate: Date,
821
+ endDate: Date,
822
+ status: CalendarEventStatus = CalendarEventStatus.CONFIRMED
823
+ ): Promise<CalendarEvent[]> {
824
+ // Create a date range for the query
825
+ const dateRange: DateRange = {
826
+ start: Timestamp.fromDate(startDate),
827
+ end: Timestamp.fromDate(endDate),
828
+ };
829
+
830
+ // Create the search parameters
831
+ const searchParams: SearchCalendarEventsParams = {
832
+ searchLocation: SearchLocationEnum.PRACTITIONER,
833
+ entityId: doctorId,
834
+ dateRange,
835
+ eventStatus: status,
836
+ eventType: CalendarEventType.APPOINTMENT,
837
+ };
838
+
839
+ // Search for the appointments
840
+ return this.searchCalendarEvents(searchParams);
841
+ }
842
+
843
+ /**
844
+ * Gets a patient's appointments for a specific date range
845
+ *
846
+ * @param {string} patientId - ID of the patient
847
+ * @param {Date} startDate - Start date of the range
848
+ * @param {Date} endDate - End date of the range
849
+ * @param {CalendarEventStatus} [status] - Optional status filter (defaults to all non-canceled appointments)
850
+ * @returns {Promise<CalendarEvent[]>} A promise that resolves to an array of appointments
851
+ */
852
+ async getPatientAppointments(
853
+ patientId: string,
854
+ startDate: Date,
855
+ endDate: Date,
856
+ status?: CalendarEventStatus
857
+ ): Promise<CalendarEvent[]> {
858
+ // Create a date range for the query
859
+ const dateRange: DateRange = {
860
+ start: Timestamp.fromDate(startDate),
861
+ end: Timestamp.fromDate(endDate),
862
+ };
863
+
864
+ // Create the search parameters
865
+ const searchParams: SearchCalendarEventsParams = {
866
+ searchLocation: SearchLocationEnum.PATIENT,
867
+ entityId: patientId,
868
+ dateRange,
869
+ eventType: CalendarEventType.APPOINTMENT,
870
+ };
871
+
872
+ // Add status filter if provided
873
+ if (status) {
874
+ searchParams.eventStatus = status;
875
+ }
876
+
877
+ // Search for the appointments
878
+ return this.searchCalendarEvents(searchParams);
879
+ }
880
+
881
+ /**
882
+ * Gets all appointments for a clinic within a specific date range
883
+ *
884
+ * @param {string} clinicId - ID of the clinic
885
+ * @param {Date} startDate - Start date of the range
886
+ * @param {Date} endDate - End date of the range
887
+ * @param {string} [doctorId] - Optional doctor ID to filter by
888
+ * @param {CalendarEventStatus} [status] - Optional status filter
889
+ * @returns {Promise<CalendarEvent[]>} A promise that resolves to an array of appointments
890
+ */
891
+ async getClinicAppointments(
892
+ clinicId: string,
893
+ startDate: Date,
894
+ endDate: Date,
895
+ doctorId?: string,
896
+ status?: CalendarEventStatus
897
+ ): Promise<CalendarEvent[]> {
898
+ // Create a date range for the query
899
+ const dateRange: DateRange = {
900
+ start: Timestamp.fromDate(startDate),
901
+ end: Timestamp.fromDate(endDate),
902
+ };
903
+
904
+ // Create the search parameters
905
+ const searchParams: SearchCalendarEventsParams = {
906
+ searchLocation: SearchLocationEnum.CLINIC,
907
+ entityId: clinicId,
908
+ dateRange,
909
+ eventType: CalendarEventType.APPOINTMENT,
910
+ };
911
+
912
+ // Add doctor filter if provided
913
+ if (doctorId) {
914
+ searchParams.practitionerId = doctorId;
915
+ }
916
+
917
+ // Add status filter if provided
918
+ if (status) {
919
+ searchParams.eventStatus = status;
920
+ }
921
+
922
+ // Search for the appointments
923
+ return this.searchCalendarEvents(searchParams);
924
+ }
925
+
779
926
  // #endregion
780
927
 
781
928
  // #region Private Helper Methods
@@ -24,6 +24,8 @@ import {
24
24
  CreateCalendarEventData,
25
25
  UpdateCalendarEventData,
26
26
  CALENDAR_COLLECTION,
27
+ SearchCalendarEventsParams,
28
+ SearchLocationEnum,
27
29
  } from "../../../types/calendar";
28
30
  import { PRACTITIONERS_COLLECTION } from "../../../types/practitioner";
29
31
  import { PATIENTS_COLLECTION } from "../../../types/patient";
@@ -508,3 +510,137 @@ export async function deleteClinicCalendarEventUtil(
508
510
  );
509
511
  await deleteDoc(eventRef);
510
512
  }
513
+
514
+ /**
515
+ * Searches for calendar events based on specified criteria
516
+ * @param db - Firestore instance
517
+ * @param params - Search parameters
518
+ * @param params.searchLocation - The primary location to search (practitioner, patient, or clinic)
519
+ * @param params.entityId - The ID of the entity (practitioner, patient, or clinic) to search within/for
520
+ * @param params.clinicId - Optional clinic ID to filter by
521
+ * @param params.practitionerId - Optional practitioner ID to filter by
522
+ * @param params.patientId - Optional patient ID to filter by
523
+ * @param params.procedureId - Optional procedure ID to filter by
524
+ * @param params.dateRange - Optional date range to filter by (event start time)
525
+ * @param params.eventStatus - Optional event status to filter by
526
+ * @param params.eventType - Optional event type to filter by
527
+ * @returns Promise resolving to an array of matching calendar events
528
+ */
529
+ export async function searchCalendarEventsUtil(
530
+ db: Firestore,
531
+ params: SearchCalendarEventsParams
532
+ ): Promise<CalendarEvent[]> {
533
+ const { searchLocation, entityId, ...filters } = params;
534
+
535
+ let baseCollectionPath: string;
536
+ const constraints: QueryConstraint[] = [];
537
+
538
+ // Determine the base collection and apply initial filter based on searchLocation
539
+ switch (searchLocation) {
540
+ case SearchLocationEnum.PRACTITIONER:
541
+ if (!entityId) {
542
+ throw new Error(
543
+ "Practitioner ID (entityId) is required when searching practitioner calendar."
544
+ );
545
+ }
546
+ baseCollectionPath = `${PRACTITIONERS_COLLECTION}/${entityId}/${CALENDAR_COLLECTION}`;
547
+ // If practitionerId filter is provided, it must match the entityId for this search location
548
+ if (filters.practitionerId && filters.practitionerId !== entityId) {
549
+ console.warn(
550
+ `Provided practitionerId filter (${filters.practitionerId}) does not match search entityId (${entityId}). Returning empty results.`
551
+ );
552
+ return [];
553
+ }
554
+ // Ensure we don't add a redundant filter if the caller also specified it
555
+ filters.practitionerId = undefined;
556
+ break;
557
+
558
+ case SearchLocationEnum.PATIENT:
559
+ if (!entityId) {
560
+ throw new Error(
561
+ "Patient ID (entityId) is required when searching patient calendar."
562
+ );
563
+ }
564
+ baseCollectionPath = `${PATIENTS_COLLECTION}/${entityId}/${CALENDAR_COLLECTION}`;
565
+ // If patientId filter is provided, it must match the entityId for this search location
566
+ if (filters.patientId && filters.patientId !== entityId) {
567
+ console.warn(
568
+ `Provided patientId filter (${filters.patientId}) does not match search entityId (${entityId}). Returning empty results.`
569
+ );
570
+ return [];
571
+ }
572
+ // Ensure we don't add a redundant filter if the caller also specified it
573
+ filters.patientId = undefined;
574
+ break;
575
+
576
+ case SearchLocationEnum.CLINIC:
577
+ if (!entityId) {
578
+ throw new Error(
579
+ "Clinic ID (entityId) is required when searching clinic-related events."
580
+ );
581
+ }
582
+ // Search the root CALENDAR_COLLECTION for events associated with the clinic
583
+ baseCollectionPath = CALENDAR_COLLECTION;
584
+ constraints.push(where("clinicBranchId", "==", entityId));
585
+ // If clinicId filter is provided, it must match the entityId for this search location
586
+ if (filters.clinicId && filters.clinicId !== entityId) {
587
+ console.warn(
588
+ `Provided clinicId filter (${filters.clinicId}) does not match search entityId (${entityId}). Returning empty results.`
589
+ );
590
+ return [];
591
+ }
592
+ // Ensure we don't add a redundant filter if the caller also specified it
593
+ filters.clinicId = undefined; // Already handled by the base query
594
+ break;
595
+
596
+ default:
597
+ throw new Error(`Invalid search location: ${searchLocation}`);
598
+ }
599
+
600
+ const collectionRef = collection(db, baseCollectionPath);
601
+
602
+ // Apply optional filters
603
+ if (filters.clinicId) {
604
+ constraints.push(where("clinicBranchId", "==", filters.clinicId));
605
+ }
606
+ if (filters.practitionerId) {
607
+ constraints.push(
608
+ where("practitionerProfileId", "==", filters.practitionerId)
609
+ );
610
+ }
611
+ if (filters.patientId) {
612
+ constraints.push(where("patientProfileId", "==", filters.patientId));
613
+ }
614
+ if (filters.procedureId) {
615
+ constraints.push(where("procedureId", "==", filters.procedureId));
616
+ }
617
+ if (filters.eventStatus) {
618
+ constraints.push(where("status", "==", filters.eventStatus));
619
+ }
620
+ if (filters.eventType) {
621
+ constraints.push(where("eventType", "==", filters.eventType));
622
+ }
623
+ if (filters.dateRange) {
624
+ // Firestore requires range filters on the same field
625
+ constraints.push(where("eventTime.start", ">=", filters.dateRange.start));
626
+ constraints.push(where("eventTime.start", "<=", filters.dateRange.end));
627
+ // Note: You might need to order by eventTime.start for range filters to work efficiently
628
+ // constraints.push(orderBy("eventTime.start"));
629
+ }
630
+
631
+ // Build and execute the query
632
+ try {
633
+ const finalQuery = query(collectionRef, ...constraints);
634
+ const querySnapshot = await getDocs(finalQuery);
635
+
636
+ const events = querySnapshot.docs.map(
637
+ (doc) => ({ id: doc.id, ...doc.data() } as CalendarEvent)
638
+ );
639
+
640
+ return events;
641
+ } catch (error) {
642
+ console.error("Error searching calendar events:", error);
643
+ // Depending on requirements, you might want to return an empty array or re-throw
644
+ return [];
645
+ }
646
+ }
@@ -1,8 +1,8 @@
1
- // Export all calendar event utilities
1
+ // Export core calendar utilities
2
2
  export * from "./calendar-event.utils";
3
-
4
- // Export all synced calendar utilities
5
3
  export * from "./synced-calendar.utils";
6
-
7
- // Export all Google Calendar utilities
8
4
  export * from "./google-calendar.utils";
5
+ export * from "./appointment.utils";
6
+
7
+ // Note: Other utility files (clinic.utils.ts, patient.utils.ts, practitioner.utils.ts, docs.utils.ts)
8
+ // should be imported directly where needed to avoid naming conflicts