# Clinic Aggregation Service (Admin)

This service centralizes the logic for updating aggregated data in various Firestore collections _when a clinic is created, updated, or deleted_. It is designed primarily for use by Cloud Functions triggered by changes in the `clinics` collection.

## `ClinicAggregationService` Class

### Constructor

```typescript
constructor(firestore?: admin.firestore.Firestore)
```

Initializes the service with an Admin Firestore instance. Uses the default instance if none is provided.

### Methods Triggered by Clinic **Creation**

- **`addClinicToClinicGroup(clinicGroupId: string, clinicInfo: ClinicInfo): Promise<void>`**
  - Adds the `clinicInfo` object and `clinicId` to the `clinicsInfo` and `clinicIds` arrays respectively in the specified parent `ClinicGroup` document.

### Methods Triggered by Clinic **Update**

- **`updateClinicInfoInPractitioners(practitionerIds: string[], clinicInfo: ClinicInfo): Promise<void>`**
  - Updates the aggregated `clinicsInfo` array within multiple `Practitioner` documents.
  - Uses a batch write to first remove the old `ClinicInfo` (matching by `id`) and then add the updated `clinicInfo`.
- **`updateClinicInfoInProcedures(procedureIds: string[], clinicInfo: ClinicInfo): Promise<void>`**
  - Updates the embedded `clinicInfo` object within multiple `Procedure` documents associated with the updated clinic.
- **`updateClinicInfoInClinicGroup(clinicGroupId: string, clinicInfo: ClinicInfo): Promise<void>`**
  - Updates the `clinicsInfo` array within the parent `ClinicGroup` document.
  - Uses a batch write to first remove the old `ClinicInfo` (matching by `id`) and then add the updated `clinicInfo`.
- **`updateClinicInfoInPatients(patientIds: string[], clinicInfo: ClinicInfo, clinicIsActive: boolean): Promise<void>`**
  - Currently logs a warning as the `PatientProfile` doesn't store full `ClinicInfo`.
  - Intended place for logic if patient records need updates based on clinic changes (e.g., clinic deactivation).
- **`updateClinicLocationInCalendarEvents(clinicId: string, newLocation: ClinicLocation): Promise<void>`**
  - Uses a Collection Group query (`calendar` subcollection) to find all _upcoming_ calendar events associated with the `clinicId`.
  - Updates the `eventLocation` field in these calendar events with the `newLocation`.
- **`updateClinicInfoInCalendarEvents(clinicId: string, clinicInfo: ClinicInfo): Promise<void>`**
  - Uses a Collection Group query (`calendar` subcollection) to find all _upcoming_ calendar events associated with the `clinicId`.
  - Updates the embedded `clinicInfo` object in these calendar events.

### Methods Triggered by Clinic **Deletion**

- **`removeClinicFromPractitioners(practitionerIds: string[], clinicId: string): Promise<void>`**
  - Removes the `clinicId` from the `clinicIds` array and the corresponding `ClinicInfo` object (matching by `id`) from the `clinicsInfo` array in multiple `Practitioner` documents.
- **`inactivateProceduresForClinic(procedureIds: string[]): Promise<void>`**
  - Sets the `isActive` flag to `false` for multiple `Procedure` documents associated with the deleted clinic.
- **`removeClinicFromClinicGroup(clinicGroupId: string, clinicId: string): Promise<void>`**
  - Removes the `clinicId` from the `clinicIds` array and the corresponding `ClinicInfo` object (matching by `id`) from the `clinicsInfo` array in the parent `ClinicGroup` document.
- **`removeClinicFromPatients(patientIds: string[], clinicId: string): Promise<void>`**
  - Removes the `clinicId` from the `clinicIds` array in multiple `Patient` documents.
- **`cancelUpcomingCalendarEventsForClinic(clinicId: string): Promise<void>`**
  - Uses a Collection Group query (`calendar` subcollection) to find all _upcoming_ calendar events associated with the `clinicId`.
  - Sets the `status` of these events to `CANCELED` and adds a `cancelReason`.
