Skip to content

Fixed wrong time format - #14

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions unicorn_contracts/src/contracts_service/create_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
28 changes: 13 additions & 15 deletions unicorn_contracts/src/contracts_service/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions unicorn_contracts/src/contracts_service/update_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = {
Expand Down