Skip to content

Commit 4eef6fa

Browse files
committed
First pass at setting up a new lambda
1 parent 677f128 commit 4eef6fa

22 files changed

Lines changed: 410 additions & 4 deletions

.vscode/eps-prescription-status-update-api.code-workspace

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
"name": "packages/nhsNotifyLambda",
3333
"path": "../packages/nhsNotifyLambda"
3434
},
35+
{
36+
"name": "packages/nhsNotifyUpdateCallback",
37+
"path": "../packages/nhsNotifyUpdateCallback"
38+
},
3539
{
3640
"name": "packages/capabilityStatement",
3741
"path": "../packages/capabilityStatement"
@@ -97,6 +101,7 @@
97101
"mermade",
98102
"milliliter",
99103
"mkhl",
104+
"nhsapp",
100105
"nHSCHI",
101106
"NHSD",
102107
"nhsdlogin",

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ lint-node: compile-node
117117
npm run lint --workspace packages/cpsuLambda
118118
npm run lint --workspace packages/checkPrescriptionStatusUpdates
119119
npm run lint --workspace packages/nhsNotifyLambda
120+
npm run lint --workspace packages/nhsNotifyUpdateCallback
120121
npm run lint --workspace packages/common/testing
121122
npm run lint --workspace packages/common/middyErrorHandler
122123
npm run lint --workspace packages/common/commonTypes
@@ -147,6 +148,7 @@ test: compile
147148
npm run test --workspace packages/cpsuLambda
148149
npm run test --workspace packages/checkPrescriptionStatusUpdates
149150
npm run test --workspace packages/nhsNotifyLambda
151+
npm run test --workspace packages/nhsNotifyUpdateCallback
150152
npm run test --workspace packages/common/middyErrorHandler
151153

152154
clean:
@@ -164,6 +166,8 @@ clean:
164166
rm -rf packages/cpsuLambda/lib
165167
rm -rf packages/nhsNotifyLambda/coverage
166168
rm -rf packages/nhsNotifyLambda/lib
169+
rm -rf packages/nhsNotifyUpdateCallback/coverage
170+
rm -rf packages/nhsNotifyUpdateCallback/lib
167171
rm -rf packages/checkPrescriptionStatusUpdates/lib
168172
rm -rf packages/common/testing/lib
169173
rm -rf packages/common/middyErrorHandler/lib

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This is the AWS layer that provides an API for EPS Prescription Status Update.
2020
- `packages/capabilityStatement/` Returns a static capability statement.
2121
- `packages/cpsuLambda` Handles updating prescription status using a custom format.
2222
- `packages/nhsNotifyLambda` Handles sending prescription notifications to the NHS notify service.
23+
- `packages/nhsNotifyUpdateCallback` Handles receiving notification updates from the NHS notify service.
2324
- `scripts/` Utilities helpful to developers of this specification.
2425
- `postman/` Postman collections to call the APIs. Documentation on how to use them are in the collections.
2526
- `SAMtemplates/` Contains the SAM templates used to define the stacks.

SAMtemplates/apis/main.yaml

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ Parameters:
5454
Type: String
5555
Default: none
5656

57+
NHSNotifyUpdateCallbackFunctionName:
58+
Type: String
59+
Default: none
60+
61+
NHSNotifyUpdateCallbackFunctionArn:
62+
Type: String
63+
Default: none
64+
5765
LogRetentionInDays:
5866
Type: Number
5967

@@ -427,6 +435,32 @@ Resources:
427435
- StatusCode: "400"
428436
- StatusCode: "500"
429437

438+
NotificationDeliveryStatusCallbackMethod:
439+
Type: AWS::ApiGateway::Method
440+
Properties:
441+
RestApiId: !Ref RestApiGateway
442+
ResourceId: !Ref NotificationDeliveryStatusCallbackResource
443+
HttpMethod: POST
444+
AuthorizationType: NONE # TODO: They authenticate with a JWT header
445+
Integration:
446+
Type: AWS_PROXY
447+
Credentials: !GetAtt RestApiGatewayResources.Outputs.ApiGwRoleArn
448+
IntegrationHttpMethod: POST
449+
Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${NHSNotifyUpdateCallbackFunctionArn}/invocations
450+
MethodResponses:
451+
- StatusCode: "202"
452+
- StatusCode: "401"
453+
- StatusCode: "403"
454+
- StatusCode: "429"
455+
- StatusCode: "500"
456+
457+
NotificationDeliveryStatusCallbackResource:
458+
Type: AWS::ApiGateway::Resource
459+
Properties:
460+
RestApiId: !Ref RestApiGateway
461+
ParentId: !GetAtt RestApiGateway.RootResourceId
462+
PathPart: notification-delivery-status-callback
463+
430464
StatusLambdaMethodResource:
431465
Type: AWS::ApiGateway::Resource
432466
Properties:
@@ -516,7 +550,7 @@ Resources:
516550
# if you add a new endpoint, then change the name of this resource
517551
# also need to change it in RestApiGatewayStage.Properties.DeploymentId
518552
# *********************************************************************
519-
RestApiGatewayDeploymentV1f:
553+
RestApiGatewayDeploymentV2f:
520554
Type: AWS::ApiGateway::Deployment
521555
DependsOn:
522556
# see note above if you add something in here when you add a new endpoint
@@ -525,6 +559,7 @@ Resources:
525559
- CapabilityStatementMethod
526560
- Format1UpdatePrescriptionStatusMethod
527561
- CheckPrescriptionStatusUpdatesWaitCondition
562+
- NotificationDeliveryStatusCallbackMethod
528563
# see note above if you add something in here when you add a new endpoint
529564
Properties:
530565
RestApiId: !Ref RestApiGateway
@@ -533,7 +568,7 @@ Resources:
533568
Type: AWS::ApiGateway::Stage
534569
Properties:
535570
RestApiId: !Ref RestApiGateway
536-
DeploymentId: !Ref RestApiGatewayDeploymentV1f
571+
DeploymentId: !Ref RestApiGatewayDeploymentV2f
537572
StageName: prod
538573
TracingEnabled: true
539574
AccessLogSetting:
@@ -557,6 +592,7 @@ Resources:
557592
- - Fn::ImportValue: !Sub ${StackName}:state-machines:${UpdatePrescriptionStatusStateMachineName}:ExecuteStateMachinePolicy
558593
- Fn::ImportValue: !Sub ${StackName}:functions:${StatusFunctionName}:ExecuteLambdaPolicyArn
559594
- Fn::ImportValue: !Sub ${StackName}:functions:${CapabilityStatementFunctionName}:ExecuteLambdaPolicyArn
595+
- Fn::ImportValue: !Sub ${StackName}:functions:${NHSNotifyUpdateCallbackFunctionName}:ExecuteLambdaPolicyArn
560596
- Fn::ImportValue: !Sub ${StackName}:state-machines:${Format1UpdatePrescriptionsStatusStateMachineName}:ExecuteStateMachinePolicy
561597
- !If
562598
- ShouldDeployCheckPrescriptionStatusUpdate

SAMtemplates/functions/main.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,53 @@ Resources:
440440
- Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStateTableName}:TableWritePolicyArn
441441
- Fn::ImportValue: !Sub ${StackName}:tables:UsePrescriptionNotificationStateKMSKeyPolicyArn
442442

