Created
June 5, 2021 01:40
-
-
Save mosufy/7748ad6417f2f32b26647e5839b85af5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export const createNewAccount = async (params) => { | |
if (!params.username) { | |
throw new Error('Missing required username'); | |
} | |
if (!params.email) { | |
throw new Error ('Missing required email'); | |
} | |
if (params.username.length > 25) { | |
throw new Error('Invalid username more than 25 characters'); | |
} | |
if (params.username.length < 2) { | |
throw new Error('Invalid username less than 2 characters'); | |
} | |
const emailValidator = new EmailValidator(); | |
const validatedEmail = emailValidator.validate(params.email); | |
if (!validatedEmail) { | |
throw new Error('Invalid email'); | |
} | |
const usernameExists = db.select('*') | |
->from('users') | |
->where('username', username) | |
->first(); | |
if (Object.keys(usernameExists).length > 0) { | |
throw new Error('Username exists'); | |
} | |
const emailExists = db.select('*') | |
->from('users') | |
->where('email', email) | |
->first(); | |
if (Object.keys(emailExists).length > 0) { | |
throw new Error('Email exists'); | |
} | |
return db.insert('username,email') | |
->values(params) | |
->table('users'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment