Skip to content
Snippets Groups Projects
Commit 10520c30 authored by Ignacio Otero's avatar Ignacio Otero
Browse files

Funcionalidad de busqueda en autorización de usuarios

parent ddadafc9
No related branches found
No related tags found
No related merge requests found
......@@ -5,10 +5,32 @@ import UserService from '../Services/UserService';
const router = Router();
const list = async (req: Request, res: Response): Promise<Response> => {
const listAll = async (req: Request, res: Response): Promise<Response> => {
try {
const users: Paginator<User> = await UserService
.list(Number(req.query.limit), Number(req.query.offset));
.listAll(Number(req.query.limit), Number(req.query.offset));
return res.status(200).send(users);
} catch (error) {
const e = error as Error;
return res.status(400).json({ error: e.message });
}
};
const listPending = async (req: Request, res: Response): Promise<Response> => {
try {
const users: Paginator<User> = await UserService
.listPending(Number(req.query.limit), Number(req.query.offset), String(req.query.search));
return res.status(200).send(users);
} catch (error) {
const e = error as Error;
return res.status(400).json({ error: e.message });
}
};
const listApproved = async (req: Request, res: Response): Promise<Response> => {
try {
const users: Paginator<User> = await UserService
.listApproved(Number(req.query.limit), Number(req.query.offset), String(req.query.search));
return res.status(200).send(users);
} catch (error) {
const e = error as Error;
......@@ -77,9 +99,15 @@ const active = async (req: Request, res: Response): Promise<Response> => {
};
router.route('/')
.get(list)
.get(listAll)
.post(create);
router.route('/pending')
.get(listPending);
router.route('/approved')
.get(listApproved);
router.route('/:id')
.put(update)
.patch(active);
......
import bcrypt from 'bcrypt';
import { Op } from 'sequelize';
import { profiles, status } from '../enums/index.enum';
import Paginator from '../interfaces/paginator.interface';
import { User } from '../models/users.model';
import { UserCreateDTO, UserDTO } from '../DTOs/UserDTO';
const list = async (limit: number, offset: number): Promise<Paginator<User>> => {
const listPending = async (limit: number, offset: number,
search: string): Promise<Paginator<User>> => {
let options = {};
if (limit && offset) {
if (limit >= 1 && offset >= 0) {
if (search && search !== '') {
options = {
where: {
status: status.pending,
[Op.or]: [
{ name: { [Op.substring]: search } },
{ email: { [Op.substring]: search } },
],
},
limit,
offset,
};
} else {
options = {
where: {
status: status.pending,
},
limit,
offset,
};
}
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
order: [
['createdAt', 'ASC'],
],
...options,
});
};
const listApproved = async (limit: number, offset: number,
search: string): Promise<Paginator<User>> => {
let options = {};
if (limit >= 1 && offset >= 0) {
if (search && search !== '') {
options = {
where: {
status: status.approved,
[Op.or]: [
{ name: { [Op.substring]: search } },
{ email: { [Op.substring]: search } },
],
},
limit,
offset,
};
} else {
options = {
where: {
status: status.approved,
},
limit,
offset,
};
}
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
order: [
['createdAt', 'ASC'],
],
...options,
});
};
const listAll = async (limit: number, offset: number): Promise<Paginator<User>> => {
let options = {};
if (limit >= 1 && offset >= 0) {
options = {
limit,
offset,
......@@ -188,7 +263,9 @@ const active = async (userId: number): Promise<User> => User.findOne({
});
export default {
list,
listAll,
listPending,
listApproved,
create,
update,
password,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment