using adas_core.Application.Repositories.Interfaces; using adas_core.Domain.Models.AppSettings; using adas_core.Domain.Models.MongoModels; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using MongoDB.Bson; using MongoDB.Driver; using MongoUtils = adas_core.Infrastructure.Utils.MongoUtils; namespace adas_core.Infrastructure.Repositories; /// /// Repository for managing archived patient care plans in a MongoDB database. /// This class provides methods to perform CRUD operations and queries related to archived patient care plans, such as retrieving care plans by patient ID, inserting new care plans, and creating indexes. /// It utilizes the MongoDB driver for database interactions and is configured using application settings for collection names and other parameters. /// public class ArchivePatientCarePlanRepository : MongoRepository, IArchivePatientCarePlanRepository { #region Properties /// /// API settings containing configuration for the repository, such as collection names and other relevant parameters. /// private readonly ApiSettings _apiSettings; /// /// Logger for logging information, warnings, and errors related to the operations performed by this repository. /// private readonly ILogger _logger; #endregion #region Constructor /// /// Initializes a new instance of the class with the specified API settings, MongoDB database, and logger. /// /// The API settings containing configuration for the repository. /// The MongoDB database instance. /// The logger for logging information, warnings, and errors. public ArchivePatientCarePlanRepository( IOptions apiSettings, IMongoDatabase database, ILogger logger) : base(database) { _logger = logger; _apiSettings = apiSettings.Value; } /// /// Creates indexes for the MongoDB collection associated with this repository. /// This method ensures that the necessary indexes are created to optimize query performance, particularly for queries based on patient ID. The indexes are created in the background to avoid blocking operations on the database. /// /// public override async Task CreateIndexes() { var options = new CreateIndexOptions { Background = true, Unique = false }; var indexes = new List> { new("{ patientId: 1 }", options) }; await MongoUtils.EnsureIndexes(Collection, indexes); } #endregion #region Create /// /// Inserts a single patient care plan into the MongoDB collection. This method takes a object as input and attempts to insert it into the database. /// If an exception occurs during the insertion process, it logs the error with details about the patient and the exception, and then rethrows the exception to be handled by the calling code. /// /// The patient care plan to be inserted. /// A task representing the asynchronous operation. public override async Task InsertOneAsync(PatientCarePlan patient) { try { await base.InsertOneAsync(patient); } catch (Exception e) { _logger.LogError( "Exception trying to insert patient: {patient}. on archive patient procedure Exception {e}", patient, e); throw; } } /// /// Inserts multiple patient care plans into the MongoDB collection. This method takes a list of objects as input and attempts to insert them into the database in a single operation. /// /// The list of patient care plans to be inserted. /// A task representing the asynchronous operation. public override async Task InsertManyAsync(List patient) { try { await base.InsertManyAsync(patient); } catch (Exception e) { _logger.LogError( "Exception trying to insert many PatientCarePlan. on archive patient procedure Exception {e}", e); throw; } } #endregion #region Read /// /// Gets the name of the MongoDB collection associated with this repository. The collection name is determined based on the API settings provided during the initialization of the repository. If the collection name is not specified in the settings, it defaults to "archive_patients_care_plan". /// This method is used by the base repository class to determine which collection to interact with for CRUD operations. /// /// public override string GetCollectionName() { return _apiSettings.ArchivePatientProcedure ?? "archive_patients_care_plan"; } /// /// Finds patient care plans by the specified patient ID. This method takes a patient ID as input and queries the MongoDB collection for care plans associated with that patient ID. /// It returns a list of objects that match the query. If no care plans are found, it returns an empty list. /// /// The ID of the patient whose care plans are to be retrieved. /// A task representing the asynchronous operation, containing a list of objects. public async Task?> FindByPatientId(ObjectId patientId) { var result = await Collection.FindAsync(Builders.Filter.Eq(p => p.PatientId, patientId)); return result.ToList(); } /// /// Finds patient care plans by the specified patient ID, where the patient ID is provided as a string. This method attempts to parse the string into an ObjectId and then queries the MongoDB collection for care plans associated with that patient ID. /// /// The ID of the patient whose care plans are to be retrieved, provided as a string. /// A task representing the asynchronous operation, containing a list of objects. public async Task?> FindByPatientId(string patientId) { var isParsed = ObjectId.TryParse(patientId, out var patientIdParsed); if (!isParsed) return []; var result = await Collection.FindAsync(Builders.Filter.Eq(p => p.PatientId, patientIdParsed)); return result.ToList(); } /// /// Finds patient care plans by the specified patient number. This method takes a patient number as input and queries the MongoDB collection for care plans associated with that patient number. /// /// The number of the patient whose care plans are to be retrieved. /// A task representing the asynchronous operation, containing a list of objects. public async Task?> FindByPatientNumber(string patientNumber) { var result = await Collection.FindAsync(Builders.Filter.Eq(p => p.PatientNumber, patientNumber)); return result.ToList(); } /// /// Finds all patient care plans in the MongoDB collection. This method retrieves all documents from the collection and returns them as a list of objects. /// /// A task representing the asynchronous operation, containing a list of objects. public async Task> FindAll() { var result = await Collection.Find(Builders.Filter.Empty).ToListAsync(); return result; } /// /// Finds patient care plans by multiple identifiers, including patient ID, patient number, and patient ID as a string. /// This method attempts to find care plans using the provided identifiers in a specific order: it first tries to find care plans by the patient ID (as an ObjectId), then by the patient ID (as a string), and finally by the patient number. /// If any of the queries return results, it returns those results immediately. If no care plans are found using any of the identifiers, it returns an empty list. /// /// The ID of the patient whose care plans are to be retrieved, provided as an ObjectId. /// The ID of the patient whose care plans are to be retrieved, provided as a string. /// The number of the patient whose care plans are to be retrieved. /// A task representing the asynchronous operation, containing a list of objects. public async Task?> FindByIds(ObjectId oldPatientId, string? oldPatientPatientId, string? oldPatientPatientNumber) { var patientFound = await FindByPatientId(oldPatientId); if (patientFound != null) return patientFound; if (oldPatientPatientId != null) { patientFound = await FindByPatientId(oldPatientPatientId); if (patientFound is { Count: > 0 }) return patientFound; } if (oldPatientPatientNumber != null) { patientFound = await FindByPatientNumber(oldPatientPatientNumber); if (patientFound is { Count: > 0 }) return patientFound; } return []; } #endregion #region Update // public async Task Update(Patient patient) // { // patient.UpdateDate = DateTime.UtcNow; // await UpdateOneAsync(patient.Id, patient); // } // // public async void UpdatePatientData(Patient oldPatient, Patient newPatient) // { // var filterBuilder = Builders.Filter; // var updateBuilder = Builders.Update // .Set(p => p.Person, newPatient.Person) // .Set(p => p.UpdateDate, DateTime.UtcNow) // .Set(p => p.Location, newPatient.Location) // .Set(p => p.PatientNumber, newPatient.PatientNumber) // .Set(p => p.UnitString, newPatient.UnitString) // .Set(p => p.Room, newPatient.Room) // .Set(p => p.PatientId, newPatient.PatientId); // var filter = filterBuilder.Eq(p => p.Id, oldPatient.Id); // var update = updateBuilder; // // await Collection.UpdateOneAsync(filter, update); // } // public async void UpdateProcedure(Patient patientFound) // { // var filterBuilder = Builders.Filter; // var updateBuilder = Builders.Update // .Set(p => p.Procedures, patientFound.Procedures); // var filter = filterBuilder.Eq(p => p.Id, patientFound.Id); // var update = updateBuilder; // // await Collection.UpdateOneAsync(filter, update); // } // // public async void UpdateTreatment(Patient patientFound) // { // var filterBuilder = Builders.Filter; // var updateBuilder = Builders.Update // .Set(p => p.Treatment, patientFound.Treatment); // var filter = filterBuilder.Eq(p => p.Id, patientFound.Id); // var update = updateBuilder; // // await Collection.UpdateOneAsync(filter, update); // } #endregion }