Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIH-9919 Display booking failure reasons in AdminWeb #1301

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -296,23 +296,29 @@ public async Task Should_catch_bookings_api_exceptions_and_return_bad_request_if
BookingDetails = bookingDetails
};

const string expectedExceptionResponse = "exception";

_mocker.Mock<IUserAccountService>().Setup(x => x.GetAdUserIdForUsername(It.IsAny<string>())).ReturnsAsync(Guid.NewGuid().ToString());

const string key = "ScheduledDateTime";
const string errorMessage = "ScheduledDateTime cannot be in the past";
var validationProblemDetails = new ValidationProblemDetails(new Dictionary<string, string[]>
{
{key, new[] {errorMessage}},
});
_mocker.Mock<IBookingsApiClient>().Setup(x => x.BookNewHearingAsync(It.IsAny<V1.BookNewHearingRequest>()))
.Throws(new BookingsApiException("", (int) HttpStatusCode.BadRequest, expectedExceptionResponse, null, null));
.Throws(ClientException.ForBookingsAPIValidation(validationProblemDetails));


// Act
var result = await _controller.Post(bookingRequest);

// Assert
result.Result.Should().BeOfType<BadRequestObjectResult>();
var badRequestObjectResult = (BadRequestObjectResult) result.Result;
badRequestObjectResult.StatusCode.Should().Be((int)HttpStatusCode.BadRequest);
badRequestObjectResult.Value.Should().Be(expectedExceptionResponse);
result.Result.Should().NotBeNull();
var objectResult = (ObjectResult)result.Result;

var validationProblems = (ValidationProblemDetails)objectResult.Value;
validationProblems.Should().NotBeNull();
validationProblems!.Errors.ContainsKey(key).Should().BeTrue();
validationProblems.Errors[key][0].Should().Be(errorMessage);

_mocker.Mock<IHearingsService>().Verify(x => x.AssignEndpointDefenceAdvocates(It.IsAny<List<EndpointRequest>>(), It.Is<IReadOnlyCollection<AdminWebsite.Contracts.Requests.ParticipantRequest>>(x => x.SequenceEqual(bookingDetails.Participants.AsReadOnly()))), Times.Once);

_mocker.Mock<IBookingsApiClient>().Verify(x => x.BookNewHearingAsync(It.IsAny<V1.BookNewHearingRequest>()), Times.Once);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,14 @@ public async Task Should_pass_bad_request_from_bookings_api()
BookingDetails = hearing
};

const string key = "ScheduledDateTime";
const string errorMessage = "ScheduledDateTime cannot be in the past";
var validationProblemDetails = new ValidationProblemDetails(new Dictionary<string, string[]>
{
{key, new[] {errorMessage}},
});
_mocker.Mock<IBookingsApiClient>().Setup(x => x.BookNewHearingAsync(It.IsAny<BookNewHearingRequest>()))
.Throws(ClientException.ForBookingsAPI(HttpStatusCode.BadRequest));
.Throws(ClientException.ForBookingsAPIValidation(validationProblemDetails));

var result = await _controller.Post(bookingRequest);
result.Result.Should().BeOfType<BadRequestObjectResult>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ export class BHClient extends ApiClientBase {
_observableMergeMap((_responseText: string) => {
let result400: any = null;
let resultData400 = _responseText === '' ? null : JSON.parse(_responseText, this.jsonParseReviver);
result400 = ProblemDetails.fromJS(resultData400);
result400 = ValidationProblemDetails.fromJS(resultData400);
return throwException('Bad Request', status, _responseText, _headers, result400);
})
);
Expand Down
9 changes: 6 additions & 3 deletions AdminWebsite/AdminWebsite/Controllers/HearingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public HearingsController(IBookingsApiClient bookingsApiClient,
[HttpPost]
[SwaggerOperation(OperationId = "BookNewHearing")]
[ProducesResponseType(typeof(HearingDetailsResponse), (int)HttpStatusCode.Created)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType(typeof(ValidationProblemDetails),(int)HttpStatusCode.BadRequest)]
[HearingInputSanitizer]
public async Task<ActionResult<HearingDetailsResponse>> Post([FromBody] BookHearingRequest request)
{
Expand All @@ -101,8 +101,11 @@ public async Task<ActionResult<HearingDetailsResponse>> Post([FromBody] BookHear
{
_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);
if (e.StatusCode == (int)HttpStatusCode.BadRequest)
{
var typedException = e as BookingsApiException<ValidationProblemDetails>;
return ValidationProblem(typedException!.Result);
}
throw;
}
catch (Exception e)
Expand Down
Loading