migrated setting routes to typescript

This commit is contained in:
canove
2020-09-15 17:16:29 -03:00
parent 149cbef98e
commit 14d90a2dd4
17 changed files with 111 additions and 34 deletions

View 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;