public void Should_return_empty_list_if_end_and_start_dates_is_the_same()
        {
            var startDate  = new DateTime(2020, 10, 1);
            var endDate    = new DateTime(2020, 10, 1);
            var expectDays = 0;

            var result = DateListMapper.GetListOfWorkingDates(startDate, endDate);

            result.Count.Should().Be(expectDays);
        }
        public void Should_return_range_of_dates_not_included_weekends()
        {
            var startDate  = new DateTime(2020, 10, 1, 4, 30, 0, 0);
            var endDate    = new DateTime(2020, 10, 6, 4, 35, 0, 0);
            var expectDays = 3;

            var result = DateListMapper.GetListOfWorkingDates(startDate, endDate);

            result.Count.Should().Be(expectDays);
        }
        public void Should_return_range_of_dates_not_included_weekends_and_public_holidays()
        {
            var startDate = new DateTime(2020, 10, 1, 4, 30, 0, 0);
            var endDate   = new DateTime(2020, 10, 6, 4, 35, 0, 0);
            var pb        = new PublicHoliday
            {
                Title = "Test Holidays",
                Date  = new DateTime(2020, 10, 5, 0, 0, 0, 0)
            };
            var publicHolidays = new List <PublicHoliday> {
                pb
            };
            var expectDays = 2;

            var result = DateListMapper.GetListOfWorkingDates(startDate, endDate, publicHolidays);

            result.Count.Should().Be(expectDays);
        }
        public async Task <IActionResult> CloneHearing(Guid hearingId, MultiHearingRequest hearingRequest)
        {
            _logger.LogDebug("Attempting to clone hearing {Hearing}", hearingId);
            var publicHolidays = await _publicHolidayRetriever.RetrieveUpcomingHolidays();

            var listOfDates = DateListMapper.GetListOfWorkingDates(hearingRequest.StartDate, hearingRequest.EndDate, publicHolidays);

            if (listOfDates.Count == 0)
            {
                _logger.LogWarning("No working dates provided to clone to");
                return(BadRequest());
            }

            var cloneHearingRequest = new CloneHearingRequest {
                Dates = listOfDates
            };

            try
            {
                _logger.LogDebug("Sending request to clone hearing to Bookings API");
                await _bookingsApiClient.CloneHearingAsync(hearingId, cloneHearingRequest);

                _logger.LogDebug("Successfully cloned hearing {Hearing}", hearingId);
                return(NoContent());
            }
            catch (BookingsApiException e)
            {
                _logger.LogError(e,
                                 "There was a problem cloning the booking. Status Code {StatusCode} - Message {Message}",
                                 e.StatusCode, e.Response);
                if (e.StatusCode == (int)HttpStatusCode.BadRequest)
                {
                    return(BadRequest(e.Response));
                }
                throw;
            }
        }
