changed contacts routes to typescript

This commit is contained in:
canove
2020-09-15 19:28:06 -03:00
parent 14d90a2dd4
commit b04c0b878e
21 changed files with 536 additions and 133 deletions

View File

@@ -0,0 +1,50 @@
import { Sequelize, Op } from "sequelize";
import Contact from "../../models/Contact";
interface Request {
searchParam?: string;
pageNumber?: string;
}
interface Response {
contacts: Contact[];
count: number;
hasMore: boolean;
}
const ListContactsService = async ({
searchParam = "",
pageNumber = "1"
}: Request): Promise<Response> => {
const whereCondition = {
[Op.or]: [
{
name: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
`%${searchParam.toLowerCase()}%`
)
},
{ number: { [Op.like]: `%${searchParam}%` } }
]
};
const limit = 20;
const offset = limit * (+pageNumber - 1);
const { count, rows: contacts } = await Contact.findAndCountAll({
where: whereCondition,
limit,
offset,
order: [["createdAt", "DESC"]]
});
const hasMore = count > offset + contacts.length;
return {
contacts,
count,
hasMore
};
};
export default ListContactsService;