diff --git a/src/Controllers/UserCotroller.ts b/src/Controllers/UserCotroller.ts index 8350dd06690c9b0f1c1a5851e4b0f1f034490ca0..55c5f21ebf697b8af65f6598214b92ed44fe5538 100644 --- a/src/Controllers/UserCotroller.ts +++ b/src/Controllers/UserCotroller.ts @@ -143,12 +143,12 @@ const login = async (req: Request, res: Response): Promise<Response> => { const validate = async (req: Request, res: Response): Promise<Response> => { const { token } = req.body; if (token) { - jwt.verify(token, secret.auth, (error: Error, decoded: {id: number; type: number}) => { + jwt.verify(token, secret.auth, (error: Error, decoded: {user: number; role: number}) => { if (error) { const message = 'Invalid token'; return res.status(401).send({ message }); } - const userId = decoded.id; + const userId = decoded.user; return res.status(200).send({ userId }); }); } else { @@ -157,6 +157,17 @@ const validate = async (req: Request, res: Response): Promise<Response> => { return res.status(500).send(); }; +const listUsersById = async (req: Request, res: Response): Promise<Response> => { + try { + const { userIds } = req.body; + const users = await UserService.listUsersById(userIds); + return res.status(200).send(users); + } catch (error) { + const e = error as Error; + return res.status(400).json({ error: e.message }); + } +}; + router.route('/login') .post(login); @@ -192,4 +203,7 @@ router.route('/:id/admin') router.route('/:id/client') .put(removeAdminPermission); +router.route('/usersById') + .post(listUsersById); + export default router; diff --git a/src/Services/UserService.ts b/src/Services/UserService.ts index 6663071eb0cc987f38c49bdd05c6645e12372a1b..a361287b969d5bde41ef852200a5755609e04c28 100644 --- a/src/Services/UserService.ts +++ b/src/Services/UserService.ts @@ -1,5 +1,5 @@ import bcrypt from 'bcrypt'; -import { Op } from 'sequelize'; +import { Op, where } from 'sequelize'; import { profiles, status } from '../enums/index.enum'; import Paginator from '../interfaces/paginator.interface'; import { User } from '../models/users.model'; @@ -418,6 +418,16 @@ const login = async (userDTO: UserLoginDTO): Promise<User> => User.findOne({ throw new Error('find user error'); }); +const listUsersById = async (ids: number[]): Promise<User[]> => { + const users = User.findAll({ + attributes: [ + 'id', 'name', 'email', 'organization', 'type', + ], + where: { id: { [Op.in]: ids } }, + }); + return users; +}; + export default { listAll, listPending, @@ -433,4 +443,5 @@ export default { giveAdminPermission, removeAdminPermission, login, + listUsersById, };