Exemple #5
0
        public async Task Should_create_multi_day_conference_from_hearing()
        {
            var request        = new BookHearingRequestBuilder(Context.Config.UsernameStem).MoveScheduledDateIfWeekend().Build();
            var initialHearing = await BookingApiClient.BookNewHearingAsync(request);

            Hearings.Add(initialHearing);

            const int numberOfDays = 2;
            var       numberOfDaysNotIncludingWeekends = CalculateDaysWithoutWeekend(numberOfDays, initialHearing.ScheduledDateTime);
            var       start = initialHearing.ScheduledDateTime;
            var       end   = initialHearing.ScheduledDateTime.AddDays(numberOfDaysNotIncludingWeekends);
            var       dates = DateListMapper.GetListOfWorkingDates(start, end);

            dates.Count.Should().Be(numberOfDays);
            // Add the initial hearing day
            const int totalNumberOfDays = numberOfDays + 1;

            var multiHearingsRequest = new CloneHearingRequest()
            {
                Dates = dates
            };

            await BookingApiClient.CloneHearingAsync(initialHearing.Id, multiHearingsRequest);

            var anyParticipant = initialHearing.Participants.First();

            var multiDays = await PollForAllMultiDayHearings(anyParticipant.Username, initialHearing.Cases.Single().Name, totalNumberOfDays);

            Hearings.AddRange(multiDays);
            multiDays.Count.Should().Be(totalNumberOfDays);

            foreach (var day in multiDays)
            {
                await ConfirmMultiDayHearing(day);
            }
        }
        public async Task <ActionResult <HearingDetailsResponse> > Post([FromBody] BookHearingRequest request)
        {
            var newBookingRequest = request.BookingDetails;

            var usernameAdIdDict = new Dictionary <string, User>();

            try
            {
                var nonJudgeParticipants = newBookingRequest.Participants
                                           .Where(p => p.CaseRoleName != "Judge" && p.HearingRoleName != "Panel Member" && p.HearingRoleName != "Winger")
                                           .ToList();
                await PopulateUserIdsAndUsernames(nonJudgeParticipants, usernameAdIdDict);

                if (newBookingRequest.Endpoints != null && newBookingRequest.Endpoints.Any())
                {
                    var endpointsWithDa = newBookingRequest.Endpoints
                                          .Where(x => !string.IsNullOrWhiteSpace(x.DefenceAdvocateUsername)).ToList();
                    _hearingsService.AssignEndpointDefenceAdvocates(endpointsWithDa,
                                                                    newBookingRequest.Participants.AsReadOnly());
                }

                newBookingRequest.CreatedBy = _userIdentity.GetUserIdentityName();

                _logger.LogInformation("BookNewHearing - Attempting to send booking request to Booking API");
                var hearingDetailsResponse = await _bookingsApiClient.BookNewHearingAsync(newBookingRequest);

                _logger.LogInformation("BookNewHearing - Successfully booked hearing {Hearing}", hearingDetailsResponse.Id);

                _logger.LogInformation("BookNewHearing - Sending email notification to the participants");
                await _hearingsService.SendNewUserEmailParticipants(hearingDetailsResponse, usernameAdIdDict);

                _logger.LogInformation("BookNewHearing - Successfully sent emails to participants- {Hearing}",
                                       hearingDetailsResponse.Id);

                _logger.LogInformation("BookNewHearing - Attempting assign participants to the correct group");
                await _hearingsService.AssignParticipantToCorrectGroups(hearingDetailsResponse, usernameAdIdDict);

                _logger.LogInformation("BookNewHearing - Successfully assigned participants to the correct group");


                if (request.IsMultiDay)
                {
                    var publicHolidays = await _publicHolidayRetriever.RetrieveUpcomingHolidays();

                    var listOfDates = DateListMapper.GetListOfWorkingDates(request.MultiHearingDetails.StartDate,
                                                                           request.MultiHearingDetails.EndDate, publicHolidays);
                    var totalDays = listOfDates.Select(x => x.DayOfYear).Distinct().Count() + 1; // include start date
                    await _hearingsService.SendMultiDayHearingConfirmationEmail(hearingDetailsResponse, totalDays);
                }
                else
                {
                    await _hearingsService.SendHearingConfirmationEmail(hearingDetailsResponse);
                }

                return(Created("", hearingDetailsResponse));
            }
            catch (BookingsApiException e)
            {
                _logger.LogError(e,
                                 "BookNewHearing - There was a problem saving the booking. Status Code {StatusCode} - Message {Message}",
                                 e.StatusCode, e.Response);
                if (e.StatusCode == (int)HttpStatusCode.BadRequest)
                {
                    return(BadRequest(e.Response));
                }

                throw;
            }
            catch (Exception e)
            {
                _logger.LogError(e, "BookNewHearing - Failed to save hearing - {Message} -  for request: {RequestBody}",
                                 e.Message, JsonConvert.SerializeObject(newBookingRequest));
                throw;
            }
        }