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

PRMP-1188 Create a lambda to handle MNS notifications #470

Merged
merged 36 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d11669e
[PRMP-1188] create lambda to handle mns sqs messages
steph-torres-nhs Nov 21, 2024
76970c3
[PRMP-1188] introduce MNS SQS message model
steph-torres-nhs Nov 21, 2024
bc0fe45
[PRMP-1188] continue work on MNS notification handler
steph-torres-nhs Nov 22, 2024
81707f5
[PRMP-1188] start creating MNSNofiticationSerive
steph-torres-nhs Nov 22, 2024
aa21f1e
[PRMP-1188] add dynamo service to mns notification service
steph-torres-nhs Nov 22, 2024
f970374
[PRMP-1188] continue handle death notification process
steph-torres-nhs Nov 22, 2024
d608e19
[PRMP-1188] handle gp change notification
steph-torres-nhs Nov 22, 2024
e7b69bb
[PRMP-1188] add lambda to workflow
steph-torres-nhs Nov 25, 2024
488b81b
[PRMP-1188] remove unnecessary fields from model
steph-torres-nhs Nov 25, 2024
8c17876
[PMRP-1188] change table env name to correct name
steph-torres-nhs Nov 25, 2024
104f8c1
[PRMP-1188] refactor log messages
steph-torres-nhs Nov 26, 2024
d47671e
[PRMP-1188] implement sending message back to queue
steph-torres-nhs Nov 26, 2024
41cf2f4
[PRMP-1188] add new MNS_SQS_QUEUE env to conftest
steph-torres-nhs Nov 27, 2024
50c8573
Merge branch 'refs/heads/main' into PRMP-1188
NogaNHS Nov 27, 2024
395c46e
[PRMP-1188] fix unit test
NogaNHS Nov 27, 2024
5bcc4fd
[PRMP-1188] remove subscription from enum
steph-torres-nhs Nov 27, 2024
68b676a
[PRMP-1188] remove capital letters from test names
steph-torres-nhs Nov 27, 2024
dc50dce
Merge branch 'main' into PRMP-1188
steph-torres-nhs Nov 29, 2024
5d90083
[PMRP-1188] continue with PR comment ammendents
steph-torres-nhs Nov 29, 2024
681a5d6
[PMRP-1188] add fixtures methods used frequently in test file
steph-torres-nhs Nov 29, 2024
48b54d0
[PRMP-1188] implement updating lastUpdated field
steph-torres-nhs Dec 2, 2024
1f94d46
[PRMP-1188] add assert with to sending message back to queue test
steph-torres-nhs Dec 2, 2024
caaf8bd
[PRMP-1188] add conditional to deploy lambdas
steph-torres-nhs Dec 2, 2024
4ffb470
[PRMP-1188] change workflow conditional
steph-torres-nhs Dec 2, 2024
41b6ca3
[PRMP-1188] change workflow to deploy MNS lambda run when not in deve…
steph-torres-nhs Dec 2, 2024
7c0fe28
Merge branch 'main' into PRMP-1188
NogaNHS Dec 2, 2024
7943515
[PRMP-1188] try different conditional for conditional deployment of l…
steph-torres-nhs Dec 2, 2024
8422e69
Merge branch 'main' into PRMP-1188
steph-torres-nhs Dec 2, 2024
bbfd720
[PRMP-1188] address PR comments
steph-torres-nhs Dec 6, 2024
f21a205
Merge branch 'PRMP-1188' of https://github.com/nhsconnect/national-do…
steph-torres-nhs Dec 6, 2024
d1e93c2
Merge branch 'main' into PRMP-1188
NogaNHS Dec 10, 2024
9a8af85
Merge branch 'main' into PRMP-1188
NogaNHS Dec 10, 2024
c34ad77
Merge branch 'main' into PRMP-1188
oliverbeumkes-nhs Dec 13, 2024
85ea123
[PRMP-1188] remove conditional deploy of lambda, doesn't work as expe…
steph-torres-nhs Dec 20, 2024
26f26de
Merge branch 'main' into PRMP-1188
steph-torres-nhs Jan 3, 2025
1077882
Merge branch 'main' into PRMP-1188
steph-torres-nhs Jan 3, 2025
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
Prev Previous commit
Next Next commit
[PRMP-1188] continue work on MNS notification handler
steph-torres-nhs committed Nov 22, 2024
commit bc0fe45f5ec78c84a94035207277a7c31720cc19
4 changes: 4 additions & 0 deletions lambdas/enums/mns_notification_types.py
Original file line number Diff line number Diff line change
@@ -4,3 +4,7 @@
class MNSNotificationTypes(StrEnum):
CHANGE_OF_GP = "pds-change-of-gp-1"
oliverbeumkes-nhs marked this conversation as resolved.
Show resolved Hide resolved
DEATH_NOTIFICATION = "pds-death-notification-1"

