import { Sequelize, Op } from "sequelize"; import Queue from "../../models/Queue"; import User from "../../models/User"; interface Request { searchParam?: string; pageNumber?: string | number; profile?: string; } interface Response { users: User[]; count: number; hasMore: boolean; } const ListUsersService = async ({ searchParam = "", pageNumber = "1", profile = "" }: Request): Promise => { let whereCondition = {} if (profile.length > 0) { whereCondition = { profile: profile } } else { whereCondition = { [Op.or]: [ { "$User.name$": Sequelize.where( Sequelize.fn("LOWER", Sequelize.col("User.name")), "LIKE", `%${searchParam.toLowerCase()}%` ) }, { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } } ] }; } // const whereCondition = { // [Op.or]: [ // { // "$User.name$": Sequelize.where( // Sequelize.fn("LOWER", Sequelize.col("User.name")), // "LIKE", // `%${searchParam.toLowerCase()}%` // ) // }, // { email: { [Op.like]: `%${searchParam.toLowerCase()}%` } } // ] // }; // const limit = 20; const limit = 100; const offset = limit * (+pageNumber - 1); const { count, rows: users } = await User.findAndCountAll({ where: whereCondition, attributes: ["name", "id", "email","positionCompany", "profile", "createdAt"], limit, offset, order: [["createdAt", "DESC"]], include: [ { model: Queue, as: "queues", attributes: ["id", "name", "color"] } ] }); const hasMore = count > offset + users.length; return { users, count, hasMore }; }; export default ListUsersService;