mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
62 lines
1.2 KiB
TypeScript
62 lines
1.2 KiB
TypeScript
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> => {
|
|
let includeCondition = [
|
|
{
|
|
model: Contact,
|
|
as: "contact",
|
|
attributes: ["name", "number", "profilePicUrl"]
|
|
}
|
|
];
|
|
|
|
// let whereCondition = {};
|
|
|
|
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,
|
|
include: includeCondition,
|
|
limit,
|
|
offset,
|
|
order: [["createdAt", "DESC"]]
|
|
});
|
|
|
|
const hasMore = count > offset + contacts.length;
|
|
|
|
return {
|
|
contacts,
|
|
count,
|
|
hasMore
|
|
};
|
|
};
|
|
|
|
export default ListContactsService;
|