# Class DischargeService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Provides the concrete implementation of the IDischargeService contract, encapsulating the business logic required to process discharge operations.

    public class DischargeService : IDischargeService, IApiRequestService

Inheritance
  object
  DischargeService

Implements
  IDischargeService
  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 acts as the default in-memory or infrastructure-backed implementation of the discharge operations defined by IDischargeService.

## Constructors

### DischargeService(ILogger<DischargeService>, ISubscribersService, IDischargeRepository, Lazy<IPatientService>, IClientMessageService, IPointOfCareService, IHttpContextAccessor, ILocalAuditService, IUnitService, IMasterListServiceFactory)

    public DischargeService(ILogger<DischargeService> logger, ISubscribersService subscribersService, IDischargeRepository dischargeRepository, Lazy<IPatientService> patientServiceLazy, IClientMessageService clientMessageService, IPointOfCareService pointOfCareService, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, IUnitService unitService, IMasterListServiceFactory masterListServiceFactory)

#### Parameters

`logger` ILogger<DischargeService>
`subscribersService` ISubscribersService
`dischargeRepository` IDischargeRepository
`patientServiceLazy` Lazy<IPatientService>
`clientMessageService` IClientMessageService
`pointOfCareService` IPointOfCareService
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`unitService` IUnitService
`masterListServiceFactory` IMasterListServiceFactory

## Methods

### CountDischargesByUnitId(ObjectId)

