feat: Modify ticket querying process to prioritize querying by ticket IDs before retrieving messages
parent
5bcfc1bdcc
commit
75b14becc3
|
@ -7,14 +7,17 @@ import Queue from "../../models/Queue";
|
|||
import Message from "../../models/Message";
|
||||
import { userInfo } from "os";
|
||||
|
||||
import { Op, where } from "sequelize";
|
||||
import { Op, QueryTypes, where } from "sequelize";
|
||||
|
||||
import { Sequelize } from "sequelize";
|
||||
import moment from "moment";
|
||||
const dbConfig = require("../../config/database");
|
||||
const sequelize = new Sequelize(dbConfig);
|
||||
|
||||
import { startOfDay, endOfDay, parseISO, getDate } from "date-fns";
|
||||
import { string } from "yup/lib/locale";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import Query from "mysql2/typings/mysql/lib/protocol/sequences/Query"
|
||||
|
||||
interface Request {
|
||||
userId: string | number;
|
||||
|
@ -40,43 +43,42 @@ const ShowTicketReport = async ({
|
|||
createdOrUpdated = "created",
|
||||
queueId
|
||||
}: Request): Promise<Response> => {
|
||||
let where_clause: any = {};
|
||||
let where_clause: any = {};
|
||||
let query = "";
|
||||
|
||||
if (userId !== "0") {
|
||||
where_clause.userid = userId;
|
||||
query = `AND t.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;
|
||||
}
|
||||
query = `AND t.queueId = ${queueId}`;
|
||||
}
|
||||
|
||||
const limit = 40;
|
||||
const offset = limit * (+pageNumber - 1);
|
||||
|
||||
const createdAtOrUpdatedAt =
|
||||
createdOrUpdated == "created" ? "createdAt" : "updatedAt";
|
||||
|
||||
const _ticketsId = await sequelize.query(
|
||||
`SELECT t.id
|
||||
FROM Tickets t
|
||||
INNER JOIN (
|
||||
SELECT DISTINCT ticketId
|
||||
FROM Messages
|
||||
WHERE ${createdAtOrUpdatedAt} >= '${startDate} 00:00:00' AND ${createdAtOrUpdatedAt} <= '${endDate} 23:59:59'
|
||||
) AS m ON m.ticketId = t.id ${query};`,
|
||||
{ type: QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
console.log('QUERY: ', query)
|
||||
|
||||
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||
where: where_clause,
|
||||
where: {
|
||||
id: { [Op.in]: _ticketsId.map((t: any) => t.id) }
|
||||
},
|
||||
limit,
|
||||
offset,
|
||||
|
||||
|
@ -107,7 +109,6 @@ const ShowTicketReport = async ({
|
|||
model: Message,
|
||||
required: true,
|
||||
separate: true,
|
||||
// where: where_clause_msg ,
|
||||
|
||||
attributes: [
|
||||
"body",
|
||||
|
@ -142,15 +143,15 @@ const ShowTicketReport = async ({
|
|||
model: Whatsapp,
|
||||
attributes: ["name"]
|
||||
}
|
||||
],
|
||||
],
|
||||
order: [["updatedAt", "DESC"]]
|
||||
});
|
||||
|
||||
|
||||
const hasMore = count > offset + tickets.length;
|
||||
|
||||
if (!tickets) {
|
||||
throw new AppError("ERR_NO_TICKET_FOUND", 404);
|
||||
}
|
||||
}
|
||||
|
||||
return { tickets, count, hasMore };
|
||||
};
|
||||
|
|
|
@ -300,6 +300,8 @@ const Report = () => {
|
|||
|
||||
const [reportTypeList,] = useState(reportOptType)
|
||||
const [reportType, setReportType] = useState('1')
|
||||
const [firstLoad, setFirstLoad] = useState(true);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
dispatch({ type: "RESET" })
|
||||
|
@ -309,6 +311,14 @@ const Report = () => {
|
|||
}, [searchParam, profile])
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (firstLoad) {
|
||||
setFirstLoad(false)
|
||||
} else {
|
||||
|
||||
}
|
||||
}, [firstLoad]);
|
||||
|
||||
useEffect(() => {
|
||||
//setLoading(true);
|
||||
|
||||
|
@ -340,16 +350,14 @@ const Report = () => {
|
|||
useEffect(() => {
|
||||
|
||||
//setLoading(true);
|
||||
if (firstLoad) return
|
||||
|
||||
const delayDebounceFn = setTimeout(() => {
|
||||
|
||||
const delayDebounceFn = setTimeout(() => {
|
||||
setLoading(true)
|
||||
const fetchQueries = async () => {
|
||||
try {
|
||||
if (reportOption === '1') {
|
||||
|
||||
// const { data } = await api.get("/reports/", { params: { userId: userId ? userId : 0, startDate: convertAndFormatDate(startDate), endDate: convertAndFormatDate(endDate), pageNumber: pageNumberTickets }, })
|
||||
|
||||
if (reportOption === '1') {
|
||||
|
||||
const { data } = await api.get("/reports/", { params: { userId, startDate, endDate, pageNumber: pageNumberTickets, createdOrUpdated: selectedValue, queueId }, userQueues: userA.queues })
|
||||
|
||||
let ticketsQueue = data.tickets
|
||||
|
|
Loading…
Reference in New Issue