# Class TreatmentRepository

Namespace
  adas_core.Infrastructure.Repositories

Assembly
  adas-core.Infrastructure.dll

Represents a MongoDB-backed repository for PatientTreatment entities, inheriting common data access functionality from MongoRepository<T> and implementing the ITreatmentRepository contract.

    public class TreatmentRepository : MongoRepository<PatientTreatment>, ITreatmentRepository, IMongoRepository<PatientTreatment>

Inheritance
  object
  MongoRepository<PatientTreatment>
  TreatmentRepository

Implements
  ITreatmentRepository
  IMongoRepository<PatientTreatment>

Inherited Members
  MongoRepository<PatientTreatment>.Db
  MongoRepository<PatientTreatment>.Collection
  MongoRepository<PatientTreatment>.UpdateOneAsync(ObjectId, PatientTreatment)
  MongoRepository<PatientTreatment>.InsertInitialLoad()
  MongoRepository<PatientTreatment>.InsertManyAsync(List<PatientTreatment>)
  MongoRepository<PatientTreatment>.UpdateManyObjectIdAsync(string, ObjectId, ObjectId)
  MongoRepository<PatientTreatment>.DeleteAsync(string)
  MongoRepository<PatientTreatment>.CollectionExists(string)
  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

### TreatmentRepository(IOptions<ApiSettings>, IMongoDatabase)

    public TreatmentRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database)

#### Parameters

`apiSettings` IOptions<ApiSettings>
`database` IMongoDatabase

## Methods

### CreateIndexes()

Ensures the required MongoDB indexes exist for the PatientTreatment collection, creating a non-unique background index on the `patientid` field to optimize query performance without blocking other database operations.

    public override Task CreateIndexes()

#### Returns

Task

### DeleteAsync(ObjectId)

Deletes a patient treatment record from the database by its unique identifier.

    public Task DeleteAsync(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient treatment to remove.

#### Returns

Task

### DeleteByPatientId(ObjectId)

Deletes all PatientTreatment records associated with the specified patient identifier. Returns `true` when the delete operation completes, and `false` if an exception is encountered, in which case the error is logged.

    public Task<bool> DeleteByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The identifier of the patient whose treatment records should be removed.

#### Returns

Task<bool>

  A task that resolves to `true` on successful deletion, or `false` if the operation failed due to an exception.

### FindBolusTreatments(ObjectId)

Retrieves all bolus treatments associated with the specified patient, filtering for records that have at least one entry in their `RequestedGiveCodesStatus` array.

    public Task<List<PatientTreatment>> FindBolusTreatments(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose bolus treatments are being queried.

#### Returns

Task<List<PatientTreatment>>

  A task that represents the asynchronous operation, containing a list of PatientTreatment documents matching the patient and having a non-empty `RequestedGiveCodesStatus`.

### FindByPatientId(ObjectId)

Asynchronously retrieves all patient treatment records associated with the specified patient identifier from the underlying data store.

    public Task<IEnumerable<PatientTreatment>> FindByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique MongoDB.Bson.ObjectId of the patient whose treatments are being queried.

#### Returns

Task<IEnumerable<PatientTreatment>>

  A task that represents the asynchronous operation, containing an IEnumerable<T> with the matching treatment records. Returns an empty sequence if no treatments are found.

### FindByPatientIdAsync(ObjectId)

Retrieves all patient treatment records associated with the specified patient identifier from the MongoDB collection.

    public Task<IAsyncCursor<PatientTreatment>> FindByPatientIdAsync(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique ObjectId of the patient whose treatment records should be returned.

#### Returns

Task<IAsyncCursor<PatientTreatment>>

  An MongoDB.Driver.IAsyncCursor<TDocument> of PatientTreatment containing the matching treatment records.

### GetActiveTreatmentsByPatientIdAndOrder(ObjectId, string)

Retrieves the list of patient treatments associated with the specified patient and placer order identifier, limited to treatments whose order control is set to New (Nw) or Xo.

    public Task<List<PatientTreatment>> GetActiveTreatmentsByPatientIdAndOrder(ObjectId patientId, string order)

#### Parameters

`patientId` ObjectId

  The identifier of the patient whose treatments will be searched.

`order` string

  The entity identifier of the placer order used to match treatments.

#### Returns

Task<List<PatientTreatment>>

  A task that represents the asynchronous operation. The task result contains the list of matching PatientTreatment entries.

### GetById(ObjectId)

Retrieves a patient treatment record from the database that matches the specified identifier.

    public Task<IAsyncCursor<PatientTreatment>> GetById(ObjectId id)

#### Parameters

`id` ObjectId

  The unique identifier of the patient treatment to locate.

#### Returns

Task<IAsyncCursor<PatientTreatment>>

  An asynchronous cursor containing the patient treatment matching the provided identifier.

### GetByPatientId(ObjectId)

Retrieves all patient treatment records associated with the specified patient identifier.

    public Task<IEnumerable<PatientTreatment>> GetByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose treatments are being queried.

#### Returns

Task<IEnumerable<PatientTreatment>>

  A collection of PatientTreatment records matching the specified patient identifier.

### GetCollectionName()

Retrieves the collection name for patients treatments, using the value configured in API settings or falling back to the default "patients_treatments" when the setting is not specified.

    public override string GetCollectionName()

#### Returns

string

  The configured patients treatments collection name, or "patients_treatments" if no setting is defined.

### GetPaginatedTreatments(PaginationFilter)

Retrieves a paginated, fluent queryable collection of patient treatments based on the provided pagination filter.

    public IFindFluent<PatientTreatment, PatientTreatment> GetPaginatedTreatments(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

  The pagination filter containing page size, page number, and other pagination criteria.

#### Returns

IFindFluent<PatientTreatment, PatientTreatment>

  An MongoDB.Driver.IFindFluent<TDocument, TProjection> for PatientTreatment that supports further fluent query composition and pagination.

### InsertOneAsync(PatientTreatment)

Inserts a patient treatment record, defaulting the order time to the current UTC time when it is not already set.

    public override Task InsertOneAsync(PatientTreatment treatment)

#### Parameters

`treatment` PatientTreatment

  The patient treatment to insert.

#### Returns

Task

### Update(PatientTreatment)

Updates an existing patient treatment record asynchronously, returning a boolean indicating success or failure. If the update operation throws an exception, the error is logged and the method returns false instead of propagating the exception.

    public Task<bool> Update(PatientTreatment treatment)

#### Parameters

`treatment` PatientTreatment

  The patient treatment entity containing the updated information, identified by its `Id`.

#### Returns

Task<bool>

  `true` if the update succeeds; otherwise, `false` if an exception occurs during the operation.

### UpdateManyObjectId(string, ObjectId, ObjectId)

Updates the identifier of a related entity by replacing the old object identifier with the new one across multiple records.

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

#### Parameters

`nameId` string

  The name of the field or relationship whose object identifier should be updated.

`id` ObjectId

  The new object identifier to replace the old one with.

`oldId` ObjectId

  The existing object identifier that should be replaced.

#### Returns

Task