11// aws/handlers/registerHandler.js
2+ require ( "dotenv" ) . config ( ) ;
23
34const { DynamoDBClient } = require ( '@aws-sdk/client-dynamodb' ) ;
45const { DynamoDBDocumentClient, GetCommand, PutCommand } = require ( '@aws-sdk/lib-dynamodb' ) ;
56
6- const isLocal = process . env . IS_OFFLINE === "true" ;
7-
8- const ddb = DynamoDBDocumentClient . from (
9- new DynamoDBClient (
10- isLocal
11- ? {
12- region : "local" ,
13- endpoint : "http://localhost:8000" ,
14- credentials : {
15- accessKeyId : "dummy" ,
16- secretAccessKey : "dummy" ,
17- } ,
18- }
19- : { region : process . env . AWS_REGION }
20- )
21- ) ;
7+ const isLocal = "true" ;
8+
9+ console . log ( "Environment variables:" , {
10+ USERS_TABLE : process . env . USERS_TABLE ,
11+ AWS_REGION : process . env . AWS_REGION ,
12+ } ) ;
13+
14+ const ddbClientConfig = isLocal
15+ ? {
16+ region : "local" ,
17+ endpoint : "http://localhost:8000" ,
18+ credentials : {
19+ accessKeyId : "dummy" ,
20+ secretAccessKey : "dummy" ,
21+ } ,
22+ }
23+ : { region : process . env . AWS_REGION } ;
24+
25+ console . log ( "DynamoDB Client Config:" , ddbClientConfig ) ;
26+
27+ const ddb = DynamoDBDocumentClient . from ( new DynamoDBClient ( ddbClientConfig ) ) ;
2228
2329exports . handler = async ( event ) => {
2430 try {
31+ console . log ( "Received event:" , JSON . stringify ( event , null , 2 ) ) ;
32+
2533 const body = JSON . parse ( event . body || '{}' ) ;
34+ console . log ( "Parsed body:" , body ) ;
35+
2636 const { userId, fullName, email } = body ;
2737
2838 if ( ! userId || ! email || ! fullName ) {
39+ console . warn ( "Missing required fields in request body" ) ;
2940 return {
3041 statusCode : 400 ,
3142 body : JSON . stringify ( { error : 'Missing required user fields: userId, fullName, email' } ) ,
3243 } ;
3344 }
3445
46+ const tableName = process . env . USERS_TABLE ;
47+ console . log ( "Using DynamoDB table:" , tableName ) ;
48+
49+ if ( ! tableName ) {
50+ console . error ( "USERS_TABLE env variable is missing or empty" ) ;
51+ return {
52+ statusCode : 500 ,
53+ body : JSON . stringify ( { error : "Server misconfiguration: USERS_TABLE is not set" } ) ,
54+ } ;
55+ }
56+
3557 // Check if user already exists
58+ console . log ( `Checking if user ${ userId } exists in table ${ tableName } ...` ) ;
3659 const existingUser = await ddb . send (
3760 new GetCommand ( {
38- TableName : process . env . USERS_TABLE ,
61+ TableName : tableName ,
3962 Key : { userId } ,
4063 } )
4164 ) ;
4265
66+ console . log ( "DynamoDB GetCommand response:" , existingUser ) ;
67+
4368 if ( existingUser . Item ) {
44- // User already registered, return success with existing user info (optional)
69+ console . log ( ` User ${ userId } already exists` ) ;
4570 return {
4671 statusCode : 200 ,
4772 body : JSON . stringify ( {
@@ -64,13 +89,17 @@ exports.handler = async (event) => {
6489 createdAt : new Date ( ) . toISOString ( ) ,
6590 } ;
6691
67- await ddb . send (
92+ console . log ( "Creating new user:" , newUser ) ;
93+
94+ const putResult = await ddb . send (
6895 new PutCommand ( {
69- TableName : process . env . USERS_TABLE ,
96+ TableName : tableName ,
7097 Item : newUser ,
7198 } )
7299 ) ;
73100
101+ console . log ( "DynamoDB PutCommand result:" , putResult ) ;
102+
74103 return {
75104 statusCode : 201 ,
76105 body : JSON . stringify ( {
0 commit comments