using MediatR; namespace DocuMed.Repository.Handlers.MedicalHistories; public class UpdateMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, IMediator mediator) : IRequestHandler { public async Task Handle(UpdateMedicalHistoryCommand template, CancellationToken cancellationToken) { if (!Guid.TryParse(currentUserService.UserId, out Guid userId)) throw new AppException("دسترسی غیرمجاز", ApiResultStatusCode.UnAuthorized); var student = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(f => f.UserId == userId, cancellationToken); if (student == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "Student not found"); var ent = await repositoryWrapper.SetRepository().TableNoTracking .FirstOrDefaultAsync(m => m.Id == template.Id, cancellationToken); if (ent == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "شرح حال پیدا نشد"); if (ent.StudentId != student.Id) throw new BaseApiException(ApiResultStatusCode.NotFound, "شرح حال متعلق به دانشجو نیست"); Guid patientId; if (template.PatientId != default) { var patient = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(f => f.Id == template.PatientId, cancellationToken); if (patient == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "Patient not found"); patientId = patient.Id; } else { var patentCommand = new CreatePatientCommand(template.FirstName, template.LastName, string.Empty, template.NationalId, string.Empty, string.Empty, DateTime.Now, template.SectionId ); patientId = await mediator.Send(patentCommand, cancellationToken); } var newEnt = MedicalHistory.Create(template.ChiefComplaint, template.SectionId, template.PresentIllnessDetail, template.PastDiseasesHistoryDetail, template.PastSurgeryHistoryDetail, template.FamilyHistoryDetail, template.AllergyDetail, template.DrugHistoryDetail, template.AddictionHistoryDetail, template.SystemReviewDetail, template.VitalSignDetail, template.GeneralAppearanceDetail, template.SystolicBloodPressure, template.DiastolicBloodPressure, template.PulseRate, template.SPO2, template.Temperature, student.Id, patientId, template.MedicalHistoryTemplateId); newEnt.Id = ent.Id; newEnt.CreatedAt = ent.CreatedAt; foreach (var answer in template.Answers.Where(a => a.Id == Guid.Empty)) newEnt.AddAnswer(answer.Answer, answer.Question, answer.Part, answer.QuestionType); foreach (var answer in template.Answers.Where(a => a.Id != Guid.Empty)) { var dbAnswer = await repositoryWrapper.SetRepository().TableNoTracking .FirstOrDefaultAsync(a => a.Id == answer.Id, cancellationToken); if (dbAnswer != null && dbAnswer.Answer != answer.Answer && answer.Answer != null) { dbAnswer = MedicalHistoryAnswer.Create(answer.Answer, answer.Question, answer.Part, answer.QuestionType, dbAnswer.MedicalHistoryId); dbAnswer.Id = answer.Id; repositoryWrapper.SetRepository().Update(dbAnswer); await repositoryWrapper.SaveChangesAsync(cancellationToken); } } repositoryWrapper.SetRepository().Update(newEnt); await repositoryWrapper.SaveChangesAsync(cancellationToken); return newEnt.Id; } }