# Class MasterListService<T>

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Provides a generic service implementation for managing MasterList entities of type `T`. Acts as the concrete implementation of the IMasterListService<T> contract for master list operations.

    public class MasterListService<T> : IMasterListService<T> where T : MasterList, new()

#### Type Parameters

`T`

  The type of master list entity managed by the service. Must derive from MasterList and expose a public parameterless constructor.

Inheritance
  object
  MasterListService<T>

Implements
  IMasterListService<T>

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

### MasterListService(ILogger<MasterListService<T>>, IServiceProvider, Lazy<IClientMessageService>, ISubscribersService, Lazy<IUnitService>, Lazy<IDisplayService>, Lazy<IPatientService>, Lazy<IDischargeService>, Lazy<IAdmissionService>, IOptions<ApiSettings>, IHttpContextAccessor, ILocalAuditService)

    public MasterListService(ILogger<MasterListService<T>> logger, IServiceProvider serviceProvider, Lazy<IClientMessageService> clientMessageService, ISubscribersService subscribersService, Lazy<IUnitService> unitService, Lazy<IDisplayService> displayService, Lazy<IPatientService> patientService, Lazy<IDischargeService> dischargeService, Lazy<IAdmissionService> admissionService, IOptions<ApiSettings> apiSettings, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService)

#### Parameters

`logger` ILogger<MasterListService<T>>
`serviceProvider` IServiceProvider
`clientMessageService` Lazy<IClientMessageService>
`subscribersService` ISubscribersService
`unitService` Lazy<IUnitService>
`displayService` Lazy<IDisplayService>
`patientService` Lazy<IPatientService>
`dischargeService` Lazy<IDischargeService>
`admissionService` Lazy<IAdmissionService>
`apiSettings` IOptions<ApiSettings>
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService

## Methods

### AddOptionToMasterList(ObjectId, FilterOptionListElement)

Adds a filter option to an existing master list, records the change in the audit log, and broadcasts the update to subscribed clients.

    public Task<OptionList?> AddOptionToMasterList(ObjectId id, FilterOptionListElement opt)

#### Parameters

`id` ObjectId

  The identifier of the master list to which the option will be added.

`opt` FilterOptionListElement

  The filter option element to add to the master list.

#### Returns

Task<OptionList>

  The resulting OptionList entry if the option is successfully added; `null` if the master list cannot be found, no result is produced, or an error is encountered during the operation.

### DeleteMasterListById(ObjectId)

Deletes a master list identified by the specified identifier. If the master list is not found, the operation is skipped and logged; if the master list is in use, a conflict exception is thrown.

    public Task DeleteMasterListById(ObjectId id)

#### Parameters

`id` ObjectId

  The identifier of the master list to delete.

#### Returns

Task

#### Exceptions

ConflictException

  Thrown when the master list is currently in use and cannot be deleted.

### DeleteMasterListOption(ObjectId, ObjectId, string)

Deletes an option from a master list by its identifier. On success, cascades the change to related patient item lists, creates an audit log entry, and broadcasts update and deletion events; returns `false` if the deletion fails, the master list or option is not found, or an error is logged.

    public Task<bool> DeleteMasterListOption(ObjectId id, ObjectId optId, string typeName)

#### Parameters

`id` ObjectId

  The identifier of the master list that contains the option.

`optId` ObjectId

  The identifier of the option to remove from the master list.

`typeName` string

  The name of the type used when cascading the deletion to related patient item lists.

#### Returns

Task<bool>

  A task that resolves to `true` when the option is successfully deleted and the side-effects are applied; otherwise, `false`.

### FindOptionItemById(ObjectId, ObjectId, LocaleEnum)

Retrieves an OptionList item that matches the specified master identifier, option identifier, and locale. Returns `null` and logs the error if the lookup fails.

    public Task<OptionList?> FindOptionItemById(ObjectId masterId, ObjectId optionId, LocaleEnum locale)

#### Parameters

`masterId` ObjectId

  The identifier of the master item that owns the option.

`optionId` ObjectId

  The identifier of the specific option to locate.

`locale` LocaleEnum

  The locale used to resolve the localized option item.

#### Returns

Task<OptionList>

  A Task<TResult> containing the matching OptionList, or `null` if no item is found or an error occurs.

### GetAllMasterList()

Retrieves all master list entries of the generic type from the underlying repository. If an exception occurs during retrieval, the error is logged and an empty collection is returned as a fallback.

    public Task<IEnumerable<T>> GetAllMasterList()

#### Returns

Task<IEnumerable<T>>

  A task that represents the asynchronous operation. The task result contains all retrieved entries, or an empty collection if an error occurs.

### GetAllMasterListCount()