443+
NHSNotifyUpdateCallback:
444+
Type: AWS::Serverless::Function
445+
Properties:
446+
FunctionName: !Sub ${StackName}-NHSNotifyUpdateCallback
447+
CodeUri: ../../packages/
448+
Handler: nhsNotifyUpdateCallback.lambdaHandler
449+
Role: !GetAtt NHSNotifyUpdateCallbackResources.Outputs.LambdaRoleArn
450+
Environment:
451+
Variables:
452+
LOG_LEVEL: !Ref LogLevel
453+
TABLE_NAME: !Ref PrescriptionNotificationStateTableName
454+
Metadata:
455+
BuildMethod: esbuild
456+
guard:
457+
SuppressedRules:
458+
- LAMBDA_DLQ_CHECK
459+
- LAMBDA_INSIDE_VPC
460+
- LAMBDA_CONCURRENCY_CHECK
461+
BuildProperties:
462+
Minify: true
463+
Target: es2020
464+
Sourcemap: true
465+
tsconfig: nhsNotifyUpdateCallback/tsconfig.json
466+
packages: bundle
467+
EntryPoints:
468+
- nhsNotifyUpdateCallback/src/lambdaHandler.ts
469+
470+
NHSNotifyUpdateCallbackResources:
471+
Type: AWS::Serverless::Application
472+
Properties:
473+
Location: lambda_resources.yaml
474+
Parameters:
475+
StackName: !Ref StackName
476+
LambdaName: !Sub ${StackName}-NHSNotifyUpdateCallback
477+
LambdaArn: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${StackName}-NHSNotifyUpdateCallback
478+
IncludeAdditionalPolicies: true
479+
AdditionalPolicies: !Join
480+
- ","
481+
- - Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStateTableName}:TableReadPolicyArn
482+
- Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStateTableName}:TableWritePolicyArn
483+
- Fn::ImportValue: !Sub ${StackName}:tables:UsePrescriptionNotificationStateKMSKeyPolicyArn
484+
LogRetentionInDays: !Ref LogRetentionInDays
485+
CloudWatchKMSKeyId: !ImportValue account-resources:CloudwatchLogsKmsKeyArn
486+
EnableSplunk: !Ref EnableSplunk
487+
SplunkSubscriptionFilterRole: !ImportValue lambda-resources:SplunkSubscriptionFilterRole
488+
SplunkDeliveryStreamArn: !ImportValue lambda-resources:SplunkDeliveryStream
489+
443490
Outputs:
444491
UpdatePrescriptionStatusFunctionName:
445492
Description: The function name of the UpdatePrescriptionStatus lambda
@@ -506,3 +553,11 @@ Outputs:
506553
NotifyProcessorFunctionArn:
507554
Description: The function ARN of the NHS Notify lambda
508555
Value: !GetAtt NotifyProcessor.Arn
556+
557+
NHSNotifyUpdateCallbackFunctionName:
558+
Description: The function name of the NHSNotifyUpdateCallback lambda
559+
Value: !Ref NHSNotifyUpdateCallback
560+
561+
NHSNotifyUpdateCallbackFunctionArn:
562+
Description: The function ARN of the NHSNotifyUpdateCallback lambda
563+
Value: !GetAtt NHSNotifyUpdateCallback.Arn

