# Class PumpService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Servicio maestro de gestión de bombas

- Procesa ApiRequest (HL7/Alaris transformado)
- Histórico clínico (pump_observations)
- Histórico de alarmas (pump_alarm_events)
- Alarmas activas (pump_alarm_state)
- Snapshot (pump_state)
- Broadcast de snapshots (PumpState + PumpAlarmState)

    public class PumpService : IPumpService, IApiRequestService

Inheritance
  object
  PumpService

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

### PumpService(IPumpObservationRepository, IPumpStateRepository, IPumpAlarmEventRepository, IPumpAlarmStateRepository, IPumpArchiveRepository, IPatientService, IConfigPumpsService, IOptions<ApiSettings>, ILogger<PumpService>, ISubscribersService, IClientMessageService, Lazy<ICalculatedObservationsService>, IHttpContextAccessor, ILocalAuditService, IConfigUnitsService)

Servicio maestro de gestión de bombas

- Procesa ApiRequest (HL7/Alaris transformado)
- Histórico clínico (pump_observations)
- Histórico de alarmas (pump_alarm_events)
- Alarmas activas (pump_alarm_state)
- Snapshot (pump_state)
- Broadcast de snapshots (PumpState + PumpAlarmState)

    public PumpService(IPumpObservationRepository pumpObservationRepository, IPumpStateRepository pumpStateRepo, IPumpAlarmEventRepository alarmEventRepo, IPumpAlarmStateRepository alarmStateRepo, IPumpArchiveRepository pumpArchiveRepo, IPatientService patientService, IConfigPumpsService configPumpsService, IOptions<ApiSettings> apiSettings, ILogger<PumpService> logger, ISubscribersService subscribersService, IClientMessageService clientMessageService, Lazy<ICalculatedObservationsService> calculatedObservationsService, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, IConfigUnitsService configUnitsService)

#### Parameters

`pumpObservationRepository` IPumpObservationRepository
`pumpStateRepo` IPumpStateRepository
`alarmEventRepo` IPumpAlarmEventRepository
`alarmStateRepo` IPumpAlarmStateRepository
`pumpArchiveRepo` IPumpArchiveRepository
`patientService` IPatientService
`configPumpsService` IConfigPumpsService
`apiSettings` IOptions<ApiSettings>
`logger` ILogger<PumpService>
`subscribersService` ISubscribersService
`clientMessageService` IClientMessageService
`calculatedObservationsService` Lazy<ICalculatedObservationsService>
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`configUnitsService` IConfigUnitsService

## Methods

### Archive(Patient)

Archives the specified patient by delegating the operation to the archive routine identified by the patient's identifier.

    public Task Archive(Patient patient)

#### Parameters

`patient` Patient

  The patient to be archived.

#### Returns

Task

### ArchiveByPatientId(ObjectId)

Archives all pump observations associated with the specified patient by moving them to the archive repository and then deleting the active data. If no pump observations are found for the patient, the archive insertion is skipped while the deletion of active data still proceeds.

    public Task ArchiveByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient whose pump observations should be archived.

#### Returns

Task

### DeleteByPatientId(ObjectId)

