Example #1
0
        public async Task <ActionResult <IList <PublicHolidayResponse> > > PublicHolidays()
        {
            var holidays = await _publicHolidayRetriever.RetrieveUpcomingHolidays();

            var response = holidays.Select(PublicHolidayResponseMapper.MapFrom).ToList();

            return(Ok(response));
        }
        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;
            }
        }