-
Notifications
You must be signed in to change notification settings - Fork 1
[PRMP-1465] Post User Restriction #1131
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
Merged
+1,208
−87
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
lambdas/handlers/user_restrictions/create_user_restriction_handler.py
SWhyteAnswer marked this conversation as resolved.
Show resolved
Hide resolved
SWhyteAnswer marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import json | ||
|
|
||
| from enums.feature_flags import FeatureFlags | ||
| from enums.lambda_error import LambdaError | ||
| from enums.logging_app_interaction import LoggingAppInteraction | ||
| from services.feature_flags_service import FeatureFlagService | ||
| from services.user_restrictions.create_user_restriction_service import ( | ||
| CreateUserRestrictionService, | ||
| ) | ||
| 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 | ||
| from utils.decorators.override_error_check import override_error_check | ||
| from utils.decorators.set_audit_arg import set_request_context_for_logging | ||
| from utils.decorators.validate_patient_id import ( | ||
| extract_nhs_number_from_event, | ||
| validate_patient_id, | ||
| ) | ||
| from utils.exceptions import ( | ||
| HealthcareWorkerAPIException, | ||
| HealthcareWorkerPractitionerModelException, | ||
| OdsErrorException, | ||
| UserRestrictionAlreadyExistsException, | ||
| ) | ||
| from utils.lambda_exceptions import LambdaException | ||
| from utils.lambda_response import ApiGatewayResponse | ||
| from utils.ods_utils import extract_creator_and_ods_code_from_request_context | ||
| from utils.request_context import request_context | ||
|
|
||
| logger = LoggingService(__name__) | ||
|
|
||
|
|
||
| def parse_body(body: str | None) -> tuple[str, str]: | ||
| if not body: | ||
| logger.error("Missing request body") | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionMissingBody, | ||
| ) | ||
|
|
||
| payload = json.loads(body) | ||
|
|
||
| restricted_smartcard_id = payload.get("smartcardId") | ||
| nhs_number = payload.get("nhsNumber") | ||
| if not restricted_smartcard_id or not nhs_number: | ||
| logger.error("Missing required fields") | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionMissingFields, | ||
| ) | ||
|
|
||
| return restricted_smartcard_id, nhs_number | ||
|
|
||
|
|
||
| @set_request_context_for_logging | ||
| @override_error_check | ||
| @ensure_environment_variables( | ||
| names=[ | ||
| "RESTRICTIONS_TABLE_NAME", | ||
| "HEALTHCARE_WORKER_API_URL", | ||
| ], | ||
| ) | ||
| @handle_lambda_exceptions | ||
| @validate_patient_id | ||
| def lambda_handler(event, context): | ||
| request_context.app_interaction = LoggingAppInteraction.USER_RESTRICTION.value | ||
|
|
||
| feature_flag_service = FeatureFlagService() | ||
| feature_flag_service.validate_feature_flag( | ||
| FeatureFlags.USER_RESTRICTION_ENABLED, | ||
| ) | ||
| logger.info("Starting create user restriction process") | ||
|
|
||
| restricted_smartcard_id, nhs_number = parse_body(event.get("body")) | ||
| request_context.patient_nhs_no = nhs_number | ||
|
|
||
| patient_id = extract_nhs_number_from_event(event) | ||
| if patient_id != nhs_number: | ||
| logger.error("patientId query param does not match nhs_number in request body") | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.PatientIdMismatch, | ||
| ) | ||
|
|
||
| try: | ||
| creator, ods_code = extract_creator_and_ods_code_from_request_context() | ||
| except OdsErrorException: | ||
| logger.error("Missing user context") | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionMissingContext, | ||
| ) | ||
|
|
||
| service = CreateUserRestrictionService() | ||
| try: | ||
| restriction_id = service.create_restriction( | ||
| restricted_smartcard_id=restricted_smartcard_id, | ||
| nhs_number=nhs_number, | ||
| custodian=ods_code, | ||
| creator=creator, | ||
| ) | ||
| except UserRestrictionAlreadyExistsException as exc: | ||
| logger.error(exc) | ||
| raise LambdaException( | ||
| 409, | ||
| LambdaError.CreateRestrictionAlreadyExists, | ||
| ) | ||
| except HealthcareWorkerAPIException as exc: | ||
| logger.error(exc) | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionInvalidWorker, | ||
| ) | ||
| except HealthcareWorkerPractitionerModelException as exc: | ||
| logger.error(exc) | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionPractitionerModelError, | ||
| ) | ||
|
|
||
| return ApiGatewayResponse( | ||
| 201, | ||
| json.dumps({"id": restriction_id}), | ||
| "POST", | ||
| ).create_api_gateway_response() |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
lambdas/services/user_restrictions/create_user_restriction_service.py
SWhyteAnswer marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from enums.lambda_error import LambdaError | ||
| from models.user_restrictions.user_restrictions import UserRestriction | ||
| from services.user_restrictions.user_restriction_dynamo_service import ( | ||
| UserRestrictionDynamoService, | ||
| ) | ||
| from services.user_restrictions.utilities import get_healthcare_worker_api_service | ||
| from utils.audit_logging_setup import LoggingService | ||
| from utils.exceptions import ( | ||
| UserRestrictionAlreadyExistsException, | ||
| ) | ||
| from utils.lambda_exceptions import LambdaException | ||
| from utils.utilities import get_pds_service | ||
|
|
||
| logger = LoggingService(__name__) | ||
|
|
||
|
|
||
| class CreateUserRestrictionService: | ||
| def __init__(self): | ||
| self.dynamo_service = UserRestrictionDynamoService() | ||
| self.healthcare_service = get_healthcare_worker_api_service() | ||
| self.pds_service = get_pds_service() | ||
|
|
||
| def create_restriction( | ||
| self, | ||
| restricted_smartcard_id: str, | ||
| nhs_number: str, | ||
| custodian: str, | ||
| creator: str, | ||
| ) -> str: | ||
| if restricted_smartcard_id == creator: | ||
| logger.error("You cannot create a restriction for yourself") | ||
| raise LambdaException( | ||
| 400, | ||
| LambdaError.CreateRestrictionSelfRestriction, | ||
| ) | ||
|
|
||
| patient = self.pds_service.fetch_patient_details(nhs_number) | ||
| if not patient: | ||
| logger.error("Patient not found in PDS") | ||
| raise LambdaException( | ||
| 404, | ||
| LambdaError.SearchPatientNoPDS, | ||
| ) | ||
| if patient.general_practice_ods != custodian: | ||
| logger.error( | ||
| "Patient's general practice ODS does not match request context ODS", | ||
| ) | ||
| raise LambdaException( | ||
| 403, | ||
| LambdaError.SearchPatientNoAuth, | ||
| ) | ||
|
|
||
| existing = self.dynamo_service.get_active_restriction( | ||
| nhs_number=nhs_number, | ||
| restricted_user=restricted_smartcard_id, | ||
| ) | ||
| if existing: | ||
| raise UserRestrictionAlreadyExistsException( | ||
| "A restriction already exists for this user and patient", | ||
| ) | ||
|
|
||
| self.healthcare_service.get_practitioner(restricted_smartcard_id) | ||
|
|
||
| restriction = UserRestriction( | ||
| restricted_user=restricted_smartcard_id, | ||
| nhs_number=nhs_number, | ||
| custodian=custodian, | ||
| creator=creator, | ||
| ) | ||
|
|
||
| self.dynamo_service.create_restriction_item(restriction) | ||
|
|
||
| logger.info("Created user restriction") | ||
| return restriction.id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.