diff --git a/packages/server/src/api/controllers/Authentication.ts b/packages/server/src/api/controllers/Authentication.ts index 44c4cd147..e0f9073ea 100644 --- a/packages/server/src/api/controllers/Authentication.ts +++ b/packages/server/src/api/controllers/Authentication.ts @@ -152,9 +152,8 @@ export default class AuthenticationController extends BaseController { const registerDTO: IRegisterDTO = this.matchedBodyData(req); try { - const registeredUser: ISystemUser = await this.authApplication.signUp( - registerDTO - ); + await this.authApplication.signUp(registerDTO); + return res.status(200).send({ type: 'success', code: 'REGISTER.SUCCESS', @@ -243,18 +242,10 @@ export default class AuthenticationController extends BaseController { errors: [{ type: 'EMAIL.NOT.REGISTERED', code: 500 }], }); } - } - if (error instanceof ServiceErrors) { - const errorReasons = []; - - if (error.hasType('PHONE_NUMBER_EXISTS')) { - errorReasons.push({ type: 'PHONE_NUMBER_EXISTS', code: 100 }); - } - if (error.hasType('EMAIL_EXISTS')) { - errorReasons.push({ type: 'EMAIL.EXISTS', code: 200 }); - } - if (errorReasons.length > 0) { - return res.boom.badRequest(null, { errors: errorReasons }); + if (error.errorType === 'EMAIL_EXISTS') { + return res.status(400).send({ + errors: [{ type: 'EMAIL.EXISTS', code: 600 }], + }); } } next(error); diff --git a/packages/server/src/api/controllers/Organization.ts b/packages/server/src/api/controllers/Organization.ts index 76c8e72bb..6eda6d627 100644 --- a/packages/server/src/api/controllers/Organization.ts +++ b/packages/server/src/api/controllers/Organization.ts @@ -8,18 +8,12 @@ import JWTAuth from '@/api/middleware/jwtAuth'; import TenancyMiddleware from '@/api/middleware/TenancyMiddleware'; import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser'; import OrganizationService from '@/services/Organization/OrganizationService'; -import { - ACCEPTED_CURRENCIES, - MONTHS, - ACCEPTED_LOCALES, -} from '@/services/Organization/constants'; +import { MONTHS, ACCEPTED_LOCALES } from '@/services/Organization/constants'; import { DATE_FORMATS } from '@/services/Miscellaneous/DateFormats/constants'; import { ServiceError } from '@/exceptions'; import BaseController from '@/api/controllers/BaseController'; -const ACCEPTED_LOCATIONS = ['libya']; - @Service() export default class OrganizationController extends BaseController { @Inject() @@ -65,8 +59,8 @@ export default class OrganizationController extends BaseController { return [ check('name').exists().trim(), check('industry').optional().isString(), - check('location').exists().isString().isIn(ACCEPTED_LOCATIONS), - check('base_currency').exists().isIn(ACCEPTED_CURRENCIES), + check('location').exists().isString().isISO31661Alpha2(), + check('base_currency').exists().isISO4217(), check('timezone').exists().isIn(moment.tz.names()), check('fiscal_year').exists().isIn(MONTHS), check('language').exists().isString().isIn(ACCEPTED_LOCALES), diff --git a/packages/server/src/services/Authentication/AuthSignin.ts b/packages/server/src/services/Authentication/AuthSignin.ts index 072aa7ab2..1331eefd5 100644 --- a/packages/server/src/services/Authentication/AuthSignin.ts +++ b/packages/server/src/services/Authentication/AuthSignin.ts @@ -2,6 +2,7 @@ import { Container, Inject } from 'typedi'; import { cloneDeep } from 'lodash'; import { Tenant } from '@/system/models'; import { + IAuthSignedInEventPayload, IAuthSigningInEventPayload, IAuthSignInPOJO, ISystemUser, @@ -22,9 +23,9 @@ export class AuthSigninService { /** * Validates the given email and password. - * @param {ISystemUser} user - * @param {string} email - * @param {string} password + * @param {ISystemUser} user + * @param {string} email + * @param {string} password */ public async validateSignIn( user: ISystemUser, @@ -69,7 +70,7 @@ export class AuthSigninService { await this.validateSignIn(user, email, password); // Triggers on signing-in event. - await this.eventPublisher.emitAsync(events.auth.logining, { + await this.eventPublisher.emitAsync(events.auth.signingIn, { email, password, user, @@ -80,12 +81,13 @@ export class AuthSigninService { // Update the last login at of the user. await systemUserRepository.patchLastLoginAt(user.id); - // Triggers `onLogin` event. - await this.eventPublisher.emitAsync(events.auth.login, { + // Triggers `onSignIn` event. + await this.eventPublisher.emitAsync(events.auth.signIn, { email, password, user, - }); + } as IAuthSignedInEventPayload); + const tenant = await Tenant.query() .findById(user.tenantId) .withGraphFetched('metadata'); diff --git a/packages/server/src/services/Authentication/AuthSignup.ts b/packages/server/src/services/Authentication/AuthSignup.ts index c8690b72f..b2d11ac22 100644 --- a/packages/server/src/services/Authentication/AuthSignup.ts +++ b/packages/server/src/services/Authentication/AuthSignup.ts @@ -39,7 +39,7 @@ export class AuthSignupService { const hashedPassword = await hashPassword(signupDTO.password); // Triggers signin up event. - await this.eventPublisher.emitAsync(events.auth.registering, { + await this.eventPublisher.emitAsync(events.auth.signingUp, { signupDTO, } as IAuthSigningUpEventPayload); @@ -52,7 +52,7 @@ export class AuthSignupService { inviteAcceptedAt: moment().format('YYYY-MM-DD'), }); // Triggers signed up event. - await this.eventPublisher.emitAsync(events.auth.register, { + await this.eventPublisher.emitAsync(events.auth.signUp, { signupDTO, tenant, user: registeredUser, diff --git a/packages/server/src/services/Organization/constants.ts b/packages/server/src/services/Organization/constants.ts index 46380409c..a1c0bd6f7 100644 --- a/packages/server/src/services/Organization/constants.ts +++ b/packages/server/src/services/Organization/constants.ts @@ -14,8 +14,6 @@ export const DATE_FORMATS = [ 'MMMM dd, YYYY', 'EEE, MMMM dd, YYYY', ]; -export const ACCEPTED_CURRENCIES = Object.keys(currencies); - export const MONTHS = [ 'january', 'february', diff --git a/packages/server/src/subscribers/Authentication/ResetLoginThrottle.ts b/packages/server/src/subscribers/Authentication/ResetLoginThrottle.ts index 781241fa1..2dcd7ed59 100644 --- a/packages/server/src/subscribers/Authentication/ResetLoginThrottle.ts +++ b/packages/server/src/subscribers/Authentication/ResetLoginThrottle.ts @@ -1,27 +1,29 @@ import { Container, Service } from 'typedi'; import events from '@/subscribers/events'; +import { IAuthSignedInEventPayload } from '@/interfaces'; @Service() export default class ResetLoginThrottleSubscriber { /** * Attaches events with handlers. - * @param bus + * @param bus */ public attach(bus) { - bus.subscribe(events.auth.login, this.resetLoginThrottleOnceSuccessLogin); + bus.subscribe(events.auth.signIn, this.resetLoginThrottleOnceSuccessLogin); } /** * Resets the login throttle once the login success. + * @param {IAuthSignedInEventPayload} payload - */ - private async resetLoginThrottleOnceSuccessLogin(payload) { - const { emailOrPhone, password, user } = payload; - + private async resetLoginThrottleOnceSuccessLogin( + payload: IAuthSignedInEventPayload + ) { + const { email, user } = payload; const loginThrottler = Container.get('rateLimiter.login'); // Reset the login throttle by the given email and phone number. await loginThrottler.reset(user.email); - await loginThrottler.reset(user.phoneNumber); - await loginThrottler.reset(emailOrPhone); + await loginThrottler.reset(email); } } diff --git a/packages/server/src/subscribers/Authentication/SendWelcomeMail.ts b/packages/server/src/subscribers/Authentication/SendWelcomeMail.ts index ea9bf9de3..e692033c1 100644 --- a/packages/server/src/subscribers/Authentication/SendWelcomeMail.ts +++ b/packages/server/src/subscribers/Authentication/SendWelcomeMail.ts @@ -10,14 +10,14 @@ export default class AuthSendWelcomeMailSubscriber { * Attaches events with handlers. */ public attach(bus) { - bus.subscribe(events.auth.register, this.sendWelcomeEmailOnceUserRegister); + bus.subscribe(events.auth.signUp, this.sendWelcomeEmailOnceUserRegister); } /** * Sends welcome email once the user register. */ private sendWelcomeEmailOnceUserRegister = async (payload) => { - const { registerDTO, tenant, user } = payload; + const { tenant, user } = payload; // Send welcome mail to the user. await this.agenda.now('welcome-email', { diff --git a/packages/server/src/subscribers/events.ts b/packages/server/src/subscribers/events.ts index 5f28dec40..19416b65d 100644 --- a/packages/server/src/subscribers/events.ts +++ b/packages/server/src/subscribers/events.ts @@ -3,13 +3,17 @@ export default { * Authentication service. */ auth: { - login: 'onLogin', - logining: 'onLogining', - register: 'onRegister', - registering: 'onAuthRegistering', - sendResetPassword: 'onSendResetPassword', + signIn: 'onSignIn', + signingIn: 'onSigningIn', + + signUp: 'onSignUp', + signingUp: 'onSigningUp', + sendingResetPassword: 'onSendingResetPassword', + sendResetPassword: 'onSendResetPassword', + resetPassword: 'onResetPassword', + resetingPassword: 'onResetingPassword' }, /** diff --git a/packages/webapp/src/containers/Preferences/General/GeneralForm.tsx b/packages/webapp/src/containers/Preferences/General/GeneralForm.tsx index faf729473..bf225a1fc 100644 --- a/packages/webapp/src/containers/Preferences/General/GeneralForm.tsx +++ b/packages/webapp/src/containers/Preferences/General/GeneralForm.tsx @@ -15,14 +15,15 @@ import { } from '@/components'; import { inputIntent } from '@/utils'; import { CLASSES } from '@/constants/classes'; -import { getCountries } from '@/constants/countries'; import { getAllCurrenciesOptions } from '@/constants/currencies'; import { getFiscalYear } from '@/constants/fiscalYearOptions'; import { getLanguages } from '@/constants/languagesOptions'; import { useGeneralFormContext } from './GeneralFormProvider'; +import { getAllCountries } from '@/utils/countries'; import { shouldBaseCurrencyUpdate } from './utils'; +const Countries = getAllCountries(); /** * Preferences general form. */ @@ -30,7 +31,6 @@ export default function PreferencesGeneralForm({ isSubmitting }) { const history = useHistory(); const FiscalYear = getFiscalYear(); - const Countries = getCountries(); const Languages = getLanguages(); const Currencies = getAllCurrenciesOptions();