# Class ObservationArchiveRepository

Namespace
  adas_core.Infrastructure.Repositories

Assembly
  adas-core.Infrastructure.dll

Repository implementation for managing PatientObservation archive entities in MongoDB. Provides operations for storing and retrieving historical patient observations.

    public class ObservationArchiveRepository : MongoRepository<PatientObservation>, IObservationArchiveRepository, IMongoRepository<PatientObservation>

Inheritance
  object
  MongoRepository<PatientObservation>
  ObservationArchiveRepository

Implements
  IObservationArchiveRepository
  IMongoRepository<PatientObservation>

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

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

Initializes a new instance of the ObservationArchiveRepository.

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

#### Parameters

`apiSettings` IOptions<ApiSettings>

  API settings containing collection names configuration.

`database` IMongoDatabase

  The MongoDB database instance.

#### Exceptions

ArgumentNullException

  Thrown when apiSettings is null.

## Methods

### AggregatedPatientLastObservations(ObjectId, int, DateTime, List<string>?)

Retrieves the aggregated last observations for a specific patient. Filters observations by name and date, returning the most recent ones.

    public Task<List<PatientObservation>> AggregatedPatientLastObservations(ObjectId patientId, int num, DateTime lastDate, List<string>? filterObservations = null)

#### Parameters

`patientId` ObjectId

  The ObjectId of the patient.

`num` int

  The maximum number of observations to return per observation type.

`lastDate` DateTime

  The cutoff date to filter observations (inclusive).

`filterObservations` List<string>

  Optional list of observation names to filter by. If null, retrieves all distinct observations.

#### Returns

Task<List<PatientObservation>>

  A list of PatientObservation entities sorted by time descending.

### DeleteBeforeDate(DateTime)

Deletes all patient observations before a specified date. Useful for archival cleanup operations.

    public Task DeleteBeforeDate(DateTime date)

#### Parameters

`date` DateTime

  The cutoff date. Observations older than this date will be deleted.

#### Returns

Task

  The number of deleted documents.

### FindAllFromPatient(ObjectId)

Retrieves all archived observations for a specific patient.

    public Task<List<PatientObservation>> FindAllFromPatient(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The ObjectId of the patient.

#### Returns

Task<List<PatientObservation>>

  A list of all PatientObservation entities for the patient.

### GetCollectionName()

Gets the name of the collection for archived patient observations.

    public override string GetCollectionName()

#### Returns

string

  The collection name from API settings, or default "archive_patients_observations".

### InsertBatch(IEnumerable<PatientObservation>)

Inserts a batch of patient observations using bulk write operation.

    public Task<long> InsertBatch(IEnumerable<PatientObservation> observations)

#### Parameters

`observations` IEnumerable<PatientObservation>

  An enumerable of PatientObservation entities to insert.

#### Returns

Task<long>

  The count of successfully inserted documents.

### InsertOneAsync(PatientObservation)

Inserts a new patient observation into the archive with retry logic for duplicate key errors. If a duplicate key error occurs, generates a new ObjectId and retries up to maxRetries times.

    public Task InsertOneAsync(PatientObservation patientObservation)

#### Parameters

`patientObservation` PatientObservation

  The PatientObservation entity to insert.

#### Returns

Task

#### Exceptions

MongoWriteException

  Throws when duplicate key error persists after max retries.

Exception

  Throws when insertion fails for reasons other than duplicate key.