diff --git a/unicorn_contracts/src/contracts_service/create_contract.py b/unicorn_contracts/src/contracts_service/create_contract.py index e4b4e1e..93da913 100644 --- a/unicorn_contracts/src/contracts_service/create_contract.py +++ b/unicorn_contracts/src/contracts_service/create_contract.py @@ -11,7 +11,7 @@ from contracts_service.contract_status import ContractStatus from contracts_service.exceptions import EventValidationException -from contracts_service.helper import get_current_date, get_event_body, publish_event +from contracts_service.helper import get_stable_date, get_event_body, validate_event, publish_event, get_env # Initialise Environment variables if (SERVICE_NAMESPACE := os.environ.get("SERVICE_NAMESPACE")) is None: @@ -55,7 +55,7 @@ def lambda_handler(event, context): return ex.apigw_return # Create new Contract - current_date: str = get_current_date(context.aws_request_id) + current_date: str = get_stable_date(context.aws_request_id) contract = { "property_id": event_json["property_id"], # PK diff --git a/unicorn_contracts/src/contracts_service/helper.py b/unicorn_contracts/src/contracts_service/helper.py index f2e9814..163fd40 100644 --- a/unicorn_contracts/src/contracts_service/helper.py +++ b/unicorn_contracts/src/contracts_service/helper.py @@ -27,28 +27,26 @@ request_dates = {} @tracer.capture_method -def get_current_date(request_id=None): - """Return current date for this invocation. To keep the return value consistent while function is - queried multiple times, function maintains the value returned for each request id and returns same - value on subsequent requests. +def get_stable_date(id=None): + """Return current date for this invocation. The return value remains consistent + across multiple requests for the same id. Parameters ---------- - request_id : str - context.aws_request_id + id : str + identifier of the local context, e.g. context.aws_request_id Returns ------- str - Current date time i.e. '01/08/2022 20:36:30' + Current date time in iso format, e.g. '2023-02-10T10:04:14Z' """ - if request_id is not None and request_id in request_dates.keys(): - return request_dates[request_id] - - now_str = datetime.now().strftime("%d/%m/%Y %H:%M:%S") - logger.info(f"Time recorded: {now_str} for request {request_id}") - request_dates[request_id] = now_str - return now_str + if not (id in request_dates.keys()): + now_str = datetime.now().isoformat() + print(f"Time recorded: {now_str} for request {id}") + request_dates[id] = now_str + + return request_dates[id] @tracer.capture_method @@ -116,7 +114,7 @@ def publish_event(contract, request_id): return event_bridge.put_events( Entries=[ { - 'Time': get_current_date(request_id), + 'Time': get_stable_date(request_id), "Source": SERVICE_NAMESPACE, "DetailType": "ContractStatusChanged", "Detail": json.dumps(contract_status_changed_event), diff --git a/unicorn_contracts/src/contracts_service/update_contract.py b/unicorn_contracts/src/contracts_service/update_contract.py index cd3cb0b..903a910 100644 --- a/unicorn_contracts/src/contracts_service/update_contract.py +++ b/unicorn_contracts/src/contracts_service/update_contract.py @@ -13,7 +13,8 @@ from contracts_service.contract_status import ContractStatus from contracts_service.exceptions import (ContractNotFoundException, EventValidationException) -from contracts_service.helper import get_current_date, get_event_body, publish_event + +from contracts_service.helper import get_stable_date, get_event_body, publish_event, validate_event, get_env # Initialise Environment variables if (SERVICE_NAMESPACE := os.environ.get("SERVICE_NAMESPACE")) is None: @@ -66,7 +67,7 @@ def lambda_handler(event, context): return ex.apigw_return # Set current date - current_date = get_current_date(context.aws_request_id) + current_date = get_stable_date(context.aws_request_id) # define contract with approved status contract = {