From c6f0c7dfe0f03a006703e403c0928d99893f12ee Mon Sep 17 00:00:00 2001 From: "Y.H LIEN" Date: Fri, 10 Nov 2023 21:03:58 +0900 Subject: [PATCH] feat: update post-profile lambda --- src/lambda/patch-profile/index.py | 4 +--- src/lambda/post-profile/index.py | 9 +++++---- src/lambda/post-profile/utils.py | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/lambda/patch-profile/index.py b/src/lambda/patch-profile/index.py index b8fa643ba..80df9a5d6 100644 --- a/src/lambda/patch-profile/index.py +++ b/src/lambda/patch-profile/index.py @@ -6,13 +6,12 @@ @resp_handler -def patch_profile(ts, uid, profile): +def patch_profile(uid, profile): dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' table.update_item( Key={ "uid": uid, - "created_at": ts, }, ConditionExpression=Attr('uid').eq(uid), UpdateExpression='SET name = :name, email = :email, year = :year, class_of = :class_of, languages = :languages, interests = :interests, school = :school, updated_at = :ts', @@ -37,7 +36,6 @@ def handler(event, context): req = json.loads(event['body']) params = { - "ts": event["queryStringParameters"]["ts"], "uid": event['requestContext']['authorizer']['claims']['sub'], "profile": req["data"] } diff --git a/src/lambda/post-profile/index.py b/src/lambda/post-profile/index.py index b1ce6761b..fb7e9bd15 100644 --- a/src/lambda/post-profile/index.py +++ b/src/lambda/post-profile/index.py @@ -2,13 +2,14 @@ import json from datetime import datetime -from utils import JsonPayloadBuilder, table, resp_handler +from utils import JsonPayloadBuilder, table, resp_handler, get_user_creation_date @resp_handler def post_profile(profile, uid): - dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' + # dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' + created_date = get_user_creation_date(uid) user_profile = { "uid": uid, @@ -19,8 +20,8 @@ def post_profile(profile, uid): "languages" : profile["languages"], "interests" : profile["interests"], "school" : profile["school"], - "created_at": dt_now, - "updated_at": dt_now, + "created_at": created_date, + "updated_at": created_date, } table.put_item(Item=user_profile) diff --git a/src/lambda/post-profile/utils.py b/src/lambda/post-profile/utils.py index 47c061aee..ebd01c2ec 100644 --- a/src/lambda/post-profile/utils.py +++ b/src/lambda/post-profile/utils.py @@ -4,11 +4,14 @@ import logging import os from decimal import Decimal +from datetime import datetime # AWS DynamoDB Resources db = boto3.resource("dynamodb", region_name="ap-northeast-1") table = db.Table(os.getenv('TABLE_NAME')) +# cognito client use to extract user created time +cognito_client = boto3.client('cognito-idp') class DecimalEncoder(json.JSONEncoder): def default(self, obj): @@ -66,3 +69,20 @@ def handle(*args, **kwargs): return api_response(500, resp) return handle + + +def get_user_creation_date(uid): + + # Make the API call to get the user details + response = cognito_client.admin_get_user( + UserPoolId='ap-northeast-1_dKhj1aZQy', + Username=uid + ) + + user_created_date_str = response['UserCreateDate'] + + date_created = datetime.strptime(user_created_date_str, '%Y-%m-%dT%H:%M:%S.%fZ') + + formatted_date = date_created.strftime('%Y-%m-%d %H:%M:%S') + + return formatted_date \ No newline at end of file