# Class AdmissionService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

    public class AdmissionService : IAdmissionService, IApiRequestService

Inheritance
  object
  AdmissionService

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

### AdmissionService(ILogger<AdmissionService>, ISubscribersService, IAdmissionRepository, IClientMessageService, IUnitService, IPatientService, IPointOfCareService, IDisplayService, IDischargeService, IPatientArchiveRepository, IHttpContextAccessor, ILocalAuditService, IMasterListServiceFactory)

    public AdmissionService(ILogger<AdmissionService> logger, ISubscribersService subscribersService, IAdmissionRepository admissionRepository, IClientMessageService clientMessageService, IUnitService unitService, IPatientService patientService, IPointOfCareService pointOfCareService, IDisplayService displayService, IDischargeService dischargeService, IPatientArchiveRepository patientArchiveRepository, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, IMasterListServiceFactory masterListServiceFactory)

#### Parameters

`logger` ILogger<AdmissionService>
`subscribersService` ISubscribersService
`admissionRepository` IAdmissionRepository
`clientMessageService` IClientMessageService
`unitService` IUnitService
`patientService` IPatientService
`pointOfCareService` IPointOfCareService
`displayService` IDisplayService
`dischargeService` IDischargeService
`patientArchiveRepository` IPatientArchiveRepository
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`masterListServiceFactory` IMasterListServiceFactory

## Methods

### AdmitPatient(Admission, bool)

Admits a patient based on the provided Admission, creating a new Patient assigned to the specified point of care, marking the point of care as in use, and updating related master lists (insulation, allergies, diagnosis, origin, language barrier, passive sitting) when present. If the point of care id, unit, or point of care cannot be resolved, the operation is skipped after logging an error. When `isNew` is `false`, the originating admission record is deleted after the patient is inserted.

    public Task AdmitPatient(Admission admission, bool isNew = false)

#### Parameters

`admission` Admission

  The admission data used to create the patient and populate location, diagnosis, allergies, and other attributes.

`isNew` bool

  When `false`, the admission record is deleted after a successful patient insertion; when `true`, the admission is retained.

#### Returns

Task

### CountAdmissionsByUnitId(ObjectId)