Asynchronously retrieves the total count of all records in the master list for the current entity type. Returns 0 if an error occurs while accessing the repository.

    public Task<int> GetAllMasterListCount()

#### Returns

Task<int>

  A task that represents the asynchronous operation. The task result contains the total count of records, or 0 if an error was encountered.

### GetAllMasterListWithPaginatedOptions(PaginationFilter)

Retrieves all master list entries, maps each one to a paginated DTO enriched with its in-use status, and returns the resulting collection. Logs the error and returns an empty collection when the retrieval or mapping process fails.

    public Task<IEnumerable<MasterListWithPaginatedOptionsDto>> GetAllMasterListWithPaginatedOptions(PaginationFilter request)

#### Parameters

`request` PaginationFilter

  The pagination filter whose page size is applied when building each result entry.

#### Returns

Task<IEnumerable<MasterListWithPaginatedOptionsDto>>

  A task that yields an enumerable of MasterListWithPaginatedOptionsDto containing the mapped master lists, or an empty enumerable if an error occurs.

### GetAllMasterListWithoutOptions()

Retrieves all master list entries without applying any optional filters or parameters. If an error occurs, the exception is logged and an empty collection is returned as a fallback.

    public Task<IEnumerable<MasterListDto>> GetAllMasterListWithoutOptions()

#### Returns

Task<IEnumerable<MasterListDto>>

  A task that represents the asynchronous operation, containing a collection of MasterListDto items, or an empty collection if an error is encountered.

### GetAssociatedList(ObjectId, MasterListType, MasterListType)

Retrieves the identifier of a master list associated with a given unit, based on the relationship between two master list types. Looks up the unit that contains the first master list reference (`masterListType1`) and returns the identifier of the secondary associated list defined by `masterListType2`. Returns `null` when no unit is found for the given list, or when `masterListType2` does not map to a known associated list.

    public Task<ObjectId?> GetAssociatedList(ObjectId id, MasterListType masterListType1, MasterListType masterListType2)

#### Parameters

`id` ObjectId

  The identifier of the master list used to locate the associated unit.

`masterListType1` MasterListType

  The master list type used to find the unit (e.g., the primary list reference on the unit).

`masterListType2` MasterListType

  The master list type that determines which associated list identifier is returned from the resolved unit.

#### Returns

Task<ObjectId?>

  A task that yields the associated MongoDB.Bson.ObjectId when a matching list exists, or `null` when no unit is found or the type is not mapped.

### GetMasterListById(ObjectId, LocaleEnum?)

Retrieves a master list by its unique identifier, optionally filtered by locale. Logs and returns null if an error occurs during the lookup.

    public Task<T?> GetMasterListById(ObjectId id, LocaleEnum? locale)

#### Parameters

`id` ObjectId

  The unique identifier of the master list to retrieve.

`locale` LocaleEnum?

  The optional locale used to localize the retrieved master list.

#### Returns

Task<T>

  The master list of type `T` if found; otherwise, null.

### GetMasterListByIdAndSearchOptions(ObjectId, FilterOptionListElement)

Retrieves a master list of OptionList items from the repository using the specified identifier and search filter options.

    public Task<List<OptionList>> GetMasterListByIdAndSearchOptions(ObjectId id, FilterOptionListElement filterOption)

#### Parameters

`id` ObjectId

  The identifier used to locate the master list.

`filterOption` FilterOptionListElement

  The filter options applied to narrow the search within the master list.

#### Returns

Task<List<OptionList>>

  A task that represents the asynchronous operation, containing a list of OptionList items matching the given criteria.

### GetMasterListByIdAndTextSearch(ObjectId, string?)

Retrieves a master list of options filtered by the specified identifier and an optional text search term. Returns an empty list if an error occurs during retrieval.

    public Task<List<OptionList>> GetMasterListByIdAndTextSearch(ObjectId id, string? textSearch)

#### Parameters

`id` ObjectId

  The identifier used to locate the master list in the repository.

`textSearch` string

  The optional text search term used to filter the results; may be null.

#### Returns

Task<List<OptionList>>

  A task that represents the asynchronous operation, containing the list of OptionList entries that match the specified identifier and text search criteria.

### GetMasterListByIdWithPaginatedOptions(ObjectId, PaginationFilter)

Retrieves a master list by its identifier and returns it with paginated options, including whether the list is currently in use. Returns null when the master list is not found or when an error occurs during retrieval.

    public Task<MasterListWithPaginatedOptionsDto?> GetMasterListByIdWithPaginatedOptions(ObjectId id, PaginationFilter request)

#### Parameters

`id` ObjectId

  The unique identifier of the master list to retrieve.

