# Class TreatmentService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Provides business logic for managing patient treatments, including insertion, update, deletion, archiving, pagination, and broadcasting of treatment events to subscribed clients.

    public class TreatmentService : ITreatmentService, IApiRequestService

Inheritance
  object
  TreatmentService

Implements
  ITreatmentService
  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?)

## Remarks

This service orchestrates interactions between the treatment repository, the patient service, configuration/calculation observation services, and audit services. It also handles inbound HL7-like pharmacy/treatment messages (`OMP_O09`, `ORM_O01`, `RAS_O17`) and emits broadcasts to subscribers based on the patient's point of care.

## Constructors

### TreatmentService(ITreatmentRepository, ITreatmentArchiveRepository, IPatientService, IConfigObservationService, ILogger<TreatmentService>, IClientMessageService, ISubscribersService, Lazy<ICalculatedObservationsService>, IUnitService, IHttpContextAccessor, ILocalAuditService)

Provides business logic for managing patient treatments, including insertion, update, deletion, archiving, pagination, and broadcasting of treatment events to subscribed clients.

    public TreatmentService(ITreatmentRepository treatmentRepository, ITreatmentArchiveRepository treatmentArchiveRepository, IPatientService patientService, IConfigObservationService configObservationService, ILogger<TreatmentService> logger, IClientMessageService clientMessageService, ISubscribersService subscribersService, Lazy<ICalculatedObservationsService> calculatedObservationsService, IUnitService unitService, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService)

#### Parameters

`treatmentRepository` ITreatmentRepository
`treatmentArchiveRepository` ITreatmentArchiveRepository
`patientService` IPatientService
`configObservationService` IConfigObservationService
`logger` ILogger<TreatmentService>
`clientMessageService` IClientMessageService
`subscribersService` ISubscribersService
`calculatedObservationsService` Lazy<ICalculatedObservationsService>
`unitService` IUnitService
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService

#### Remarks

This service orchestrates interactions between the treatment repository, the patient service, configuration/calculation observation services, and audit services. It also handles inbound HL7-like pharmacy/treatment messages (`OMP_O09`, `ORM_O01`, `RAS_O17`) and emits broadcasts to subscribers based on the patient's point of care.

## Methods

### Archive(Patient)

Archives all treatments associated with the supplied patient.

    public Task Archive(Patient patient)

#### Parameters

`patient` Patient

  The Patient whose treatments will be archived.

#### Returns

Task

  A task that represents the asynchronous archive operation.

### ArchiveByPatientId(ObjectId)

Copies all treatments of the specified patient into the treatment archive repository and subsequently removes them from the primary repository.

    public Task ArchiveByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId of the patient whose treatments will be archived.

#### Returns

Task

  A task that represents the asynchronous archive operation.

### DeleteById(ObjectId)

Deletes a treatment by its identifier and writes an audit log entry capturing the deleted state.

    public Task DeleteById(ObjectId id)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId of the treatment to delete.

#### Returns

Task

  A task that represents the asynchronous delete operation.

#### Exceptions

ConflictException

  Thrown when the treatment does not exist.

### DeleteByPatientId(ObjectId)

Deletes all treatments belonging to the specified patient and creates an audit log entry capturing the deleted state.

    public Task<bool> DeleteByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId of the patient whose treatments will be deleted.

#### Returns

Task<bool>

  A task that represents the asynchronous operation. The task result is true if the deletion succeeds or there is nothing to delete; otherwise the method throws.

#### Exceptions

ConflictException

  Thrown when the patient has no treatments to delete (no record was found) or when the underlying delete operation fails.

### FindByPatientId(ObjectId)

Returns all treatments of the given patient as an enumerable.

    public Task<IEnumerable<PatientTreatment>> FindByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The MongoDB.Bson.ObjectId of the patient.

#### Returns

Task<IEnumerable<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains an IEnumerable<T> of PatientTreatment.

### FindByPatientIdAsync(ObjectId)

Returns an asynchronous cursor over all treatments of the given patient.

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

#### Parameters

`patientId` ObjectId

  The MongoDB.Bson.ObjectId of the patient.

#### Returns

Task<IAsyncCursor<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains an MongoDB.Driver.IAsyncCursor<TDocument> of PatientTreatment.

