From 1cd48ed5fbd0a8c354fb228026644c7dc926f25f Mon Sep 17 00:00:00 2001 From: Lakshay Manchanda Date: Mon, 19 Aug 2024 01:49:19 +0530 Subject: [PATCH] Resolved comments --- app/controllers/new-signup.js | 105 +++++++++++++++++----------------- app/templates/new-signup.hbs | 25 +++++--- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/app/controllers/new-signup.js b/app/controllers/new-signup.js index 4b61935f..d947581c 100644 --- a/app/controllers/new-signup.js +++ b/app/controllers/new-signup.js @@ -13,6 +13,7 @@ import { toastNotificationTimeoutOptions } from '../constants/toast-notification export default class NewSignUpController extends Controller { @service analytics; @service featureFlag; + @service toast; queryParams = ['currentStep', 'dev']; @@ -44,6 +45,9 @@ export default class NewSignUpController extends Controller { } async generateUsername(firstname, lastname) { + if (typeof firstname !== 'string' || typeof lastname !== 'string') { + throw new Error('Invalid input: firstname and lastname must be strings'); + } try { const sanitizedFirstname = firstname.toLowerCase(); const sanitizedLastname = lastname.toLowerCase(); @@ -63,12 +67,16 @@ export default class NewSignUpController extends Controller { if (user && user.username) { return user; } + throw new Error( + 'Username generation failed: Invalid response from server' + ); } catch (err) { this.toast.error( ERROR_MESSAGES.usernameGeneration, 'error!', toastNotificationTimeoutOptions ); + throw new Error(ERROR_MESSAGES.usernameGeneration); } } @@ -122,34 +130,36 @@ export default class NewSignUpController extends Controller { } @action async signup() { - const user = await this.generateUsername( - this.signupDetails.firstName, - this.signupDetails.lastName - ); - const signupDetails = { - first_name: this.signupDetails.firstName, - last_name: this.signupDetails.lastName, - username: user?.username, - }; - const roles = {}; - Object.entries(this.signupDetails.roles).forEach(([key, value]) => { - if (value === true) { - roles[key] = value; - } - }); + try { + let user; + if (!this.isDevMode) + user = await this.generateUsername( + this.signupDetails.firstName, + this.signupDetails.lastName + ); + const signupDetails = { + first_name: this.signupDetails.firstName, + last_name: this.signupDetails.lastName, + username: this.isDevMode ? this.signupDetails.username : user.username, + }; + const roles = {}; + Object.entries(this.signupDetails.roles).forEach(([key, value]) => { + if (value === true) { + roles[key] = value; + } + }); - this.isLoading = true; + this.isLoading = true; - const isUsernameAvailable = await checkUserName(signupDetails.username); - if (!isUsernameAvailable) { - this.analytics.trackEvent(NEW_SIGNUP_FLOW.USERNAME_NOT_AVAILABLE); - this.isLoading = false; - this.isButtonDisabled = false; - return (this.error = ERROR_MESSAGES.userName); - } + const isUsernameAvailable = await checkUserName(signupDetails.username); + if (!isUsernameAvailable) { + this.analytics.trackEvent(NEW_SIGNUP_FLOW.USERNAME_NOT_AVAILABLE); + this.isLoading = false; + this.isButtonDisabled = false; + return (this.error = ERROR_MESSAGES.userName); + } - if (this.isDevMode) { - try { + if (this.isDevMode) { const res = await newRegisterUser(signupDetails, roles); if (res.status === 204) { this.analytics.identifyUser(); @@ -160,35 +170,26 @@ export default class NewSignUpController extends Controller { this.error = ERROR_MESSAGES.others; this.isButtonDisabled = false; } - } catch (error) { - this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_REGISTER); - this.error = ERROR_MESSAGES.others; - this.isButtonDisabled = false; - } finally { - this.isLoading = false; - } - } else { - //this will get removed after removing feature flag - registerUser(signupDetails) - .then((res) => { - if (res.status === 204) { - this.analytics.identifyUser(); - this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_REGISTERED); - this.currentStep = this.LAST_STEP; - } else { - this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_SIGNUP); - this.error = ERROR_MESSAGES.others; - this.isButtonDisabled = false; - } - }) - .catch(() => { - this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_REGISTER); + } else { + //this will get removed after removing feature flag + const res = await registerUser(signupDetails); + if (res.status === 204) { + this.analytics.identifyUser(); + this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_REGISTERED); + this.currentStep = this.LAST_STEP; + } else { + this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_SIGNUP); this.error = ERROR_MESSAGES.others; this.isButtonDisabled = false; - }) - .finally(() => { - this.isLoading = false; - }); + } + } + } catch (error) { + this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_REGISTER); + console.log(error); + this.error = error?.message || ERROR_MESSAGES.others; + this.isButtonDisabled = false; + } finally { + this.isLoading = false; } } } diff --git a/app/templates/new-signup.hbs b/app/templates/new-signup.hbs index 3887e6bc..d208ed48 100644 --- a/app/templates/new-signup.hbs +++ b/app/templates/new-signup.hbs @@ -18,13 +18,24 @@ {{/if}} {{#if (eq this.currentStep this.THIRD_STEP)}} - + {{#if this.isDevMode}} + + {{else}} + + {{/if}} {{/if}} {{#if this.isDevMode}}