Created
November 8, 2024 09:44
-
-
Save tarasowski/0d21c75e1db99a6a657fd9bf4f9ac02e to your computer and use it in GitHub Desktop.
post user js lambda function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { randomUUID } from 'crypto' | |
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | |
import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | |
const client = new DynamoDBClient({}); | |
const docClient = DynamoDBDocumentClient.from(client); | |
const TABLE_NAME = "Techstarter" | |
const addToDB = async (entry) => { | |
const command = new PutCommand({ | |
TableName: TABLE_NAME, | |
Item: {...entry}, | |
}); | |
const response = await docClient.send(command); | |
console.log(response); | |
return response; | |
}; | |
function decodeBase64(base64String) { | |
try { | |
// Use the built-in atob() function to decode the Base64 string | |
const decodedString = atob(base64String); | |
return decodedString; | |
} catch (error) { | |
console.error('Error decoding Base64 string:', error); | |
return null; | |
} | |
} | |
export const handler = async (event) => { | |
const body = event.body | |
const decoded = decodeBase64(body) | |
const bodyObj = JSON.parse(decoded) | |
const userId = crypto.randomUUID() | |
const entry = { | |
userId: userId, | |
firstName: bodyObj.firstName, | |
lastName: bodyObj.lastName, | |
role: bodyObj.role | |
} | |
await addToDB(entry) | |
// TODO implement | |
// create a random uuid | |
// read from the body the data: firstName, lastName, role | |
// write this data to the database | |
// return a response with statusCode and a message | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify(entry), | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment