From f62f79a11d9703813b95556d178ba146cfcf816f Mon Sep 17 00:00:00 2001 From: Jason Park Date: Fri, 17 Nov 2023 03:06:58 +0900 Subject: [PATCH] fix: fixing keys for patch profile --- src/lambda/patch-profile/index.py | 15 +++++++++++---- src/lambda/patch-profile/utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/lambda/patch-profile/index.py b/src/lambda/patch-profile/index.py index 6eeea4335..7475b30f9 100644 --- a/src/lambda/patch-profile/index.py +++ b/src/lambda/patch-profile/index.py @@ -2,16 +2,17 @@ from boto3.dynamodb.conditions import Attr from datetime import datetime -from utils import JsonPayloadBuilder, table, resp_handler +from utils import JsonPayloadBuilder, table, resp_handler, extract_and_format_date @resp_handler -def patch_profile(uid, profile): +def patch_profile(uid, profile, created_date): dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' table.update_item( Key={ "uid": uid, + "created_at": created_date }, ConditionExpression=Attr('uid').eq(uid), UpdateExpression='SET #nm = :name, email = :email, #yr = :year, class_of = :class_of, languages = :languages, interests = :interests, school = :school, updated_at = :ts', @@ -37,11 +38,17 @@ def patch_profile(uid, profile): def handler(event, context): - req = json.loads(event['body']) params = { + "profile": req["data"], "uid": event['requestContext']['authorizer']['claims']['sub'], - "profile": req["data"] } + try: + formatted_time = extract_and_format_date(event) + if formatted_time: + params["created_date"] = formatted_time + except Exception as e: + print(f"An error occurred: {e}") + return patch_profile(**params) \ No newline at end of file diff --git a/src/lambda/patch-profile/utils.py b/src/lambda/patch-profile/utils.py index 47c061aee..abf5a186b 100644 --- a/src/lambda/patch-profile/utils.py +++ b/src/lambda/patch-profile/utils.py @@ -4,6 +4,7 @@ import logging import os from decimal import Decimal +from datetime import datetime, timezone # AWS DynamoDB Resources db = boto3.resource("dynamodb", region_name="ap-northeast-1") @@ -66,3 +67,27 @@ def handle(*args, **kwargs): return api_response(500, resp) return handle + +def extract_and_format_date(event): + try: + identities_str = event['requestContext']['authorizer']['claims']['identities'] + + # Debug print to see the identities string + print("Identities String:", identities_str) + + identities = json.loads(identities_str) + + date_created_at = identities.get("dateCreated") + formatted_time = format_time(date_created_at) + + return formatted_time + + except Exception as e: + print(f"Error in extract_and_format_date: {e}") + return None + +def format_time(timestamp_ms): + + timestamp_s = int(timestamp_ms) / 1000.0 + dt = datetime.fromtimestamp(timestamp_s, tz=timezone.utc) + return dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' \ No newline at end of file