# Class ConfigObservationService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Provides a concrete implementation of the IConfigObservationService contract for observing configuration state.

    public class ConfigObservationService : IConfigObservationService

Inheritance
  object
  ConfigObservationService

Implements
  IConfigObservationService

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

Acts as the default service type that fulfills the configuration observation interface.

## Constructors

### ConfigObservationService(IConfigObservationRepository, IOptions<ApiSettings>, IOptions<CacheSettings>, ILogger<ConfigObservationService>, IUnitService, IHttpContextAccessor, ILocalAuditService, ICacheService)

    public ConfigObservationService(IConfigObservationRepository configObservationRepository, IOptions<ApiSettings> apiSettings, IOptions<CacheSettings> cacheSettings, ILogger<ConfigObservationService> logger, IUnitService unitService, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, ICacheService cacheService)

#### Parameters

`configObservationRepository` IConfigObservationRepository
`apiSettings` IOptions<ApiSettings>
`cacheSettings` IOptions<CacheSettings>
`logger` ILogger<ConfigObservationService>
`unitService` IUnitService
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`cacheService` ICacheService

## Methods

### CreateConfig(ConfigObservation)

Creates a new configuration observation after verifying that no existing record shares the same identifier.

    public Task<ConfigObservation?> CreateConfig(ConfigObservation configObservation)

#### Parameters

`configObservation` ConfigObservation

  The configuration observation entity to persist.

#### Returns

Task<ConfigObservation>

  The created ConfigObservation if the operation succeeds.

#### Exceptions

BadRequestException

  Thrown when a configuration observation with the same identifier already exists.

### DeleteSingleConfigObservationItem(ConfigObservation)

Deletes a single configuration observation item, invalidating the related cache entries and recording an audit log of the operation. Throws a conflict exception if the item does not exist or the delete operation cannot be completed.

    public Task<bool> DeleteSingleConfigObservationItem(ConfigObservation configObservationItem)

#### Parameters

`configObservationItem` ConfigObservation

  The configuration observation item to delete; its identifier is used to locate the existing record.

#### Returns

Task<bool>

  A task that resolves to `true` when the item is successfully deleted.

#### Exceptions

ConflictException

  Thrown when no configuration observation is found with the specified identifier, or when the underlying delete operation fails.

### Get(string?)

Retrieves a ConfigObservation by its name, returning `null` when the name is not provided or the configuration is not found.

    public Task<ConfigObservation?> Get(string? name)

#### Parameters

`name` string

  The name of the configuration to look up; if null or empty, the method returns `null`.

#### Returns

Task<ConfigObservation>

  A ConfigObservation when a matching configuration is found and successfully processed; otherwise, `null`.

### GetAllCompact()

Retrieves a compact representation of all configuration observations by returning the total item count.

    public Task<ConfigObservationDto> GetAllCompact()

#### Returns

Task<ConfigObservationDto>

  A ConfigObservationDto containing the total number of configuration observations.

### GetAllConfigs(CancellationToken)

Retrieves all configuration observations using a cache-aside strategy. If no cached value exists, the data is fetched from the repository and cached using the key and TTL determined by the cache settings.

    public Task<ICollection<ConfigObservation>> GetAllConfigs(CancellationToken ct = default)

#### Parameters

`ct` CancellationToken

  A token to monitor for cancellation requests.

#### Returns

Task<ICollection<ConfigObservation>>

  A collection of all ConfigObservation entries, sourced from cache when available or from the repository otherwise.

### GetByCodeSysAndCode(string?, string?)

Retrieves a ConfigObservation that matches the specified coding system and code, processing it before returning. If no matching observation is found, a warning is logged and null is returned.

    public Task<ConfigObservation?> GetByCodeSysAndCode(string? codingSystem, string? code)

#### Parameters

`codingSystem` string

  The coding system identifier used to filter the observation. May be null.

`code` string

  The code value used to filter the observation. May be null.

#### Returns

Task<ConfigObservation>

  A processed ConfigObservation if a match is found; otherwise, null.

### GetConfig(ObjectId)

Retrieves a configuration observation by its identifier, using an in-memory cache with a configurable refresh timeout to reduce repository calls. Returns the cached value when available and not yet expired; otherwise fetches from the repository and caches the result, falling back to a new empty ConfigObservation when the repository does not find a matching record.

    public Task<ConfigObservation?> GetConfig(ObjectId configObservationId)

#### Parameters

`configObservationId` ObjectId

  The unique identifier of the configuration observation to retrieve.

#### Returns

