mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
fix(InviteUsers): fix invite users bugs.
This commit is contained in:
@@ -7,13 +7,13 @@ import BaseController from 'api/controllers/BaseController';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import AuthenticationService from 'services/Authentication';
|
||||
import { ILoginDTO, ISystemUser, IRegisterDTO } from 'interfaces';
|
||||
import { ServiceError, ServiceErrors } from "exceptions";
|
||||
import { ServiceError, ServiceErrors } from 'exceptions';
|
||||
import { DATATYPES_LENGTH } from 'data/DataTypes';
|
||||
import LoginThrottlerMiddleware from 'api/middleware/LoginThrottlerMiddleware';
|
||||
import config from 'config';
|
||||
|
||||
@Service()
|
||||
export default class AuthenticationController extends BaseController{
|
||||
export default class AuthenticationController extends BaseController {
|
||||
@Inject()
|
||||
authService: AuthenticationService;
|
||||
|
||||
@@ -116,19 +116,21 @@ export default class AuthenticationController extends BaseController{
|
||||
* Country validator.
|
||||
*/
|
||||
countryValidator(value, { req }) {
|
||||
const { countries: { whitelist, blacklist } } = config.registration;
|
||||
const {
|
||||
countries: { whitelist, blacklist },
|
||||
} = config.registration;
|
||||
const foundCountry = countries.findOne('countryCode', value);
|
||||
|
||||
if (!foundCountry) {
|
||||
throw new Error('The country code is invalid.');
|
||||
}
|
||||
if (
|
||||
// Focus with me! In case whitelist is not empty and the given coutry is not
|
||||
// Focus with me! In case whitelist is not empty and the given coutry is not
|
||||
// in whitelist throw the error.
|
||||
//
|
||||
// Or in case the blacklist is not empty and the given country exists
|
||||
//
|
||||
// Or in case the blacklist is not empty and the given country exists
|
||||
// in the blacklist throw the goddamn error.
|
||||
(whitelist.length > 0 && whitelist.indexOf(value) === -1) ||
|
||||
(whitelist.length > 0 && whitelist.indexOf(value) === -1) ||
|
||||
(blacklist.length > 0 && blacklist.indexOf(value) !== -1)
|
||||
) {
|
||||
throw new Error('The country code is not supported yet.');
|
||||
@@ -153,7 +155,9 @@ export default class AuthenticationController extends BaseController{
|
||||
*/
|
||||
get resetPasswordSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('password').exists().isLength({ min: 5 })
|
||||
check('password')
|
||||
.exists()
|
||||
.isLength({ min: 5 })
|
||||
.custom((value, { req }) => {
|
||||
if (value !== req.body.confirm_password) {
|
||||
throw new Error("Passwords don't match");
|
||||
@@ -168,15 +172,13 @@ export default class AuthenticationController extends BaseController{
|
||||
* Send reset password validation schema.
|
||||
*/
|
||||
get sendResetPasswordSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('email').exists().isEmail().trim().escape(),
|
||||
];
|
||||
return [check('email').exists().isEmail().trim().escape()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle user login.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async login(req: Request, res: Response, next: Function): Response {
|
||||
const userDTO: ILoginDTO = this.matchedBodyData(req);
|
||||
@@ -194,14 +196,16 @@ export default class AuthenticationController extends BaseController{
|
||||
|
||||
/**
|
||||
* Organization register handler.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async register(req: Request, res: Response, next: Function) {
|
||||
const registerDTO: IRegisterDTO = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
const registeredUser: ISystemUser = await this.authService.register(registerDTO);
|
||||
const registeredUser: ISystemUser = await this.authService.register(
|
||||
registerDTO
|
||||
);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
@@ -215,8 +219,8 @@ export default class AuthenticationController extends BaseController{
|
||||
|
||||
/**
|
||||
* Send reset password handler
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async sendResetPassword(req: Request, res: Response, next: Function) {
|
||||
const { email } = this.matchedBodyData(req);
|
||||
@@ -226,11 +230,10 @@ export default class AuthenticationController extends BaseController{
|
||||
|
||||
return res.status(200).send({
|
||||
code: 'SEND_RESET_PASSWORD_SUCCESS',
|
||||
message: 'The reset password message has been sent successfully.'
|
||||
message: 'The reset password message has been sent successfully.',
|
||||
});
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
@@ -238,8 +241,8 @@ export default class AuthenticationController extends BaseController{
|
||||
|
||||
/**
|
||||
* Reset password handler
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async resetPassword(req: Request, res: Response, next: Function) {
|
||||
const { token } = req.params;
|
||||
@@ -250,9 +253,9 @@ export default class AuthenticationController extends BaseController{
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'RESET_PASSWORD_SUCCESS',
|
||||
message: 'The password has been reset successfully.'
|
||||
})
|
||||
} catch(error) {
|
||||
message: 'The password has been reset successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -262,7 +265,9 @@ export default class AuthenticationController extends BaseController{
|
||||
*/
|
||||
handlerErrors(error, req: Request, res: Response, next: Function) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (['INVALID_DETAILS', 'invalid_password'].indexOf(error.errorType) !== -1) {
|
||||
if (
|
||||
['INVALID_DETAILS', 'invalid_password'].indexOf(error.errorType) !== -1
|
||||
) {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'INVALID_DETAILS', code: 100 }],
|
||||
});
|
||||
@@ -272,7 +277,10 @@ export default class AuthenticationController extends BaseController{
|
||||
errors: [{ type: 'USER_INACTIVE', code: 200 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'TOKEN_INVALID' || error.errorType === 'TOKEN_EXPIRED') {
|
||||
if (
|
||||
error.errorType === 'TOKEN_INVALID' ||
|
||||
error.errorType === 'TOKEN_EXPIRED'
|
||||
) {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'TOKEN_INVALID', code: 300 }],
|
||||
});
|
||||
@@ -303,4 +311,4 @@ export default class AuthenticationController extends BaseController{
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -30,12 +30,6 @@ export default class ExpensesController extends BaseController {
|
||||
asyncMiddleware(this.newExpense.bind(this)),
|
||||
this.catchServiceErrors
|
||||
);
|
||||
router.post(
|
||||
'/publish',
|
||||
[...this.bulkSelectSchema],
|
||||
this.bulkPublishExpenses.bind(this),
|
||||
this.catchServiceErrors
|
||||
);
|
||||
router.post(
|
||||
'/:id/publish',
|
||||
[...this.expenseParamSchema],
|
||||
@@ -57,13 +51,6 @@ export default class ExpensesController extends BaseController {
|
||||
asyncMiddleware(this.deleteExpense.bind(this)),
|
||||
this.catchServiceErrors
|
||||
);
|
||||
router.delete(
|
||||
'/',
|
||||
[...this.bulkSelectSchema],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.bulkDeleteExpenses.bind(this)),
|
||||
this.catchServiceErrors
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
[...this.expensesListSchema],
|
||||
@@ -250,63 +237,6 @@ export default class ExpensesController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the expenses in bulk.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async bulkDeleteExpenses(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId, user } = req;
|
||||
const { ids: expensesIds } = req.query;
|
||||
|
||||
try {
|
||||
await this.expensesService.deleteBulkExpenses(
|
||||
tenantId,
|
||||
expensesIds,
|
||||
user
|
||||
);
|
||||
return res.status(200).send({
|
||||
ids: expensesIds,
|
||||
message: 'The expenses have been deleted successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes the given expenses in bulk.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async bulkPublishExpenses(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId, user } = req;
|
||||
const { ids: expensesIds } = req.query;
|
||||
|
||||
try {
|
||||
const {
|
||||
meta: { alreadyPublished, published, total },
|
||||
} = await this.expensesService.publishBulkExpenses(
|
||||
tenantId,
|
||||
expensesIds,
|
||||
user
|
||||
);
|
||||
return res.status(200).send({
|
||||
ids: expensesIds,
|
||||
message: 'The expenses have been published successfully.',
|
||||
meta: {
|
||||
alreadyPublished,
|
||||
published,
|
||||
total,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve expneses list.
|
||||
* @param {Request} req
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { check, body, param } from 'express-validator';
|
||||
import { IInviteUserInput } from 'interfaces';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
@@ -25,6 +25,15 @@ export default class InviteUsersController extends BaseController {
|
||||
asyncMiddleware(this.sendInvite.bind(this)),
|
||||
this.handleServicesError
|
||||
);
|
||||
router.post(
|
||||
'/resend/:userId',
|
||||
[
|
||||
param('userId').exists().isNumeric().toInt()
|
||||
],
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.resendInvite.bind(this)),
|
||||
this.handleServicesError
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -67,9 +76,9 @@ export default class InviteUsersController extends BaseController {
|
||||
|
||||
/**
|
||||
* Invite a user to the authorized user organization.
|
||||
* @param {Request} req -
|
||||
* @param {Response} res -
|
||||
* @param {NextFunction} next -
|
||||
* @param {Request} req - Request object.
|
||||
* @param {Response} res - Response object.
|
||||
* @param {NextFunction} next - Next function.
|
||||
*/
|
||||
async sendInvite(req: Request, res: Response, next: Function) {
|
||||
const { email } = req.body;
|
||||
@@ -90,7 +99,29 @@ export default class InviteUsersController extends BaseController {
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
return res.status(200).send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resend the user invite.
|
||||
* @param {Request} req - Request object.
|
||||
* @param {Response} res - Response object.
|
||||
* @param {NextFunction} next - Next function.
|
||||
*/
|
||||
async resendInvite(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId, user } = req;
|
||||
const { userId } = req.params;
|
||||
|
||||
try {
|
||||
await this.inviteUsersService.resendInvite(tenantId, userId, user);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'INVITE.RESEND.SUCCESSFULLY',
|
||||
message: 'The invite has been sent to the given email.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,38 +182,59 @@ export default class InviteUsersController extends BaseController {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'EMAIL_EXISTS') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'EMAIL.ALREADY.EXISTS',
|
||||
code: 100,
|
||||
message: 'Email already exists in the users.'
|
||||
}],
|
||||
errors: [
|
||||
{
|
||||
type: 'EMAIL.ALREADY.EXISTS',
|
||||
code: 100,
|
||||
message: 'Email already exists in the users.',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'EMAIL_ALREADY_INVITED') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'EMAIL.ALREADY.INVITED',
|
||||
code: 200,
|
||||
message: 'Email already invited.',
|
||||
}],
|
||||
errors: [
|
||||
{
|
||||
type: 'EMAIL.ALREADY.INVITED',
|
||||
code: 200,
|
||||
message: 'Email already invited.',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'INVITE_TOKEN_INVALID') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'INVITE.TOKEN.INVALID',
|
||||
code: 300,
|
||||
message: 'Invite token is invalid, please try another one.',
|
||||
}],
|
||||
errors: [
|
||||
{
|
||||
type: 'INVITE.TOKEN.INVALID',
|
||||
code: 300,
|
||||
message: 'Invite token is invalid, please try another one.',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'PHONE_NUMBER_EXISTS') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'PHONE_NUMBER.EXISTS',
|
||||
code: 400,
|
||||
message: 'Phone number is already invited, please try another unique one.'
|
||||
}],
|
||||
errors: [
|
||||
{
|
||||
type: 'PHONE_NUMBER.EXISTS',
|
||||
code: 400,
|
||||
message:
|
||||
'Phone number is already invited, please try another unique one.',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'USER_RECENTLY_INVITED') {
|
||||
return res.status(400).send({
|
||||
errors: [
|
||||
{
|
||||
type: 'USER_RECENTLY_INVITED',
|
||||
code: 500,
|
||||
message:
|
||||
'This person was recently invited. No need to invite them again just yet.',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user