@staticmethod
def list() -> list[str]:
return [str(type.value) for type in MNSNotificationTypes]
19 changes: 7 additions & 12 deletions lambdas/handlers/mns_notification_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json

from enums.mns_notification_types import MNSNotificationTypes
from models.mns_sqs_message import MNSSQSMessage
from services.process_mns_message_service import MNSNotificationService
from utils.audit_logging_setup import LoggingService
from utils.decorators.ensure_env_var import ensure_environment_variables
from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions
@@ -16,7 +18,6 @@
"APPCONFIG_CONFIGURATION",
"APPCONFIG_ENVIRONMENT",
"LLOYD_GEORGE_DYNAMODB_NAME",
# might not need the name of the queue, as this is what is trigging the lamdba
]
)
# need to check what this does
@@ -35,17 +36,11 @@ def lambda_handler(event, context):
mns_message = MNSSQSMessage(**sqs_message)
MNSSQSMessage.model_validate(mns_message)

NogaNHS marked this conversation as resolved.
Show resolved Hide resolved
return mns_message.subject.nhs_number
if mns_message.type in MNSNotificationTypes.list():
notification_service = MNSNotificationService(mns_message)
notification_service.handle_mns_notification()

continue
except Exception as error:
abbas-khan10 marked this conversation as resolved.
Show resolved Hide resolved
logger.error(f"Error processing SQS message: {error}.")
logger.info("Continuing to next message.")
pass

# how do we want to handle empty messages, bulk up load
# if "Records" not in event or len(event["Records"]) < 1:
# http_status_code = 400
# response_body = f"No sqs messages found in event: {event}. Event ignored."
# logger.error(response_body, {"Result": "Did not process MNS notification."})
# return ApiGatewayResponse(
# status_code=http_status_code, body=response_body, methods="GET"
# )
6 changes: 6 additions & 0 deletions lambdas/services/process_mns_message_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MNSNotificationService:
def __init__(self, message):
self.message = message

def handle_mns_notification(self):
pass
57 changes: 51 additions & 6 deletions lambdas/tests/unit/handlers/test_mns_notification_handler.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import json
from copy import deepcopy

import pytest
from enums.mns_notification_types import MNSNotificationTypes
from handlers.mns_notification_handler import lambda_handler
from unit.conftest import FAKE_URL, TEST_NHS_NUMBER, TEST_UUID
from unit.services.base.test_cloudwatch_logs_query_service import MOCK_START_TIME
from tests.unit.conftest import FAKE_URL, TEST_NHS_NUMBER, TEST_UUID

MOCK_DATE = "2010-10-22"

MOCK_GP_CHANGE_MESSAGE_BODY = {
"id": TEST_UUID,
"type": MNSNotificationTypes.CHANGE_OF_GP.value,
"subject": {
"nhs_number": TEST_NHS_NUMBER,
"family_name": "SMITH",
"dob": "2010-10-22",
"dob": MOCK_DATE,
},
"source": {
"name": FAKE_URL,
@@ -20,7 +23,7 @@
"value": TEST_UUID,
},
},
"time": f"{MOCK_START_TIME}",
"time": MOCK_DATE,
"data": {
"full_url": FAKE_URL,
"version_id": TEST_UUID,
@@ -35,9 +38,51 @@
},
}

MOCK_DEATH_MESSAGE_BODY = {}

MOCK_OTHER_NOTIFICATION_MESSAGE_BODY = deepcopy(MOCK_GP_CHANGE_MESSAGE_BODY)
MOCK_OTHER_NOTIFICATION_MESSAGE_BODY["type"] = "imms-vaccinations-1"


@pytest.fixture
def mock_service(mocker):
mocked_class = mocker.patch(
"handlers.mns_notification_handler.MNSNotificationService"
)
mocked_instance = mocked_class.return_value
return mocked_instance

def test_handler(context, set_env):

def test_handle_notification_called_message_type_GP_change(
context, set_env, mock_service
):

event = {"Records": [{"body": json.dumps(MOCK_GP_CHANGE_MESSAGE_BODY)}]}
lambda_handler(event, context)

mock_service.handle_mns_notification.assert_called()


def test_handle_notification_called_message_type_death_notification(
context, set_env, mock_service
):
pass


def test_handle_notification_not_called_message_type_not_death_or_GP_notification(
context, set_env, mock_service
):

event = {"Records": [{"body": json.dumps(MOCK_OTHER_NOTIFICATION_MESSAGE_BODY)}]}
lambda_handler(event, context)

mock_service.handle_mns_notification.assert_not_called()


def test_handle_notification_not_called_no_records_in_event(
context, set_env, mock_service
):
event = {"Records": []}
lambda_handler(event, context)

assert lambda_handler(event, context) == TEST_NHS_NUMBER
mock_service.handle_mns_notification.assert_not_called()