-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetSiteMetaDataFunction.cs
More file actions
68 lines (64 loc) · 3.31 KB
/
GetSiteMetaDataFunction.cs
File metadata and controls
68 lines (64 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using FluentValidation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Nhs.Appointments.Api.Auth;
using Nhs.Appointments.Api.Models;
using Nhs.Appointments.Core.Inspectors;
using Nhs.Appointments.Core.Sites;
using Nhs.Appointments.Core.Users;
namespace Nhs.Appointments.Api.Functions.HttpFunctions;
public class GetSiteMetaDataFunction(
ISiteService siteService,
IValidator<SiteBasedResourceRequest> validator,
IUserContextProvider userContextProvider,
ILogger<GetSiteMetaDataFunction> logger,
IMetricsRecorder metricsRecorder)
: SiteBasedResourceFunction<GetSiteMetaDataResponse>(validator, userContextProvider, logger, metricsRecorder)
{
[OpenApiOperation(operationId: "GetSiteMetaData", tags: ["Sites"],
Summary = "Get meta data about the site specific to appointments")]
[OpenApiParameter("site", In = ParameterLocation.Path, Required = true, Type = typeof(string),
Description = "The id of the site to retrieve configuration for")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, "application/json", typeof(GetSiteMetaDataResponse),
Description = "The meta data for the specified site")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, "application/json",
typeof(IEnumerable<ErrorMessageResponseItem>),
Description = "The request did not contain a valid site in the query string")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, "application/json", typeof(ApiResult<object>),
Description = "No meta data was found for the specified site")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Unauthorized, "application/json",
typeof(ErrorMessageResponseItem), Description = "Unauthorized request to a protected API")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Forbidden, "application/json", typeof(ErrorMessageResponseItem),
Description = "Request failed due to insufficient permissions")]
[RequiresPermission(Permissions.ViewSiteMetadata, typeof(SiteFromQueryStringInspector))]
[Function("GetSiteMetaData")]
public override Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "sites/{site}/meta")]
HttpRequest req)
{
return base.RunAsync(req);
}
protected override async Task<ApiResult<GetSiteMetaDataResponse>> HandleRequest(SiteBasedResourceRequest request,
ILogger logger)
{
const string scope = "site_details";
var site = await siteService.GetSiteByIdAsync(request.Site, false, scope);
if (site != null)
{
var patientInformation = site.Accessibilities.Any()
? site.Accessibilities?.FirstOrDefault(a => a.Id == $"{scope}/info_for_citizen")?.Value ?? string.Empty
: string.Empty;
return ApiResult<GetSiteMetaDataResponse>.Success(
new GetSiteMetaDataResponse(site.Name, patientInformation));
}
return Failed(HttpStatusCode.NotFound, "No site configuration was found for the specified site");
}
}