# Class AppointmentRepository

Namespace
  adas_core.Infrastructure.Repositories

Assembly
  adas-core.Infrastructure.dll

Repository for managing patient appointments in a MongoDB database. This class provides methods to perform CRUD operations and queries related to patient appointments, such as retrieving appointments by patient ID, finding appointments by point of care, and updating or deleting appointments. It utilizes the MongoDB driver for database interactions and is configured using application settings for collection names and other parameters.

    public class AppointmentRepository : MongoRepository<PatientAppointment>, IAppointmentRepository, IMongoRepository<PatientAppointment>

Inheritance
  object
  MongoRepository<PatientAppointment>
  AppointmentRepository

Implements
  IAppointmentRepository
  IMongoRepository<PatientAppointment>

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

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

Initializes a new instance of the AppointmentRepository class with the specified API settings and MongoDB database.

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

#### Parameters

`apiSettings` IOptions<ApiSettings>

  The API settings containing configuration for the repository.

`database` IMongoDatabase

  The MongoDB database instance.

#### Exceptions

ArgumentNullException

  Thrown when any of the input parameters are null.

## Methods

### CreateIndexes()

Creates indexes for the patient appointments collection in MongoDB. This method defines a list of indexes to be created, including an index on the patient ID field, and then calls the EnsureIndexes method from the MongoUtils class to ensure that the specified indexes are created in the database. The indexes are created in the background and are not unique.

    public override Task CreateIndexes()

#### Returns

Task

  A task representing the asynchronous operation.

### DeleteAsync(ObjectId)

Deletes a patient appointment from the MongoDB collection based on the appointment's ID. This method constructs a filter to identify the document to be deleted using the appointment's ID and then calls the DeleteOneAsync method to remove the document from the database.

    public Task DeleteAsync(ObjectId id)

#### Parameters

`id` ObjectId

  The ID of the patient appointment to be deleted.

#### Returns

Task

  A task representing the asynchronous operation.

### DeleteByPatientId(ObjectId)

Deletes all patient appointments associated with a specific patient ID. This method constructs a filter to identify all documents in the MongoDB collection that match the specified patient ID and then calls the DeleteManyAsync method to remove all matching documents from the database.

    public Task DeleteByPatientId(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The ID of the patient whose appointments are to be deleted.

#### Returns

Task

  A task representing the asynchronous operation.

### FindByLocation(PatientLocation)

Finds patient appointments based on a given patient location. This method constructs a filter to query the MongoDB collection for appointments that match the specified patient location, which includes details such as bed, room, and unit name. It uses the Builders class to create a filter that checks for the existence of the Locations field and matches the specified location details within the ResourceGroups of the appointments.

    public Task<List<PatientAppointment>> FindByLocation(PatientLocation location)

#### Parameters

`location` PatientLocation

  The location details to filter patient appointments by.

#### Returns

Task<List<PatientAppointment>>

  A list of patient appointments that match the specified location.

### FindByPatientAndReason(ObjectId, string?)

Finds a patient appointment based on the patient ID and appointment reason. This method constructs a filter to query the MongoDB collection for an appointment that matches both the specified patient ID and appointment reason, and returns the first matching appointment if found, or null if no match is found.

    public Task<PatientAppointment?> FindByPatientAndReason(ObjectId patientId, string? appointmentReason)

#### Parameters

`patientId` ObjectId

  The ID of the patient whose appointment is to be retrieved.

`appointmentReason` string

  The reason for the appointment to be retrieved.

#### Returns

Task<PatientAppointment>

  The first matching patient appointment if found, or null if no match is found.

### FindByPatientAndVisitNumber(ObjectId, string)

Finds a patient appointment based on the patient ID and visit number. This method constructs a filter to query the MongoDB collection for an appointment that matches both the specified patient ID and visit number, and returns the first matching appointment if found, or null if no match is found.

    public Task<PatientAppointment?> FindByPatientAndVisitNumber(ObjectId patientId, string visitNumber)

#### Parameters

`patientId` ObjectId

  The ID of the patient whose appointment is to be retrieved.

`visitNumber` string

  The visit number of the appointment to be retrieved.

#### Returns

Task<PatientAppointment>

  The first matching patient appointment if found, or null if no match is found.

### FindByPatientIdAsync(ObjectId)

Finds patient appointments based on the patient ID. This method constructs a filter to query the MongoDB collection for appointments that match the specified patient ID and returns an asynchronous cursor to iterate through the results.

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

#### Parameters

`patientId` ObjectId

  The ID of the patient whose appointments are to be retrieved.

#### Returns

Task<IAsyncCursor<PatientAppointment>>

  An asynchronous cursor to iterate through the patient appointments.

### FindByPoC(PointOfCare)

Finds patient appointments based on a given point of care (PoC). This method constructs a filter to query the MongoDB collection for appointments that match the specified point of care, which includes details such as bed, room, and unit name. It utilizes the FindByLocation method to perform the actual query based on the constructed patient location.

    public Task<List<PatientAppointment>> FindByPoC(PointOfCare poc)

#### Parameters

`poc` PointOfCare

  The point of care details to filter appointments.

#### Returns

Task<List<PatientAppointment>>

  A list of patient appointments that match the specified point of care.

### GetByPatient(ObjectId)

Retrieves a list of patient appointments for a given patient ID. This method constructs a filter to query the MongoDB collection based on the patient ID and sorts the results by creation time in descending order.

    public Task<List<PatientAppointment>> GetByPatient(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The ID of the patient whose appointments are to be retrieved.

#### Returns

Task<List<PatientAppointment>>

  A list of patient appointments for the specified patient ID.

### GetCollectionName()

Gets the name of the MongoDB collection for patient appointments. This method retrieves the collection name from the API settings, allowing for flexible configuration. If the collection name is not specified in the settings, it defaults to "patients_appointments".

    public override string GetCollectionName()

#### Returns

string

  The name of the MongoDB collection for patient appointments.

### InsertOneAsync(PatientAppointment)

Inserts a new patient appointment into the MongoDB collection. This method ensures that the creation time of the appointment is set to the current UTC time if it is not already specified before inserting the appointment into the database using the base class's InsertOneAsync method.

    public override Task InsertOneAsync(PatientAppointment appointment)

#### Parameters

`appointment` PatientAppointment

  The patient appointment to be inserted.

#### Returns

Task

  A task representing the asynchronous operation.

### Update(PatientAppointment)

Updates an existing patient appointment in the MongoDB collection. This method takes a patient appointment object as input and updates the corresponding document in the database based on the appointment's ID. It uses the UpdateOneAsync method from the base class to perform the update operation.

    public Task Update(PatientAppointment appointment)

#### Parameters

`appointment` PatientAppointment

  The patient appointment to be updated.

#### Returns

Task

  A task representing the asynchronous operation.

### UpdateManyObjectId(string, ObjectId, ObjectId)

Updates the ObjectId references in patient appointments when a patient's ID changes. This method constructs a filter to identify all documents in the MongoDB collection that match the specified old patient ID and then updates those documents to reference the new patient ID using the UpdateManyAsync method.

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

#### Parameters

`nameId` string

  The name ID associated with the patient appointments to be updated.

`id` ObjectId

  The new ObjectId to replace the old patient ID.

`oldId` ObjectId

  The old ObjectId of the patient whose appointments are to be updated.

#### Returns

Task

  A task representing the asynchronous operation.