@blackcode_sa/metaestetics-api 1.14.26 → 1.14.28

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.
@@ -10,7 +10,12 @@ import {
10
10
  getDoc,
11
11
  QueryConstraint,
12
12
  } from "firebase/firestore";
13
- import { PatientProfile, PATIENTS_COLLECTION } from "../../../types/patient";
13
+ import {
14
+ PatientProfile,
15
+ PATIENTS_COLLECTION,
16
+ PatientSensitiveInfo,
17
+ } from "../../../types/patient";
18
+ import { getSensitiveInfoDocRef } from "./docs.utils";
14
19
 
15
20
  /**
16
21
  * Retrieves all patients associated with a specific clinic with pagination support.
@@ -78,3 +83,77 @@ export const getPatientsByClinicUtil = async (
78
83
  );
79
84
  }
80
85
  };
86
+
87
+ /**
88
+ * Retrieves all patients associated with a specific clinic, including their sensitive info.
89
+ * This merges data from PatientProfile and PatientSensitiveInfo subcollection.
90
+ *
91
+ * @param {Firestore} db - Firestore instance
92
+ * @param {string} clinicId - ID of the clinic whose patients to retrieve
93
+ * @param {Object} options - Optional parameters for pagination
94
+ * @param {number} options.limit - Maximum number of profiles to return
95
+ * @param {string} options.startAfter - The ID of the document to start after (for pagination)
96
+ * @returns {Promise<PatientProfile[]>} A promise resolving to an array of patient profiles with merged sensitive info
97
+ */
98
+ export const getPatientsByClinicWithDetailsUtil = async (
99
+ db: Firestore,
100
+ clinicId: string,
101
+ options?: { limit?: number; startAfter?: string }
102
+ ): Promise<PatientProfile[]> => {
103
+ try {
104
+ console.log(
105
+ `[getPatientsByClinicWithDetailsUtil] Fetching patients with details for clinic ID: ${clinicId} with options:`,
106
+ options
107
+ );
108
+
109
+ // First, get all patient profiles for this clinic
110
+ const patientProfiles = await getPatientsByClinicUtil(db, clinicId, options);
111
+
112
+ // Then, fetch sensitive info for each patient and merge it
113
+ const patientsWithDetails: PatientProfile[] = await Promise.all(
114
+ patientProfiles.map(async (profile) => {
115
+ try {
116
+ const sensitiveInfoDoc = await getDoc(
117
+ getSensitiveInfoDocRef(db, profile.id)
118
+ );
119
+ const sensitiveInfo = sensitiveInfoDoc.exists()
120
+ ? (sensitiveInfoDoc.data() as PatientSensitiveInfo)
121
+ : null;
122
+
123
+ // Merge sensitive info into profile (sensitive info takes precedence)
124
+ return {
125
+ ...profile,
126
+ // Merge phoneNumber from sensitive info if not in profile
127
+ phoneNumber: profile.phoneNumber || sensitiveInfo?.phoneNumber || null,
128
+ // Merge dateOfBirth from sensitive info if not in profile
129
+ dateOfBirth: profile.dateOfBirth || sensitiveInfo?.dateOfBirth || null,
130
+ // Merge photoUrl from sensitive info if available
131
+ photoUrl: sensitiveInfo?.photoUrl || null,
132
+ } as PatientProfile & { photoUrl?: string | null };
133
+ } catch (error) {
134
+ console.error(
135
+ `[getPatientsByClinicWithDetailsUtil] Error fetching sensitive info for patient ${profile.id}:`,
136
+ error
137
+ );
138
+ // Return profile without sensitive info in case of error
139
+ return profile;
140
+ }
141
+ })
142
+ );
143
+
144
+ console.log(
145
+ `[getPatientsByClinicWithDetailsUtil] Found ${patientsWithDetails.length} patients with details for clinic ID: ${clinicId}`
146
+ );
147
+ return patientsWithDetails;
148
+ } catch (error) {
149
+ console.error(
150
+ `[getPatientsByClinicWithDetailsUtil] Error fetching patients with details:`,
151
+ error
152
+ );
153
+ throw new Error(
154
+ `Failed to retrieve patients with details: ${
155
+ error instanceof Error ? error.message : String(error)
156
+ }`
157
+ );
158
+ }
159
+ };