-
-
Save navanathjadhav/b2f42b95141c9f764a73819754a0914a to your computer and use it in GitHub Desktop.
Clean Code Example
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
/** | |
* This method will verify user credentials and return adequate response | |
* @param userName username or email of user | |
* @param password password of the user | |
*/ | |
function login(userName, password) { | |
/* | |
* Validation: Check if username is not supplied | |
*/ | |
if (!userName) { | |
throw new Error(`No username`); | |
} | |
/* | |
* Validation: Check if password is not supplied | |
*/ | |
if (!password) { | |
throw new Error(`No password`); | |
} | |
/* | |
* DB Operation: Query the user by his credentials | |
*/ | |
const user = UserSchema.find({ email: userName, password: password }); | |
/* | |
* Validation: user not found, invalid credentials | |
*/ | |
if (!user) { | |
throw new Error(`Invalid credentials`); | |
} | |
let response = {}; | |
/* | |
* User status check: return response as per user status | |
*/ | |
switch (user.status) { | |
////////////////////////////// | |
// Active // | |
////////////////////////////// | |
case "Active": | |
response = { message: "Login successful", user: user }; | |
break; | |
////////////////////////////// | |
// Inactive // | |
////////////////////////////// | |
case "Inactive": | |
response = { message: "User is inactive", user: undefined }; | |
break; | |
////////////////////////////// | |
// Invited // | |
////////////////////////////// | |
case "Invited": | |
response = { message: "User is not activated yet", user: undefined }; | |
break; | |
////////////////////////////// | |
// Blocked // | |
////////////////////////////// | |
case "Blocked": | |
response = { message: "User is blocked", user: undefined }; | |
break; | |
////////////////////////////// | |
// Invalid Status // | |
////////////////////////////// | |
default: | |
response = { message: "User with invalid status", user: undefined }; | |
break; | |
} | |
/* | |
* Return response | |
*/ | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment