mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 07:10:33 +00:00
fix: user invite check api.
This commit is contained in:
@@ -21,7 +21,8 @@ export default class InviteUsersController extends BaseController {
|
|||||||
'/send',
|
'/send',
|
||||||
[body('email').exists().trim().escape()],
|
[body('email').exists().trim().escape()],
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.sendInvite.bind(this))
|
asyncMiddleware(this.sendInvite.bind(this)),
|
||||||
|
this.handleServicesError,
|
||||||
);
|
);
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
@@ -36,13 +37,15 @@ export default class InviteUsersController extends BaseController {
|
|||||||
'/accept/:token',
|
'/accept/:token',
|
||||||
[...this.inviteUserDTO],
|
[...this.inviteUserDTO],
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.accept.bind(this))
|
asyncMiddleware(this.accept.bind(this)),
|
||||||
|
this.handleServicesError,
|
||||||
);
|
);
|
||||||
router.get(
|
router.get(
|
||||||
'/invited/:token',
|
'/invited/:token',
|
||||||
[param('token').exists().trim().escape()],
|
[param('token').exists().trim().escape()],
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.invited.bind(this))
|
asyncMiddleware(this.invited.bind(this)),
|
||||||
|
this.handleServicesError,
|
||||||
);
|
);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
@@ -81,13 +84,6 @@ export default class InviteUsersController extends BaseController {
|
|||||||
message: 'The invite has been sent to the given email.',
|
message: 'The invite has been sent to the given email.',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ServiceError) {
|
|
||||||
if (error.errorType === 'email_already_invited') {
|
|
||||||
return res.status(400).send({
|
|
||||||
errors: [{ type: 'EMAIL.ALREADY.INVITED' }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
return res.status(200).send();
|
return res.status(200).send();
|
||||||
@@ -114,18 +110,6 @@ export default class InviteUsersController extends BaseController {
|
|||||||
message: 'User invite has been accepted successfully.',
|
message: 'User invite has been accepted successfully.',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ServiceError) {
|
|
||||||
if (error.errorType === 'phone_number_exists') {
|
|
||||||
return res.status(400).send({
|
|
||||||
errors: [{ type: 'PHONE_NUMBER.EXISTS' }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (error.errorType === 'invite_token_invalid') {
|
|
||||||
return res.status(400).send({
|
|
||||||
errors: [{ type: 'INVITE.TOKEN.INVALID' }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,14 +135,31 @@ export default class InviteUsersController extends BaseController {
|
|||||||
organizationName: orgName?.value,
|
organizationName: orgName?.value,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ServiceError) {
|
|
||||||
if (error.errorType === 'invite_token_invalid') {
|
|
||||||
return res.status(400).send({
|
|
||||||
errors: [{ type: 'INVITE.TOKEN.INVALID' }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the service error.
|
||||||
|
*/
|
||||||
|
handleServicesError(error, req: Request, res: Response, next: Function) {
|
||||||
|
if (error instanceof ServiceError) {
|
||||||
|
if (error.errorType === 'EMAIL_ALREADY_INVITED') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{ type: 'EMAIL.ALREADY.INVITED' }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (error.errorType === 'INVITE_TOKEN_INVALID') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{ type: 'INVITE.TOKEN.INVALID' }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (error.errorType === 'PHONE_NUMBER_EXISTS') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{ type: 'PHONE_NUMBER.EXISTS' }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import Option from './Option';
|
import Option from './Option';
|
||||||
|
import Setting from './Setting';
|
||||||
import SaleEstimate from './SaleEstimate';
|
import SaleEstimate from './SaleEstimate';
|
||||||
import SaleEstimateEntry from './SaleEstimateEntry';
|
import SaleEstimateEntry from './SaleEstimateEntry';
|
||||||
import SaleReceipt from './SaleReceipt';
|
import SaleReceipt from './SaleReceipt';
|
||||||
@@ -52,4 +53,5 @@ export {
|
|||||||
ManualJournal,
|
ManualJournal,
|
||||||
Customer,
|
Customer,
|
||||||
Vendor,
|
Vendor,
|
||||||
|
Setting
|
||||||
};
|
};
|
||||||
@@ -7,13 +7,19 @@ import {
|
|||||||
} from 'decorators/eventDispatcher';
|
} from 'decorators/eventDispatcher';
|
||||||
import { ServiceError } from 'exceptions';
|
import { ServiceError } from 'exceptions';
|
||||||
import { Invite, Tenant } from 'system/models';
|
import { Invite, Tenant } from 'system/models';
|
||||||
import { Option } from 'models';
|
import { Setting } from 'models';
|
||||||
import { hashPassword } from 'utils';
|
import { hashPassword } from 'utils';
|
||||||
import TenancyService from 'services/Tenancy/TenancyService';
|
import TenancyService from 'services/Tenancy/TenancyService';
|
||||||
import InviteUsersMailMessages from 'services/InviteUsers/InviteUsersMailMessages';
|
import InviteUsersMailMessages from 'services/InviteUsers/InviteUsersMailMessages';
|
||||||
import events from 'subscribers/events';
|
import events from 'subscribers/events';
|
||||||
import { ISystemUser, IInviteUserInput } from 'interfaces';
|
import { ISystemUser, IInviteUserInput } from 'interfaces';
|
||||||
|
import TenantsManagerService from 'services/Tenancy/TenantsManager';
|
||||||
|
|
||||||
|
const ERRORS = {
|
||||||
|
EMAIL_ALREADY_INVITED: 'EMAIL_ALREADY_INVITED',
|
||||||
|
INVITE_TOKEN_INVALID: 'INVITE_TOKEN_INVALID',
|
||||||
|
PHONE_NUMBER_EXISTS: 'PHONE_NUMBER_EXISTS'
|
||||||
|
};
|
||||||
@Service()
|
@Service()
|
||||||
export default class InviteUserService {
|
export default class InviteUserService {
|
||||||
@EventDispatcher()
|
@EventDispatcher()
|
||||||
@@ -31,6 +37,9 @@ export default class InviteUserService {
|
|||||||
@Inject('repositories')
|
@Inject('repositories')
|
||||||
sysRepositories: any;
|
sysRepositories: any;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
tenantsManager: TenantsManagerService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accept the received invite.
|
* Accept the received invite.
|
||||||
* @param {string} token
|
* @param {string} token
|
||||||
@@ -135,13 +144,17 @@ export default class InviteUserService {
|
|||||||
const inviteToken = await this.getInviteOrThrowError(token);
|
const inviteToken = await this.getInviteOrThrowError(token);
|
||||||
|
|
||||||
// Find the tenant that associated to the given token.
|
// Find the tenant that associated to the given token.
|
||||||
const tenant = await Tenant.query().findOne('id', inviteToken.tenantId);
|
const tenant = await Tenant.query().findById(inviteToken.tenantId);
|
||||||
|
|
||||||
const tenantDb = this.tenantsManager.knexInstance(tenant.organizationId);
|
// Setup the knex instance.
|
||||||
|
this.tenantsManager.setupKnexInstance(tenant);
|
||||||
|
|
||||||
const orgName = await Option.bindKnex(tenantDb)
|
// Retrieve the knex instance of the given tenant.
|
||||||
|
const tenantKnexInstance = this.tenantsManager.getKnexInstance(tenant.id);
|
||||||
|
|
||||||
|
const orgName = await Setting.bindKnex(tenantKnexInstance)
|
||||||
.query()
|
.query()
|
||||||
.findOne('key', 'organization_name');
|
.findOne({ key: 'name', group: 'organization' });
|
||||||
|
|
||||||
// Triggers `onUserCheckInvite` event.
|
// Triggers `onUserCheckInvite` event.
|
||||||
this.eventDispatcher.dispatch(events.inviteUser.checkInvite, {
|
this.eventDispatcher.dispatch(events.inviteUser.checkInvite, {
|
||||||
@@ -162,7 +175,7 @@ export default class InviteUserService {
|
|||||||
const foundUser = await systemUserRepository.findOneByEmail(email);
|
const foundUser = await systemUserRepository.findOneByEmail(email);
|
||||||
|
|
||||||
if (foundUser) {
|
if (foundUser) {
|
||||||
throw new ServiceError('email_already_invited');
|
throw new ServiceError(ERRORS.EMAIL_ALREADY_INVITED);
|
||||||
}
|
}
|
||||||
return foundUser;
|
return foundUser;
|
||||||
}
|
}
|
||||||
@@ -178,7 +191,7 @@ export default class InviteUserService {
|
|||||||
|
|
||||||
if (!inviteToken) {
|
if (!inviteToken) {
|
||||||
this.logger.info('[aceept_invite] the invite token is invalid.');
|
this.logger.info('[aceept_invite] the invite token is invalid.');
|
||||||
throw new ServiceError('invite_token_invalid');
|
throw new ServiceError(ERRORS.INVITE_TOKEN_INVALID);
|
||||||
}
|
}
|
||||||
return inviteToken;
|
return inviteToken;
|
||||||
}
|
}
|
||||||
@@ -189,14 +202,13 @@ export default class InviteUserService {
|
|||||||
*/
|
*/
|
||||||
private async validateUserPhoneNumber(
|
private async validateUserPhoneNumber(
|
||||||
inviteUserInput: IInviteUserInput
|
inviteUserInput: IInviteUserInput
|
||||||
): Promise<ISystemUser> {
|
): Promise<void> {
|
||||||
const { systemUserRepository } = this.sysRepositories;
|
const { systemUserRepository } = this.sysRepositories;
|
||||||
const foundUser = await systemUserRepository.findOneByPhoneNumber(
|
const foundUser = await systemUserRepository.findOneByPhoneNumber(
|
||||||
inviteUserInput.phoneNumber
|
inviteUserInput.phoneNumber
|
||||||
);
|
);
|
||||||
|
|
||||||
if (foundUser) {
|
if (foundUser) {
|
||||||
throw new ServiceError('phone_number_exists');
|
throw new ServiceError(ERRORS.PHONE_NUMBER_EXISTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user