-
Notifications
You must be signed in to change notification settings - Fork 14
adding plan for the user #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
ed2157e
5635eb8
5ae257a
5d364d2
7f6cde4
c82c466
2478dfd
a378f27
5e39433
ee8ca7d
e9b29f2
8223de2
a192303
e7c6fc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your changes to this page conflict with the UI changes to the page #287, and I'm not comfortable with the current use of For now, I think you can either try to resolve the conflicts, and if you need help in that, get in contact with the rest of the team. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { | ||
DynamoDBDocumentClient, | ||
GetCommand, | ||
UpdateCommand, | ||
} from '@aws-sdk/lib-dynamodb'; | ||
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | ||
import { Table } from 'sst/node/table'; | ||
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; | ||
|
||
const client = new DynamoDBClient({}); | ||
const dynamoDb = DynamoDBDocumentClient.from(client); | ||
|
||
export const main = async ( | ||
event: APIGatewayProxyEvent, | ||
): Promise<APIGatewayProxyResult> => { | ||
console.log('Lambda function invoked'); | ||
console.log('Event body:', event.body); | ||
|
||
let data; | ||
try { | ||
if (!event.body) { | ||
throw new Error('Request body is missing'); | ||
} | ||
const body = event.isBase64Encoded | ||
? Buffer.from(event.body, 'base64').toString('utf-8') | ||
: event.body; | ||
data = JSON.parse(body); | ||
} catch (err) { | ||
console.error('Error parsing request body:', err.message); | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ error: 'Invalid JSON in request body' }), | ||
}; | ||
} | ||
|
||
const planType = data.planType; | ||
console.log('Received plan type:', planType); | ||
const level = data.level; | ||
console.log('Received level:', level); | ||
|
||
const userId = event.requestContext.authorizer?.jwt.claims.sub; | ||
console.log('user id: ', userId); | ||
|
||
if (!userId) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ error: 'User ID not found in the token' }), | ||
}; | ||
} | ||
|
||
const tableName = Table.Records.tableName; | ||
|
||
if (!tableName) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ error: 'Table name not set' }), | ||
}; | ||
} | ||
|
||
const key = { | ||
PK: userId, | ||
SK: 'plan', // Fixed SK value | ||
}; | ||
|
||
try { | ||
console.log('Fetching existing item...'); | ||
const getParams = { TableName: tableName, Key: key }; | ||
const getCommand = new GetCommand(getParams); | ||
const result = await dynamoDb.send(getCommand); | ||
const existingItem = result.Item; | ||
|
||
if (!existingItem) { | ||
console.log('Item not found'); | ||
return { | ||
statusCode: 404, | ||
body: JSON.stringify({ error: 'Item not found' }), | ||
}; | ||
} | ||
|
||
console.log('Item found, updating...'); | ||
const updateParams = { | ||
TableName: tableName, | ||
Key: key, | ||
UpdateExpression: `set #planType = :value`, | ||
ExpressionAttributeNames: { | ||
'#planType': planType, | ||
}, | ||
Comment on lines
+120
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before this add a check to limit |
||
ExpressionAttributeValues: { | ||
':value': 'test', // Update with the value you want | ||
}, | ||
ReturnValues: 'ALL_NEW' as const, | ||
}; | ||
|
||
const updateCommand = new UpdateCommand(updateParams); | ||
const updateResult = await dynamoDb.send(updateCommand); | ||
console.log('Item updated successfully'); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
status: 'success', | ||
updatedAttributes: updateResult.Attributes, | ||
}), | ||
}; | ||
} catch (error) { | ||
console.error('Error updating item:', error.message); | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ | ||
error: 'Could not update item', | ||
details: error.message, | ||
}), | ||
}; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we even need this page? Please confirm on me if we can just delete it or we still need it.
@saraisa12 @ThatIsHero