Asynchronously counts the number of discharge records associated with the specified unit identifier.

    public Task<long> CountDischargesByUnitId(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The identifier of the unit whose discharge records will be counted.

#### Returns

Task<long>

  A task representing the asynchronous operation, containing the total number of discharges for the given unit.

### DeleteDischargeAsync(Discharge)

Deletes a discharge record asynchronously. The method is intended to validate that the patient can be discharged (requiring both medical and administrative discharge values and an allowed discharge status), and otherwise falls back to deleting the discharge by its identifier.

    public Task DeleteDischargeAsync(Discharge discharge)

#### Parameters

`discharge` Discharge

  The discharge entity to be deleted.

#### Returns

Task

### DeleteDischargeByIdAsync(ObjectId)

Deletes a discharge record by its identifier. If the discharge is not found, an error is logged and the operation is skipped; otherwise the record is removed, an audit log entry is created, and a delete broadcast is sent.

    public Task DeleteDischargeByIdAsync(ObjectId dischargeId)

#### Parameters

`dischargeId` ObjectId

  The unique identifier of the discharge to delete.

#### Returns

Task

### DeleteDischargesByUnitId(ObjectId)

Asynchronously deletes all discharge records associated with the specified unit identifier by delegating the operation to the discharge repository.

    public Task DeleteDischargesByUnitId(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The unique identifier of the unit whose discharge records should be removed.

#### Returns

Task

### DeletePatientMasterListItem(OptionList, IEnumerable<Unit>, string)

Deletes a patient master list option for the specified units and type, then processes the resulting updated discharge records by creating audit log entries and broadcasting update notifications (only when the updated discharge record is found).

    public Task DeletePatientMasterListItem(OptionList opt, IEnumerable<Unit> unitList, string typeName)

#### Parameters

`opt` OptionList

  The option list entry to be removed from the patient master list.

`unitList` IEnumerable<Unit>

  The collection of units whose identifiers are used to scope the deletion.

`typeName` string

  The name of the master list type/category to which the option belongs.

#### Returns

Task

### GetDischargeByIdAsync(ObjectId)

Retrieves a discharge by its identifier, enriching the result with patient location details when an associated point of care is available.

    public Task<Discharge?> GetDischargeByIdAsync(ObjectId dischargeId)

#### Parameters

`dischargeId` ObjectId

  The unique identifier of the discharge to retrieve.

#### Returns

Task<Discharge>

  The matching Discharge, populated with PatientLocation information if a point of care is linked; otherwise the discharge as stored.

#### Exceptions

NotFoundException

  Thrown when no discharge is found for the specified `dischargeId`.

### GetDischargeByLocation(PatientLocation)

Asynchronously retrieves the Discharge associated with the specified patient location. Returns `null` if an exception occurs while accessing the underlying repository.

    public Task<Discharge?> GetDischargeByLocation(PatientLocation location)

#### Parameters

`location` PatientLocation

  The patient location used to look up the associated discharge record.

#### Returns

Task<Discharge>

  A Discharge if one is found for the given location; otherwise, `null` when an error occurs.

### GetDischargeByPatientId(ObjectId)

Retrieves the discharge record associated with the specified patient identifier, enriching it with patient location details when a point of care is linked.

    public Task<Discharge?> GetDischargeByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose discharge record should be retrieved.

#### Returns

Task<Discharge>

  A Discharge object populated with patient location information when a point of care is associated, or `null` if no discharge is found or an error occurs.

### GetDischargeByPointOfCareId(ObjectId)

Retrieves a discharge record by the specified point of care identifier and enriches it with patient location details. If the discharge is not found, an information message is logged; when a related point of care is available, its unit, bed, and room are mapped to the discharge's PatientLocation. On failure, the error is logged and `null` is returned.

    public Task<Discharge?> GetDischargeByPointOfCareId(ObjectId poc)

#### Parameters

`poc` ObjectId

  The point of care identifier used to look up the discharge record.

#### Returns

Task<Discharge>

  A Discharge with the patient location populated when available, or `null` if the discharge is not found or an error occurs.

### GetDischargeByPointOfCareIdWithLocale(ObjectId, LocaleEnum)

Retrieves a discharge record by its point of care (location) identifier and applies locale-specific data using the associated unit. Returns the discharge as-is if no associated unit is found, or `null` if no discharge exists for the given location.

    public Task<Discharge?> GetDischargeByPointOfCareIdWithLocale(ObjectId location, LocaleEnum dataLocale)

#### Parameters

`location` ObjectId

  The ObjectId identifying the point of care (location) whose discharge record should be retrieved.

`dataLocale` LocaleEnum

  The locale used to localize the discharge data when the associated unit is found.

#### Returns

Task<Discharge>

  A Task<TResult> containing the localized discharge, the unmodified discharge when its unit cannot be found, or `null` when no discharge exists for the specified location.

### GetDischargesAsync()

Asynchronously retrieves all discharge records from the repository.

    public Task<IEnumerable<Discharge>> GetDischargesAsync()

#### Returns

Task<IEnumerable<Discharge>>

  A task that represents the asynchronous operation, containing an enumerable collection of Discharge records.

### InsertDischarge(Discharge)

Inserts a new discharge record into the repository, verifies its persistence, logs the operation, and broadcasts a notification. Throws a ConflictException if the discharge cannot be retrieved after insertion, indicating a creation failure.

    public Task<Discharge?> InsertDischarge(Discharge discharge)

#### Parameters

`discharge` Discharge

  The discharge entity to be inserted.

#### Returns

Task<Discharge>

  The persisted Discharge entity retrieved from the repository after insertion.

#### Exceptions

ConflictException

  Thrown when the inserted discharge cannot be found by its identifier, indicating that the creation failed.

### SaveRequest(ApiRequest)

Asynchronously saves the specified API request.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to persist.

#### Returns

Task

### SaveRequestAsync(ApiRequest)

Asynchronously persists the specified API request by executing the save operation on a background thread.

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to save.

#### Returns

Task

  A task that completes when the request has been saved.

### SendDischargeBroadcast(Discharge, OperationType)

Sends a discharge broadcast to all subscribers associated with the discharge's point of care, grouped by their locale. Validates that the point of care identifier is present; logs and returns early if it is null. Exceptions during the broadcast are caught and logged without rethrowing.

    public void SendDischargeBroadcast(Discharge discharge, OperationType operation)

#### Parameters

`discharge` Discharge

  The discharge whose point of care is used to locate matching subscribers and whose data is sent in the broadcast.

`operation` OperationType

  The operation type associated with the outgoing message sent to each subscriber.

### UpdateDischargeAsync(Discharge)

Updates an existing discharge record, auditing the change and broadcasting the update. Throws a conflict exception if the discharge cannot be found by its identifier.

    public Task UpdateDischargeAsync(Discharge discharge)

#### Parameters

`discharge` Discharge

  The discharge entity to be updated.

#### Returns

Task

#### Exceptions

ConflictException

  Thrown when no discharge is found with the specified identifier, preventing the update from proceeding.

### UpdatePatientMasterListItemChange(UpdateOptionMasterListDto, IEnumerable<Unit>, string)

Updates the master list option for the specified units and type, then records an audit log entry and broadcasts the change for each affected discharge.

    public Task UpdatePatientMasterListItemChange(UpdateOptionMasterListDto opt, IEnumerable<Unit> unitList, string typeName)

#### Parameters

`opt` UpdateOptionMasterListDto

  The master list update options to apply to the discharges.

`unitList` IEnumerable<Unit>

  The collection of units whose associated discharges will be updated.

`typeName` string

  The name of the master list type used to target the update.

#### Returns

Task