Created
September 23, 2020 02:32
-
-
Save damiancipolat/dde145bd03f85093296b20830eb2061f to your computer and use it in GitHub Desktop.
This is a lambda to handle an api beta tester landing registration in a table.
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
const AWS = require('aws-sdk'); | |
//Define aws region | |
AWS.config.update({ | |
region:'us-east-1' | |
}); | |
const TABLE = 'landing_page_emails'; | |
//Get aws dynamo client instance. | |
const docClient = new AWS.DynamoDB.DocumentClient(); | |
//Register data in dynamodb table. | |
const addElement = (TableName,element)=>{ | |
//Create params. | |
const params = { | |
TableName, | |
Item: element | |
}; | |
return docClient.put(params).promise(); | |
} | |
//DynamoDB find items. | |
const findEmail = (email) => { | |
const params = { | |
TableName: TABLE, | |
FilterExpression: "#email_field = :email_val", | |
ExpressionAttributeNames: { | |
"#email_field": "email", | |
}, | |
ExpressionAttributeValues: { ":email_val": email } | |
}; | |
//Run query as promise. | |
return docClient.scan(params).promise(); | |
} | |
//Detect if the email is duplicated. | |
const isDuplicated = async(email)=>{ | |
//Find email in the BD. | |
const found = await findEmail(email); | |
//Detect if is duplicated. | |
return (found && found.Items && found.Items.length > 0); | |
} | |
//Save the email, create user randomly. | |
const saveEmail = async (email)=>{ | |
const emailObj = { | |
user_id: `beta-${new Date().getTime()}`, | |
}; | |
//Record in the bd. | |
return await addElement(TABLE,emailObj); | |
} | |
//Validate email string. | |
const validateEmail = (email)=>{ | |
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return re.test(String(email).toLowerCase()); | |
} | |
//Make a generic error api builder. | |
const errorFormatter = (error)=>{ | |
return { | |
statusCode: (error&&error.statusCode)?error.statusCode:500, | |
body:JSON.stringify({ | |
message:(error&&error.message)?error.message:'Internal error' | |
}) | |
}; | |
} | |
//Lambda handler. | |
const emailHandler = async (event,context) => { | |
try{ | |
const { | |
body | |
} = event; | |
//Validate body. | |
if (!body) | |
throw {statusCode:400, message:'Bad request format'}; | |
const { | |
} = JSON.parse(body); | |
//Validate email format. | |
if (!(email&&validateEmail(email))) | |
throw {statusCode:400, message:'Wrong email format'}; | |
//Detect if is duplicated. | |
if (await isDuplicated(email)) | |
throw {statusCode:406, message:'Email alredy registered'}; | |
console.info('Registration request', event.body); | |
//Record in the table. | |
const saved = await saveEmail(email); | |
return { | |
statusCode:200, | |
body:JSON.stringify({ | |
email, | |
message:"Email registered OK" | |
}) | |
}; | |
} catch(error){ | |
console.error('ERROR',error); | |
return errorFormatter(error); | |
} | |
}; | |
exports.handler = emailHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment