Skip to content

Commit

Permalink
fix: fixing keys for patch profile
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson committed Nov 16, 2023
1 parent 3b8954a commit f62f79a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/lambda/patch-profile/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
25 changes: 25 additions & 0 deletions src/lambda/patch-profile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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'

0 comments on commit f62f79a

Please sign in to comment.