Asynchronously counts the number of admissions associated with the specified unit identifier by delegating to the admission repository. Returns 0 and logs the error if the repository operation fails, ensuring the method does not propagate exceptions to the caller.

    public Task<long> CountAdmissionsByUnitId(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The identifier of the unit whose admissions should be counted.

#### Returns

Task<long>

  A task that represents the asynchronous operation. The task result contains the number of admissions for the given unit, or 0 if an error occurs.

### DeleteAdmissionAsync(Admission)

Deletes the specified admission by delegating to the delete operation using the admission's identifier.

    public Task DeleteAdmissionAsync(Admission admission)

#### Parameters

`admission` Admission

  The admission entity to delete, identified by its Id.

#### Returns

Task

### DeleteAdmissionByIdAsync(ObjectId)

Deletes an admission identified by the given id. If the admission is not found, the operation is skipped and logged; otherwise the admission is removed, any associated point of care is detached (clearing its `AdmissionId` and `Admission`) and set to `Available` when not currently `Locked` or `InUse`, a delete broadcast is sent, and an audit log entry is created.

    public Task DeleteAdmissionByIdAsync(ObjectId admissionId)

#### Parameters

`admissionId` ObjectId

  The identifier of the admission to delete.

#### Returns

Task

### DeleteAdmissionsByUnitId(ObjectId)

Asynchronously deletes all admissions associated with the specified unit identifier by delegating the operation to the admission repository.

    public Task DeleteAdmissionsByUnitId(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The unique identifier of the unit whose admissions should be removed.

#### Returns

Task

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

Deletes a master list option from patient admissions associated with the specified units and type, records an audit log entry for each affected admission, and broadcasts the admission update when the updated admission is found.

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

#### Parameters

`opt` OptionList

  The master list option to remove from the admissions.

`unitList` IEnumerable<Unit>

  The collection of units whose admissions will be processed for the deletion.

`typeName` string

  The name of the option type being deleted.

#### Returns

Task

### GetAdmissionByIdAsync(ObjectId)

Retrieves an admission by its identifier and, when a point of care is associated, enriches the result with the patient's location (unit, bed, and room) obtained from the point of care service. Returns null if the admission cannot be found.

    public Task<Admission?> GetAdmissionByIdAsync(ObjectId admissionId)

#### Parameters

`admissionId` ObjectId

  The unique identifier of the admission to retrieve.

#### Returns

Task<Admission>

  The matching Admission with its PatientLocation populated when applicable, or null if no admission is found.

### GetAdmissionByLocation(PatientLocation)

Retrieves a list of admissions for the specified patient location. If an error occurs during retrieval, the error is logged and an empty list is returned.

    public Task<List<Admission>> GetAdmissionByLocation(PatientLocation location)

#### Parameters

`location` PatientLocation

  The patient location used to filter admissions.

#### Returns

Task<List<Admission>>

  A task that represents the asynchronous operation. The task result contains a list of admissions matching the specified location, or an empty list if an error occurs.

### GetAdmissionByPatientNumber(string)

Retrieves the admission record associated with the specified patient clinical record number (NHC) from the admission repository.

    public Task<Admission?> GetAdmissionByPatientNumber(string patientNumber)

#### Parameters

`patientNumber` string

  The patient's clinical record number (NHC) used to look up the admission.

#### Returns

Task<Admission>

  A task that resolves to the matching Admission if found, or `null` when no admission exists for the given patient number.

### GetAdmissionByPointOfCareId(ObjectId)

Retrieves a list of admissions associated with the specified point of care identifier, enriching each admission with its patient location information when available.

    public Task<List<Admission>> GetAdmissionByPointOfCareId(ObjectId pocId)

#### Parameters

`pocId` ObjectId

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

#### Returns

Task<List<Admission>>

  A task representing the asynchronous operation, containing the list of admissions for the given point of care, or an empty list if an error occurs.

### GetAdmissionByPointOfCareIdAndLocale(ObjectId, LocaleEnum)

Retrieves all admissions associated with the specified point of care and applies translations according to the given locale in parallel.

    public Task<List<Admission>> GetAdmissionByPointOfCareIdAndLocale(ObjectId pocId, LocaleEnum locale)

#### Parameters

`pocId` ObjectId

  The identifier of the point of care whose admissions will be retrieved.

`locale` LocaleEnum

  The locale used to translate the admission fields.

#### Returns

Task<List<Admission>>

  A task that represents the asynchronous operation, containing a list of admissions with their fields translated to the specified locale.

### GetAdmissionByUnitIdWithOutPoC(ObjectId)

Retrieves admissions associated with the specified unit, excluding those linked to a Point of Care (PoC). If an error occurs during retrieval, the exception is logged and an empty list is returned as a fallback.

    public Task<List<Admission>> GetAdmissionByUnitIdWithOutPoC(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The identifier of the unit whose admissions (without PoC) are being requested.

#### Returns

Task<List<Admission>>

  A task that returns a list of Admission objects for the given unit, or an empty list if an error occurs.

### GetAdmissionsAsync()

Asynchronously retrieves all admissions and enriches each one with its associated point of care information (unit, bed, and room) when available.

    public Task<IEnumerable<Admission>> GetAdmissionsAsync()

#### Returns

Task<IEnumerable<Admission>>

  A task that represents the asynchronous operation. The task result contains a collection of Admission objects with patient location details populated for those linked to a point of care.

#### Exceptions

NotFoundException

  Thrown when the admission repository returns no results.

### InsertAdmission(Admission)

Inserts a new admission, preventing duplicates by NHC and optionally linking it to a Point of Care. When a Point of Care is assigned, its information is used to populate the patient location and, if free, it is reserved and associated with the newly created admission.

    public Task<Admission?> InsertAdmission(Admission admission)

#### Parameters

`admission` Admission

  The admission to insert, optionally including a PointOfCareId to associate with a care location.

#### Returns

Task<Admission>

  The newly inserted Admission, or `null` if no result is produced.

#### Exceptions

ConflictException

  Thrown when an admission with the same NHC already exists, or when the insertion fails to return a result.

NotFoundException

  Thrown when the specified Point of Care does not exist.

### ReturnPatientToAdmissions(ObjectId)

Returns a patient to the admissions workflow by creating a new admission record, removing any existing discharge, and archiving the patient. Validates that the patient and its associated unit exist before proceeding, and only builds the admission when a point of care is assigned.

    public Task ReturnPatientToAdmissions(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The identifier of the patient to be returned to admissions.

#### Returns

Task

### ReturnPatientToAdmissions(ObjectId, Admission)

Returns a patient to the admissions flow by creating a new admission record from the patient's existing data, removing any prior discharge, and archiving the patient.

    public Task ReturnPatientToAdmissions(ObjectId patientId, Admission adm)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient to be returned to admissions.

`adm` Admission

  The admission context used to resolve the unit and point of care for the new admission record.

#### Returns

Task

### SaveRequest(ApiRequest)

Processes an admission API request by performing the appropriate action based on the request type: inserts a new admission, updates an existing one, or deletes it. Required fields (Nhc, Origin, and Diagnosis) are validated before insert and update operations, and the method exits early when the admission or any required value is missing.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request containing the admission payload and the operation type to execute.

#### Returns

Task

### SaveRequestAsync(ApiRequest)

Asynchronously saves the specified API request by scheduling the underlying save operation on a background task.

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to persist.

#### Returns

Task

  A task that represents the asynchronous save operation.

### SearchByPatientNumberAndDistinctUnit(string, ObjectId)

Searches for a patient by patient number, enriching the current patient record with its point of care and unit name when available, and combines it with archived patient and admission lookups scoped to the specified unit.

    public Task<PatientSearch?> SearchByPatientNumberAndDistinctUnit(string patientNumber, ObjectId unitId)

#### Parameters

`patientNumber` string

  The unique patient number used as the primary search key.

`unitId` ObjectId

  The identifier of the unit used to filter the archived patient and admission searches.

#### Returns

Task<PatientSearch>

  A PatientSearch aggregating the current patient, archived patient, and admission data, including flags indicating whether the patient exists only in the archive and whether any of the three sources returned a result.

### UpdateAdmissionAsync(Admission)

Updates an existing admission record, enriching its patient location details from the linked point of care on both the prior and incoming states, and records the change via audit log and broadcast. If the admission is not found, the method returns without making changes; point of care lookups are only applied when a `PointOfCareId` is present and yields a result.

    public Task UpdateAdmissionAsync(Admission admission)

#### Parameters

`admission` Admission

  The admission entity containing the updated information to persist.

#### Returns

Task

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

Updates the master list option for admissions associated with the specified units, records an audit log entry for each modified admission, and broadcasts the updates.

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

#### Parameters

`opt` UpdateOptionMasterListDto

  The master list update options to apply to the matching admissions.

`unitList` IEnumerable<Unit>

  The collection of units whose admissions are affected by the update.

`typeName` string

  The name of the master list type being modified.

#### Returns

Task