### GetActiveTreatmentsByPatient(ObjectId)

Returns the active (non-canceled, in-range) treatments of the specified patient, keeping the most recent active order per placer-order identifier.

    public Task<IEnumerable<PatientTreatment?>> GetActiveTreatmentsByPatient(ObjectId id)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId of the patient.

#### Returns

Task<IEnumerable<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains an IEnumerable<T> of nullable PatientTreatment representing the active treatments. Treatments marked as `Dc` or outside their validity window are excluded.

### GetActiveTreatmentsByPatientIdAndOrder(ObjectId, string)

Returns the active treatments of a patient that match the specified placer order.

    public Task<List<PatientTreatment>> GetActiveTreatmentsByPatientIdAndOrder(ObjectId patientId, string order)

#### Parameters

`patientId` ObjectId

  The MongoDB.Bson.ObjectId of the patient.

`order` string

  The placer-order identifier to filter by.

#### Returns

Task<List<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains a List<T> of PatientTreatment.

### GetBolusTreatments(ObjectId)

Returns all bolus treatments of the specified patient.

    public Task<List<PatientTreatment>> GetBolusTreatments(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The MongoDB.Bson.ObjectId of the patient.

#### Returns

Task<List<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains a List<T> of PatientTreatment representing the bolus treatments.

### GetPaginatedTreatments(PaginationFilter)

Returns a paginated list of treatments based on the supplied filter.

    public Task<PaginationResponse<PatientTreatment>> GetPaginatedTreatments(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

  The PaginationFilter containing page number and page size.

#### Returns

Task<PaginationResponse<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains a PaginationResponse<T> of PatientTreatment.

### GetTreatmentsByPatientId(ObjectId)

Retrieves all treatments associated with the specified patient identifier.

    public Task<IEnumerable<PatientTreatment>> GetTreatmentsByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId of the patient.

#### Returns

Task<IEnumerable<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains an IEnumerable<T> of PatientTreatment for the patient.

### Insert(PatientTreatment)

Inserts a new patient treatment after applying mapping/calculation rules and creates an audit log entry.

    public Task Insert(PatientTreatment treatment)

#### Parameters

`treatment` PatientTreatment

  The PatientTreatment to insert.

#### Returns

Task

  A task that represents the asynchronous insert operation.

#### Remarks

If the treatment cannot be mapped, it is silently ignored. A broadcast is dispatched asynchronously upon successful insertion.

### SaveRequest(ApiRequest)

Processes an inbound API request and persists the corresponding treatments for the referenced patient.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The ApiRequest to process.

#### Returns

Task

  A task that represents the asynchronous save operation.

#### Remarks

Supported message types are `OMP_O09`, `ORM_O01` and `RAS_O17`. The method also enforces the unit's `AutoAdt` configuration when the request does not originate from a panel.

#### Exceptions

ApiRequestException

  Thrown when the patient number is missing or the request type is not supported.

### SaveRequestAsync(ApiRequest)

Asynchronous entry point that delegates to SaveRequest(ApiRequest).

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The inbound ApiRequest to process.

#### Returns

Task

  A task that represents the asynchronous save operation.

### UpdateManyObjectId(string, ObjectId, ObjectId)

Updates every occurrence of an identifier (e.g. a referenced MongoDB.Bson.ObjectId) in stored treatments from `oldId` to `id` and broadcasts the changes to subscribers.

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

#### Parameters

`nameId` string

  The name of the field that holds the identifier.

`id` ObjectId

  The new MongoDB.Bson.ObjectId value.

`oldId` ObjectId

  The previous MongoDB.Bson.ObjectId value being replaced.

#### Returns

Task

  A task that represents the asynchronous operation.

### UpdateTreatment(PatientTreatment)

Updates an existing treatment and writes an audit log entry comparing the previous and updated values.

    public Task<bool> UpdateTreatment(PatientTreatment patientTreatment)

#### Parameters

`patientTreatment` PatientTreatment

  The PatientTreatment containing the updated values.

#### Returns

Task<bool>

  A task that represents the asynchronous operation. The task result is true if the update succeeds.

#### Exceptions

ConflictException

  Thrown when the treatment does not exist or the update operation fails.