# Class DiagnosisService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Provides the implementation of the IDiagnosisService contract, offering diagnosis-related operations as defined by the interface.

    public class DiagnosisService : IDiagnosisService, IApiRequestService

Inheritance
  object
  DiagnosisService

Implements
  IDiagnosisService
  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

### DiagnosisService(Lazy<IPatientService>, IOptions<ApiSettings>, IDiagnosisRepository, IDiagnosisArchiveRepository, ILogger<DiagnosisService>, IClientMessageService, ISubscribersService, Lazy<ICalculatedObservationsService>, IHttpContextAccessor, ILocalAuditService, IUnitService)

    public DiagnosisService(Lazy<IPatientService> patientService, IOptions<ApiSettings> apiSettings, IDiagnosisRepository diagnosisRepository, IDiagnosisArchiveRepository diagnosisArchiveRepository, ILogger<DiagnosisService> logger, IClientMessageService clientMessageService, ISubscribersService subscribersService, Lazy<ICalculatedObservationsService> calculatedObservations, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, IUnitService unitService)

#### Parameters

`patientService` Lazy<IPatientService>
`apiSettings` IOptions<ApiSettings>
`diagnosisRepository` IDiagnosisRepository
`diagnosisArchiveRepository` IDiagnosisArchiveRepository
`logger` ILogger<DiagnosisService>
`clientMessageService` IClientMessageService
`subscribersService` ISubscribersService
`calculatedObservations` Lazy<ICalculatedObservationsService>
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`unitService` IUnitService

## Methods

### Archive(Patient)

Archives the specified patient by delegating to the archival routine keyed by the patient's identifier.

    public Task Archive(Patient patient)

#### Parameters

`patient` Patient

  The patient to be archived. Its `Id` is used to locate the record to archive.

#### Returns

Task

### ArchiveByPatientId(ObjectId)

Archives all diagnoses associated with the specified patient by copying them to the diagnosis archive repository and then deleting the original records.

    public Task ArchiveByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The identifier of the patient whose diagnoses will be archived.

#### Returns

Task

### DeleteByPatientId(ObjectId)

Deletes all diagnoses associated with the specified patient identifier and records an audit log entry capturing the previous state of the records.

    public Task DeleteByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient whose diagnoses should be deleted.

#### Returns

Task

### FindByPatientIdAsync(ObjectId)

Asynchronously retrieves all patient diagnosis records associated with the specified patient identifier by delegating to the diagnosis repository.

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

#### Parameters

`patientId` ObjectId

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

#### Returns

Task<IAsyncCursor<PatientDiagnosis>>

  A task that represents the asynchronous operation, containing an asynchronous cursor over the matching PatientDiagnosis records.

### GetByPatient(ObjectId)

Retrieves all diagnoses associated with the specified patient identifier from the diagnosis repository.

    public Task<List<PatientDiagnosis>> GetByPatient(ObjectId id)

#### Parameters

`id` ObjectId

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

#### Returns

Task<List<PatientDiagnosis>>

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

### GetByPatientId(ObjectId)

Asynchronously retrieves the list of patient diagnoses associated with the specified patient identifier by delegating to the diagnosis repository.

    public Task<List<PatientDiagnosis>> GetByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose diagnoses are being retrieved.

#### Returns

Task<List<PatientDiagnosis>>

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

### Insert(PatientDiagnosis)

Inserts a patient diagnosis into the repository after mapping it to the underlying data model, and broadcasts the stored record. If the diagnosis cannot be mapped (returns null), the insert and broadcast operations are skipped.

    public Task Insert(PatientDiagnosis diagnosis)

#### Parameters

`diagnosis` PatientDiagnosis

  The patient diagnosis to insert.

#### Returns

Task

### InsertDiagnosis(PatientDiagnosis)

Inserts or updates a patient diagnosis, creating an audit log entry. If a diagnosis already exists for the same patient, code, and coding system, it is updated while preserving its identifier and original timestamp; otherwise a new record is inserted. A broadcast is dispatched asynchronously after a successful insert or update.

    public Task InsertDiagnosis(PatientDiagnosis diagnosis)

#### Parameters

`diagnosis` PatientDiagnosis

  The patient diagnosis to persist.

#### Returns

Task

### ProcessDiagnosis(List<PatientDiagnosis>, Patient, DateTime)

Processes a list of patient diagnoses by associating each entry with the specified patient and message time, then inserting them as diagnosis observations.

    public Task ProcessDiagnosis(List<PatientDiagnosis> diagnosis, Patient patient, DateTime messageTime)

#### Parameters

`diagnosis` List<PatientDiagnosis>

  The list of patient diagnoses to be processed and inserted.

`patient` Patient

  The patient whose identifier is assigned to each diagnosis entry.

`messageTime` DateTime

  The timestamp assigned to each diagnosis entry during processing.

#### Returns

Task

### ProcessDiagnosisObservation(ApiRequest, Patient)

Processes a diagnosis observation from an API request and persists it as a patient diagnosis. Maps SNOMED-coded observation values to diagnosis properties (description, label, code, state, category, start/end time), falling back to the current date when the observation time is missing, and skipping processing if the observations collection is null.

    public Task ProcessDiagnosisObservation(ApiRequest apiRequest, Patient patient)

#### Parameters

`apiRequest` ApiRequest

  The API request containing the observation codes, values, message time, and optional observation time used to build the diagnosis.

`patient` Patient

  The patient associated with the diagnosis, whose identifier is assigned to the new PatientDiagnosis.

#### Returns

Task

  A task that represents the asynchronous insertion of the resulting PatientDiagnosis.

### SaveRequest(ApiRequest)

Asynchronously saves the specified API request by delegating to an overload that accepts a secondary parameter, which is passed as null.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to save.

#### Returns

Task

  A task that represents the asynchronous save operation.

### SaveRequest(ApiRequest, Patient?)

Processes an incoming API request for diagnosis-related observations, resolving the patient from the request when not supplied. Validates that at least one of the patient number or location unit name is provided, handles ORU_R01 and ORU_R40 observation messages, and triggers diagnosis observation processing when the observation code is recognized.

    public Task SaveRequest(ApiRequest apiRequest, Patient? patient)

#### Parameters

`apiRequest` ApiRequest

  The API request containing the type, patient identifiers, location, and observation data to be processed.

`patient` Patient

  An optional pre-resolved patient; when null, the patient is resolved via the patient service using the request data.

#### Returns

Task

#### Exceptions

ApiRequestException

  Thrown when both the patient number and the location unit name are missing from the request, or when the request type is not valid for diagnosis processing.

### SaveRequestAsync(ApiRequest)

Asynchronously saves the specified API request by delegating to the underlying save operation with a null secondary parameter.

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request instance to persist.

#### Returns

Task

  A task that represents the asynchronous save operation.

### UpdateManyObjectId(string, ObjectId, ObjectId)

Updates many diagnosis records, replacing the `oldId` with the new `id` for the specified `nameId` field, by delegating to the diagnosis repository.

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

#### Parameters

`nameId` string

  The name of the identifier field used to locate the records to update.

`id` ObjectId

  The new ObjectId to assign to the matching records.

`oldId` ObjectId

  The existing ObjectId to be replaced in the matching records.

#### Returns

Task