public async Task <IActionResult> UpdateSuitabilityAnswers(Guid hearingId, Guid participantId, [FromBody] List <SuitabilityAnswersRequest> answers) { if (hearingId == Guid.Empty) { ModelState.AddModelError(nameof(hearingId), $"Please provide a valid {nameof(hearingId)}"); return(BadRequest(ModelState)); } if (participantId == Guid.Empty) { ModelState.AddModelError(nameof(participantId), $"Please provide a valid {nameof(participantId)}"); return(BadRequest(ModelState)); } // Reject any requests with duplicate keys var duplicateKeyFound = answers.GroupBy(x => x.Key).Any(g => g.Count() > 1); if (duplicateKeyFound) { ModelState.AddModelError(nameof(participantId), $"Request '{nameof(answers)}' cannot contain duplicate keys."); return(BadRequest(ModelState)); } var suitabilityAnswers = answers.Select(answer => new SuitabilityAnswer(answer.Key, answer.Answer, answer.ExtendedAnswer)) .ToList(); var command = new UpdateSuitabilityAnswersCommand(hearingId, participantId, suitabilityAnswers); try { await _commandHandler.Handle(command); } catch (DomainRuleException e) { ModelState.AddDomainRuleErrors(e.ValidationFailures); return(BadRequest(ModelState)); } catch (HearingNotFoundException) { return(NotFound("Hearing not found")); } catch (ParticipantNotFoundException) { return(NotFound("Participant not found")); } return(NoContent()); }
public async Task <IActionResult> AddParticipantsToHearing(Guid hearingId, [FromBody] AddParticipantsToHearingRequest request) { if (hearingId == Guid.Empty) { ModelState.AddModelError(nameof(hearingId), $"Please provide a valid {nameof(hearingId)}"); return(BadRequest(ModelState)); } var result = new AddParticipantsToHearingRequestValidation().Validate(request); if (!result.IsValid) { ModelState.AddFluentValidationErrors(result.Errors); return(BadRequest(ModelState)); } var query = new GetHearingByIdQuery(hearingId); var videoHearing = await _queryHandler.Handle <GetHearingByIdQuery, VideoHearing>(query); if (videoHearing == null) { return(NotFound()); } var caseTypequery = new GetCaseTypeQuery(videoHearing.CaseType.Name); var caseType = await _queryHandler.Handle <GetCaseTypeQuery, CaseType>(caseTypequery); var individualRoles = caseType.CaseRoles.SelectMany(x => x.HearingRoles).Where(x => x.UserRole.IsIndividual).Select(x => x.Name).ToList(); var addressValidationResult = AddressValidationHelper.ValidateAddress(individualRoles, request.Participants); if (!addressValidationResult.IsValid) { ModelState.AddFluentValidationErrors(addressValidationResult.Errors); return(BadRequest(ModelState)); } var representativeRoles = caseType.CaseRoles.SelectMany(x => x.HearingRoles).Where(x => x.UserRole.IsRepresentative).Select(x => x.Name).ToList(); var reprensentatives = request.Participants.Where(x => representativeRoles.Contains(x.HearingRoleName)).ToList(); var representativeValidationResult = RepresentativeValidationHelper.ValidateRepresentativeInfo(reprensentatives); if (!representativeValidationResult.IsValid) { ModelState.AddFluentValidationErrors(representativeValidationResult.Errors); return(BadRequest(ModelState)); } var mapper = new ParticipantRequestToNewParticipantMapper(); var participants = request.Participants .Select(x => mapper.MapRequestToNewParticipant(x, videoHearing.CaseType)).ToList(); var command = new AddParticipantsToVideoHearingCommand(hearingId, participants); try { await _commandHandler.Handle(command); } catch (DomainRuleException e) { ModelState.AddDomainRuleErrors(e.ValidationFailures); return(BadRequest(ModelState)); } var hearing = await _queryHandler.Handle <GetHearingByIdQuery, VideoHearing>(query); // ONLY publish this event when Hearing is set for ready for video if (hearing.Status == Domain.Enumerations.BookingStatus.Created) { await PublishParticipantsAddedEvent(participants, hearing); } return(NoContent()); }
public async Task <IActionResult> UpdateHearingParticipants(Guid hearingId, [FromBody] UpdateHearingParticipantsRequest request) { if (hearingId == Guid.Empty) { ModelState.AddModelError(nameof(hearingId), $"Please provide a valid {nameof(hearingId)}"); return(BadRequest(ModelState)); } var result = new UpdateHearingParticipantsRequestValidation().Validate(request); if (!result.IsValid) { ModelState.AddFluentValidationErrors(result.Errors); return(BadRequest(ModelState)); } var query = new GetHearingByIdQuery(hearingId); var videoHearing = await _queryHandler.Handle <GetHearingByIdQuery, VideoHearing>(query); if (videoHearing == null) { return(NotFound()); } var caseTypequery = new GetCaseTypeQuery(videoHearing.CaseType.Name); var caseType = await _queryHandler.Handle <GetCaseTypeQuery, CaseType>(caseTypequery); var representativeRoles = caseType.CaseRoles.SelectMany(x => x.HearingRoles).Where(x => x.UserRole.IsRepresentative).Select(x => x.Name).ToList(); var representatives = request.NewParticipants.Where(x => representativeRoles.Contains(x.HearingRoleName)).ToList(); var representativeValidationResult = RepresentativeValidationHelper.ValidateRepresentativeInfo(representatives); if (!representativeValidationResult.IsValid) { ModelState.AddFluentValidationErrors(representativeValidationResult.Errors); return(BadRequest(ModelState)); } var newParticipants = request.NewParticipants .Select(x => ParticipantRequestToNewParticipantMapper.Map(x, videoHearing.CaseType)).ToList(); var existingParticipants = request.ExistingParticipants .Select(x => new ExistingParticipantDetails { DisplayName = x.DisplayName, OrganisationName = x.OrganisationName, ParticipantId = x.ParticipantId, RepresentativeInformation = new RepresentativeInformation { Representee = x.Representee }, TelephoneNumber = x.TelephoneNumber, Title = x.Title }).ToList(); var linkedParticipants = LinkedParticipantRequestToLinkedParticipantDtoMapper.MapToDto(request.LinkedParticipants); var command = new UpdateHearingParticipantsCommand(hearingId, existingParticipants, newParticipants, request.RemovedParticipantIds, linkedParticipants); try { await _commandHandler.Handle(command); } catch (DomainRuleException e) { ModelState.AddDomainRuleErrors(e.ValidationFailures); return(BadRequest(ModelState)); } var hearing = await _queryHandler.Handle <GetHearingByIdQuery, VideoHearing>(query); // Publish this event if the hearing is ready for video if (hearing.Status == BookingStatus.Created) { await PublishUpdateHearingParticipantsEvent(hearing, existingParticipants, newParticipants, request.RemovedParticipantIds, linkedParticipants); } return(NoContent()); }