Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
 public void OneTimeSetUp()
 {
     _validator = new UpdateHearingParticipantsRequestValidation();
 }