mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { Sequelize, Op } from "sequelize";
|
|
import Queue from "../../models/Queue";
|
|
import User from "../../models/User";
|
|
import Whatsapp from "../../models/Whatsapp";
|
|
|
|
interface Request {
|
|
searchParam?: string;
|
|
pageNumber?: string | number;
|
|
}
|
|
|
|
interface Response {
|
|
users: User[];
|
|
count: number;
|
|
hasMore: boolean;
|
|
}
|
|
|
|
const ListUsersService = async ({
|
|
searchParam = "",
|
|
pageNumber = "1"
|
|
}: Request): Promise<Response> => {
|
|
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 offset = limit * (+pageNumber - 1);
|
|
|
|
const { count, rows: users } = await User.findAndCountAll({
|
|
where: whereCondition,
|
|
attributes: ["name", "id", "email", "profile", "createdAt"],
|
|
limit,
|
|
offset,
|
|
order: [["createdAt", "DESC"]],
|
|
include: [
|
|
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
|
|
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] },
|
|
]
|
|
});
|
|
|
|
const hasMore = count > offset + users.length;
|
|
|
|
return {
|
|
users,
|
|
count,
|
|
hasMore
|
|
};
|
|
};
|
|
|
|
export default ListUsersService;
|