# Class PatientArchiveRepository

Namespace
  adas_core.Infrastructure.Repositories

Assembly
  adas-core.Infrastructure.dll

Repository implementation for managing Patient archive entities in MongoDB. Provides CRUD operations for historical/archived patient data.

    public class PatientArchiveRepository : MongoRepository<Patient>, IPatientArchiveRepository, IMongoRepository<Patient>

Inheritance
  object
  MongoRepository<Patient>
  PatientArchiveRepository

Implements
  IPatientArchiveRepository
  IMongoRepository<Patient>

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

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

Initializes a new instance of the PatientArchiveRepository.

    public PatientArchiveRepository(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

### CreateIndexes()

Creates the necessary indexes for the PatientArchive collection. Creates an index on patientNumber for improved query performance.

    public override Task CreateIndexes()

#### Returns

Task

### FindAll()

Retrieves all archived patients from the collection.

    public Task<List<Patient>> FindAll()

#### Returns

Task<List<Patient>>

  A list of all Patient entities in the archive.

### FindByPatientNumber(string)

Finds an archived patient by their patient number.

    public Task<Patient?> FindByPatientNumber(string patientNumber)

#### Parameters

`patientNumber` string

  The patient number to search for.

#### Returns

Task<Patient>

  The Patient if found; otherwise, null.

### GetCollectionName()

Gets the name of the collection for archived patients.

    public override string GetCollectionName()

#### Returns

string

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

### InsertOneAsync(Patient)

Inserts or updates an archived patient. If a patient with the same PatientNumber already exists, merges the data and updates the record. If no match exists, performs a regular insert.

    public override Task InsertOneAsync(Patient obj)

#### Parameters

`obj` Patient

  The Patient entity to insert or merge.

#### Returns

Task

#### Exceptions

Exception

  Logs warning and silently fails on error.

### SearchByPatientNumberAndDistinctUnit(string, ObjectId)

Searches for an archived patient by patient number, returning null if multiple matches exist. This is useful when patientNumber may be incomplete and could match multiple patients.

    public Task<Patient?> SearchByPatientNumberAndDistinctUnit(string patientNumber, ObjectId unitId)

#### Parameters

`patientNumber` string

  The patient number to search for.

`unitId` ObjectId

  The unit ID (currently not used in query, kept for interface compatibility).

#### Returns

Task<Patient>

  The unique Patient if exactly one match is found; otherwise, null if multiple or none.