Task<ConfigObservation>

  The configuration observation obtained from cache or repository, or a new empty instance when no matching record exists.

### GetConfigById(ObjectId)

Retrieves a configuration observation by its unique identifier from the repository. Returns null if no matching configuration observation is found.

    public Task<ConfigObservation?> GetConfigById(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the configuration observation to retrieve.

#### Returns

Task<ConfigObservation>

  The configuration observation matching the specified identifier, or null if not found.

### GetConfigByName(string)

Retrieves a configuration observation by its name, using a time-limited in-memory cache before falling back to the repository. Returns `null` if `name` is null, empty, or whitespace, or if no matching configuration exists in the cache or repository. Cache hits require a non-expired `NextRefresh` and use a case-insensitive name comparison.

    public Task<ConfigObservation?> GetConfigByName(string name)

#### Parameters

`name` string

  The case-insensitive name of the configuration observation to look up.

#### Returns

Task<ConfigObservation>

  The matching ConfigObservation, or `null` if not found or the name is invalid.

### GetConfigNames()

Retrieves the list of configuration names from the configuration observation repository.

    public Task<List<string>> GetConfigNames()

#### Returns

Task<List<string>>

  A task that represents the asynchronous operation. The task result contains a list of configuration names.

### GetConfigNames(string)

Retrieves the list of configuration names associated with the specified identifier by delegating to the configuration observation repository.

    public Task<List<string>> GetConfigNames(string id)

#### Parameters

`id` string

  The identifier used to look up the related configuration names.

#### Returns

Task<List<string>>

  A task that represents the asynchronous operation, containing the list of configuration names matching the given identifier.

### GetConfigObservationItem(string)

Retrieves configuration observation items matching the specified name. Returns the matching items if any are found; otherwise logs a warning and throws a NotFoundException.

    public Task<IEnumerable<ConfigObservation>?> GetConfigObservationItem(string name)

#### Parameters

`name` string

  The name used to look up the configuration observation items.

#### Returns

Task<IEnumerable<ConfigObservation>>

  A collection of ConfigObservation items matching the specified name.

#### Exceptions

NotFoundException

  Thrown when no configuration observation items are found for the given name.

### GetConfigObservationItemsByName(string)

Retrieves configuration observation items that match the specified name.

    public Task<IEnumerable<ConfigObservation>> GetConfigObservationItemsByName(string name)

#### Parameters

`name` string

  The name used to filter configuration observation items.

#### Returns

Task<IEnumerable<ConfigObservation>>

  A collection of ConfigObservation items matching the specified name.

### GetPaginatedItems(PaginationFilter)

Retrieves a paginated list of ConfigObservation items from the repository along with the total count, used to build pagination metadata for the response.

    public Task<PaginationResponse<ConfigObservation>> GetPaginatedItems(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

  The pagination filter containing the requested page number and page size used to retrieve the items and populate the response metadata.

#### Returns

Task<PaginationResponse<ConfigObservation>>

  A PaginationResponse<T> containing the items for the requested page and the total count of all available items.

### GetSingleConfigObservationItem(string?, string?, string?, string?)

Retrieves a single ConfigObservation item that matches the specified code, coding system, name, and original name. Throws a not-found exception when no matching item exists in the repository.

    public Task<ConfigObservation?> GetSingleConfigObservationItem(string? code, string? codingSystem, string? name, string? originalName)

#### Parameters

`code` string

  The code used to identify the configuration observation item.

`codingSystem` string

  The coding system associated with the item.

`name` string

  The name of the configuration observation item.

`originalName` string

  The original name of the configuration observation item.

#### Returns

Task<ConfigObservation>

  The matching ConfigObservation item.

#### Exceptions

NotFoundException

  Thrown when no matching configuration observation item is found.

### Get<T>(T, bool)

Retrieves a ConfigObservation that matches the supplied patient observation, either by name only or by a combination of code, coding system, and parent data fields.

    public Task<ConfigObservation?> Get<T>(T obs, bool onlyByName = false) where T : BasePatientObservation

#### Parameters

`obs` T

  The patient observation whose matching configuration should be resolved.

`onlyByName` bool

  When `true`, the lookup is restricted to matching by Name only; otherwise matching also considers code, coding system, and parent observation data.

#### Returns

Task<ConfigObservation>

  A Task<TResult> containing the matched ConfigObservation processed via `Process`, or `null` if no configuration items are available or no match is found.

#### Type Parameters

`T`

### GroupedObservationStatus(GroupedField, Result, string, object, double?, double?)

Resolves the status of a grouped patient observation by retrieving the configuration for the given name and mapping the observation through group-specific, result-specific, or default configuration. Returns Ok when no configuration exists for the name or when the mapping does not produce a status.

    public Task<StatusEnum.Type> GroupedObservationStatus(GroupedField groupedField, GroupedObservationEnum.Result result, string name, object value, double? min, double? max)

#### Parameters

`groupedField` GroupedField

  The grouped field whose `Group` key is used to look up group-specific configuration.

`result` GroupedObservationEnum.Result

  The grouped observation result whose name is used to look up result-specific configuration.

`name` string

  The observation name used to retrieve the configuration.

`value` object

  The observation value included in the mapping.

`min` double?

  The optional minimum reference value included in the mapping.

`max` double?

  The optional maximum reference value included in the mapping.

#### Returns

Task<StatusEnum.Type>

  A task that resolves to the StatusEnum.Type computed from the mapped observation, or Ok when no applicable configuration or mapping status is found.

### Map(PatientTreatment)

Maps a PatientTreatment by resolving the configuration observation for each of its requested give codes. Looks up the configuration by text when both the coding system and identifier are empty, otherwise by coding system and identifier. Returns `null` when the configuration is unknown and unknown treatments are ignored, or when the resolved configuration has no name; otherwise returns the original treatment.

    public Task<PatientTreatment?> Map(PatientTreatment treatment)

#### Parameters

`treatment` PatientTreatment

  The patient treatment whose requested give codes are resolved against the configuration store.

#### Returns

Task<PatientTreatment>

  The original PatientTreatment if a valid configuration is found, or `null` when the treatment should be discarded.

### Map<T>(T, bool)

Maps a patient observation to a configured representation by looking up its corresponding configuration and applying the mapping. When no matching configuration is found, returns `null` if unknown observations should be ignored, or the original observation otherwise. If the resolved configuration has an empty name, the mapping is aborted and `null` is returned.

    public Task<T?> Map<T>(T obs, bool onlyByName = false) where T : BasePatientObservation

#### Parameters

`obs` T

  The patient observation to be mapped.

`onlyByName` bool

  If `true`, the configuration lookup is performed by name only; otherwise the full lookup is used.

#### Returns

Task<T>

  The mapped observation, the original observation when unknown observations are allowed, or `null` when mapping is ignored or the configuration is invalid.

#### Type Parameters

`T`

### RemoveConfigItem(ObjectId)

Removes a configuration observation item by its identifier. Returns `null` when the item does not exist, otherwise deletes it from the repository and invalidates the cached collection of configuration observations.

    public Task<ConfigObservation?> RemoveConfigItem(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the configuration observation to remove.

#### Returns

Task<ConfigObservation>

  The removed ConfigObservation if it was found and deleted; otherwise, `null`.

### RemoveConfigItem(string)

Removes a configuration item identified by its name, creating an audit log entry prior to deletion and invalidating the related cache. Throws a conflict exception when no configuration item with the specified name is found.

    public Task<ConfigObservation?> RemoveConfigItem(string itemName)

#### Parameters

`itemName` string

  The name of the configuration item to remove.

#### Returns

Task<ConfigObservation>

  The removed ConfigObservation, or `null` if the repository did not return a result.

#### Exceptions

ConflictException

  Thrown when no configuration item is found with the specified name.

### RetentionActions<T>(T)

Determines the retention action to apply for a patient observation by resolving its configured retention policy. Falls back to a "NoDelete" retention policy with no value when the observation has no associated configuration or its retention policy is null.

    public Task<ObservatitonRetentionResult?> RetentionActions<T>(T obs) where T : BasePatientObservation

#### Parameters

`obs` T

  The patient observation for which the retention action is being evaluated.

#### Returns

Task<ObservatitonRetentionResult>

  A task containing the resolved ObservatitonRetentionResult, or null when no configuration is available.

#### Type Parameters

`T`

  The patient observation type, constrained to BasePatientObservation.

### UpdateConfig(ConfigObservation)

Updates an existing ConfigObservation identified by its id and returns the updated entity. Throws a not found exception when the configuration observation does not exist, invalidates the related cache entries, and records an audit log of the change.

    public Task<ConfigObservation?> UpdateConfig(ConfigObservation configObservationItem)

#### Parameters

`configObservationItem` ConfigObservation

  The configuration observation payload containing the identifier of the record to update.

#### Returns

Task<ConfigObservation>

  The updated ConfigObservation.

#### Exceptions

NotFoundException

  Thrown when no configuration observation exists for the supplied id.