Conversation
| @override_error_check | ||
| @ensure_environment_variables( | ||
| names=[ | ||
| "USER_RESTRICTIONS_TABLE_NAME", |
There was a problem hiding this comment.
double check this is the right var from the terraform
| body = event.get("body") | ||
| if not body: | ||
| logger.error("Missing request body") | ||
| return ApiGatewayResponse( | ||
| 400, "Missing request body", "POST" | ||
| ).create_api_gateway_response() | ||
|
|
||
| try: | ||
| payload = json.loads(body) | ||
| except json.JSONDecodeError as exc: | ||
| logger.error(exc) | ||
| return ApiGatewayResponse( | ||
| 400, "Invalid request body", "POST" | ||
| ).create_api_gateway_response() | ||
|
|
||
| # Required fields for creating a restriction. | ||
| smart_card_id = payload.get("smart_card_id") | ||
| nhs_number = payload.get("nhs_number") | ||
| if not smart_card_id or not nhs_number: | ||
| logger.error("Missing required fields") | ||
| return ApiGatewayResponse( | ||
| 400, "Missing required fields", "POST" | ||
| ).create_api_gateway_response() |
There was a problem hiding this comment.
this could be extracted into a separate function, parse_body?
think the body should always be valid JSON so might not need to handle this error
do we want a model to validate the request body against?
body will be coming from front end so probably camel case
| authorization = request_context.authorization or {} | ||
| creator = authorization.get("nhs_user_id") | ||
| ods_code = authorization.get("selected_organisation", {}).get("org_ods_code") | ||
| if not creator or not ods_code: | ||
| logger.error("Missing user context") | ||
| return ApiGatewayResponse( | ||
| 400, "Missing user context", "POST" | ||
| ).create_api_gateway_response() |
There was a problem hiding this comment.
we could use the ods_util here, or create a new one that does both the creator and ods to be used with the other lambdas too?
| creator: str, | ||
| ) -> UserRestriction: | ||
| if smart_card_id == creator: | ||
| raise ValueError("You cannot create a restriction for yourself") |
There was a problem hiding this comment.
create custom exception
| nhs_number=nhs_number, | ||
| custodian=ods_code, | ||
| creator=creator, | ||
| restricted_user_name=restricted_user_name, |
There was a problem hiding this comment.
not on the model, no need to write to db
| restriction = service.create_restriction( | ||
| smart_card_id=smart_card_id, | ||
| nhs_number=nhs_number, | ||
| ods_code=ods_code, |
| raise ValueError("You cannot create a restriction for yourself") | ||
|
|
||
| practitioner = self.healthcare_service.get_practitioner(smart_card_id) | ||
| restricted_user_name = f"{practitioner.first_name} {practitioner.last_name}" |
There was a problem hiding this comment.
believe we are returning this the the frontend, may need to change what is returned from this function
| @pytest.fixture | ||
| def set_env(monkeypatch, set_env): | ||
| monkeypatch.setenv("USER_RESTRICTIONS_TABLE_NAME", MOCK_RESTRICTIONS_TABLE) | ||
| monkeypatch.setenv("HEALTHCARE_WORKER_API_URL", HEALTHCARE_WORKER_API_URL) | ||
| yield | ||
|
|
| MOCK_SMART_CARD_ID = "smartcard-uuid-1234" | ||
| MOCK_CREATOR_ID = "creator-uuid-5678" | ||
| MOCK_RESTRICTIONS_TABLE = "test-user-restrictions-table" |
5d7ec4e to
8f6b18c
Compare
8f6b18c to
5af470e
Compare
Code security issues foundView full details here. |
|
| "HEALTHCARE_WORKER_API_URL", | ||
| ], | ||
| ) | ||
| @handle_lambda_exceptions |
|
|
||
| restricted_smartcard_id, nhs_number = parse_body(event.get("body")) | ||
|
|
||
| patient_id = (event.get("queryStringParameters") or {}).get("patientId") |
There was a problem hiding this comment.
extract_nhs_number_from_event
| """ | ||
| Errors for UserRestriction lambda | ||
| """ |
There was a problem hiding this comment.
could we make some of these more generic?
| raise CreateUserRestrictionLambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionMissingBody, | ||
| ) |
There was a problem hiding this comment.
should raise a generic e.g "RequestError" and allow the handler to handle status code?
| class CreateUserRestrictionService: | ||
| def __init__(self): | ||
| self.table_name = os.getenv("RESTRICTIONS_TABLE_NAME") | ||
| self.db_service = DynamoDBService() |
There was a problem hiding this comment.
use the user_restriction_dyanmodb_service from branch PRMP-1446? handles model validation etc for us
There was a problem hiding this comment.
should be in user_restrictions dir
| mock_request_context, | ||
| mock_user_restriction_enabled, | ||
| ): | ||
| mock_service.create_restriction.return_value = "some-uuid" |
There was a problem hiding this comment.
we can use TEST_UUID here
| if restricted_smartcard_id == creator: | ||
| raise UserRestrictionException( | ||
| "You cannot create a restriction for yourself", | ||
| ) |
There was a problem hiding this comment.
should the handler handle this?
| restriction = UserRestriction( | ||
| restricted_user=restricted_smartcard_id, | ||
| nhs_number=nhs_number, | ||
| custodian=custodian, |
There was a problem hiding this comment.
Do we need to call PDS to validate the custodian here?
| self.table_name = os.getenv("RESTRICTIONS_TABLE_NAME") | ||
| self.db_service = DynamoDBService() | ||
| self.healthcare_service = get_healthcare_worker_api_service() | ||
|
|
There was a problem hiding this comment.
Before creating we need to check that there isn't an existing one for the patient/user



Overview
Jira ticket: TBC
Description
Context
Checklist
Tasks for all changes:
I have run git pre-commits.(WIP)Deploy - Sandbox- workflow run - TBCSANDBOX Full- Deploy feature branch to sandbox- workflow run - TBCSANDBOX - UI Smoke Test- workflow run - TBCAdditional tasks for UI changes (delete if not applicable):