SAMtemplates/main_template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ Resources:
131131
CapabilityStatementFunctionArn: !GetAtt Functions.Outputs.CapabilityStatementFunctionArn
132132
CheckPrescriptionStatusUpdatesFunctionName: !GetAtt Functions.Outputs.CheckPrescriptionStatusUpdatesFunctionName
133133
CheckPrescriptionStatusUpdatesFunctionArn: !GetAtt Functions.Outputs.CheckPrescriptionStatusUpdatesFunctionArn
134+
NHSNotifyUpdateCallbackFunctionName: !GetAtt Functions.Outputs.NHSNotifyUpdateCallbackFunctionName
135+
NHSNotifyUpdateCallbackFunctionArn: !GetAtt Functions.Outputs.NHSNotifyUpdateCallbackFunctionArn
134136
LogRetentionInDays: !Ref LogRetentionInDays
135137
EnableSplunk: !Ref EnableSplunk
136138
DeployCheckPrescriptionStatusUpdate: !Ref DeployCheckPrescriptionStatusUpdate

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"packages/cpsuLambda",
2424
"packages/checkPrescriptionStatusUpdates",
2525
"packages/nhsNotifyLambda",
26+
"packages/nhsNotifyUpdateCallback",
2627
"packages/common/testing",
2728
"packages/common/middyErrorHandler",
2829
"packages/common/commonTypes"

packages/nhsNotifyLambda/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {DynamoDBDocumentClient, GetCommand, PutCommand} from "@aws-sdk/lib-dynam
1010

1111
import {PSUDataItem} from "@PrescriptionStatusUpdate_common/commonTypes"
1212

13+
import {v4} from "uuid"
14+
1315
const TTL_DELTA = 60 * 60 * 24 * 7 // Keep records for a week
1416

1517
const dynamoTable = process.env.TABLE_NAME
@@ -167,6 +169,7 @@ export interface LastNotificationStateType {
167169
MessageID: string // The SQS message ID
168170
LastNotifiedPrescriptionStatus: string
169171
DeliveryStatus: string
172+
NotifyMessageID: string // The UUID we got back from Notify for the submitted message
170173
LastNotificationRequestTimestamp: string // ISO-8601 string
171174
ExpiryTime: number // DynamoDB expiration time (UNIX timestamp)
172175
}
@@ -191,6 +194,7 @@ export async function addPrescriptionMessagesToNotificationStateStore(
191194
MessageID: data.MessageId!,
192195
LastNotifiedPrescriptionStatus: data.PSUDataItem.Status,
193196
DeliveryStatus: "requested",
197+
NotifyMessageID: v4(), // Dummy message ID
194198
LastNotificationRequestTimestamp: new Date().toISOString(),
195199
ExpiryTime: (Math.floor(+new Date() / 1000) + TTL_DELTA)
196200
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable no-undef */
2+
process.env.TABLE_NAME = "dummy_table";
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"name": "vscode-jest-tests.v2",
10+
"request": "launch",
11+
"args": [
12+
"--runInBand",
13+
"--watchAll=false",
14+
"--testNamePattern",
15+
"${jest.testNamePattern}",
16+
"--runTestsByPath",
17+
"${jest.testFile}",
18+
"--config",
19+
"${workspaceFolder}/jest.debug.config.ts"
20+
],
21+
"cwd": "${workspaceFolder}",
22+
"console": "integratedTerminal",
23+
"internalConsoleOptions": "neverOpen",
24+
"disableOptimisticBPs": true,
25+
"program": "${workspaceFolder}/../../node_modules/.bin/jest",
26+
"windows": {
27+
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
28+
},
29+
"env": {
30+
"POWERTOOLS_DEV": true,
31+
"NODE_OPTIONS": "--experimental-vm-modules"
32+
}
33+
}
34+
]
35+
}

0 commit comments

Comments
 (0)