fix: invite user to the system.

fix: soft delete system user.
This commit is contained in:
a.bouhuolia
2021-01-19 13:18:48 +02:00
parent ef53b25c18
commit 59f44d9ef6
17 changed files with 320 additions and 136 deletions

View File

@@ -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.'
}],
});
}
}

View File

@@ -2,7 +2,7 @@ import { Router, Request, Response } from 'express';
export default class Ping {
/**
* Router constur
* Router constructor.
*/
router() {
const router = Router();

View File

@@ -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);
}