`request` PaginationFilter

  The pagination filter that determines the page size for the returned options.

#### Returns

Task<MasterListWithPaginatedOptionsDto>

  A task that yields a MasterListWithPaginatedOptionsDto when the master list is found; otherwise, null.

### GetMasterListByName(string)

Retrieves a master list entry by its name. Returns the matching item, or null if no entry is found or if an error occurs while querying the repository.

    public Task<T?> GetMasterListByName(string name)

#### Parameters

`name` string

  The name of the master list entry to look up.

#### Returns

Task<T>

  A task that resolves to the matching item of type T, or null when no item is found or the lookup fails.

### GetMasterListOptionsNamesById(ObjectId)

Retrieves the names of all options associated with a master list identified by the specified id using the default locale. Returns an empty list if the master list is not found or if an error occurs while retrieving the data.

    public Task<List<string>> GetMasterListOptionsNamesById(ObjectId id)

#### Parameters

`id` ObjectId

  The identifier of the master list whose option names are to be retrieved.

#### Returns

Task<List<string>>

  A list of option names for the found master list, or an empty list if the list is not found or an error is encountered.

### GetOptionsOfList(ObjectId)

Retrieves the names of all options associated with the list identified by the specified identifier, using the default locale. Returns an empty list if no list is found for the given identifier.

    public Task<List<string>> GetOptionsOfList(ObjectId id)

#### Parameters

`id` ObjectId

  The identifier of the list whose options should be retrieved.

#### Returns

Task<List<string>>

  A list of option names belonging to the matching list, or an empty list if the list is not found.

### GetPaginatedMasterList(PaginationFilter)

