Skip to content

Commit 1dc8862

Browse files
committed
made required in src and refactoring for test now
1 parent 613bd88 commit 1dc8862

14 files changed

+152
-170
lines changed

filenameprocessor/poetry.lock

Lines changed: 10 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

filenameprocessor/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["Your Name <[email protected]>"]
77
packages = [{ include = "src" }]
88

99
[tool.poetry.dependencies]
10-
python = "~3.10"
10+
python = "~3.11"
1111
"fhir.resources" = "~7.0.2"
1212
boto3 = "~1.38.42"
1313
boto3-stubs-lite = { extras = ["dynamodb"], version = "~1.38.42" }

filenameprocessor/src/clients.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,17 @@
1919

2020
dynamodb_resource = boto3_resource("dynamodb", region_name=REGION_NAME)
2121

22-
# Redis Client
23-
redis_client = redis.StrictRedis(host=os.getenv("REDIS_HOST"), port=os.getenv("REDIS_PORT"), decode_responses=True)
22+
23+
REDIS_HOST = os.getenv("REDIS_HOST", "")
24+
REDIS_PORT = int(os.getenv("REDIS_PORT", 6379))
25+
26+
27+
logging.basicConfig(level="INFO")
28+
logger = logging.getLogger()
29+
logger.info(f"Connecting to Redis at {REDIS_HOST}:{REDIS_PORT}")
30+
31+
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)
32+
2433

2534
# Logger
2635
logging.basicConfig(level="INFO")

filenameprocessor/src/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class Constants:
5757
# NOTE: Any ODS code not found in this dictionary's keys is invalid for this service
5858
ODS_TO_SUPPLIER_MAPPINGS = {
5959
"YGM41": "EMIS",
60+
6061
"8J1100001": "PINNACLE",
62+
"V0V8L":"MAVIS",
63+
"X8E5B":"RAVS",
6164
"8HK48": "SONAR",
6265
"YGA": "TPP",
6366
"0DE": "AGEM-NIVS",

filenameprocessor/src/elasticache.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

filenameprocessor/src/file_key_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from re import match
44
from datetime import datetime
55
from constants import Constants
6-
from elasticache import get_valid_vaccine_types_from_cache
6+
from vaccine_types import get_valid_vaccine_types_from_cache
77
from utils_for_filenameprocessor import identify_supplier
88
from errors import InvalidFileKeyError
99

filenameprocessor/src/file_name_processor.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from make_and_upload_ack_file import make_and_upload_the_ack_file
1515
from audit_table import upsert_audit_table, get_next_queued_file_details, ensure_file_is_not_a_duplicate
1616
from clients import logger
17-
from elasticache import upload_to_elasticache
1817
from logging_decorator import logging_decorator
1918
from supplier_permissions import validate_vaccine_type_permissions
2019
from errors import (
@@ -140,23 +139,12 @@ def handle_record(record) -> dict:
140139
"supplier": supplier
141140
}
142141

143-
elif "config" in bucket_name:
144-
try:
145-
upload_to_elasticache(file_key, bucket_name)
146-
logger.info("%s content successfully uploaded to cache", file_key)
147-
message = "File content successfully uploaded to cache"
148-
return {"statusCode": 200, "message": message, "file_key": file_key}
149-
except Exception as error: # pylint: disable=broad-except
150-
logger.error("Error uploading to cache for file '%s': %s", file_key, error)
151-
message = "Failed to upload file content to cache"
152-
return {"statusCode": 500, "message": message, "file_key": file_key, "error": str(error)}
153-
154142
else:
155143
try:
156144
vaccine_type, supplier = validate_file_key(file_key)
157145
logger.error("Unable to process file %s due to unexpected bucket name %s", file_key, bucket_name)
158146
message = f"Failed to process file due to unexpected bucket name {bucket_name}"
159-
147+
print(f"vaccine_type {vaccine_type}, supplier {supplier}")
160148
return {"statusCode": 500, "message": message, "file_key": file_key,
161149
"vaccine_type": vaccine_type, "supplier": supplier}
162150

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Functions for fetching supplier permissions"""
2+
3+
from clients import logger, redis_client
4+
import json
5+
from errors import VaccineTypePermissionsError
6+
7+
8+
def get_supplier_permissions(supplier: str) -> list[str]:
9+
"""
10+
Returns the permissions for the given supplier.
11+
Defaults return value is an empty list, including when the supplier has no permissions.
12+
"""
13+
14+
permissions_data = redis_client.hget("supplier_permissions", supplier)
15+
if not permissions_data:
16+
return []
17+
return json.loads(permissions_data)
18+
19+
def validate_vaccine_type_permissions(vaccine_type: str, supplier: str) -> list:
20+
"""
21+
Returns the list of permissions for the given supplier.
22+
Raises an exception if the supplier does not have at least one permission for the vaccine type.
23+
"""
24+
supplier_permissions = get_supplier_permissions(supplier)
25+
26+
# Validate that supplier has at least one permissions for the vaccine type
27+
if not any(permission.split(".")[0] == vaccine_type for permission in supplier_permissions):
28+
error_message = f"Initial file validation failed: {supplier} does not have permissions for {vaccine_type}"
29+
logger.error(error_message)
30+
raise VaccineTypePermissionsError(error_message)
31+
32+
return supplier_permissions

filenameprocessor/src/supplier_permissions.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
"""Functions for fetching supplier permissions"""
22

3-
from clients import logger
3+
from clients import logger, redis_client
4+
import json
5+
from constants import PERMISSIONS_CONFIG_FILE_KEY, VACCINE_TYPE_TO_DISEASES_HASH_KEY
46
from errors import VaccineTypePermissionsError
57
from elasticache import get_permissions_config_json_from_cache
68

79

8-
def get_supplier_permissions(supplier: str) -> list:
10+
def get_supplier_permissions(supplier: str) -> list[str]:
911
"""
1012
Returns the permissions for the given supplier.
1113
Defaults return value is an empty list, including when the supplier has no permissions.
1214
"""
13-
permissions_config = get_permissions_config_json_from_cache()
14-
return permissions_config.get("all_permissions", {}).get(supplier, [])
15+
16+
permissions_data = redis_client.hget(PERMISSIONS_CONFIG_FILE_KEY, supplier)
17+
if not permissions_data:
18+
return []
19+
return json.loads(permissions_data)
20+
21+
def get_permissions_config_json_from_cache() -> dict:
22+
"""Gets and returns the permissions config file content from ElastiCache (Redis)."""
23+
return json.loads(redis_client.get(PERMISSIONS_CONFIG_FILE_KEY))
1524

1625

1726
def validate_vaccine_type_permissions(vaccine_type: str, supplier: str) -> list:
@@ -22,7 +31,7 @@ def validate_vaccine_type_permissions(vaccine_type: str, supplier: str) -> list:
2231
supplier_permissions = get_supplier_permissions(supplier)
2332

2433
# Validate that supplier has at least one permissions for the vaccine type
25-
if not any(vaccine_type in permission for permission in supplier_permissions):
34+
if not any(permission.split(".")[0] == vaccine_type for permission in supplier_permissions):
2635
error_message = f"Initial file validation failed: {supplier} does not have permissions for {vaccine_type}"
2736
logger.error(error_message)
2837
raise VaccineTypePermissionsError(error_message)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
from clients import redis_client
3+
from constants import VACCINE_TYPE_TO_DISEASES_HASH_KEY
4+
5+
def get_valid_vaccine_types_from_cache() -> list[str]:
6+
return redis_client.hkeys(VACCINE_TYPE_TO_DISEASES_HASH_KEY)

0 commit comments

Comments
 (0)