2022-01-06 01:26:15 +00:00
|
|
|
import { Op, fn, where, col, Filterable, Includeable } from "sequelize";
|
2022-06-17 22:48:05 +00:00
|
|
|
import { startOfDay, endOfDay, parseISO, format } from "date-fns";
|
2024-04-12 21:33:15 +00:00
|
|
|
import ptBR from "date-fns/locale/pt-BR";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
import Ticket from "../../models/Ticket";
|
|
|
|
import Contact from "../../models/Contact";
|
|
|
|
import Message from "../../models/Message";
|
|
|
|
import Queue from "../../models/Queue";
|
|
|
|
import ShowUserService from "../UserServices/ShowUserService";
|
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
const unflatten = require("flat").unflatten;
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-06-17 22:48:05 +00:00
|
|
|
import { splitDateTime } from "../../helpers/SplitDateTime";
|
2024-04-12 21:33:15 +00:00
|
|
|
const dateToday = splitDateTime(
|
|
|
|
new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }))
|
|
|
|
);
|
2022-06-17 22:48:05 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
import ListTicketServiceCache from "./ListTicketServiceCache";
|
2022-10-02 06:22:44 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
import { searchTicketCache, loadTicketsCache } from "../../helpers/TicketCache";
|
2023-02-07 15:47:40 +00:00
|
|
|
import { getWbot } from "../../libs/wbot";
|
2023-07-12 14:54:29 +00:00
|
|
|
import User from "../../models/User";
|
2024-04-23 22:51:11 +00:00
|
|
|
import { get } from "../../helpers/RedisClient";
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface Request {
|
|
|
|
searchParam?: string;
|
|
|
|
pageNumber?: string;
|
|
|
|
status?: string;
|
|
|
|
date?: string;
|
|
|
|
showAll?: string;
|
|
|
|
userId: string;
|
|
|
|
withUnreadMessages?: string;
|
|
|
|
queueIds: number[];
|
2022-04-18 18:21:28 +00:00
|
|
|
unlimited?: string;
|
2023-07-12 14:54:29 +00:00
|
|
|
searchParamContent?: string;
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Response {
|
|
|
|
tickets: Ticket[];
|
|
|
|
count: number;
|
|
|
|
hasMore: boolean;
|
2024-04-23 22:51:11 +00:00
|
|
|
remoteTicketsControll?: object[];
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ListTicketsService = async ({
|
|
|
|
searchParam = "",
|
|
|
|
pageNumber = "1",
|
|
|
|
queueIds,
|
|
|
|
status,
|
|
|
|
date,
|
|
|
|
showAll,
|
|
|
|
userId,
|
2022-04-18 18:21:28 +00:00
|
|
|
withUnreadMessages,
|
2024-04-12 21:33:15 +00:00
|
|
|
unlimited = "false",
|
2023-07-12 14:54:29 +00:00
|
|
|
searchParamContent = ""
|
2022-01-06 01:26:15 +00:00
|
|
|
}: Request): Promise<Response> => {
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log("----------> searchParamContent: ", searchParamContent);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
let whereCondition: Filterable["where"] = {
|
|
|
|
[Op.or]: [{ userId }, { status: "pending" }],
|
|
|
|
queueId: { [Op.or]: [queueIds, null] }
|
|
|
|
};
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log("PAGE NUMBER TICKET: ", pageNumber);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-12-21 00:28:41 +00:00
|
|
|
if (pageNumber.trim().length == 0) {
|
2024-04-12 21:33:15 +00:00
|
|
|
pageNumber = "1";
|
2022-12-21 00:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 23:16:39 +00:00
|
|
|
if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) {
|
2022-10-25 14:16:36 +00:00
|
|
|
try {
|
|
|
|
const offset = 40 * (+pageNumber - 1);
|
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
searchParam = searchParam.replace(/\s+/g, " ").trim().toLowerCase();
|
2022-12-21 00:28:41 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log("QUERY TICKET SEARCH PARAM FROM CACHE: ", searchParam);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
|
|
|
let tickets: any = await searchTicketCache(searchParam, offset, 40);
|
|
|
|
|
|
|
|
if (tickets) {
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log(
|
|
|
|
"QUERY TICKET SEARCH PARAM FROM CACHE LENGTH...: ",
|
|
|
|
tickets.length
|
|
|
|
);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
|
|
|
tickets.map((t: any) => {
|
2024-04-12 21:33:15 +00:00
|
|
|
t["contact.number"] = t["contact_number"];
|
|
|
|
delete t["contact_number"];
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
return { ...["contact_number"] };
|
|
|
|
});
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
tickets = tickets.map((e: any) => unflatten(e));
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
return {
|
|
|
|
tickets,
|
|
|
|
count: tickets.length,
|
|
|
|
hasMore: tickets.length > 0 ? true : false
|
|
|
|
};
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log(
|
|
|
|
"There was an error on search ListTicketservice.ts search cache: ",
|
|
|
|
error
|
|
|
|
);
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
console.log("QUERY TICKETS FROM DATABASE...");
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
let includeCondition: Includeable[];
|
|
|
|
|
|
|
|
includeCondition = [
|
|
|
|
{
|
|
|
|
model: Contact,
|
|
|
|
as: "contact",
|
|
|
|
attributes: ["id", "name", "number", "profilePicUrl"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: Queue,
|
|
|
|
as: "queue",
|
|
|
|
attributes: ["id", "name", "color"]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
if (showAll === "true") {
|
|
|
|
whereCondition = { queueId: { [Op.or]: [queueIds, null] } };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status) {
|
2022-07-28 21:21:14 +00:00
|
|
|
whereCondition = { ...whereCondition, status };
|
|
|
|
|
2024-02-26 19:58:39 +00:00
|
|
|
if (unlimited === "current" && status !== "pending") {
|
2022-07-28 21:21:14 +00:00
|
|
|
whereCondition = {
|
|
|
|
...whereCondition,
|
|
|
|
createdAt: {
|
2024-02-26 19:58:39 +00:00
|
|
|
[Op.gte]: dateToday.fullDate + " 00:00:00.000000",
|
|
|
|
[Op.lte]: dateToday.fullDate + " 23:59:59.999999"
|
2022-07-28 21:21:14 +00:00
|
|
|
}
|
2024-02-26 19:58:39 +00:00
|
|
|
};
|
2022-07-28 21:21:14 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (searchParam) {
|
|
|
|
const sanitizedSearchParam = searchParam.toLocaleLowerCase().trim();
|
2024-04-12 21:33:15 +00:00
|
|
|
const sanitizedSearchParamContent = searchParamContent
|
|
|
|
.toLocaleLowerCase()
|
|
|
|
.trim();
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
if (searchParamContent.length > 0) {
|
|
|
|
includeCondition = [
|
|
|
|
...includeCondition,
|
|
|
|
{
|
|
|
|
model: Message,
|
|
|
|
as: "messages",
|
|
|
|
attributes: ["id", "body"],
|
|
|
|
where: {
|
2024-04-12 21:33:15 +00:00
|
|
|
body: where(
|
|
|
|
fn("LOWER", col("body")),
|
|
|
|
"LIKE",
|
|
|
|
`%${sanitizedSearchParamContent}%`
|
|
|
|
)
|
2023-07-12 14:54:29 +00:00
|
|
|
},
|
|
|
|
required: false,
|
|
|
|
duplicating: false
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
whereCondition = {
|
|
|
|
...whereCondition,
|
2024-04-12 21:33:15 +00:00
|
|
|
"$message.body$": where(
|
|
|
|
fn("LOWER", col("body")),
|
|
|
|
"LIKE",
|
|
|
|
`%${sanitizedSearchParamContent}%`
|
|
|
|
)
|
2023-07-12 14:54:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
whereCondition = {
|
|
|
|
...whereCondition,
|
|
|
|
[Op.or]: [
|
|
|
|
{
|
2024-04-12 21:33:15 +00:00
|
|
|
"$contact.name$": where(
|
|
|
|
fn("LOWER", col("contact.name")),
|
|
|
|
"LIKE",
|
|
|
|
`%${sanitizedSearchParam}%`
|
|
|
|
)
|
2022-01-06 01:26:15 +00:00
|
|
|
},
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
{ "$contact.number$": { [Op.like]: `%${sanitizedSearchParam}%` } }
|
|
|
|
]
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
const userProfile: any = await User.findByPk(userId);
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2024-01-30 12:50:37 +00:00
|
|
|
if (
|
|
|
|
userProfile.dataValues.profile != "admin" &&
|
2024-04-12 21:33:15 +00:00
|
|
|
userProfile.dataValues.profile != "master" &&
|
2024-02-26 19:58:39 +00:00
|
|
|
userProfile.dataValues.profile != "supervisor"
|
2024-01-30 12:50:37 +00:00
|
|
|
) {
|
|
|
|
whereCondition = { ...whereCondition, userId };
|
2023-07-12 14:54:29 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
whereCondition = {
|
|
|
|
createdAt: {
|
|
|
|
[Op.between]: [+startOfDay(parseISO(date)), +endOfDay(parseISO(date))]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (withUnreadMessages === "true") {
|
|
|
|
const user = await ShowUserService(userId);
|
|
|
|
const userQueueIds = user.queues.map(queue => queue.id);
|
|
|
|
|
|
|
|
whereCondition = {
|
|
|
|
[Op.or]: [{ userId }, { status: "pending" }],
|
|
|
|
queueId: { [Op.or]: [userQueueIds, null] },
|
|
|
|
unreadMessages: { [Op.gt]: 0 }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
let limit;
|
|
|
|
|
|
|
|
if (unlimited === "current") limit = 10000;
|
|
|
|
else if (unlimited === "all") limit = 100;
|
|
|
|
else limit = 40;
|
|
|
|
|
|
|
|
// const limit = unlimited === "current" || unlimited === "all" ? 1000 : 40;
|
|
|
|
const offset = limit * (+pageNumber - 1);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2024-02-26 19:58:39 +00:00
|
|
|
console.log("kkkkkkkkk limit: ", limit);
|
2022-10-02 06:22:44 +00:00
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
let { count, rows: tickets } = await Ticket.findAndCountAll({
|
2022-01-06 01:26:15 +00:00
|
|
|
where: whereCondition,
|
|
|
|
include: includeCondition,
|
|
|
|
distinct: true,
|
2022-07-28 21:21:14 +00:00
|
|
|
limit,
|
2022-01-06 01:26:15 +00:00
|
|
|
offset,
|
|
|
|
order: [["updatedAt", "DESC"]]
|
|
|
|
});
|
|
|
|
|
|
|
|
const hasMore = count > offset + tickets.length;
|
|
|
|
|
2024-04-23 22:51:11 +00:00
|
|
|
const ticketIds = await get({ key: `remote:controll`, parse: true });
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return {
|
|
|
|
tickets,
|
|
|
|
count,
|
2024-04-23 22:51:11 +00:00
|
|
|
hasMore,
|
|
|
|
remoteTicketsControll: ticketIds ? ticketIds : []
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ListTicketsService;
|