# Class AppointmentService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

    public class AppointmentService : IAppointmentService, IApiRequestService

Inheritance
  object
  AppointmentService

Implements
  IAppointmentService
  IApiRequestService

Inherited Members
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.MemberwiseClone()
  object.ReferenceEquals(object, object)
  object.ToString()

Extension Methods
  NumberUtils.IsNumber(object)
  NumberUtils.ToDouble(object)
  ObjectUtils.ToStr(object?)

## Constructors

### AppointmentService(IAppointmentRepository, IAppointmentArchiveRepository, Lazy<IPatientService>, Lazy<IObservationService>, IDiagnosisService, IUnitService, IOptions<ApiSettings>, IOptions<CacheSettings>, ILogger<AppointmentService>, IHttpContextAccessor, ILocalAuditService, IPointOfCareService, ISubscribersService, IClientMessageService, ICacheService)

    public AppointmentService(IAppointmentRepository appointmentRepository, IAppointmentArchiveRepository appointmentArchiveRepository, Lazy<IPatientService> patientService, Lazy<IObservationService> observationService, IDiagnosisService diagnosisService, IUnitService unitService, IOptions<ApiSettings> apiSettings, IOptions<CacheSettings> cacheSettings, ILogger<AppointmentService> logger, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, IPointOfCareService pointOfCareService, ISubscribersService subscribersService, IClientMessageService clientMessageService, ICacheService cacheService)

#### Parameters

`appointmentRepository` IAppointmentRepository
`appointmentArchiveRepository` IAppointmentArchiveRepository
`patientService` Lazy<IPatientService>
`observationService` Lazy<IObservationService>
`diagnosisService` IDiagnosisService
`unitService` IUnitService
`apiSettings` IOptions<ApiSettings>
`cacheSettings` IOptions<CacheSettings>
`logger` ILogger<AppointmentService>
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`pointOfCareService` IPointOfCareService
`subscribersService` ISubscribersService
`clientMessageService` IClientMessageService
`cacheService` ICacheService

## Methods

### Archive(Patient)

Archives the specified patient by delegating the operation to ArchiveByPatientId(ObjectId) using the patient's identifier.

    public Task Archive(Patient patient)

#### Parameters

`patient` Patient

  The patient to be archived.

#### Returns

Task

### ArchiveByPatientId(ObjectId)

Archives all appointments associated with the specified patient by copying them to the appointment archive repository and then deleting them from the source collection.

    public Task ArchiveByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient whose appointments should be archived.

#### Returns

Task

### DeleteByPatientId(ObjectId)

Deletes all appointments associated with the specified patient identifier, invalidates the appointments cache, and records the action in the audit log.

    public Task DeleteByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient whose appointments will be deleted.

#### Returns

Task

### FindByLocation(PatientLocation)

Retrieves all patient appointments associated with the specified location.

    public Task<List<PatientAppointment>> FindByLocation(PatientLocation location)

#### Parameters

`location` PatientLocation

  The location used to filter the patient appointments.

#### Returns

Task<List<PatientAppointment>>

  A task that represents the asynchronous operation, containing a list of patient appointments for the specified location.

### FindByPatientIdAsync(ObjectId)

Asynchronously retrieves all patient appointments associated with the specified patient identifier by delegating to the appointment repository.

    public Task<IAsyncCursor<PatientAppointment>> FindByPatientIdAsync(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose appointments are to be retrieved.

#### Returns

Task<IAsyncCursor<PatientAppointment>>

  A task that represents the asynchronous operation, containing an async cursor over the matching PatientAppointment documents.

### GetByPatient(ObjectId)

Retrieves a list of patient appointments associated with the specified patient identifier by delegating to the appointment repository.

    public Task<List<PatientAppointment>> GetByPatient(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose appointments are being requested.

#### Returns

Task<List<PatientAppointment>>

  A task that represents the asynchronous operation, containing a list of PatientAppointment records for the given patient.

### GetTodayByPatient(ObjectId, CancellationToken)

Retrieves the list of appointments scheduled for today (UTC) for the specified patient, using a cache-aside pattern to avoid repeated database queries. The full list of patient appointments is fetched from cache (or loaded from the repository on a cache miss) and then filtered locally to include only those whose start time falls on the current UTC date.

    public Task<List<PatientAppointment>> GetTodayByPatient(ObjectId patientId, CancellationToken ct = default)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose appointments are being queried.

`ct` CancellationToken

  Cancellation token used to cancel the asynchronous operation.

#### Returns

Task<List<PatientAppointment>>

  A task that resolves to a list of PatientAppointment instances scheduled for today; an empty list is returned when no appointments match.

### GetTodayByPoc(ObjectId, CancellationToken)

Retrieves the patient appointments scheduled for today at the specified point of care. Uses a cache to store the full appointment list for the point of care and filters it by today's date; returns an empty list if the point of care is not found.

    public Task<List<PatientAppointment>> GetTodayByPoc(ObjectId pocId, CancellationToken ct = default)

#### Parameters

`pocId` ObjectId

  The identifier of the point of care whose appointments should be retrieved.

`ct` CancellationToken

  A cancellation token to cancel the asynchronous operation.

#### Returns

Task<List<PatientAppointment>>

  A task that represents the asynchronous operation, containing the list of patient appointments scheduled for today at the specified point of care.

### ProcessApiRequest(ApiRequest, Patient?)

Processes an incoming API request for a patient, handling HL7 SIU message types to create, update, or cancel appointments. Validates the patient and auto-ADT configuration, then routes the request to the appropriate handler based on message type: SIU_S12-S14 and SIU_S18-S22 (booking/rescheduling/modification), SIU_S15-S17 (cancellation), and other types (blocked slots / no-show), persisting changes, refreshing cache, creating audit logs, and emitting broadcasts.

    public Task ProcessApiRequest(ApiRequest apiRequest, Patient? patient)

#### Parameters

`apiRequest` ApiRequest

  The API request containing the HL7 message type, timestamp, and appointment payload to process.

`patient` Patient

  The patient associated with the request; if null, the method returns without processing.

#### Returns

Task

### SaveRequest(ApiRequest)

Saves an API request by validating the patient, resolving or creating the patient record, processing the request, and handling associated observations and diagnoses.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request containing the patient number, location, type, observations, diagnosis, and related data to be processed.

#### Returns

Task

#### Exceptions

ApiRequestException

  Thrown when the `apiRequest` has a null or empty patient number.

### SaveRequestAsync(ApiRequest)

Asynchronously saves the provided API request by running the save operation on a background task.

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to be saved.

#### Returns

Task

  A task that represents the asynchronous save operation.

### UpdateManyObjectId(string, ObjectId, ObjectId)

Updates multiple appointment records by replacing the specified `oldId` with the new `id`, scoped by the given `nameId`. Delegates the operation to the underlying appointment repository.

    public Task UpdateManyObjectId(string nameId, ObjectId id, ObjectId oldId)

#### Parameters

`nameId` string

  The identifier used to scope which appointment records are affected by the update.

`id` ObjectId

  The new ObjectId that will replace the existing one in the matching records.

`oldId` ObjectId

  The current ObjectId to be replaced in the matching records.

#### Returns

Task