mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
feat: fix issues in invite user.
feat: fix update last login at after login.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { matchedData } from "express-validator";
|
||||
import { Response, Request } from 'express';
|
||||
import { matchedData, validationResult } from "express-validator";
|
||||
import { mapKeys, camelCase, omit } from "lodash";
|
||||
|
||||
export default class BaseController {
|
||||
@@ -11,4 +12,16 @@ export default class BaseController {
|
||||
});
|
||||
return mapKeys(data, (v, k) => camelCase(k));
|
||||
}
|
||||
|
||||
validationResult(req: Request, res: Response, next: NextFunction) {
|
||||
const validationErrors = validationResult(req);
|
||||
|
||||
if (!validationErrors.isEmpty()) {
|
||||
return res.boom.badData(null, {
|
||||
code: 'validation_error',
|
||||
...validationErrors,
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
@@ -4,50 +4,61 @@ import {
|
||||
check,
|
||||
body,
|
||||
param,
|
||||
matchedData,
|
||||
} from 'express-validator';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import jwtAuth from '@/http/middleware/jwtAuth';
|
||||
import TenancyMiddleware from '@/http/middleware/TenancyMiddleware';
|
||||
import InviteUserService from '@/services/InviteUsers';
|
||||
import { ServiceErrors, ServiceError } from '@/exceptions';
|
||||
import BaseController from './BaseController';
|
||||
|
||||
@Service()
|
||||
export default class InviteUsersController {
|
||||
export default class InviteUsersController extends BaseController {
|
||||
@Inject()
|
||||
inviteUsersService: InviteUserService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
* Routes that require authentication.
|
||||
*/
|
||||
router() {
|
||||
authRouter() {
|
||||
const router = Router();
|
||||
|
||||
router.use('/send', jwtAuth);
|
||||
router.use('/send', TenancyMiddleware);
|
||||
|
||||
router.post('/send', [
|
||||
body('email').exists().trim().escape(),
|
||||
],
|
||||
asyncMiddleware(this.sendInvite),
|
||||
body('email').exists().trim().escape(),
|
||||
],
|
||||
asyncMiddleware(this.sendInvite.bind(this)),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes that non-required authentication.
|
||||
*/
|
||||
nonAuthRouter() {
|
||||
const router = Router();
|
||||
|
||||
router.post('/accept/:token', [
|
||||
...this.inviteUserDTO,
|
||||
],
|
||||
asyncMiddleware(this.accept.bind(this)));
|
||||
|
||||
router.get('/invited/:token', [
|
||||
param('token').exists().trim().escape(),
|
||||
],
|
||||
asyncMiddleware(this.invited.bind(this)));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invite DTO schema validation.
|
||||
*/
|
||||
get inviteUserDTO() {
|
||||
return [
|
||||
check('first_name').exists().trim().escape(),
|
||||
check('last_name').exists().trim().escape(),
|
||||
check('phone_number').exists().trim().escape(),
|
||||
check('password').exists().trim().escape(),
|
||||
param('token').exists().trim().escape(),
|
||||
],
|
||||
asyncMiddleware(this.accept)
|
||||
);
|
||||
|
||||
router.get('/invited/:token', [
|
||||
param('token').exists().trim().escape(),
|
||||
],
|
||||
asyncMiddleware(this.invited)
|
||||
);
|
||||
return router;
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,6 +74,11 @@ export default class InviteUsersController {
|
||||
|
||||
try {
|
||||
await this.inviteUsersService.sendInvite(tenantId, email, user);
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'INVITE.SENT.SUCCESSFULLY',
|
||||
message: 'The invite has been sent to the given email.',
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
if (error instanceof ServiceError) {
|
||||
@@ -84,7 +100,7 @@ export default class InviteUsersController {
|
||||
* @param {NextFunction} next -
|
||||
*/
|
||||
async accept(req: Request, res: Response, next: Function) {
|
||||
const inviteUserInput: IInviteUserInput = matchedData(req, {
|
||||
const inviteUserInput: IInviteUserInput = this.matchedBodyData(req, {
|
||||
locations: ['body'],
|
||||
includeOptionals: true,
|
||||
});
|
||||
@@ -92,20 +108,24 @@ export default class InviteUsersController {
|
||||
|
||||
try {
|
||||
await this.inviteUsersService.acceptInvite(token, inviteUserInput);
|
||||
return res.status(200).send();
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'USER.INVITE.ACCEPTED',
|
||||
message: 'User invite has been accepted successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
if (error instanceof ServiceErrors) {
|
||||
const errorReasons = [];
|
||||
if (error instanceof ServiceError) {
|
||||
|
||||
if (error.hasType('email_exists')) {
|
||||
errorReasons.push({ type: 'EMAIL.EXISTS' });
|
||||
if (error.errorType === 'phone_number_exists') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PHONE_NUMBER.EXISTS' }],
|
||||
});
|
||||
}
|
||||
if (error.hasType('phone_number_exists')) {
|
||||
errorReasons.push({ type: 'PHONE_NUMBER.EXISTS' });
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
if (error.errorType === 'invite_token_invalid') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'INVITE.TOKEN.INVALID' }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
@@ -122,9 +142,13 @@ export default class InviteUsersController {
|
||||
const { token } = req.params;
|
||||
|
||||
try {
|
||||
await this.inviteUsersService.checkInvite(token);
|
||||
const { inviteToken, orgName } = await this.inviteUsersService.checkInvite(token);
|
||||
|
||||
return res.status(200).send();
|
||||
return res.status(200).send({
|
||||
inviteToken: inviteToken.token,
|
||||
email: inviteToken.email,
|
||||
organizationName: orgName?.value,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
|
||||
@@ -44,7 +44,9 @@ export default class OrganizationController {
|
||||
await this.organizationService.build(buildOTD.organizationId);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'ORGANIZATION.DATABASE.INITIALIZED',
|
||||
type: 'success',
|
||||
code: 'ORGANIZATION.DATABASE.INITIALIZED',
|
||||
message: 'The organization database has been initialized.'
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import 'reflect-metadata';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import i18n from 'i18n';
|
||||
|
||||
export default class Ping {
|
||||
/**
|
||||
|
||||
@@ -101,8 +101,9 @@ export default class PaymentViaVoucherController extends PaymentMethodController
|
||||
.subscriptionViaVoucher(tenant.id, planSlug, voucherCode);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'PAYMENT.SUCCESSFULLY.MADE',
|
||||
code: 100,
|
||||
type: 'success',
|
||||
code: 'PAYMENT.SUCCESSFULLY.MADE',
|
||||
message: 'Payment via voucher has been made successfully.',
|
||||
});
|
||||
} catch (exception) {
|
||||
const errorReasons = [];
|
||||
|
||||
@@ -165,6 +165,7 @@ export default class VouchersController extends BaseController {
|
||||
);
|
||||
return res.status(200).send({
|
||||
code: 100,
|
||||
type: 'VOUCHERES.GENERATED.SUCCESSFULLY',
|
||||
message: 'The vouchers have been generated successfully.'
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,249 +0,0 @@
|
||||
import express from 'express';
|
||||
import {
|
||||
check,
|
||||
query,
|
||||
param,
|
||||
validationResult,
|
||||
} from 'express-validator';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import SystemUser from '@/system/models/SystemUser';
|
||||
|
||||
export default {
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.put('/:id/inactive',
|
||||
this.inactiveUser.validation,
|
||||
asyncMiddleware(this.inactiveUser.handler));
|
||||
|
||||
router.put('/:id/active',
|
||||
this.activeUser.validation,
|
||||
asyncMiddleware(this.activeUser.handler));
|
||||
|
||||
router.post('/:id',
|
||||
this.editUser.validation,
|
||||
asyncMiddleware(this.editUser.handler));
|
||||
|
||||
router.get('/',
|
||||
this.listUsers.validation,
|
||||
asyncMiddleware(this.listUsers.handler));
|
||||
|
||||
router.get('/:id',
|
||||
this.getUser.validation,
|
||||
asyncMiddleware(this.getUser.handler));
|
||||
|
||||
router.delete('/:id',
|
||||
this.deleteUser.validation,
|
||||
asyncMiddleware(this.deleteUser.handler));
|
||||
|
||||
return router;
|
||||
},
|
||||
|
||||
/**
|
||||
* Edit details of the given user.
|
||||
*/
|
||||
editUser: {
|
||||
validation: [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
check('first_name').exists(),
|
||||
check('last_name').exists(),
|
||||
check('email').exists().isEmail(),
|
||||
check('phone_number').optional().isMobilePhone(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const { id } = req.params;
|
||||
const validationErrors = validationResult(req);
|
||||
|
||||
if (!validationErrors.isEmpty()) {
|
||||
return res.boom.badData(null, {
|
||||
code: 'validation_error', ...validationErrors,
|
||||
});
|
||||
}
|
||||
const { TenantUser } = req.models;
|
||||
const { user } = req;
|
||||
const form = { ...req.body };
|
||||
|
||||
const foundUsers = await TenantUser.query()
|
||||
.whereNot('id', id)
|
||||
.andWhere((q) => {
|
||||
q.where('email', form.email);
|
||||
q.orWhere('phone_number', form.phone_number);
|
||||
});
|
||||
|
||||
const foundUserEmail = foundUsers.find((u) => u.email === form.email);
|
||||
const foundUserPhone = foundUsers.find((u) => u.phoneNumber === form.phone_number);
|
||||
|
||||
const errorReasons = [];
|
||||
|
||||
if (foundUserEmail) {
|
||||
errorReasons.push({ type: 'EMAIL_ALREADY_EXIST', code: 100 });
|
||||
}
|
||||
if (foundUserPhone) {
|
||||
errorReasons.push({ type: 'PHONE_NUMBER_ALREADY_EXIST', code: 120 });
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.boom.badRequest(null, { errors: errorReasons });
|
||||
}
|
||||
const userForm = {
|
||||
first_name: form.first_name,
|
||||
last_name: form.last_name,
|
||||
email: form.email,
|
||||
phone_number: form.phone_number,
|
||||
};
|
||||
const updateTenantUser = TenantUser.query()
|
||||
.where('id', id).update({ ...userForm });
|
||||
|
||||
const updateSystemUser = SystemUser.query()
|
||||
.where('id', user.id).update({ ...userForm });
|
||||
|
||||
await Promise.all([
|
||||
updateTenantUser, updateSystemUser,
|
||||
]);
|
||||
return res.status(200).send();
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Soft deleting the given user.
|
||||
*/
|
||||
deleteUser: {
|
||||
validation: [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const { id } = req.params;
|
||||
const { TenantUser } = req.models;
|
||||
const user = await TenantUser.query().where('id', id).first();
|
||||
|
||||
if (!user) {
|
||||
return res.boom.notFound(null, {
|
||||
errors: [{ type: 'USER_NOT_FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
const tenantUserDel = TenantUser.query().where('id', id).delete();
|
||||
const systemUserDel = SystemUser.query().where('id', id).delete();
|
||||
|
||||
await Promise.all([
|
||||
tenantUserDel,
|
||||
systemUserDel,
|
||||
]);
|
||||
return res.status(200).send();
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieve user details of the given user id.
|
||||
*/
|
||||
getUser: {
|
||||
validation: [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const { id } = req.params;
|
||||
const { TenantUser } = req.models;
|
||||
const user = await TenantUser.query().where('id', id).first();
|
||||
|
||||
if (!user) {
|
||||
return res.boom.notFound();
|
||||
}
|
||||
return res.status(200).send({ user });
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieve the list of users.
|
||||
*/
|
||||
listUsers: {
|
||||
validation: [
|
||||
query('page_size').optional().isNumeric().toInt(),
|
||||
query('page').optional().isNumeric().toInt(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const filter = {
|
||||
page_size: 10,
|
||||
page: 1,
|
||||
...req.query,
|
||||
};
|
||||
const { TenantUser } = req.models;
|
||||
const users = await TenantUser.query()
|
||||
.page(filter.page - 1, filter.page_size);
|
||||
|
||||
return res.status(200).send({ users });
|
||||
},
|
||||
},
|
||||
|
||||
inactiveUser: {
|
||||
validation: [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const validationErrors = validationResult(req);
|
||||
|
||||
if (!validationErrors.isEmpty()) {
|
||||
return res.boom.badData(null, {
|
||||
code: 'validation_error', ...validationErrors,
|
||||
});
|
||||
}
|
||||
const { id } = req.params;
|
||||
const { user } = req;
|
||||
const { TenantUser } = req.models;
|
||||
const tenantUser = TenantUser.query().where('id', id).first();
|
||||
|
||||
if (!tenantUser) {
|
||||
return res.boom.notFound(null, {
|
||||
errors: [{ type: 'USER.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
const updateTenantUser = TenantUser.query()
|
||||
.where('id', id).update({ active: false });
|
||||
|
||||
const updateSystemUser = SystemUser.query()
|
||||
.where('id', user.id).update({ active: false });
|
||||
|
||||
await Promise.all([
|
||||
updateTenantUser, updateSystemUser,
|
||||
]);
|
||||
|
||||
return res.status(200).send({ id: tenantUser.id });
|
||||
},
|
||||
},
|
||||
|
||||
activeUser: {
|
||||
validation: [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const validationErrors = validationResult(req);
|
||||
|
||||
if (!validationErrors.isEmpty()) {
|
||||
return res.boom.badData(null, {
|
||||
code: 'validation_error', ...validationErrors,
|
||||
});
|
||||
}
|
||||
const { id } = req.params;
|
||||
const { user } = req;
|
||||
const { TenantUser } = req.models;
|
||||
const tenantUser = TenantUser.query().where('id', id).first();
|
||||
|
||||
if (!tenantUser) {
|
||||
return res.boom.notFound(null, {
|
||||
errors: [{ type: 'USER.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
const updateTenantUser = TenantUser.query()
|
||||
.where('id', id).update({ active: true });
|
||||
|
||||
const updateSystemUser = SystemUser.query()
|
||||
.where('id', user.id).update({ active: true });
|
||||
|
||||
await Promise.all([
|
||||
updateTenantUser, updateSystemUser,
|
||||
]);
|
||||
return res.status(200).send({ id: tenantUser.id });
|
||||
},
|
||||
},
|
||||
};
|
||||
251
server/src/http/controllers/Users.ts
Normal file
251
server/src/http/controllers/Users.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import {
|
||||
check,
|
||||
query,
|
||||
param,
|
||||
} from 'express-validator';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import BaseController from '@/http/controllers/BaseController';
|
||||
import UsersService from '@/services/Users/UsersService';
|
||||
import { ServiceError, ServiceErrors } from '@/exceptions';
|
||||
import { ISystemUserDTO } from '@/interfaces';
|
||||
|
||||
@Service()
|
||||
export default class UsersController extends BaseController{
|
||||
@Inject()
|
||||
usersService: UsersService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.put('/:id/inactivate', [
|
||||
...this.specificUserSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.inactivateUser.bind(this))
|
||||
);
|
||||
router.put('/:id/activate', [
|
||||
...this.specificUserSchema
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.activateUser.bind(this))
|
||||
);
|
||||
router.post('/:id', [
|
||||
...this.userDTOSchema,
|
||||
...this.specificUserSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.editUser.bind(this))
|
||||
);
|
||||
router.get('/',
|
||||
this.listUsersSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.listUsers.bind(this))
|
||||
);
|
||||
router.get('/:id', [
|
||||
...this.specificUserSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getUser.bind(this))
|
||||
);
|
||||
router.delete('/:id', [
|
||||
...this.specificUserSchema
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteUser.bind(this))
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* User DTO Schema.
|
||||
*/
|
||||
get userDTOSchema() {
|
||||
return [
|
||||
check('first_name').exists(),
|
||||
check('last_name').exists(),
|
||||
check('email').exists().isEmail(),
|
||||
check('phone_number').optional().isMobilePhone(),
|
||||
]
|
||||
}
|
||||
|
||||
get specificUserSchema() {
|
||||
return [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
get listUsersSchema() {
|
||||
return [
|
||||
query('page_size').optional().isNumeric().toInt(),
|
||||
query('page').optional().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit details of the given user.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response|void}
|
||||
*/
|
||||
async editUser(req: Request, res: Response, next: NextFunction) {
|
||||
const userDTO: ISystemUserDTO = this.matchedBodyData(req);
|
||||
const { tenantId } = req;
|
||||
const { id: userId } = req.params;
|
||||
|
||||
try {
|
||||
await this.usersService.editUser(tenantId, userId, userDTO);
|
||||
return res.status(200).send({ id: userId });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
if (error instanceof ServiceErrors) {
|
||||
const errorReasons = [];
|
||||
|
||||
if (error.errorType === 'email_already_exists') {
|
||||
errorReasons.push({ type: 'EMAIL_ALREADY_EXIST', code: 100 });
|
||||
}
|
||||
if (error.errorType === 'phone_number_already_exist') {
|
||||
errorReasons.push({ type: 'PHONE_NUMBER_ALREADY_EXIST', code: 200 });
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft deleting the given user.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response|void}
|
||||
*/
|
||||
async deleteUser(req: Request, res: Response, next: Function) {
|
||||
const { id } = req.params;
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
await this.usersService.deleteUser(tenantId, id);
|
||||
return res.status(200).send({ id });
|
||||
} catch (error) {
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.boom.notFound(null, {
|
||||
errors: [{ type: 'USER_NOT_FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve user details of the given user id.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response|void}
|
||||
*/
|
||||
async getUser(req: Request, res: Response, next: NextFunction) {
|
||||
const { id: userId } = req.params;
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
const user = await this.usersService.getUser(tenantId, userId);
|
||||
return res.status(200).send({ user });
|
||||
} catch (error) {
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.boom.notFound(null, {
|
||||
errors: [{ type: 'USER_NOT_FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of users.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response|void}
|
||||
*/
|
||||
async listUsers(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
try {
|
||||
const users = await this.usersService.getList(tenantId);
|
||||
|
||||
return res.status(200).send({ users });
|
||||
} catch (error) {
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate the given user.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async activateUser(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: userId } = req.params;
|
||||
|
||||
try {
|
||||
await this.usersService.activateUser(tenantId, userId);
|
||||
return res.status(200).send({ id: userId });
|
||||
} catch(error) {
|
||||
console.log(error);
|
||||
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.status(404).send({
|
||||
errors: [{ type: 'USER.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'user_already_active') {
|
||||
return res.status(404).send({
|
||||
errors: [{ type: 'USER.ALREADY.ACTIVE', code: 200 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inactivate the given user.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response|void}
|
||||
*/
|
||||
async inactivateUser(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId, user } = req;
|
||||
const { id: userId } = req.params;
|
||||
|
||||
try {
|
||||
await this.usersService.inactivateUser(tenantId, userId);
|
||||
return res.status(200).send({ id: userId });
|
||||
} catch(error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'user_not_found') {
|
||||
return res.status(404).send({
|
||||
errors: [{ type: 'USER.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'user_already_inactive') {
|
||||
return res.status(404).send({
|
||||
errors: [{ type: 'USER.ALREADY.INACTIVE', code: 200 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user