@blackcode_sa/metaestetics-api 1.5.28 → 1.5.29
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/dist/admin/index.d.mts +1199 -1
- package/dist/admin/index.d.ts +1199 -1
- package/dist/admin/index.js +1337 -2
- package/dist/admin/index.mjs +1333 -2
- package/dist/backoffice/index.d.mts +99 -7
- package/dist/backoffice/index.d.ts +99 -7
- package/dist/index.d.mts +4035 -2364
- package/dist/index.d.ts +4035 -2364
- package/dist/index.js +2616 -1929
- package/dist/index.mjs +2646 -1952
- package/package.json +1 -1
- package/src/admin/aggregation/clinic/clinic.aggregation.service.ts +642 -0
- package/src/admin/aggregation/patient/patient.aggregation.service.ts +141 -0
- package/src/admin/aggregation/practitioner/practitioner.aggregation.service.ts +433 -0
- package/src/admin/aggregation/procedure/procedure.aggregation.service.ts +508 -0
- package/src/admin/index.ts +53 -4
- package/src/index.ts +28 -4
- package/src/services/clinic/clinic.service.ts +320 -107
- package/src/services/clinic/utils/clinic.utils.ts +66 -117
- package/src/services/clinic/utils/filter.utils.d.ts +23 -0
- package/src/services/clinic/utils/filter.utils.ts +264 -0
- package/src/services/practitioner/practitioner.service.ts +616 -5
- package/src/services/procedure/procedure.service.ts +599 -352
- package/src/services/reviews/reviews.service.ts +842 -0
- package/src/types/clinic/index.ts +24 -56
- package/src/types/practitioner/index.ts +34 -33
- package/src/types/procedure/index.ts +32 -0
- package/src/types/profile/index.ts +1 -1
- package/src/types/reviews/index.ts +126 -0
- package/src/validations/clinic.schema.ts +37 -64
- package/src/validations/practitioner.schema.ts +42 -32
- package/src/validations/procedure.schema.ts +11 -3
- package/src/validations/reviews.schema.ts +189 -0
- package/src/services/clinic/utils/review.utils.ts +0 -93
package/dist/admin/index.mjs
CHANGED
|
@@ -20,9 +20,9 @@ var NotificationStatus = /* @__PURE__ */ ((NotificationStatus2) => {
|
|
|
20
20
|
import * as admin from "firebase-admin";
|
|
21
21
|
import { Expo } from "expo-server-sdk";
|
|
22
22
|
var NotificationsAdmin = class {
|
|
23
|
-
constructor(
|
|
23
|
+
constructor(firestore6) {
|
|
24
24
|
this.expo = new Expo();
|
|
25
|
-
this.db =
|
|
25
|
+
this.db = firestore6 || admin.firestore();
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Dohvata notifikaciju po ID-u
|
|
@@ -186,6 +186,1330 @@ var NotificationsAdmin = class {
|
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
|
|
189
|
+
// src/admin/aggregation/clinic/clinic.aggregation.service.ts
|
|
190
|
+
import * as admin2 from "firebase-admin";
|
|
191
|
+
|
|
192
|
+
// src/types/practitioner/index.ts
|
|
193
|
+
var PRACTITIONERS_COLLECTION = "practitioners";
|
|
194
|
+
|
|
195
|
+
// src/types/procedure/index.ts
|
|
196
|
+
var PROCEDURES_COLLECTION = "procedures";
|
|
197
|
+
|
|
198
|
+
// src/types/clinic/index.ts
|
|
199
|
+
var CLINIC_GROUPS_COLLECTION = "clinic_groups";
|
|
200
|
+
var CLINICS_COLLECTION = "clinics";
|
|
201
|
+
|
|
202
|
+
// src/types/patient/index.ts
|
|
203
|
+
var PATIENTS_COLLECTION = "patients";
|
|
204
|
+
|
|
205
|
+
// src/admin/aggregation/clinic/clinic.aggregation.service.ts
|
|
206
|
+
var CALENDAR_SUBCOLLECTION_ID = "calendar";
|
|
207
|
+
var ClinicAggregationService = class {
|
|
208
|
+
/**
|
|
209
|
+
* Constructor for ClinicAggregationService.
|
|
210
|
+
* @param firestore Optional Firestore instance. If not provided, it uses the default admin SDK instance.
|
|
211
|
+
*/
|
|
212
|
+
constructor(firestore6) {
|
|
213
|
+
this.db = firestore6 || admin2.firestore();
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Adds clinic information to a clinic group when a new clinic is created
|
|
217
|
+
* @param clinicGroupId - ID of the parent clinic group
|
|
218
|
+
* @param clinicInfo - The clinic information to add to the group
|
|
219
|
+
* @returns {Promise<void>}
|
|
220
|
+
* @throws Will throw an error if the batch write fails
|
|
221
|
+
*/
|
|
222
|
+
async addClinicToClinicGroup(clinicGroupId, clinicInfo) {
|
|
223
|
+
if (!clinicGroupId) {
|
|
224
|
+
console.log(
|
|
225
|
+
"[ClinicAggregationService] No Clinic Group ID provided for clinic addition. Skipping."
|
|
226
|
+
);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const clinicId = clinicInfo.id;
|
|
230
|
+
const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
|
|
231
|
+
console.log(
|
|
232
|
+
`[ClinicAggregationService] Adding ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}.`
|
|
233
|
+
);
|
|
234
|
+
try {
|
|
235
|
+
await groupRef.update({
|
|
236
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayUnion(clinicInfo),
|
|
237
|
+
clinicIds: admin2.firestore.FieldValue.arrayUnion(clinicId),
|
|
238
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
239
|
+
});
|
|
240
|
+
console.log(
|
|
241
|
+
`[ClinicAggregationService] Successfully added ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}.`
|
|
242
|
+
);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error(
|
|
245
|
+
`[ClinicAggregationService] Error adding ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}:`,
|
|
246
|
+
error
|
|
247
|
+
);
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Updates the ClinicInfo within the clinicsInfo array for multiple practitioners.
|
|
253
|
+
* This method is designed to be called when a clinic's core information changes.
|
|
254
|
+
* @param practitionerIds - IDs of practitioners associated with the clinic.
|
|
255
|
+
* @param clinicInfo - The updated ClinicInfo object to aggregate.
|
|
256
|
+
* @returns {Promise<void>}
|
|
257
|
+
* @throws Will throw an error if the batch write fails.
|
|
258
|
+
*/
|
|
259
|
+
async updateClinicInfoInPractitioners(practitionerIds, clinicInfo) {
|
|
260
|
+
if (!practitionerIds || practitionerIds.length === 0) {
|
|
261
|
+
console.log(
|
|
262
|
+
"[ClinicAggregationService] No practitioner IDs provided for clinic info update. Skipping."
|
|
263
|
+
);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const batch = this.db.batch();
|
|
267
|
+
const clinicId = clinicInfo.id;
|
|
268
|
+
console.log(
|
|
269
|
+
`[ClinicAggregationService] Starting batch update of ClinicInfo (ID: ${clinicId}) in ${practitionerIds.length} practitioners.`
|
|
270
|
+
);
|
|
271
|
+
for (const practitionerId of practitionerIds) {
|
|
272
|
+
const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
|
|
273
|
+
batch.update(practitionerRef, {
|
|
274
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
|
|
275
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
276
|
+
});
|
|
277
|
+
batch.update(practitionerRef, {
|
|
278
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayUnion(clinicInfo),
|
|
279
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
await batch.commit();
|
|
284
|
+
console.log(
|
|
285
|
+
`[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in ${practitionerIds.length} practitioners.`
|
|
286
|
+
);
|
|
287
|
+
} catch (error) {
|
|
288
|
+
console.error(
|
|
289
|
+
`[ClinicAggregationService] Error committing batch update for ClinicInfo (ID: ${clinicId}) in practitioners:`,
|
|
290
|
+
error
|
|
291
|
+
);
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Updates the aggregated clinicInfo field within relevant Procedure documents.
|
|
297
|
+
* @param procedureIds IDs of procedures performed at the clinic.
|
|
298
|
+
* @param clinicInfo The updated ClinicInfo object.
|
|
299
|
+
*/
|
|
300
|
+
async updateClinicInfoInProcedures(procedureIds, clinicInfo) {
|
|
301
|
+
if (!procedureIds || procedureIds.length === 0) {
|
|
302
|
+
console.log(
|
|
303
|
+
"[ClinicAggregationService] No procedure IDs provided for clinic info update. Skipping."
|
|
304
|
+
);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const batch = this.db.batch();
|
|
308
|
+
const clinicId = clinicInfo.id;
|
|
309
|
+
console.log(
|
|
310
|
+
`[ClinicAggregationService] Starting batch update of clinicInfo field (for Clinic ID: ${clinicId}) in ${procedureIds.length} procedures.`
|
|
311
|
+
);
|
|
312
|
+
for (const procedureId of procedureIds) {
|
|
313
|
+
const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
|
|
314
|
+
batch.update(procedureRef, {
|
|
315
|
+
clinicInfo,
|
|
316
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
await batch.commit();
|
|
321
|
+
console.log(
|
|
322
|
+
`[ClinicAggregationService] Successfully updated clinicInfo field in ${procedureIds.length} procedures for clinic ${clinicId}.`
|
|
323
|
+
);
|
|
324
|
+
} catch (error) {
|
|
325
|
+
console.error(
|
|
326
|
+
`[ClinicAggregationService] Error committing batch update for clinicInfo field in procedures (Clinic ID: ${clinicId}):`,
|
|
327
|
+
error
|
|
328
|
+
);
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Updates the aggregated clinicsInfo array within the parent ClinicGroup document.
|
|
334
|
+
* @param clinicGroupId The ID of the parent clinic group.
|
|
335
|
+
* @param clinicInfo The updated ClinicInfo object.
|
|
336
|
+
*/
|
|
337
|
+
async updateClinicInfoInClinicGroup(clinicGroupId, clinicInfo) {
|
|
338
|
+
if (!clinicGroupId) {
|
|
339
|
+
console.log(
|
|
340
|
+
"[ClinicAggregationService] No Clinic Group ID provided for clinic info update. Skipping."
|
|
341
|
+
);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const batch = this.db.batch();
|
|
345
|
+
const clinicId = clinicInfo.id;
|
|
346
|
+
const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
|
|
347
|
+
console.log(
|
|
348
|
+
`[ClinicAggregationService] Starting update of ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
|
|
349
|
+
);
|
|
350
|
+
batch.update(groupRef, {
|
|
351
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
|
|
352
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
353
|
+
});
|
|
354
|
+
batch.update(groupRef, {
|
|
355
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayUnion(clinicInfo),
|
|
356
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
357
|
+
});
|
|
358
|
+
try {
|
|
359
|
+
await batch.commit();
|
|
360
|
+
console.log(
|
|
361
|
+
`[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
|
|
362
|
+
);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
console.error(
|
|
365
|
+
`[ClinicAggregationService] Error committing update for ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
|
|
366
|
+
error
|
|
367
|
+
);
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Updates relevant clinic information within Patient documents.
|
|
373
|
+
* NOTE: PatientProfile stores an array of PatientClinic objects, which only contain
|
|
374
|
+
* clinicId, assignedAt, assignedBy, isActive, notes. It does *not* store aggregated
|
|
375
|
+
* ClinicInfo (like name, photo). Therefore, this method currently has limited use
|
|
376
|
+
* for general clinic info updates. It might be relevant if Clinic.isActive status changes.
|
|
377
|
+
*
|
|
378
|
+
* @param patientIds IDs of patients associated with the clinic (e.g., from PatientProfile.clinicIds).
|
|
379
|
+
* @param clinicInfo The updated ClinicInfo object (primarily for logging/context).
|
|
380
|
+
* @param clinicIsActive The current active status of the updated clinic.
|
|
381
|
+
*/
|
|
382
|
+
async updateClinicInfoInPatients(patientIds, clinicInfo, clinicIsActive) {
|
|
383
|
+
if (!patientIds || patientIds.length === 0) {
|
|
384
|
+
console.log(
|
|
385
|
+
"[ClinicAggregationService] No patient IDs provided for clinic info update. Skipping."
|
|
386
|
+
);
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const clinicId = clinicInfo.id;
|
|
390
|
+
console.warn(
|
|
391
|
+
`[ClinicAggregationService] updateClinicInfoInPatients called for clinic ${clinicId}. Current PatientProfile structure doesn't store full ClinicInfo per patient clinic entry. Consider specific logic if Clinic active status changes or if structure evolves. No direct patient updates performed by this method for info changes. Clinic isActive: ${clinicIsActive}`,
|
|
392
|
+
{ patientIds, clinicInfo }
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Updates the eventLocation for upcoming calendar events associated with a specific clinic.
|
|
397
|
+
* Uses a collection group query to find relevant events across all potential parent collections.
|
|
398
|
+
*
|
|
399
|
+
* @param clinicId The ID of the clinic whose location might have changed.
|
|
400
|
+
* @param newLocation The new ClinicLocation object.
|
|
401
|
+
*/
|
|
402
|
+
async updateClinicLocationInCalendarEvents(clinicId, newLocation) {
|
|
403
|
+
if (!clinicId || !newLocation) {
|
|
404
|
+
console.log(
|
|
405
|
+
"[ClinicAggregationService] Missing clinicId or newLocation for calendar update. Skipping."
|
|
406
|
+
);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
console.log(
|
|
410
|
+
`[ClinicAggregationService] Querying upcoming calendar events via collection group '${CALENDAR_SUBCOLLECTION_ID}' for clinic ${clinicId} to update location.`
|
|
411
|
+
);
|
|
412
|
+
const now = admin2.firestore.Timestamp.now();
|
|
413
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID).where("clinicBranchId", "==", clinicId).where("eventTime.start", ">", now);
|
|
414
|
+
try {
|
|
415
|
+
const snapshot = await calendarEventsQuery.get();
|
|
416
|
+
if (snapshot.empty) {
|
|
417
|
+
console.log(
|
|
418
|
+
`[ClinicAggregationService] No upcoming calendar events found via collection group for clinic ${clinicId}. No location updates needed.`
|
|
419
|
+
);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const batch = this.db.batch();
|
|
423
|
+
snapshot.docs.forEach((doc) => {
|
|
424
|
+
console.log(
|
|
425
|
+
`[ClinicAggregationService] Updating location for calendar event ${doc.ref.path}`
|
|
426
|
+
);
|
|
427
|
+
batch.update(doc.ref, {
|
|
428
|
+
eventLocation: newLocation,
|
|
429
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
await batch.commit();
|
|
433
|
+
console.log(
|
|
434
|
+
`[ClinicAggregationService] Successfully updated location in ${snapshot.size} upcoming calendar events via collection group for clinic ${clinicId}.`
|
|
435
|
+
);
|
|
436
|
+
} catch (error) {
|
|
437
|
+
console.error(
|
|
438
|
+
`[ClinicAggregationService] Error updating location in calendar events via collection group for clinic ${clinicId}:`,
|
|
439
|
+
error
|
|
440
|
+
);
|
|
441
|
+
throw error;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Updates clinic info in all upcoming calendar events associated with a specific clinic.
|
|
446
|
+
* @param clinicId The ID of the clinic whose info has been updated.
|
|
447
|
+
* @param clinicInfo The updated ClinicInfo object.
|
|
448
|
+
*/
|
|
449
|
+
async updateClinicInfoInCalendarEvents(clinicId, clinicInfo) {
|
|
450
|
+
if (!clinicId || !clinicInfo) {
|
|
451
|
+
console.log(
|
|
452
|
+
"[ClinicAggregationService] Missing clinicId or clinicInfo for calendar update. Skipping."
|
|
453
|
+
);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
console.log(
|
|
457
|
+
`[ClinicAggregationService] Querying upcoming calendar events for clinic ${clinicId} to update clinic info.`
|
|
458
|
+
);
|
|
459
|
+
const now = admin2.firestore.Timestamp.now();
|
|
460
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID).where("clinicBranchId", "==", clinicId).where("eventTime.start", ">", now);
|
|
461
|
+
try {
|
|
462
|
+
const snapshot = await calendarEventsQuery.get();
|
|
463
|
+
if (snapshot.empty) {
|
|
464
|
+
console.log(
|
|
465
|
+
`[ClinicAggregationService] No upcoming calendar events found for clinic ${clinicId}. No clinic info updates needed.`
|
|
466
|
+
);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const batch = this.db.batch();
|
|
470
|
+
snapshot.docs.forEach((doc) => {
|
|
471
|
+
console.log(
|
|
472
|
+
`[ClinicAggregationService] Updating clinic info for calendar event ${doc.ref.path}`
|
|
473
|
+
);
|
|
474
|
+
batch.update(doc.ref, {
|
|
475
|
+
clinicInfo,
|
|
476
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
await batch.commit();
|
|
480
|
+
console.log(
|
|
481
|
+
`[ClinicAggregationService] Successfully updated clinic info in ${snapshot.size} upcoming calendar events for clinic ${clinicId}.`
|
|
482
|
+
);
|
|
483
|
+
} catch (error) {
|
|
484
|
+
console.error(
|
|
485
|
+
`[ClinicAggregationService] Error updating clinic info in calendar events for clinic ${clinicId}:`,
|
|
486
|
+
error
|
|
487
|
+
);
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Removes clinic from practitioners when a clinic is deleted.
|
|
493
|
+
* @param practitionerIds IDs of practitioners associated with the clinic.
|
|
494
|
+
* @param clinicId The ID of the deleted clinic.
|
|
495
|
+
*/
|
|
496
|
+
async removeClinicFromPractitioners(practitionerIds, clinicId) {
|
|
497
|
+
if (!practitionerIds || practitionerIds.length === 0) {
|
|
498
|
+
console.log(
|
|
499
|
+
"[ClinicAggregationService] No practitioner IDs provided for clinic removal. Skipping."
|
|
500
|
+
);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const batch = this.db.batch();
|
|
504
|
+
console.log(
|
|
505
|
+
`[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
|
|
506
|
+
);
|
|
507
|
+
for (const practitionerId of practitionerIds) {
|
|
508
|
+
const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
|
|
509
|
+
batch.update(practitionerRef, {
|
|
510
|
+
clinicIds: admin2.firestore.FieldValue.arrayRemove(clinicId),
|
|
511
|
+
// Remove all clinic info objects where id matches the clinic ID
|
|
512
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
|
|
513
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
try {
|
|
517
|
+
await batch.commit();
|
|
518
|
+
console.log(
|
|
519
|
+
`[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
|
|
520
|
+
);
|
|
521
|
+
} catch (error) {
|
|
522
|
+
console.error(
|
|
523
|
+
`[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from practitioners:`,
|
|
524
|
+
error
|
|
525
|
+
);
|
|
526
|
+
throw error;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Inactivates all procedures associated with a deleted clinic
|
|
531
|
+
* @param procedureIds IDs of procedures associated with the clinic
|
|
532
|
+
*/
|
|
533
|
+
async inactivateProceduresForClinic(procedureIds) {
|
|
534
|
+
if (!procedureIds || procedureIds.length === 0) {
|
|
535
|
+
console.log(
|
|
536
|
+
"[ClinicAggregationService] No procedure IDs provided for inactivation. Skipping."
|
|
537
|
+
);
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
const batch = this.db.batch();
|
|
541
|
+
console.log(
|
|
542
|
+
`[ClinicAggregationService] Starting inactivation of ${procedureIds.length} procedures.`
|
|
543
|
+
);
|
|
544
|
+
for (const procedureId of procedureIds) {
|
|
545
|
+
const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
|
|
546
|
+
batch.update(procedureRef, {
|
|
547
|
+
isActive: false,
|
|
548
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
try {
|
|
552
|
+
await batch.commit();
|
|
553
|
+
console.log(
|
|
554
|
+
`[ClinicAggregationService] Successfully inactivated ${procedureIds.length} procedures.`
|
|
555
|
+
);
|
|
556
|
+
} catch (error) {
|
|
557
|
+
console.error(
|
|
558
|
+
`[ClinicAggregationService] Error committing batch inactivation of procedures:`,
|
|
559
|
+
error
|
|
560
|
+
);
|
|
561
|
+
throw error;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Removes clinic from clinic group when a clinic is deleted
|
|
566
|
+
* @param clinicGroupId ID of the parent clinic group
|
|
567
|
+
* @param clinicId ID of the deleted clinic
|
|
568
|
+
*/
|
|
569
|
+
async removeClinicFromClinicGroup(clinicGroupId, clinicId) {
|
|
570
|
+
if (!clinicGroupId) {
|
|
571
|
+
console.log(
|
|
572
|
+
"[ClinicAggregationService] No Clinic Group ID provided for clinic removal. Skipping."
|
|
573
|
+
);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
|
|
577
|
+
console.log(
|
|
578
|
+
`[ClinicAggregationService] Removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
|
|
579
|
+
);
|
|
580
|
+
try {
|
|
581
|
+
await groupRef.update({
|
|
582
|
+
clinicIds: admin2.firestore.FieldValue.arrayRemove(clinicId),
|
|
583
|
+
// Remove all clinic info objects where id matches the clinic ID
|
|
584
|
+
clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
|
|
585
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
586
|
+
});
|
|
587
|
+
console.log(
|
|
588
|
+
`[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
|
|
589
|
+
);
|
|
590
|
+
} catch (error) {
|
|
591
|
+
console.error(
|
|
592
|
+
`[ClinicAggregationService] Error removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}:`,
|
|
593
|
+
error
|
|
594
|
+
);
|
|
595
|
+
throw error;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Removes clinic from patients when a clinic is deleted
|
|
600
|
+
* @param patientIds IDs of patients associated with the clinic
|
|
601
|
+
* @param clinicId ID of the deleted clinic
|
|
602
|
+
*/
|
|
603
|
+
async removeClinicFromPatients(patientIds, clinicId) {
|
|
604
|
+
if (!patientIds || patientIds.length === 0) {
|
|
605
|
+
console.log(
|
|
606
|
+
"[ClinicAggregationService] No patient IDs provided for clinic removal. Skipping."
|
|
607
|
+
);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
const batch = this.db.batch();
|
|
611
|
+
console.log(
|
|
612
|
+
`[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${patientIds.length} patients.`
|
|
613
|
+
);
|
|
614
|
+
for (const patientId of patientIds) {
|
|
615
|
+
const patientRef = this.db.collection(PATIENTS_COLLECTION).doc(patientId);
|
|
616
|
+
batch.update(patientRef, {
|
|
617
|
+
clinicIds: admin2.firestore.FieldValue.arrayRemove(clinicId),
|
|
618
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
try {
|
|
622
|
+
await batch.commit();
|
|
623
|
+
console.log(
|
|
624
|
+
`[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${patientIds.length} patients.`
|
|
625
|
+
);
|
|
626
|
+
} catch (error) {
|
|
627
|
+
console.error(
|
|
628
|
+
`[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from patients:`,
|
|
629
|
+
error
|
|
630
|
+
);
|
|
631
|
+
throw error;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Cancels all upcoming calendar events associated with a deleted clinic
|
|
636
|
+
* @param clinicId ID of the deleted clinic
|
|
637
|
+
*/
|
|
638
|
+
async cancelUpcomingCalendarEventsForClinic(clinicId) {
|
|
639
|
+
if (!clinicId) {
|
|
640
|
+
console.log(
|
|
641
|
+
"[ClinicAggregationService] No clinic ID provided for canceling calendar events. Skipping."
|
|
642
|
+
);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
console.log(
|
|
646
|
+
`[ClinicAggregationService] Querying upcoming calendar events for clinic ${clinicId} to cancel.`
|
|
647
|
+
);
|
|
648
|
+
const now = admin2.firestore.Timestamp.now();
|
|
649
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID).where("clinicBranchId", "==", clinicId).where("eventTime.start", ">", now);
|
|
650
|
+
try {
|
|
651
|
+
const snapshot = await calendarEventsQuery.get();
|
|
652
|
+
if (snapshot.empty) {
|
|
653
|
+
console.log(
|
|
654
|
+
`[ClinicAggregationService] No upcoming calendar events found for clinic ${clinicId}. No events to cancel.`
|
|
655
|
+
);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const batch = this.db.batch();
|
|
659
|
+
snapshot.docs.forEach((doc) => {
|
|
660
|
+
console.log(
|
|
661
|
+
`[ClinicAggregationService] Canceling calendar event ${doc.ref.path}`
|
|
662
|
+
);
|
|
663
|
+
batch.update(doc.ref, {
|
|
664
|
+
status: "CANCELED",
|
|
665
|
+
cancelReason: "Clinic deleted",
|
|
666
|
+
updatedAt: admin2.firestore.FieldValue.serverTimestamp()
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
await batch.commit();
|
|
670
|
+
console.log(
|
|
671
|
+
`[ClinicAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for clinic ${clinicId}.`
|
|
672
|
+
);
|
|
673
|
+
} catch (error) {
|
|
674
|
+
console.error(
|
|
675
|
+
`[ClinicAggregationService] Error canceling calendar events for clinic ${clinicId}:`,
|
|
676
|
+
error
|
|
677
|
+
);
|
|
678
|
+
throw error;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
// src/admin/aggregation/practitioner/practitioner.aggregation.service.ts
|
|
684
|
+
import * as admin3 from "firebase-admin";
|
|
685
|
+
var CALENDAR_SUBCOLLECTION_ID2 = "calendar";
|
|
686
|
+
var PractitionerAggregationService = class {
|
|
687
|
+
constructor(firestore6) {
|
|
688
|
+
this.db = firestore6 || admin3.firestore();
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Adds practitioner information to a clinic when a new practitioner is created
|
|
692
|
+
* @param clinicId - ID of the clinic to update
|
|
693
|
+
* @param doctorInfo - Doctor information to add to the clinic
|
|
694
|
+
* @returns {Promise<void>}
|
|
695
|
+
*/
|
|
696
|
+
async addPractitionerToClinic(clinicId, doctorInfo) {
|
|
697
|
+
if (!clinicId || !doctorInfo) {
|
|
698
|
+
console.log(
|
|
699
|
+
"[PractitionerAggregationService] Missing clinicId or doctorInfo for adding practitioner to clinic. Skipping."
|
|
700
|
+
);
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const practitionerId = doctorInfo.id;
|
|
704
|
+
console.log(
|
|
705
|
+
`[PractitionerAggregationService] Adding practitioner ${practitionerId} to clinic ${clinicId}.`
|
|
706
|
+
);
|
|
707
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
708
|
+
try {
|
|
709
|
+
await clinicRef.update({
|
|
710
|
+
doctors: admin3.firestore.FieldValue.arrayUnion(practitionerId),
|
|
711
|
+
doctorsInfo: admin3.firestore.FieldValue.arrayUnion(doctorInfo),
|
|
712
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
713
|
+
});
|
|
714
|
+
console.log(
|
|
715
|
+
`[PractitionerAggregationService] Successfully added practitioner ${practitionerId} to clinic ${clinicId}.`
|
|
716
|
+
);
|
|
717
|
+
} catch (error) {
|
|
718
|
+
console.error(
|
|
719
|
+
`[PractitionerAggregationService] Error adding practitioner ${practitionerId} to clinic ${clinicId}:`,
|
|
720
|
+
error
|
|
721
|
+
);
|
|
722
|
+
throw error;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Updates practitioner information in associated clinics
|
|
727
|
+
* @param clinicIds - IDs of clinics associated with the practitioner
|
|
728
|
+
* @param doctorInfo - Updated doctor information
|
|
729
|
+
* @returns {Promise<void>}
|
|
730
|
+
*/
|
|
731
|
+
async updatePractitionerInfoInClinics(clinicIds, doctorInfo) {
|
|
732
|
+
if (!clinicIds || clinicIds.length === 0 || !doctorInfo) {
|
|
733
|
+
console.log(
|
|
734
|
+
"[PractitionerAggregationService] Missing clinicIds or doctorInfo for updating practitioner in clinics. Skipping."
|
|
735
|
+
);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
const batch = this.db.batch();
|
|
739
|
+
const practitionerId = doctorInfo.id;
|
|
740
|
+
console.log(
|
|
741
|
+
`[PractitionerAggregationService] Starting batch update of practitioner ${practitionerId} in ${clinicIds.length} clinics.`
|
|
742
|
+
);
|
|
743
|
+
for (const clinicId of clinicIds) {
|
|
744
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
745
|
+
batch.update(clinicRef, {
|
|
746
|
+
doctorsInfo: admin3.firestore.FieldValue.arrayRemove({
|
|
747
|
+
id: practitionerId
|
|
748
|
+
}),
|
|
749
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
750
|
+
});
|
|
751
|
+
batch.update(clinicRef, {
|
|
752
|
+
doctorsInfo: admin3.firestore.FieldValue.arrayUnion(doctorInfo),
|
|
753
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
try {
|
|
757
|
+
await batch.commit();
|
|
758
|
+
console.log(
|
|
759
|
+
`[PractitionerAggregationService] Successfully updated practitioner ${practitionerId} info in ${clinicIds.length} clinics.`
|
|
760
|
+
);
|
|
761
|
+
} catch (error) {
|
|
762
|
+
console.error(
|
|
763
|
+
`[PractitionerAggregationService] Error updating practitioner ${practitionerId} info in clinics:`,
|
|
764
|
+
error
|
|
765
|
+
);
|
|
766
|
+
throw error;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* Updates practitioner information in associated procedures
|
|
771
|
+
* @param procedureIds - IDs of procedures associated with the practitioner
|
|
772
|
+
* @param doctorInfo - Updated doctor information
|
|
773
|
+
* @returns {Promise<void>}
|
|
774
|
+
*/
|
|
775
|
+
async updatePractitionerInfoInProcedures(procedureIds, doctorInfo) {
|
|
776
|
+
if (!procedureIds || procedureIds.length === 0 || !doctorInfo) {
|
|
777
|
+
console.log(
|
|
778
|
+
"[PractitionerAggregationService] Missing procedureIds or doctorInfo for updating practitioner in procedures. Skipping."
|
|
779
|
+
);
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
const batch = this.db.batch();
|
|
783
|
+
const practitionerId = doctorInfo.id;
|
|
784
|
+
console.log(
|
|
785
|
+
`[PractitionerAggregationService] Starting batch update of practitioner ${practitionerId} in ${procedureIds.length} procedures.`
|
|
786
|
+
);
|
|
787
|
+
for (const procedureId of procedureIds) {
|
|
788
|
+
const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
|
|
789
|
+
batch.update(procedureRef, {
|
|
790
|
+
doctorInfo,
|
|
791
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
try {
|
|
795
|
+
await batch.commit();
|
|
796
|
+
console.log(
|
|
797
|
+
`[PractitionerAggregationService] Successfully updated practitioner ${practitionerId} info in ${procedureIds.length} procedures.`
|
|
798
|
+
);
|
|
799
|
+
} catch (error) {
|
|
800
|
+
console.error(
|
|
801
|
+
`[PractitionerAggregationService] Error updating practitioner ${practitionerId} info in procedures:`,
|
|
802
|
+
error
|
|
803
|
+
);
|
|
804
|
+
throw error;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Updates practitioner information in calendar events
|
|
809
|
+
* @param practitionerId - ID of the practitioner
|
|
810
|
+
* @param practitionerInfo - Updated practitioner information
|
|
811
|
+
* @returns {Promise<void>}
|
|
812
|
+
*/
|
|
813
|
+
async updatePractitionerInfoInCalendarEvents(practitionerId, practitionerInfo) {
|
|
814
|
+
if (!practitionerId || !practitionerInfo) {
|
|
815
|
+
console.log(
|
|
816
|
+
"[PractitionerAggregationService] Missing practitionerId or practitionerInfo for calendar update. Skipping."
|
|
817
|
+
);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
console.log(
|
|
821
|
+
`[PractitionerAggregationService] Querying upcoming calendar events for practitioner ${practitionerId} to update practitioner info.`
|
|
822
|
+
);
|
|
823
|
+
const now = admin3.firestore.Timestamp.now();
|
|
824
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID2).where("practitionerId", "==", practitionerId).where("eventTime.start", ">", now);
|
|
825
|
+
try {
|
|
826
|
+
const snapshot = await calendarEventsQuery.get();
|
|
827
|
+
if (snapshot.empty) {
|
|
828
|
+
console.log(
|
|
829
|
+
`[PractitionerAggregationService] No upcoming calendar events found for practitioner ${practitionerId}. No doctor info updates needed.`
|
|
830
|
+
);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
const batch = this.db.batch();
|
|
834
|
+
snapshot.docs.forEach((doc) => {
|
|
835
|
+
console.log(
|
|
836
|
+
`[PractitionerAggregationService] Updating practitioner info for calendar event ${doc.ref.path}`
|
|
837
|
+
);
|
|
838
|
+
batch.update(doc.ref, {
|
|
839
|
+
practitionerInfo,
|
|
840
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
841
|
+
});
|
|
842
|
+
});
|
|
843
|
+
await batch.commit();
|
|
844
|
+
console.log(
|
|
845
|
+
`[PractitionerAggregationService] Successfully updated practitioner info in ${snapshot.size} upcoming calendar events for practitioner ${practitionerId}.`
|
|
846
|
+
);
|
|
847
|
+
} catch (error) {
|
|
848
|
+
console.error(
|
|
849
|
+
`[PractitionerAggregationService] Error updating practitioner info in calendar events for practitioner ${practitionerId}:`,
|
|
850
|
+
error
|
|
851
|
+
);
|
|
852
|
+
throw error;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Removes practitioner from clinics when a practitioner is deleted
|
|
857
|
+
* @param clinicIds - IDs of clinics associated with the practitioner
|
|
858
|
+
* @param practitionerId - ID of the deleted practitioner
|
|
859
|
+
* @returns {Promise<void>}
|
|
860
|
+
*/
|
|
861
|
+
async removePractitionerFromClinics(clinicIds, practitionerId) {
|
|
862
|
+
if (!clinicIds || clinicIds.length === 0 || !practitionerId) {
|
|
863
|
+
console.log(
|
|
864
|
+
"[PractitionerAggregationService] Missing clinicIds or practitionerId for removing practitioner from clinics. Skipping."
|
|
865
|
+
);
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
const batch = this.db.batch();
|
|
869
|
+
console.log(
|
|
870
|
+
`[PractitionerAggregationService] Starting batch removal of practitioner ${practitionerId} from ${clinicIds.length} clinics.`
|
|
871
|
+
);
|
|
872
|
+
for (const clinicId of clinicIds) {
|
|
873
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
874
|
+
batch.update(clinicRef, {
|
|
875
|
+
doctors: admin3.firestore.FieldValue.arrayRemove(practitionerId),
|
|
876
|
+
// Remove all doctor info objects where id matches the practitioner ID
|
|
877
|
+
doctorsInfo: admin3.firestore.FieldValue.arrayRemove({
|
|
878
|
+
id: practitionerId
|
|
879
|
+
}),
|
|
880
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
try {
|
|
884
|
+
await batch.commit();
|
|
885
|
+
console.log(
|
|
886
|
+
`[PractitionerAggregationService] Successfully removed practitioner ${practitionerId} from ${clinicIds.length} clinics.`
|
|
887
|
+
);
|
|
888
|
+
} catch (error) {
|
|
889
|
+
console.error(
|
|
890
|
+
`[PractitionerAggregationService] Error removing practitioner ${practitionerId} from clinics:`,
|
|
891
|
+
error
|
|
892
|
+
);
|
|
893
|
+
throw error;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Cancels all upcoming calendar events for a deleted practitioner
|
|
898
|
+
* @param practitionerId - ID of the deleted practitioner
|
|
899
|
+
* @returns {Promise<void>}
|
|
900
|
+
*/
|
|
901
|
+
async cancelUpcomingCalendarEventsForPractitioner(practitionerId) {
|
|
902
|
+
if (!practitionerId) {
|
|
903
|
+
console.log(
|
|
904
|
+
"[PractitionerAggregationService] Missing practitionerId for canceling calendar events. Skipping."
|
|
905
|
+
);
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
console.log(
|
|
909
|
+
`[PractitionerAggregationService] Querying upcoming calendar events for practitioner ${practitionerId} to cancel.`
|
|
910
|
+
);
|
|
911
|
+
const now = admin3.firestore.Timestamp.now();
|
|
912
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID2).where("practitionerId", "==", practitionerId).where("eventTime.start", ">", now);
|
|
913
|
+
try {
|
|
914
|
+
const snapshot = await calendarEventsQuery.get();
|
|
915
|
+
if (snapshot.empty) {
|
|
916
|
+
console.log(
|
|
917
|
+
`[PractitionerAggregationService] No upcoming calendar events found for practitioner ${practitionerId}. No events to cancel.`
|
|
918
|
+
);
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
const batch = this.db.batch();
|
|
922
|
+
snapshot.docs.forEach((doc) => {
|
|
923
|
+
console.log(
|
|
924
|
+
`[PractitionerAggregationService] Canceling calendar event ${doc.ref.path}`
|
|
925
|
+
);
|
|
926
|
+
batch.update(doc.ref, {
|
|
927
|
+
status: "CANCELED",
|
|
928
|
+
cancelReason: "Practitioner deleted",
|
|
929
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
930
|
+
});
|
|
931
|
+
});
|
|
932
|
+
await batch.commit();
|
|
933
|
+
console.log(
|
|
934
|
+
`[PractitionerAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for practitioner ${practitionerId}.`
|
|
935
|
+
);
|
|
936
|
+
} catch (error) {
|
|
937
|
+
console.error(
|
|
938
|
+
`[PractitionerAggregationService] Error canceling calendar events for practitioner ${practitionerId}:`,
|
|
939
|
+
error
|
|
940
|
+
);
|
|
941
|
+
throw error;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Removes practitioner from patients when a practitioner is deleted
|
|
946
|
+
* @param patientIds - IDs of patients associated with the practitioner
|
|
947
|
+
* @param practitionerId - ID of the deleted practitioner
|
|
948
|
+
* @returns {Promise<void>}
|
|
949
|
+
*/
|
|
950
|
+
async removePractitionerFromPatients(patientIds, practitionerId) {
|
|
951
|
+
if (!patientIds || patientIds.length === 0 || !practitionerId) {
|
|
952
|
+
console.log(
|
|
953
|
+
"[PractitionerAggregationService] Missing patientIds or practitionerId for removing practitioner from patients. Skipping."
|
|
954
|
+
);
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
const batch = this.db.batch();
|
|
958
|
+
console.log(
|
|
959
|
+
`[PractitionerAggregationService] Starting batch removal of practitioner ${practitionerId} from ${patientIds.length} patients.`
|
|
960
|
+
);
|
|
961
|
+
for (const patientId of patientIds) {
|
|
962
|
+
const patientRef = this.db.collection(PATIENTS_COLLECTION).doc(patientId);
|
|
963
|
+
batch.update(patientRef, {
|
|
964
|
+
doctorIds: admin3.firestore.FieldValue.arrayRemove(practitionerId),
|
|
965
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
try {
|
|
969
|
+
await batch.commit();
|
|
970
|
+
console.log(
|
|
971
|
+
`[PractitionerAggregationService] Successfully removed practitioner ${practitionerId} from ${patientIds.length} patients.`
|
|
972
|
+
);
|
|
973
|
+
} catch (error) {
|
|
974
|
+
console.error(
|
|
975
|
+
`[PractitionerAggregationService] Error removing practitioner ${practitionerId} from patients:`,
|
|
976
|
+
error
|
|
977
|
+
);
|
|
978
|
+
throw error;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Inactivates all procedures associated with a deleted practitioner
|
|
983
|
+
* @param procedureIds - IDs of procedures provided by the practitioner
|
|
984
|
+
* @returns {Promise<void>}
|
|
985
|
+
*/
|
|
986
|
+
async inactivateProceduresForPractitioner(procedureIds) {
|
|
987
|
+
if (!procedureIds || procedureIds.length === 0) {
|
|
988
|
+
console.log(
|
|
989
|
+
"[PractitionerAggregationService] No procedure IDs provided for inactivation. Skipping."
|
|
990
|
+
);
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
const batch = this.db.batch();
|
|
994
|
+
console.log(
|
|
995
|
+
`[PractitionerAggregationService] Starting inactivation of ${procedureIds.length} procedures.`
|
|
996
|
+
);
|
|
997
|
+
for (const procedureId of procedureIds) {
|
|
998
|
+
const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
|
|
999
|
+
batch.update(procedureRef, {
|
|
1000
|
+
isActive: false,
|
|
1001
|
+
updatedAt: admin3.firestore.FieldValue.serverTimestamp()
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
try {
|
|
1005
|
+
await batch.commit();
|
|
1006
|
+
console.log(
|
|
1007
|
+
`[PractitionerAggregationService] Successfully inactivated ${procedureIds.length} procedures.`
|
|
1008
|
+
);
|
|
1009
|
+
} catch (error) {
|
|
1010
|
+
console.error(
|
|
1011
|
+
`[PractitionerAggregationService] Error committing batch inactivation of procedures:`,
|
|
1012
|
+
error
|
|
1013
|
+
);
|
|
1014
|
+
throw error;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
|
|
1019
|
+
// src/admin/aggregation/procedure/procedure.aggregation.service.ts
|
|
1020
|
+
import * as admin4 from "firebase-admin";
|
|
1021
|
+
var CALENDAR_SUBCOLLECTION_ID3 = "calendar";
|
|
1022
|
+
var ProcedureAggregationService = class {
|
|
1023
|
+
constructor(firestore6) {
|
|
1024
|
+
this.db = firestore6 || admin4.firestore();
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Adds procedure information to a practitioner when a new procedure is created
|
|
1028
|
+
* @param practitionerId - ID of the practitioner who performs the procedure
|
|
1029
|
+
* @param procedureSummary - Summary information about the procedure
|
|
1030
|
+
* @returns {Promise<void>}
|
|
1031
|
+
*/
|
|
1032
|
+
async addProcedureToPractitioner(practitionerId, procedureSummary) {
|
|
1033
|
+
if (!practitionerId || !procedureSummary) {
|
|
1034
|
+
console.log(
|
|
1035
|
+
"[ProcedureAggregationService] Missing practitionerId or procedureSummary for adding procedure to practitioner. Skipping."
|
|
1036
|
+
);
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
const procedureId = procedureSummary.id;
|
|
1040
|
+
console.log(
|
|
1041
|
+
`[ProcedureAggregationService] Adding procedure ${procedureId} to practitioner ${practitionerId}.`
|
|
1042
|
+
);
|
|
1043
|
+
const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
|
|
1044
|
+
try {
|
|
1045
|
+
await practitionerRef.update({
|
|
1046
|
+
procedureIds: admin4.firestore.FieldValue.arrayUnion(procedureId),
|
|
1047
|
+
proceduresInfo: admin4.firestore.FieldValue.arrayUnion(procedureSummary),
|
|
1048
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1049
|
+
});
|
|
1050
|
+
console.log(
|
|
1051
|
+
`[ProcedureAggregationService] Successfully added procedure ${procedureId} to practitioner ${practitionerId}.`
|
|
1052
|
+
);
|
|
1053
|
+
} catch (error) {
|
|
1054
|
+
console.error(
|
|
1055
|
+
`[ProcedureAggregationService] Error adding procedure ${procedureId} to practitioner ${practitionerId}:`,
|
|
1056
|
+
error
|
|
1057
|
+
);
|
|
1058
|
+
throw error;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Adds procedure information to a clinic when a new procedure is created
|
|
1063
|
+
* @param clinicId - ID of the clinic where the procedure is performed
|
|
1064
|
+
* @param procedureSummary - Summary information about the procedure
|
|
1065
|
+
* @returns {Promise<void>}
|
|
1066
|
+
*/
|
|
1067
|
+
async addProcedureToClinic(clinicId, procedureSummary) {
|
|
1068
|
+
if (!clinicId || !procedureSummary) {
|
|
1069
|
+
console.log(
|
|
1070
|
+
"[ProcedureAggregationService] Missing clinicId or procedureSummary for adding procedure to clinic. Skipping."
|
|
1071
|
+
);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
const procedureId = procedureSummary.id;
|
|
1075
|
+
console.log(
|
|
1076
|
+
`[ProcedureAggregationService] Adding procedure ${procedureId} to clinic ${clinicId}.`
|
|
1077
|
+
);
|
|
1078
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
1079
|
+
try {
|
|
1080
|
+
await clinicRef.update({
|
|
1081
|
+
procedures: admin4.firestore.FieldValue.arrayUnion(procedureId),
|
|
1082
|
+
proceduresInfo: admin4.firestore.FieldValue.arrayUnion(procedureSummary),
|
|
1083
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1084
|
+
});
|
|
1085
|
+
console.log(
|
|
1086
|
+
`[ProcedureAggregationService] Successfully added procedure ${procedureId} to clinic ${clinicId}.`
|
|
1087
|
+
);
|
|
1088
|
+
} catch (error) {
|
|
1089
|
+
console.error(
|
|
1090
|
+
`[ProcedureAggregationService] Error adding procedure ${procedureId} to clinic ${clinicId}:`,
|
|
1091
|
+
error
|
|
1092
|
+
);
|
|
1093
|
+
throw error;
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Updates procedure information in a practitioner document
|
|
1098
|
+
* @param practitionerId - ID of the practitioner who performs the procedure
|
|
1099
|
+
* @param procedureSummary - Updated summary information about the procedure
|
|
1100
|
+
* @returns {Promise<void>}
|
|
1101
|
+
*/
|
|
1102
|
+
async updateProcedureInfoInPractitioner(practitionerId, procedureSummary) {
|
|
1103
|
+
if (!practitionerId || !procedureSummary) {
|
|
1104
|
+
console.log(
|
|
1105
|
+
"[ProcedureAggregationService] Missing practitionerId or procedureSummary for updating procedure in practitioner. Skipping."
|
|
1106
|
+
);
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
const procedureId = procedureSummary.id;
|
|
1110
|
+
console.log(
|
|
1111
|
+
`[ProcedureAggregationService] Updating procedure ${procedureId} info in practitioner ${practitionerId}.`
|
|
1112
|
+
);
|
|
1113
|
+
const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
|
|
1114
|
+
try {
|
|
1115
|
+
await this.db.runTransaction(async (transaction) => {
|
|
1116
|
+
const practitionerDoc = await transaction.get(practitionerRef);
|
|
1117
|
+
if (!practitionerDoc.exists) {
|
|
1118
|
+
throw new Error(
|
|
1119
|
+
`Practitioner ${practitionerId} does not exist for procedure update`
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
const practitionerData = practitionerDoc.data();
|
|
1123
|
+
if (!practitionerData) {
|
|
1124
|
+
throw new Error(
|
|
1125
|
+
`Practitioner ${practitionerId} data is empty for procedure update`
|
|
1126
|
+
);
|
|
1127
|
+
}
|
|
1128
|
+
const proceduresInfo = practitionerData.proceduresInfo || [];
|
|
1129
|
+
const updatedProceduresInfo = proceduresInfo.filter(
|
|
1130
|
+
(p) => p.id !== procedureId
|
|
1131
|
+
);
|
|
1132
|
+
updatedProceduresInfo.push(procedureSummary);
|
|
1133
|
+
transaction.update(practitionerRef, {
|
|
1134
|
+
proceduresInfo: updatedProceduresInfo,
|
|
1135
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1136
|
+
});
|
|
1137
|
+
});
|
|
1138
|
+
console.log(
|
|
1139
|
+
`[ProcedureAggregationService] Successfully updated procedure ${procedureId} info in practitioner ${practitionerId}.`
|
|
1140
|
+
);
|
|
1141
|
+
} catch (error) {
|
|
1142
|
+
console.error(
|
|
1143
|
+
`[ProcedureAggregationService] Error updating procedure ${procedureId} info in practitioner ${practitionerId}:`,
|
|
1144
|
+
error
|
|
1145
|
+
);
|
|
1146
|
+
throw error;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* Updates procedure information in a clinic document
|
|
1151
|
+
* @param clinicId - ID of the clinic where the procedure is performed
|
|
1152
|
+
* @param procedureSummary - Updated summary information about the procedure
|
|
1153
|
+
* @returns {Promise<void>}
|
|
1154
|
+
*/
|
|
1155
|
+
async updateProcedureInfoInClinic(clinicId, procedureSummary) {
|
|
1156
|
+
if (!clinicId || !procedureSummary) {
|
|
1157
|
+
console.log(
|
|
1158
|
+
"[ProcedureAggregationService] Missing clinicId or procedureSummary for updating procedure in clinic. Skipping."
|
|
1159
|
+
);
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1162
|
+
const procedureId = procedureSummary.id;
|
|
1163
|
+
console.log(
|
|
1164
|
+
`[ProcedureAggregationService] Updating procedure ${procedureId} info in clinic ${clinicId}.`
|
|
1165
|
+
);
|
|
1166
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
1167
|
+
try {
|
|
1168
|
+
await this.db.runTransaction(async (transaction) => {
|
|
1169
|
+
const clinicDoc = await transaction.get(clinicRef);
|
|
1170
|
+
if (!clinicDoc.exists) {
|
|
1171
|
+
throw new Error(
|
|
1172
|
+
`Clinic ${clinicId} does not exist for procedure update`
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
const clinicData = clinicDoc.data();
|
|
1176
|
+
if (!clinicData) {
|
|
1177
|
+
throw new Error(
|
|
1178
|
+
`Clinic ${clinicId} data is empty for procedure update`
|
|
1179
|
+
);
|
|
1180
|
+
}
|
|
1181
|
+
const proceduresInfo = clinicData.proceduresInfo || [];
|
|
1182
|
+
const updatedProceduresInfo = proceduresInfo.filter(
|
|
1183
|
+
(p) => p.id !== procedureId
|
|
1184
|
+
);
|
|
1185
|
+
updatedProceduresInfo.push(procedureSummary);
|
|
1186
|
+
transaction.update(clinicRef, {
|
|
1187
|
+
proceduresInfo: updatedProceduresInfo,
|
|
1188
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1189
|
+
});
|
|
1190
|
+
});
|
|
1191
|
+
console.log(
|
|
1192
|
+
`[ProcedureAggregationService] Successfully updated procedure ${procedureId} info in clinic ${clinicId}.`
|
|
1193
|
+
);
|
|
1194
|
+
} catch (error) {
|
|
1195
|
+
console.error(
|
|
1196
|
+
`[ProcedureAggregationService] Error updating procedure ${procedureId} info in clinic ${clinicId}:`,
|
|
1197
|
+
error
|
|
1198
|
+
);
|
|
1199
|
+
throw error;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
* Updates procedure information in calendar events
|
|
1204
|
+
* @param procedureId - ID of the procedure
|
|
1205
|
+
* @param procedureInfo - Updated procedure information
|
|
1206
|
+
* @returns {Promise<void>}
|
|
1207
|
+
*/
|
|
1208
|
+
async updateProcedureInfoInCalendarEvents(procedureId, procedureInfo) {
|
|
1209
|
+
if (!procedureId || !procedureInfo) {
|
|
1210
|
+
console.log(
|
|
1211
|
+
"[ProcedureAggregationService] Missing procedureId or procedureInfo for calendar update. Skipping."
|
|
1212
|
+
);
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
console.log(
|
|
1216
|
+
`[ProcedureAggregationService] Querying upcoming calendar events for procedure ${procedureId} to update procedure info.`
|
|
1217
|
+
);
|
|
1218
|
+
const now = admin4.firestore.Timestamp.now();
|
|
1219
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID3).where("procedureId", "==", procedureId).where("eventTime.start", ">", now);
|
|
1220
|
+
try {
|
|
1221
|
+
const snapshot = await calendarEventsQuery.get();
|
|
1222
|
+
if (snapshot.empty) {
|
|
1223
|
+
console.log(
|
|
1224
|
+
`[ProcedureAggregationService] No upcoming calendar events found for procedure ${procedureId}. No procedure info updates needed.`
|
|
1225
|
+
);
|
|
1226
|
+
return;
|
|
1227
|
+
}
|
|
1228
|
+
const batch = this.db.batch();
|
|
1229
|
+
snapshot.docs.forEach((doc) => {
|
|
1230
|
+
console.log(
|
|
1231
|
+
`[ProcedureAggregationService] Updating procedure info for calendar event ${doc.ref.path}`
|
|
1232
|
+
);
|
|
1233
|
+
batch.update(doc.ref, {
|
|
1234
|
+
procedureInfo,
|
|
1235
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1236
|
+
});
|
|
1237
|
+
});
|
|
1238
|
+
await batch.commit();
|
|
1239
|
+
console.log(
|
|
1240
|
+
`[ProcedureAggregationService] Successfully updated procedure info in ${snapshot.size} upcoming calendar events for procedure ${procedureId}.`
|
|
1241
|
+
);
|
|
1242
|
+
} catch (error) {
|
|
1243
|
+
console.error(
|
|
1244
|
+
`[ProcedureAggregationService] Error updating procedure info in calendar events for procedure ${procedureId}:`,
|
|
1245
|
+
error
|
|
1246
|
+
);
|
|
1247
|
+
throw error;
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Cancels all upcoming calendar events for a procedure
|
|
1252
|
+
* @param procedureId - ID of the procedure
|
|
1253
|
+
* @returns {Promise<void>}
|
|
1254
|
+
*/
|
|
1255
|
+
async cancelUpcomingCalendarEventsForProcedure(procedureId) {
|
|
1256
|
+
if (!procedureId) {
|
|
1257
|
+
console.log(
|
|
1258
|
+
"[ProcedureAggregationService] Missing procedureId for canceling calendar events. Skipping."
|
|
1259
|
+
);
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
console.log(
|
|
1263
|
+
`[ProcedureAggregationService] Querying upcoming calendar events for procedure ${procedureId} to cancel.`
|
|
1264
|
+
);
|
|
1265
|
+
const now = admin4.firestore.Timestamp.now();
|
|
1266
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID3).where("procedureId", "==", procedureId).where("eventTime.start", ">", now);
|
|
1267
|
+
try {
|
|
1268
|
+
const snapshot = await calendarEventsQuery.get();
|
|
1269
|
+
if (snapshot.empty) {
|
|
1270
|
+
console.log(
|
|
1271
|
+
`[ProcedureAggregationService] No upcoming calendar events found for procedure ${procedureId}. No events to cancel.`
|
|
1272
|
+
);
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
const batch = this.db.batch();
|
|
1276
|
+
snapshot.docs.forEach((doc) => {
|
|
1277
|
+
console.log(
|
|
1278
|
+
`[ProcedureAggregationService] Canceling calendar event ${doc.ref.path}`
|
|
1279
|
+
);
|
|
1280
|
+
batch.update(doc.ref, {
|
|
1281
|
+
status: "CANCELED",
|
|
1282
|
+
cancelReason: "Procedure deleted or inactivated",
|
|
1283
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1284
|
+
});
|
|
1285
|
+
});
|
|
1286
|
+
await batch.commit();
|
|
1287
|
+
console.log(
|
|
1288
|
+
`[ProcedureAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for procedure ${procedureId}.`
|
|
1289
|
+
);
|
|
1290
|
+
} catch (error) {
|
|
1291
|
+
console.error(
|
|
1292
|
+
`[ProcedureAggregationService] Error canceling calendar events for procedure ${procedureId}:`,
|
|
1293
|
+
error
|
|
1294
|
+
);
|
|
1295
|
+
throw error;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Removes procedure from a practitioner when a procedure is deleted or inactivated
|
|
1300
|
+
* @param practitionerId - ID of the practitioner who performs the procedure
|
|
1301
|
+
* @param procedureId - ID of the procedure
|
|
1302
|
+
* @returns {Promise<void>}
|
|
1303
|
+
*/
|
|
1304
|
+
async removeProcedureFromPractitioner(practitionerId, procedureId) {
|
|
1305
|
+
if (!practitionerId || !procedureId) {
|
|
1306
|
+
console.log(
|
|
1307
|
+
"[ProcedureAggregationService] Missing practitionerId or procedureId for removing procedure from practitioner. Skipping."
|
|
1308
|
+
);
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
console.log(
|
|
1312
|
+
`[ProcedureAggregationService] Removing procedure ${procedureId} from practitioner ${practitionerId}.`
|
|
1313
|
+
);
|
|
1314
|
+
const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
|
|
1315
|
+
try {
|
|
1316
|
+
await this.db.runTransaction(async (transaction) => {
|
|
1317
|
+
const practitionerDoc = await transaction.get(practitionerRef);
|
|
1318
|
+
if (!practitionerDoc.exists) {
|
|
1319
|
+
throw new Error(
|
|
1320
|
+
`Practitioner ${practitionerId} does not exist for procedure removal`
|
|
1321
|
+
);
|
|
1322
|
+
}
|
|
1323
|
+
const practitionerData = practitionerDoc.data();
|
|
1324
|
+
if (!practitionerData) {
|
|
1325
|
+
throw new Error(
|
|
1326
|
+
`Practitioner ${practitionerId} data is empty for procedure removal`
|
|
1327
|
+
);
|
|
1328
|
+
}
|
|
1329
|
+
const proceduresInfo = practitionerData.proceduresInfo || [];
|
|
1330
|
+
const updatedProceduresInfo = proceduresInfo.filter(
|
|
1331
|
+
(p) => p.id !== procedureId
|
|
1332
|
+
);
|
|
1333
|
+
transaction.update(practitionerRef, {
|
|
1334
|
+
procedureIds: admin4.firestore.FieldValue.arrayRemove(procedureId),
|
|
1335
|
+
proceduresInfo: updatedProceduresInfo,
|
|
1336
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1337
|
+
});
|
|
1338
|
+
});
|
|
1339
|
+
console.log(
|
|
1340
|
+
`[ProcedureAggregationService] Successfully removed procedure ${procedureId} from practitioner ${practitionerId}.`
|
|
1341
|
+
);
|
|
1342
|
+
} catch (error) {
|
|
1343
|
+
console.error(
|
|
1344
|
+
`[ProcedureAggregationService] Error removing procedure ${procedureId} from practitioner ${practitionerId}:`,
|
|
1345
|
+
error
|
|
1346
|
+
);
|
|
1347
|
+
throw error;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Removes procedure from a clinic when a procedure is deleted or inactivated
|
|
1352
|
+
* @param clinicId - ID of the clinic where the procedure is performed
|
|
1353
|
+
* @param procedureId - ID of the procedure
|
|
1354
|
+
* @returns {Promise<void>}
|
|
1355
|
+
*/
|
|
1356
|
+
async removeProcedureFromClinic(clinicId, procedureId) {
|
|
1357
|
+
if (!clinicId || !procedureId) {
|
|
1358
|
+
console.log(
|
|
1359
|
+
"[ProcedureAggregationService] Missing clinicId or procedureId for removing procedure from clinic. Skipping."
|
|
1360
|
+
);
|
|
1361
|
+
return;
|
|
1362
|
+
}
|
|
1363
|
+
console.log(
|
|
1364
|
+
`[ProcedureAggregationService] Removing procedure ${procedureId} from clinic ${clinicId}.`
|
|
1365
|
+
);
|
|
1366
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
1367
|
+
try {
|
|
1368
|
+
await this.db.runTransaction(async (transaction) => {
|
|
1369
|
+
const clinicDoc = await transaction.get(clinicRef);
|
|
1370
|
+
if (!clinicDoc.exists) {
|
|
1371
|
+
throw new Error(
|
|
1372
|
+
`Clinic ${clinicId} does not exist for procedure removal`
|
|
1373
|
+
);
|
|
1374
|
+
}
|
|
1375
|
+
const clinicData = clinicDoc.data();
|
|
1376
|
+
if (!clinicData) {
|
|
1377
|
+
throw new Error(
|
|
1378
|
+
`Clinic ${clinicId} data is empty for procedure removal`
|
|
1379
|
+
);
|
|
1380
|
+
}
|
|
1381
|
+
const proceduresInfo = clinicData.proceduresInfo || [];
|
|
1382
|
+
const updatedProceduresInfo = proceduresInfo.filter(
|
|
1383
|
+
(p) => p.id !== procedureId
|
|
1384
|
+
);
|
|
1385
|
+
transaction.update(clinicRef, {
|
|
1386
|
+
procedures: admin4.firestore.FieldValue.arrayRemove(procedureId),
|
|
1387
|
+
proceduresInfo: updatedProceduresInfo,
|
|
1388
|
+
updatedAt: admin4.firestore.FieldValue.serverTimestamp()
|
|
1389
|
+
});
|
|
1390
|
+
});
|
|
1391
|
+
console.log(
|
|
1392
|
+
`[ProcedureAggregationService] Successfully removed procedure ${procedureId} from clinic ${clinicId}.`
|
|
1393
|
+
);
|
|
1394
|
+
} catch (error) {
|
|
1395
|
+
console.error(
|
|
1396
|
+
`[ProcedureAggregationService] Error removing procedure ${procedureId} from clinic ${clinicId}:`,
|
|
1397
|
+
error
|
|
1398
|
+
);
|
|
1399
|
+
throw error;
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
|
|
1404
|
+
// src/admin/aggregation/patient/patient.aggregation.service.ts
|
|
1405
|
+
import * as admin5 from "firebase-admin";
|
|
1406
|
+
var CALENDAR_SUBCOLLECTION_ID4 = "calendar";
|
|
1407
|
+
var PatientAggregationService = class {
|
|
1408
|
+
constructor(firestore6) {
|
|
1409
|
+
this.db = firestore6 || admin5.firestore();
|
|
1410
|
+
}
|
|
1411
|
+
// --- Methods for Patient Creation --- >
|
|
1412
|
+
// No specific aggregations defined for patient creation in the plan.
|
|
1413
|
+
// --- Methods for Patient Update --- >
|
|
1414
|
+
/**
|
|
1415
|
+
* Updates patient information in calendar events
|
|
1416
|
+
* @param patientId - ID of the patient
|
|
1417
|
+
* @param patientInfo - Updated patient information
|
|
1418
|
+
* @returns {Promise<void>}
|
|
1419
|
+
*/
|
|
1420
|
+
async updatePatientInfoInCalendarEvents(patientId, patientInfo) {
|
|
1421
|
+
if (!patientId || !patientInfo) {
|
|
1422
|
+
console.log(
|
|
1423
|
+
"[PatientAggregationService] Missing patientId or patientInfo for calendar update. Skipping."
|
|
1424
|
+
);
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
console.log(
|
|
1428
|
+
`[PatientAggregationService] Querying upcoming calendar events for patient ${patientId} to update patient info.`
|
|
1429
|
+
);
|
|
1430
|
+
const now = admin5.firestore.Timestamp.now();
|
|
1431
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID4).where("patientId", "==", patientId).where("eventTime.start", ">", now);
|
|
1432
|
+
try {
|
|
1433
|
+
const snapshot = await calendarEventsQuery.get();
|
|
1434
|
+
if (snapshot.empty) {
|
|
1435
|
+
console.log(
|
|
1436
|
+
`[PatientAggregationService] No upcoming calendar events found for patient ${patientId}. No patient info updates needed.`
|
|
1437
|
+
);
|
|
1438
|
+
return;
|
|
1439
|
+
}
|
|
1440
|
+
const batch = this.db.batch();
|
|
1441
|
+
snapshot.docs.forEach((doc) => {
|
|
1442
|
+
console.log(
|
|
1443
|
+
`[PatientAggregationService] Updating patient info for calendar event ${doc.ref.path}`
|
|
1444
|
+
);
|
|
1445
|
+
batch.update(doc.ref, {
|
|
1446
|
+
patientInfo,
|
|
1447
|
+
updatedAt: admin5.firestore.FieldValue.serverTimestamp()
|
|
1448
|
+
});
|
|
1449
|
+
});
|
|
1450
|
+
await batch.commit();
|
|
1451
|
+
console.log(
|
|
1452
|
+
`[PatientAggregationService] Successfully updated patient info in ${snapshot.size} upcoming calendar events for patient ${patientId}.`
|
|
1453
|
+
);
|
|
1454
|
+
} catch (error) {
|
|
1455
|
+
console.error(
|
|
1456
|
+
`[PatientAggregationService] Error updating patient info in calendar events for patient ${patientId}:`,
|
|
1457
|
+
error
|
|
1458
|
+
);
|
|
1459
|
+
throw error;
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
// --- Methods for Patient Deletion --- >
|
|
1463
|
+
/**
|
|
1464
|
+
* Cancels all upcoming calendar events associated with a deleted patient
|
|
1465
|
+
* @param patientId - ID of the deleted patient
|
|
1466
|
+
* @returns {Promise<void>}
|
|
1467
|
+
*/
|
|
1468
|
+
async cancelUpcomingCalendarEventsForPatient(patientId) {
|
|
1469
|
+
if (!patientId) {
|
|
1470
|
+
console.log(
|
|
1471
|
+
"[PatientAggregationService] Missing patientId for canceling calendar events. Skipping."
|
|
1472
|
+
);
|
|
1473
|
+
return;
|
|
1474
|
+
}
|
|
1475
|
+
console.log(
|
|
1476
|
+
`[PatientAggregationService] Querying upcoming calendar events for patient ${patientId} to cancel.`
|
|
1477
|
+
);
|
|
1478
|
+
const now = admin5.firestore.Timestamp.now();
|
|
1479
|
+
const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID4).where("patientId", "==", patientId).where("eventTime.start", ">", now);
|
|
1480
|
+
try {
|
|
1481
|
+
const snapshot = await calendarEventsQuery.get();
|
|
1482
|
+
if (snapshot.empty) {
|
|
1483
|
+
console.log(
|
|
1484
|
+
`[PatientAggregationService] No upcoming calendar events found for patient ${patientId}. No events to cancel.`
|
|
1485
|
+
);
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1488
|
+
const batch = this.db.batch();
|
|
1489
|
+
snapshot.docs.forEach((doc) => {
|
|
1490
|
+
console.log(
|
|
1491
|
+
`[PatientAggregationService] Canceling calendar event ${doc.ref.path}`
|
|
1492
|
+
);
|
|
1493
|
+
batch.update(doc.ref, {
|
|
1494
|
+
status: "CANCELED",
|
|
1495
|
+
cancelReason: "Patient deleted",
|
|
1496
|
+
updatedAt: admin5.firestore.FieldValue.serverTimestamp()
|
|
1497
|
+
});
|
|
1498
|
+
});
|
|
1499
|
+
await batch.commit();
|
|
1500
|
+
console.log(
|
|
1501
|
+
`[PatientAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for patient ${patientId}.`
|
|
1502
|
+
);
|
|
1503
|
+
} catch (error) {
|
|
1504
|
+
console.error(
|
|
1505
|
+
`[PatientAggregationService] Error canceling calendar events for patient ${patientId}:`,
|
|
1506
|
+
error
|
|
1507
|
+
);
|
|
1508
|
+
throw error;
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
};
|
|
1512
|
+
|
|
189
1513
|
// src/types/index.ts
|
|
190
1514
|
var UserRole = /* @__PURE__ */ ((UserRole2) => {
|
|
191
1515
|
UserRole2["PATIENT"] = "patient";
|
|
@@ -194,10 +1518,17 @@ var UserRole = /* @__PURE__ */ ((UserRole2) => {
|
|
|
194
1518
|
UserRole2["CLINIC_ADMIN"] = "clinic_admin";
|
|
195
1519
|
return UserRole2;
|
|
196
1520
|
})(UserRole || {});
|
|
1521
|
+
|
|
1522
|
+
// src/admin/index.ts
|
|
1523
|
+
console.log("[Admin Module] Initialized and services exported.");
|
|
197
1524
|
export {
|
|
1525
|
+
ClinicAggregationService,
|
|
198
1526
|
NOTIFICATIONS_COLLECTION,
|
|
199
1527
|
NotificationStatus,
|
|
200
1528
|
NotificationType,
|
|
201
1529
|
NotificationsAdmin,
|
|
1530
|
+
PatientAggregationService,
|
|
1531
|
+
PractitionerAggregationService,
|
|
1532
|
+
ProcedureAggregationService,
|
|
202
1533
|
UserRole
|
|
203
1534
|
};
|