using DocuMed.Domain.Entities.Staffs; namespace DocuMed.Repository.Handlers.MedicalHistories; public class CreateMedicalHistoryCommandHandler( IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, IMediator mediator) : IRequestHandler { public async Task Handle(CreateMedicalHistoryCommand 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"); 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 ent = 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); foreach (var answer in template.Answers) ent.AddAnswer(answer.Answer, answer.Question, answer.Part, answer.QuestionType); repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return ent.Id; } }