Skip to content

Commit

Permalink
feat: update post-profile lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
LIEN-YUHSIANG committed Nov 10, 2023
1 parent 7742ab7 commit c6f0c7d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/lambda/patch-profile/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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"]
}
Expand Down
9 changes: 5 additions & 4 deletions src/lambda/post-profile/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions src/lambda/post-profile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit c6f0c7d

Please sign in to comment.