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

feat: DTOSS-4476 create participant screening episode azure function #35

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 6 additions & 51 deletions scripts/database/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,6 @@ BEGIN
);
END

IF NOT EXISTS
(
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'ANALYTICS'
)
BEGIN
CREATE TABLE ANALYTICS
(
ID BIGINT NOT NULL IDENTITY(1, 1),
CONSTRAINT PK_ID
PRIMARY KEY (ID),
EPISODE_ID NVARCHAR (50) NULL,
EPISODE_TYPE NVARCHAR (50) NULL,
EPISODE_DATE NVARCHAR (50) NULL,
APPOINTMENT_MADE NVARCHAR (50) NULL,
DATE_OF_FOA NVARCHAR (50) NULL,
DATE_OF_AS NVARCHAR (50) NULL,
EARLY_RECALL_DATE NVARCHAR (50) NULL,
CALL_RECALL_STATUS_AUTHORISED_BY NVARCHAR (50) NULL,
END_CODE NVARCHAR (50) NULL,
END_CODE_LAST_UPDATED NVARCHAR (50) NULL,
BSO_ORGANISATION_CODE NVARCHAR (50) NULL,
BSO_BATCH_ID NVARCHAR (50) NULL,
NHS_NUMBER NVARCHAR (50) NULL,
GP_PRACTICE_ID NVARCHAR (50) NULL,
BSO_ORGANISATION_ID NVARCHAR (50) NULL,
NEXT_TEST_DUE_DATE NVARCHAR (50) NULL,
SUBJECT_STATUS_CODE NVARCHAR (50) NULL,
LATEST_INVITATION_DATE NVARCHAR (50) NULL,
REMOVAL_REASON NVARCHAR (50) NULL,
REMOVAL_DATE NVARCHAR (50) NULL,
CEASED_REASON NVARCHAR (50) NULL,
REASON_FOR_CEASED_CODE NVARCHAR (50) NULL,
REASON_DEDUCTED NVARCHAR (50) NULL,
IS_HIGHER_RISK NVARCHAR (50) NULL,
HIGHER_RISK_NEXT_TEST_DUE_DATE NVARCHAR (50) NULL,
HIGHER_RISK_REFERRAL_REASON_CODE NVARCHAR (50) NULL,
DATE_IRRADIATED NVARCHAR (50) NULL,
IS_HIGHER_RISK_ACTIVE NVARCHAR (50) NULL,
GENE_CODE NVARCHAR (50) NULL,
NTDD_CALCULATION_METHOD NVARCHAR (50) NULL,
PREFERRED_LANGUAGE NVARCHAR (50) NULL,
);
END



