mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
fix: invite user to the system.
fix: soft delete system user.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { check, body, param } from 'express-validator';
|
||||
import { IInviteUserInput } from 'interfaces';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import InviteUserService from 'services/InviteUsers';
|
||||
import { ServiceErrors, ServiceError } from 'exceptions';
|
||||
@@ -22,7 +23,7 @@ export default class InviteUsersController extends BaseController {
|
||||
[body('email').exists().trim().escape()],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.sendInvite.bind(this)),
|
||||
this.handleServicesError,
|
||||
this.handleServicesError
|
||||
);
|
||||
return router;
|
||||
}
|
||||
@@ -38,14 +39,14 @@ export default class InviteUsersController extends BaseController {
|
||||
[...this.inviteUserDTO],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.accept.bind(this)),
|
||||
this.handleServicesError,
|
||||
this.handleServicesError
|
||||
);
|
||||
router.get(
|
||||
'/invited/:token',
|
||||
[param('token').exists().trim().escape()],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.invited.bind(this)),
|
||||
this.handleServicesError,
|
||||
this.handleServicesError
|
||||
);
|
||||
|
||||
return router;
|
||||
@@ -76,8 +77,11 @@ export default class InviteUsersController extends BaseController {
|
||||
const { user } = req;
|
||||
|
||||
try {
|
||||
await this.inviteUsersService.sendInvite(tenantId, email, user);
|
||||
|
||||
const { invite } = await this.inviteUsersService.sendInvite(
|
||||
tenantId,
|
||||
email,
|
||||
user
|
||||
);
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'INVITE.SENT.SUCCESSFULLY',
|
||||
@@ -104,6 +108,7 @@ export default class InviteUsersController extends BaseController {
|
||||
|
||||
try {
|
||||
await this.inviteUsersService.acceptInvite(token, inviteUserInput);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'USER.INVITE.ACCEPTED',
|
||||
@@ -144,19 +149,40 @@ export default class InviteUsersController extends BaseController {
|
||||
*/
|
||||
handleServicesError(error, req: Request, res: Response, next: Function) {
|
||||
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.'
|
||||
}],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'EMAIL_ALREADY_INVITED') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: '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' }],
|
||||
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' }],
|
||||
errors: [{
|
||||
type: 'PHONE_NUMBER.EXISTS',
|
||||
code: 400,
|
||||
message: 'Phone number is already invited, please try another unique one.'
|
||||
}],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Router, Request, Response } from 'express';
|
||||
|
||||
export default class Ping {
|
||||
/**
|
||||
* Router constur
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
@@ -126,6 +126,7 @@ export default class UsersController extends BaseController{
|
||||
|
||||
try {
|
||||
await this.usersService.deleteUser(tenantId, id);
|
||||
|
||||
return res.status(200).send({
|
||||
id,
|
||||
message: 'The user has been deleted successfully.'
|
||||
@@ -225,10 +226,10 @@ export default class UsersController extends BaseController{
|
||||
if (error instanceof ServiceErrors) {
|
||||
const errorReasons = [];
|
||||
|
||||
if (error.errorType === 'email_already_exists') {
|
||||
if (error.errorType === 'EMAIL_ALREADY_EXISTS') {
|
||||
errorReasons.push({ type: 'EMAIL_ALREADY_EXIST', code: 100 });
|
||||
}
|
||||
if (error.errorType === 'phone_number_already_exist') {
|
||||
if (error.errorType === 'PHONE_NUMBER_ALREADY_EXIST') {
|
||||
errorReasons.push({ type: 'PHONE_NUMBER_ALREADY_EXIST', code: 200 });
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
@@ -236,30 +237,36 @@ export default class UsersController extends BaseController{
|
||||
}
|
||||
}
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
if (error.errorType === 'USER_NOT_FOUND') {
|
||||
return res.boom.badRequest(
|
||||
'User not found.',
|
||||
{ errors: [{ type: 'USER.NOT.FOUND', code: 100 }] }
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'user_already_active') {
|
||||
if (error.errorType === 'USER_ALREADY_ACTIVE') {
|
||||
return res.boom.badRequest(
|
||||
'User is already active.',
|
||||
{ errors: [{ type: 'USER.ALREADY.ACTIVE', code: 200 }] },
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'user_already_inactive') {
|
||||
if (error.errorType === 'USER_ALREADY_INACTIVE') {
|
||||
return res.boom.badRequest(
|
||||
'User is already inactive.',
|
||||
{ errors: [{ type: 'USER.ALREADY.INACTIVE', code: 200 }] },
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'user_same_the_authorized_user') {
|
||||
if (error.errorType === 'USER_SAME_THE_AUTHORIZED_USER') {
|
||||
return res.boom.badRequest(
|
||||
'You could not activate/inactivate the same authorized user.',
|
||||
{ errors: [{ type: 'CANNOT.TOGGLE.ACTIVATE.AUTHORIZED.USER', code: 300 }] },
|
||||
)
|
||||
}
|
||||
if (error.errorType === 'CANNOT_DELETE_LAST_USER') {
|
||||
return res.boom.badRequest(
|
||||
'Cannot delete last user in the organization.',
|
||||
{ errors: [{ type: 'CANNOT_DELETE_LAST_USER', code: 400 }] },
|
||||
);
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ const attachCurrentUser = async (req: Request, res: Response, next: Function) =>
|
||||
try {
|
||||
Logger.info('[attach_user_middleware] finding system user by id.');
|
||||
const user = await systemUserRepository.findOneById(req.token.id);
|
||||
console.log(user);
|
||||
|
||||
if (!user) {
|
||||
Logger.info('[attach_user_middleware] the system user not found.');
|
||||
|
||||
@@ -9,11 +9,13 @@ export default (req: Request, res: Response, next: Function) => {
|
||||
throw new Error('Should load this middleware after `TenancyMiddleware`.');
|
||||
}
|
||||
if (!req.tenant.seededAt) {
|
||||
Logger.info('[ensure_tenant_initialized_middleware] tenant databae not seeded.');
|
||||
Logger.info(
|
||||
'[ensure_tenant_initialized_middleware] tenant databae not seeded.'
|
||||
);
|
||||
return res.boom.badRequest(
|
||||
'Tenant database is not seeded with initial data yet.',
|
||||
{ errors: [{ type: 'TENANT.DATABASE.NOT.SEED' }] },
|
||||
{ errors: [{ type: 'TENANT.DATABASE.NOT.SEED' }] }
|
||||
);
|
||||
}
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Container } from 'typedi';
|
||||
|
||||
export default (subscriptionSlug = 'main') => async (req: Request, res: Response, next: NextFunction) => {
|
||||
export default (subscriptionSlug = 'main') => async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
const { tenant, tenantId } = req;
|
||||
const Logger = Container.get('logger');
|
||||
const { subscriptionRepository } = Container.get('repositories');
|
||||
@@ -10,22 +14,28 @@ export default (subscriptionSlug = 'main') => async (req: Request, res: Response
|
||||
throw new Error('Should load `TenancyMiddlware` before this middleware.');
|
||||
}
|
||||
Logger.info('[subscription_middleware] trying get tenant main subscription.');
|
||||
const subscription = await subscriptionRepository.getBySlugInTenant(subscriptionSlug, tenantId);
|
||||
|
||||
const subscription = await subscriptionRepository.getBySlugInTenant(
|
||||
subscriptionSlug,
|
||||
tenantId
|
||||
);
|
||||
// Validate in case there is no any already subscription.
|
||||
if (!subscription) {
|
||||
Logger.info('[subscription_middleware] tenant has no subscription.', { tenantId });
|
||||
return res.boom.badRequest(
|
||||
'Tenant has no subscription.',
|
||||
{ errors: [{ type: 'TENANT.HAS.NO.SUBSCRIPTION' }] }
|
||||
);
|
||||
Logger.info('[subscription_middleware] tenant has no subscription.', {
|
||||
tenantId,
|
||||
});
|
||||
return res.boom.badRequest('Tenant has no subscription.', {
|
||||
errors: [{ type: 'TENANT.HAS.NO.SUBSCRIPTION' }],
|
||||
});
|
||||
}
|
||||
// Validate in case the subscription is inactive.
|
||||
else if (subscription.inactive()) {
|
||||
Logger.info('[subscription_middleware] tenant main subscription is expired.', { tenantId });
|
||||
Logger.info(
|
||||
'[subscription_middleware] tenant main subscription is expired.',
|
||||
{ tenantId }
|
||||
);
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'ORGANIZATION.SUBSCRIPTION.INACTIVE' }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import logger from "src/loaders/logger";
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Container } from 'typedi';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user