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> BookNewHearing(BookNewHearingRequest request) { var result = new BookNewHearingRequestValidation().Validate(request); if (!result.IsValid) { ModelState.AddFluentValidationErrors(result.Errors); return(BadRequest(ModelState)); } var query = new GetCaseTypeQuery(request.CaseTypeName); var caseType = await _queryHandler.Handle <GetCaseTypeQuery, CaseType>(query); if (caseType == null) { ModelState.AddModelError(nameof(request.CaseTypeName), "Case type does not exist"); return(BadRequest(ModelState)); } 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 hearingType = caseType.HearingTypes.SingleOrDefault(x => x.Name == request.HearingTypeName); if (hearingType == null) { ModelState.AddModelError(nameof(request.HearingTypeName), "Hearing type does not exist"); return(BadRequest(ModelState)); } var venue = await GetVenue(request.HearingVenueName); if (venue == null) { ModelState.AddModelError(nameof(request.HearingVenueName), "Hearing venue does not exist"); return(BadRequest(ModelState)); } var mapper = new ParticipantRequestToNewParticipantMapper(); var newParticipants = request.Participants.Select(x => mapper.MapRequestToNewParticipant(x, caseType)) .ToList(); var cases = request.Cases.Select(x => new Case(x.Number, x.Name)).ToList(); var createVideoHearingCommand = new CreateVideoHearingCommand(caseType, hearingType, request.ScheduledDateTime, request.ScheduledDuration, venue, newParticipants, cases, request.QuestionnaireNotRequired, request.AudioRecordingRequired) { HearingRoomName = request.HearingRoomName, OtherInformation = request.OtherInformation, CreatedBy = request.CreatedBy }; await _commandHandler.Handle(createVideoHearingCommand); var videoHearingId = createVideoHearingCommand.NewHearingId; var getHearingByIdQuery = new GetHearingByIdQuery(videoHearingId); var queriedVideoHearing = await _queryHandler.Handle <GetHearingByIdQuery, VideoHearing>(getHearingByIdQuery); var hearingMapper = new HearingToDetailResponseMapper(); var response = hearingMapper.MapHearingToDetailedResponse(queriedVideoHearing); return(CreatedAtAction(nameof(GetHearingDetailsById), new { hearingId = response.Id }, response)); }