mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
migrated setting routes to typescript
This commit is contained in:
51
backend/src/services/UserServices/ListUsersService.ts
Normal file
51
backend/src/services/UserServices/ListUsersService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Sequelize, Op } from "sequelize";
|
||||
import User from "../../models/User";
|
||||
|
||||
interface Request {
|
||||
searchParam?: string;
|
||||
pageNumber?: number;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
users: User[];
|
||||
count: number;
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
const ListUsersService = async ({
|
||||
searchParam = "",
|
||||
pageNumber = 1
|
||||
}: Request): Promise<Response> => {
|
||||
const whereCondition = {
|
||||
[Op.or]: [
|
||||
{
|
||||
name: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("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"],
|
||||
limit,
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]]
|
||||
});
|
||||
|
||||
const hasMore = count > limit + users.length;
|
||||
|
||||
return {
|
||||
users,
|
||||
count,
|
||||
hasMore
|
||||
};
|
||||
};
|
||||
|
||||
export default ListUsersService;
|
||||
Reference in New Issue
Block a user