IF NOT EXISTS
(
SELECT *
Expand All @@ -92,7 +43,9 @@ IF NOT EXISTS
BEGIN
CREATE TABLE PARTICIPANT_SCREENING_PROFILE
(
NHS_NUMBER VARCHAR(50) NULL,
NHS_NUMBER NVARCHAR (50) NOT NULL,
CONSTRAINT PK_NHS_NUMBER
PRIMARY KEY (NHS_NUMBER),
SCREENING_NAME VARCHAR(50) NULL,
PRIMARY_CARE_PROVIDER VARCHAR(50) NULL,
PREFERRED_LANGUAGE VARCHAR(50) NULL,
Expand Down Expand Up @@ -124,7 +77,9 @@ IF NOT EXISTS
BEGIN
CREATE TABLE PARTICIPANT_SCREENING_EPISODE
(
EPISODE_ID VARCHAR(50) NULL,
EPISODE_ID NVARCHAR (50) NOT NULL,
CONSTRAINT PK_EPISODE_ID
PRIMARY KEY (EPISODE_ID),
SCREENING_NAME VARCHAR(50) NULL,
NHS_NUMBER VARCHAR(50) NULL,
EPISODE_TYPE VARCHAR(50) NULL,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Text;
using System.Text.Json;
using NHS.ServiceInsights.Data;
using NHS.ServiceInsights.Model;

namespace NHS.ServiceInsights.BIAnalyticsDataService;

public class CreateParticipantScreeningEpisode
{
private readonly ILogger<CreateParticipantScreeningEpisode> _logger;
private readonly IParticipantScreeningEpisodeRepository _participantScreeningEpisodeRepository;

public CreateParticipantScreeningEpisode(ILogger<CreateParticipantScreeningEpisode> logger, IParticipantScreeningEpisodeRepository participantScreeningEpisodeRepository)
{
_logger = logger;
_participantScreeningEpisodeRepository = participantScreeningEpisodeRepository;
}

[Function("CreateParticipantScreeningEpisode")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
ParticipantScreeningEpisode Episode = new ParticipantScreeningEpisode();

try
{
using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8))
{
var postData = reader.ReadToEnd();
Episode = JsonSerializer.Deserialize<ParticipantScreeningEpisode>(postData);
}
}
catch(Exception ex)
{
_logger.LogError("CreateParticipantScreeningEpisode: Could not read Json data.\nException: {ex}", ex);
return req.CreateResponse(HttpStatusCode.BadRequest);
}

try
{
bool successful = _participantScreeningEpisodeRepository.CreateParticipantEpisode(Episode);
if (!successful)
{
_logger.LogError("CreateParticipantScreeningEpisode: Could not save participant episode. Data: " + Episode);
return req.CreateResponse(HttpStatusCode.InternalServerError);
}

_logger.LogInformation("CreateParticipantScreeningEpisode: participant episode saved successfully.");

var response = req.CreateResponse(HttpStatusCode.OK);

return response;
}
catch (Exception ex)
{
_logger.LogError("CreateParticipantScreeningEpisode: Failed to save participant episode to the database.\nException: {ex}", ex);
return req.CreateResponse(HttpStatusCode.InternalServerError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddScoped<IAnalyticsRepository, AnalyticsRepository>();
services.AddScoped<IParticipantScreeningEpisodeRepository, ParticipantScreeningEpisodeRepository>();
services.AddDbContext<ServiceInsightsDbContext>(
options => options.UseSqlServer(Environment.GetEnvironmentVariable("ServiceInsightsDbConnectionString")));
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"ServiceInsightsDbConnectionString": "YOUR_CONNECTION_STRING"
},
"Host": {
"LocalHttpPort": 6008
"LocalHttpPort": 6010
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Text;
using System.Text.Json;
using NHS.ServiceInsights.Data;
using NHS.ServiceInsights.Model;

namespace NHS.ServiceInsights.BIAnalyticsDataService;

public class CreateParticipantScreeningProfile
{
private readonly ILogger<CreateParticipantScreeningProfile> _logger;
private readonly IParticipantScreeningProfileRepository _participantScreeningProfileRepository;

public CreateParticipantScreeningProfile(ILogger<CreateParticipantScreeningProfile> logger, IParticipantScreeningProfileRepository participantScreeningProfileRepository)
{
_logger = logger;
_participantScreeningProfileRepository = participantScreeningProfileRepository;
}

[Function("CreateParticipantScreeningProfile")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
ParticipantScreeningProfile Profile = new ParticipantScreeningProfile();

try
{
using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8))
{
var postData = reader.ReadToEnd();
Profile = JsonSerializer.Deserialize<ParticipantScreeningProfile>(postData);
}
}
catch(Exception ex)
{
_logger.LogError("CreateParticipantScreeningProfile: Could not read Json data.\nException: {ex}", ex);
return req.CreateResponse(HttpStatusCode.BadRequest);
}

try
{
bool successful = _participantScreeningProfileRepository.CreateParticipantProfile(Profile);
if (!successful)
{
_logger.LogError("CreateParticipantScreeningProfile: Could not save participant profile. Data: " + Profile);
return req.CreateResponse(HttpStatusCode.InternalServerError);
}

_logger.LogInformation("CreateParticipantScreeningProfile: participant profile saved successfully.");

var response = req.CreateResponse(HttpStatusCode.OK);

return response;
}
catch (Exception ex)
{
_logger.LogError("CreateParticipantScreeningProfile: Failed to save participant profile to the database.\nException: {ex}", ex);
return req.CreateResponse(HttpStatusCode.InternalServerError);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.15.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Shared\Data\Data.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NHS.ServiceInsights.Data;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddScoped<IParticipantScreeningProfileRepository, ParticipantScreeningProfileRepository>();
services.AddDbContext<ServiceInsightsDbContext>(
options => options.UseSqlServer(Environment.GetEnvironmentVariable("ServiceInsightsDbConnectionString")));
})
.Build();
host.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"CreateEpisode": {
"commandName": "Project",
"commandLineArgs": "--port 7254",
"launchBrowser": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"ServiceInsightsDbConnectionString": "YOUR_CONNECTION_STRING"
},
"Host": {
"LocalHttpPort": 6011
}
}
Loading
Loading