2022-01-15 18:32:46 +00:00
|
|
|
import Ticket from "../../models/Ticket";
|
|
|
|
import AppError from "../../errors/AppError";
|
|
|
|
import Contact from "../../models/Contact";
|
|
|
|
import User from "../../models/User";
|
|
|
|
import Queue from "../../models/Queue";
|
|
|
|
|
|
|
|
import Message from "../../models/Message";
|
|
|
|
import { userInfo } from "os";
|
|
|
|
|
2022-01-25 21:54:46 +00:00
|
|
|
import { Op, where } from "sequelize";
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2024-02-28 13:48:36 +00:00
|
|
|
import { Sequelize } from "sequelize";
|
|
|
|
import moment from "moment";
|
|
|
|
|
|
|
|
import { startOfDay, endOfDay, parseISO, getDate } from "date-fns";
|
2022-01-25 21:54:46 +00:00
|
|
|
import { string } from "yup/lib/locale";
|
2022-04-17 21:02:15 +00:00
|
|
|
import Whatsapp from "../../models/Whatsapp";
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
interface Request {
|
|
|
|
userId: string | number;
|
|
|
|
startDate: string;
|
|
|
|
endDate: string;
|
2024-02-28 13:48:36 +00:00
|
|
|
createdOrUpdated?: string;
|
|
|
|
queueId?: string;
|
2022-07-28 17:55:23 +00:00
|
|
|
pageNumber?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Response {
|
|
|
|
tickets: Ticket[];
|
|
|
|
count: number;
|
|
|
|
hasMore: boolean;
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
//Report by user, startDate, endDate
|
2022-07-28 17:55:23 +00:00
|
|
|
const ShowTicketReport = async ({
|
|
|
|
userId,
|
|
|
|
startDate,
|
|
|
|
endDate,
|
2024-02-28 13:48:36 +00:00
|
|
|
pageNumber = "1",
|
|
|
|
createdOrUpdated = "created",
|
|
|
|
queueId
|
|
|
|
}: Request): Promise<Response> => {
|
2024-03-13 21:16:56 +00:00
|
|
|
let where_clause: any = {};
|
2024-03-01 20:47:18 +00:00
|
|
|
|
|
|
|
if (userId !== "0") {
|
|
|
|
where_clause.userid = userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (createdOrUpdated === "updated") {
|
|
|
|
where_clause = {
|
|
|
|
...where_clause,
|
|
|
|
updatedAt: {
|
|
|
|
[Op.gte]: startDate + " 00:00:00.000000",
|
|
|
|
[Op.lte]: endDate + " 23:59:59.999999"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (createdOrUpdated === "created") {
|
|
|
|
where_clause = {
|
|
|
|
...where_clause,
|
|
|
|
createdAt: {
|
|
|
|
[Op.gte]: startDate + " 00:00:00.000000",
|
|
|
|
[Op.lte]: endDate + " 23:59:59.999999"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let { userid, ...where_clause_msg } = where_clause;
|
|
|
|
|
|
|
|
if (queueId) {
|
|
|
|
where_clause.queueId = queueId;
|
2024-03-13 21:16:56 +00:00
|
|
|
}
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
const limit = 40;
|
|
|
|
const offset = limit * (+pageNumber - 1);
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2024-02-28 13:48:36 +00:00
|
|
|
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
|
|
|
where: where_clause,
|
2022-07-28 17:55:23 +00:00
|
|
|
limit,
|
|
|
|
offset,
|
2022-01-26 02:05:48 +00:00
|
|
|
|
2024-02-28 13:48:36 +00:00
|
|
|
attributes: [
|
|
|
|
"id",
|
|
|
|
"status",
|
|
|
|
"statusChatEnd",
|
|
|
|
[
|
|
|
|
Sequelize.fn(
|
|
|
|
"DATE_FORMAT",
|
|
|
|
Sequelize.col("Ticket.createdAt"),
|
|
|
|
"%d/%m/%Y %H:%i:%s"
|
|
|
|
),
|
|
|
|
"createdAt"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
Sequelize.fn(
|
|
|
|
"DATE_FORMAT",
|
|
|
|
Sequelize.col("Ticket.updatedAt"),
|
|
|
|
"%d/%m/%Y %H:%i:%s"
|
|
|
|
),
|
|
|
|
"updatedAt"
|
|
|
|
]
|
|
|
|
],
|
2022-01-26 02:05:48 +00:00
|
|
|
|
2022-01-15 18:32:46 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Message,
|
2024-02-28 13:48:36 +00:00
|
|
|
required: true,
|
2022-01-15 18:32:46 +00:00
|
|
|
separate: true,
|
2024-03-13 21:16:56 +00:00
|
|
|
// where: where_clause_msg ,
|
2022-01-26 02:05:48 +00:00
|
|
|
|
2024-02-28 13:48:36 +00:00
|
|
|
attributes: [
|
|
|
|
"body",
|
|
|
|
"read",
|
|
|
|
"mediaType",
|
|
|
|
"fromMe",
|
|
|
|
"mediaUrl",
|
|
|
|
[
|
|
|
|
Sequelize.fn(
|
|
|
|
"DATE_FORMAT",
|
|
|
|
Sequelize.col("createdAt"),
|
|
|
|
"%d/%m/%Y %H:%i:%s"
|
|
|
|
),
|
|
|
|
"createdAt"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
order: [["createdAt", "ASC"]]
|
2022-01-15 18:32:46 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 13:48:36 +00:00
|
|
|
model: Contact,
|
|
|
|
attributes: ["name", "number"]
|
2022-01-15 18:32:46 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 13:48:36 +00:00
|
|
|
model: User,
|
|
|
|
attributes: ["name", "email"]
|
2022-01-15 18:32:46 +00:00
|
|
|
},
|
2022-04-17 21:02:15 +00:00
|
|
|
{
|
2024-02-28 13:48:36 +00:00
|
|
|
model: Queue,
|
|
|
|
attributes: ["name"]
|
2022-04-17 21:02:15 +00:00
|
|
|
},
|
2024-02-28 13:48:36 +00:00
|
|
|
{
|
|
|
|
model: Whatsapp,
|
|
|
|
attributes: ["name"]
|
|
|
|
}
|
2024-03-01 20:47:18 +00:00
|
|
|
],
|
2024-02-28 13:48:36 +00:00
|
|
|
order: [["updatedAt", "DESC"]]
|
|
|
|
});
|
2024-03-01 20:47:18 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
const hasMore = count > offset + tickets.length;
|
2024-02-28 13:48:36 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
if (!tickets) {
|
2022-01-15 18:32:46 +00:00
|
|
|
throw new AppError("ERR_NO_TICKET_FOUND", 404);
|
2024-03-13 21:16:56 +00:00
|
|
|
}
|
2022-01-15 18:32:46 +00:00
|
|
|
|
2024-02-28 13:48:36 +00:00
|
|
|
return { tickets, count, hasMore };
|
|
|
|
};
|
2022-01-15 18:32:46 +00:00
|
|
|
|
|
|
|
export default ShowTicketReport;
|