rama creada apartir de master en j

This commit is contained in:
jrojas
2026-06-26 10:29:23 +02:00
parent 319fd3dfb0
commit c1517fda87
2810 changed files with 1927392 additions and 25392 deletions
@@ -10,25 +10,40 @@ using MongoUtils = adas_core.Infrastructure.Utils.MongoUtils;
namespace adas_core.Infrastructure.Repositories;
/// <summary>
/// Repository implementation for managing Patient archive entities in MongoDB.
/// Provides CRUD operations for historical/archived patient data.
/// </summary>
public class PatientArchiveRepository : MongoRepository<Patient>, IPatientArchiveRepository
{
private readonly ApiSettings _apiSettings;
/// <summary>
/// Initializes a new instance of the PatientArchiveRepository.
/// </summary>
/// <param name="apiSettings">API settings containing collection names configuration.</param>
/// <param name="database">The MongoDB database instance.</param>
/// <exception cref="ArgumentNullException">Thrown when apiSettings is null.</exception>
public PatientArchiveRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database) : base(database)
{
if (apiSettings == null) throw new ArgumentNullException(nameof(apiSettings));
_apiSettings = apiSettings.Value;
} //For testing
/// <summary>
/// Gets the name of the collection for archived patients.
/// </summary>
/// <returns>The collection name from API settings, or default "archive_patient".</returns>
public override string GetCollectionName()
{
return _apiSettings.ArchivePatient ?? "archive_patient";
}
/// <summary>
/// Finds an archived patient by their patient number.
/// </summary>
/// <param name="patientNumber">The patient number to search for.</param>
/// <returns>The Patient if found; otherwise, null.</returns>
public async Task<Patient?> FindByPatientNumber(string patientNumber)
{
if (string.IsNullOrWhiteSpace(patientNumber)) return null;
@@ -41,7 +56,10 @@ public class PatientArchiveRepository : MongoRepository<Patient>, IPatientArchiv
return await result.FirstOrDefaultAsync();
}
/// <summary>
/// Retrieves all archived patients from the collection.
/// </summary>
/// <returns>A list of all Patient entities in the archive.</returns>
public async Task<List<Patient>> FindAll()
{
var filterBuilder = Builders<Patient>.Filter;
@@ -52,6 +70,13 @@ public class PatientArchiveRepository : MongoRepository<Patient>, IPatientArchiv
return await result.ToListAsync();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="patientNumber">The patient number to search for.</param>
/// <param name="unitId">The unit ID (currently not used in query, kept for interface compatibility).</param>
/// <returns>The unique Patient if exactly one match is found; otherwise, null if multiple or none.</returns>
public async Task<Patient?> SearchByPatientNumberAndDistinctUnit(string patientNumber, ObjectId unitId)
{
if (string.IsNullOrWhiteSpace(patientNumber)) return null;
@@ -64,6 +89,13 @@ public class PatientArchiveRepository : MongoRepository<Patient>, IPatientArchiv
return patient.Count > 1 ? null : patient.FirstOrDefault();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="obj">The Patient entity to insert or merge.</param>
/// <exception cref="Exception">Logs warning and silently fails on error.</exception>
public override async Task InsertOneAsync(Patient obj)
{
try
@@ -118,7 +150,10 @@ public class PatientArchiveRepository : MongoRepository<Patient>, IPatientArchiv
}
}
/// <summary>
/// Creates the necessary indexes for the PatientArchive collection.
/// Creates an index on patientNumber for improved query performance.
/// </summary>
public override async Task CreateIndexes()
{
var options = new CreateIndexOptions { Background = true, Unique = false };