feat: Modify ticket querying process to prioritize querying by ticket IDs before retrieving messages

feat-scaling-ticket-remote-creation
adriano 2024-03-14 12:10:26 -03:00
parent 5bcfc1bdcc
commit 75b14becc3
2 changed files with 45 additions and 36 deletions

View File

@ -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;
@ -41,42 +44,41 @@ const ShowTicketReport = async ({
queueId
}: Request): Promise<Response> => {
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",

View File

@ -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(() => {
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 }, })
const { data } = await api.get("/reports/", { params: { userId, startDate, endDate, pageNumber: pageNumberTickets, createdOrUpdated: selectedValue, queueId }, userQueues: userA.queues })
let ticketsQueue = data.tickets