Skip to content

Commit 40b42cf

Browse files
committed
setup
1 parent a1ecd1d commit 40b42cf

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

backend/src/clients.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
"""Initialise s3, kinesis and lambda clients"""
1+
"""Initialise s3, kinesis, lambda and redis clients"""
22

33
from boto3 import client as boto3_client
4+
import os
5+
import logging
6+
import redis
47

5-
REGION_NAME = "eu-west-2"
8+
REGION_NAME = os.getenv("AWS_REGION", "eu-west-2")
69

710
s3_client = boto3_client("s3", region_name=REGION_NAME)
811
kinesis_client = boto3_client("kinesis", region_name=REGION_NAME)
912
lambda_client = boto3_client("lambda", region_name=REGION_NAME)
1013
firehose_client = boto3_client("firehose", region_name=REGION_NAME)
1114
sqs_client = boto3_client("sqs", region_name=REGION_NAME)
15+
16+
REDIS_HOST = os.getenv("REDIS_HOST", "")
17+
REDIS_PORT = int(os.getenv("REDIS_PORT", 6379))
18+
19+
20+
logging.basicConfig(level="INFO")
21+
logger = logging.getLogger()
22+
logger.setLevel("INFO")
23+
logger.info(f"Connecting to Redis at {REDIS_HOST}:{REDIS_PORT}")
24+
25+
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)

backend/src/models/errors.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,27 @@ class RecordProcessorError(Exception):
265265

266266
def __init__(self, diagnostics_dictionary: dict):
267267
self.diagnostics_dictionary = diagnostics_dictionary
268+
269+
class VaccineTypePermissionsError(Exception):
270+
"""
271+
Raised when a supplier tries to access a vaccine type they don't have permission for.
272+
"""
273+
274+
def __init__(self, message: str):
275+
super().__init__(message)
276+
self.message = message
277+
278+
def to_operation_outcome(self) -> dict:
279+
"""
280+
Converts the error to a FHIR-compliant OperationOutcome resource.
281+
"""
282+
return {
283+
"resourceType": "OperationOutcome",
284+
"issue": [
285+
{
286+
"severity": "error",
287+
"code": "forbidden",
288+
"diagnostics": self.message,
289+
}
290+
]
291+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from clients import redis_client
2+
from errors import VaccineTypePermissionsError
3+
import json
4+
5+
def get_supplier_permissions(supplier: str) -> list[str]:
6+
permissions_data = redis_client.hget("supplier_permissions", supplier)
7+
if not permissions_data:
8+
return []
9+
return json.loads(permissions_data)
10+
11+
def validate_vaccine_type_permissions(vaccine_type: str, supplier: str):
12+
permissions = get_supplier_permissions(supplier)
13+
if not any(vaccine_type in perm for perm in permissions):
14+
raise VaccineTypePermissionsError(f"{supplier} is not allowed to access {vaccine_type}")

0 commit comments

Comments
 (0)