Retrieves a paginated master list based on the specified pagination filter, applying skip and limit operations according to the page number and page size.

    public Task<PaginationResponse<T>> GetPaginatedMasterList(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

  The pagination filter containing the page number and page size used to determine the subset of records to return.

#### Returns

Task<PaginationResponse<T>>

  A task that represents the asynchronous operation. The task result contains a PaginationResponse<T> with the paginated data, the current page number, the page size, and the total document count.

### GetPaginatedMasterListWithPaginatedOptions(PaginationFilter, PaginationFilter)

Retrieves a paginated collection of master list items, each enriched with a paginated set of associated options and a usage indicator. Applies the supplied `listFilter` to paginate the master lists and the `optionFilter` to size the embedded options within each list DTO.

    public Task<PaginationResponse<MasterListWithPaginatedOptionsDto>> GetPaginatedMasterListWithPaginatedOptions(PaginationFilter listFilter, PaginationFilter optionFilter)

#### Parameters

`listFilter` PaginationFilter

  Pagination parameters (page number and page size) used to slice the master list result set.

`optionFilter` PaginationFilter

  Pagination parameters whose page size is applied to the options associated with each returned master list item.

#### Returns

Task<PaginationResponse<MasterListWithPaginatedOptionsDto>>

  A Task<TResult> containing a PaginationResponse<T> of MasterListWithPaginatedOptionsDto with the requested page of master lists, their paginated options, the current page metadata, and the total document count.

### GetPaginatedOptions(PaginationFilter, ObjectId)

Retrieves a paginated subset of options for the specified list, applying the page number and page size from the filter after fetching the full result set from the repository.

    public Task<PaginationResponse<OptionList>> GetPaginatedOptions(PaginationFilter filter, ObjectId listId)

#### Parameters

`filter` PaginationFilter

  The pagination filter that defines the page number and page size to apply to the results.

`listId` ObjectId

  The identifier of the list whose options are being retrieved.

#### Returns

Task<PaginationResponse<OptionList>>

  A PaginationResponse<T> containing the requested page of options, the current page metadata, and the total count of available options.

### InsertMasterList(T)

Inserts a new master list item, broadcasts the change to subscribers, and records an audit log entry for the operation.

    public Task<T?> InsertMasterList(T item)

#### Parameters

`item` T

  The master list entity to insert.

#### Returns

Task<T>

  The inserted entity retrieved from the repository, or `null` if the operation fails.

### RemoveMasterListOption(ObjectId, OptionList)

Removes the specified option from a master list identified by its id, auditing the change and broadcasting the update and deleted item to subscribers.

    public Task<bool> RemoveMasterListOption(ObjectId id, OptionList oldOpt)

#### Parameters

`id` ObjectId

  The identifier of the master list from which the option will be removed.

`oldOpt` OptionList

  The option to remove from the master list.

#### Returns

Task<bool>

  A task that resolves to `true` if the option was successfully removed; `false` if the operation failed or an error occurred.

### SaveRequest(ApiRequest)

Saves the specified API request.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to save.

#### Returns

Task

### SaveRequestAsync(ApiRequest)

Asynchronously saves the specified API request by offloading the save operation to a background task.

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to be saved.

#### Returns

Task

### UpdateFullMasterListOption(ObjectId, OptionList, string)

Updates a full master list option, records the change in the audit log, notifies subscribers via broadcast, and propagates the update to the associated patient item list. Returns `null` if the underlying repository update fails or an exception is encountered, in which case the error is logged. Uses the default locale when retrieving the updated master list for audit purposes (locale handling is pending).

    public Task<OptionList?> UpdateFullMasterListOption(ObjectId id, OptionList opt, string typeName)

#### Parameters

`id` ObjectId

  The identifier of the master list that contains the option to update.

`opt` OptionList

  The option with the new values to apply to the master list.

`typeName` string

  The name of the entity type used when updating the related patient item list.

#### Returns

Task<OptionList>

  The updated OptionList when the operation succeeds; `null` when the update fails or an error is caught.

### UpdateMasterList(T)

Updates an existing item in the master list, broadcasts the update operation, and creates an audit log entry comparing the old and new state. Returns `null` if the update fails, logging the error internally.

    public Task<T?> UpdateMasterList(T item)

#### Parameters

`item` T

  The item to update in the master list.

#### Returns

Task<T>

  The updated item retrieved from the repository, or `null` if an error occurred during the update.

### UpdateMasterListDescription(ObjectId, string)

Updates the description of a master list identified by the specified id, creating an audit log entry and broadcasting the change when the update succeeds.

    public Task<bool> UpdateMasterListDescription(ObjectId id, string name)

#### Parameters

`id` ObjectId

  The identifier of the master list to update.

`name` string

  The new description to apply to the master list.

#### Returns

Task<bool>

  A task that resolves to `true` if the update succeeded; otherwise, `false` when the update fails or an exception is caught and logged.

### UpdateMasterListName(ObjectId, string)

Updates the name of an existing master list identified by the specified id, records an audit log entry for the change, and broadcasts the update to subscribers. Returns `false` if the update fails or an exception is encountered, in which case the error is logged.

    public Task<bool> UpdateMasterListName(ObjectId id, string name)

#### Parameters

`id` ObjectId

  The identifier of the master list to rename.

`name` string

  The new name to assign to the master list.

#### Returns

Task<bool>

  A task that resolves to `true` if the master list name was updated successfully; otherwise, `false`.

### UpdateMasterListOption(ObjectId, OptionList, string)

Updates a master list option, performing audit logging, broadcasting the change, and updating the patient item list when successful. Returns `null` if the option cannot be found or an error occurs.

    public Task<OptionList?> UpdateMasterListOption(ObjectId id, OptionList opt, string typeName)

#### Parameters

`id` ObjectId

  The identifier of the master list containing the option to update.

`opt` OptionList

  The option containing the new values to apply.

`typeName` string

  The name of the type used when updating the associated patient item list.

#### Returns

Task<OptionList>

  The updated OptionList if the operation succeeds, or `null` if the master list is not found or an exception is thrown.

### UpdateMasterListOption(ObjectId, OptionList, string, LocaleEnum)

Updates an option within the master list for the specified locale, and when the update succeeds, creates an audit log entry, broadcasts the change, and propagates the update to the affected patient item list. Returns `null` if the update fails, the result is `null`, or an exception is thrown during processing.

    public Task<OptionList?> UpdateMasterListOption(ObjectId id, OptionList opt, string typeName, LocaleEnum locale)

#### Parameters

`id` ObjectId

  The identifier of the master list whose option will be updated.

`opt` OptionList

  The option to be applied to the master list.

`typeName` string

  The type name used when propagating the update to the patient item list.

`locale` LocaleEnum

  The locale used for the master list option update and retrieval of the updated entity for auditing.

#### Returns

Task<OptionList>

  The updated OptionList, or `null` if the update failed, the result was `null`, or an exception occurred.

### UpdateOptionDetailsToMasterList(ObjectId, UpdateMasterListDetailsDto)

Updates the option details of a master list identified by the given identifier, records an audit log entry for the change, and broadcasts the update. Returns the updated details, or `null` if the update fails or an error occurs.

    public Task<UpdateMasterListDetailsDto?> UpdateOptionDetailsToMasterList(ObjectId id, UpdateMasterListDetailsDto opt)

#### Parameters

`id` ObjectId

  The identifier of the master list whose option details will be updated.

`opt` UpdateMasterListDetailsDto

  The new option details to apply to the master list.

#### Returns

Task<UpdateMasterListDetailsDto>

  A task that yields the updated UpdateMasterListDetailsDto on success, or `null` when the update fails or an exception is caught and logged.