Asynchronously deletes all pump observation, alarm event, and alarm state data associated with the specified patient identifier.

    public Task DeleteByPatientId(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient whose related pump data should be removed.

#### Returns

Task

  A task that represents the asynchronous deletion of the patient's data across the pump observation, alarm event, and alarm state repositories.

### DeletePumpConfig(ConfigPumps)

Deletes the specified pump configuration and records an audit log entry when the operation succeeds.

    public Task<bool> DeletePumpConfig(ConfigPumps config)

#### Parameters

`config` ConfigPumps

  The pump configuration to delete.

#### Returns

Task<bool>

  A task that represents the asynchronous operation, containing a value indicating whether the deletion was successful.

#### Exceptions

ConflictException

  Thrown when the underlying deletion operation fails.

### FindAllLastPatientObservationTime()

Asynchronously retrieves the most recent pump observation timestamp for every patient by delegating to the pump observation repository.

    public Task<Dictionary<ObjectId, DateTime>> FindAllLastPatientObservationTime()

#### Returns

Task<Dictionary<ObjectId, DateTime>>

  A task that resolves to a dictionary mapping each patient's MongoDB.Bson.ObjectId to the DateTime of their last pump observation.

### FindLastPumpObservations(ObjectId, int)

Retrieves the most recent pump observations for a specified patient, returning up to the requested number of records ordered by time in descending order. Returns an empty list if the repository result is not a list of pump observations or if no observations exist for the patient.

    public Task<List<PumpObservation>> FindLastPumpObservations(ObjectId patientId, int num = 1)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose pump observations are being retrieved.

`num` int

  The maximum number of recent pump observations to return. Defaults to 1.

#### Returns

Task<List<PumpObservation>>

  A task that resolves to a list of the most recent PumpObservation records for the patient, or an empty list when none are available.

### GetAllPumpConfig()

Retrieves all available pump configurations from the configuration service. Throws a NotFoundException if the service returns no data, indicating that the pump configuration resource is missing.

    public Task<List<ConfigPumps>?> GetAllPumpConfig()

#### Returns

Task<List<ConfigPumps>>

  A task that represents the asynchronous operation, containing a list of ConfigPumps configurations.

#### Exceptions

NotFoundException

  Thrown when the underlying service returns a null result, meaning the pump configuration resource was not found.

### GetItemsById(string)

Retrieves a list of configuration pump items associated with the specified identifier by delegating to the configuration pumps service.

    public Task<List<ConfigPumpItem>?> GetItemsById(string id)

#### Parameters

`id` string

  The identifier used to look up the configuration pump items.

#### Returns

Task<List<ConfigPumpItem>>

  A task that represents the asynchronous operation. The task result is a list of ConfigPumpItem matching the given identifier, or `null` if no items are found.

### GetPaginatedPump(PaginationFilter)

Retrieves a paginated collection of PumpObservation records based on the supplied filter. When a `PatientId` is provided, the patient identifier is validated and results are filtered by an optional date range; when a `DeviceId` is provided instead, the repository query already applies the date range. If no filter criteria or only invalid input is supplied, an empty paged response is returned and the total count is reported as zero. Results are ordered by observation time in descending order before pagination is applied.

    public Task<PaginationResponse<PumpObservation>?> GetPaginatedPump(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

  The pagination and filter criteria, including page number, page size, optional patient identifier, device identifier, and date range.

#### Returns

Task<PaginationResponse<PumpObservation>>

  A task that yields the paginated response of pump observations, or `null` if the operation cannot be performed.

### GetPumpConfigsById(string)

Retrieves the pump configuration that matches the specified identifier. Throws a not-found exception when no matching configuration exists in the underlying service.

    public Task<ConfigPumps?> GetPumpConfigsById(string id)

#### Parameters

`id` string

  The unique identifier of the pump configuration to retrieve.

#### Returns

Task<ConfigPumps>

  The matching ConfigPumps instance, or `null` if the service returns one; otherwise a NotFoundException is thrown.

#### Exceptions

NotFoundException

  Thrown when the configuration service returns `null`, indicating that the requested resource is missing.

### InsertPumpConfig(ConfigPumps)

Inserts a new pump configuration after recording an audit log entry. Throws a conflict exception if the underlying service fails to create the configuration.

    public Task<ConfigPumps?> InsertPumpConfig(ConfigPumps config)

#### Parameters

`config` ConfigPumps

  The pump configuration to insert.

#### Returns

Task<ConfigPumps>

  The newly inserted ConfigPumps, or null if the operation yields no result.

#### Exceptions

ConflictException

  Thrown when the pump configuration could not be created by the underlying service.

### InsertPumpObservation(PumpObservation)

Inserts a pump observation into the repository, applying default values for missing identifier and timestamp, and triggers post-insertion side effects such as audit logging, state updates, snapshot broadcasting, and retention actions. If the mapped observation is null, no further action is taken. Observations with a number of zero are skipped from post-insertion processing when zero-value emissions are disabled.

    public Task InsertPumpObservation(PumpObservation obs)

#### Parameters

`obs` PumpObservation

  The pump observation to insert. Its `Id` is generated if not set, and its `Time` defaults to UTC now if set to MinValue.

#### Returns

Task

### MapPumpObservation(PumpObservation)

Maps a PumpObservation through the configuration pumps and configuration units services in sequence, and additionally evaluates it against the calculated observations service to determine whether the mapping should be ignored (logged as debug when the result is null). Returns the observation produced after the configuration units mapping stage.

    public Task<PumpObservation?> MapPumpObservation(PumpObservation obs)

#### Parameters

`obs` PumpObservation

  The pump observation to be transformed through the mapping pipeline.

#### Returns

Task<PumpObservation>

  A task that yields the mapped PumpObservation, or `null` when the calculated observations stage produces no result.

### SaveRequest(ApiRequest)

Processes an incoming ApiRequest by normalizing its pump observations, resolving or creating the associated patient, and handling each observation according to its message type (HL7 PCD-01/04/10 or AlarisPump). For each observation, it dispatches to the appropriate observation or alarm pipeline, updates the pump state snapshot, broadcasts the resulting snapshots with any active device alarms, and applies retention rules on the historical observations. AlarisPump observations without a PatientId are discarded, and per-observation processing errors are logged without aborting the whole request.

    public Task SaveRequest(ApiRequest req)

#### Parameters

`req` ApiRequest

  The API request containing the pump observations and message type to be persisted and broadcast.

#### Returns

Task

### SaveRequestAsync(ApiRequest)

Asynchronously saves an API request by delegating to the underlying synchronous save operation.

    public Task SaveRequestAsync(ApiRequest req)

#### Parameters

`req` ApiRequest

  The API request to be saved.

#### Returns

Task

  A task that represents the asynchronous save operation.

### UpdateManyObjectId(string, ObjectId, ObjectId)

Updates the ObjectId of pump observations and related alarms from an old identifier to a new one for the specified field, logging the operation and recording an audit entry.

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

#### Parameters

`nameId` string

  The name of the field whose ObjectId should be updated.

`id` ObjectId

  The new ObjectId to assign.

`oldId` ObjectId

  The existing ObjectId to be replaced.

#### Returns

Task

#### Exceptions

ArgumentException

  Thrown when `nameId` is null, empty, or whitespace.

### UpdatePumpConfig(ConfigPumps)

Updates the pump configuration identified by `config`, recording an audit log entry that captures the previous configuration and the new values before applying the change.

    public Task<ConfigPumps?> UpdatePumpConfig(ConfigPumps config)

#### Parameters

`config` ConfigPumps

  The pump configuration containing the updated values to persist.

#### Returns

Task<ConfigPumps>

  The updated ConfigPumps configuration if the update succeeds.

#### Exceptions

NotFoundException

  Thrown when the underlying update operation does not return a configuration, indicating the resource is missing.