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 Message from "../../models/Message";
|
||||||
import { userInfo } from "os";
|
import { userInfo } from "os";
|
||||||
|
|
||||||
import { Op, where } from "sequelize";
|
import { Op, QueryTypes, where } from "sequelize";
|
||||||
|
|
||||||
import { Sequelize } from "sequelize";
|
import { Sequelize } from "sequelize";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
|
const dbConfig = require("../../config/database");
|
||||||
|
const sequelize = new Sequelize(dbConfig);
|
||||||
|
|
||||||
import { startOfDay, endOfDay, parseISO, getDate } from "date-fns";
|
import { startOfDay, endOfDay, parseISO, getDate } from "date-fns";
|
||||||
import { string } from "yup/lib/locale";
|
import { string } from "yup/lib/locale";
|
||||||
import Whatsapp from "../../models/Whatsapp";
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import Query from "mysql2/typings/mysql/lib/protocol/sequences/Query"
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
userId: string | number;
|
userId: string | number;
|
||||||
|
@ -41,42 +44,41 @@ const ShowTicketReport = async ({
|
||||||
queueId
|
queueId
|
||||||
}: Request): Promise<Response> => {
|
}: Request): Promise<Response> => {
|
||||||
let where_clause: any = {};
|
let where_clause: any = {};
|
||||||
|
let query = "";
|
||||||
|
|
||||||
if (userId !== "0") {
|
if (userId !== "0") {
|
||||||
where_clause.userid = userId;
|
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) {
|
if (queueId) {
|
||||||
where_clause.queueId = queueId;
|
where_clause.queueId = queueId;
|
||||||
|
query = `AND t.queueId = ${queueId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const limit = 40;
|
const limit = 40;
|
||||||
const offset = limit * (+pageNumber - 1);
|
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({
|
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||||
where: where_clause,
|
where: {
|
||||||
|
id: { [Op.in]: _ticketsId.map((t: any) => t.id) }
|
||||||
|
},
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
|
|
||||||
|
@ -107,7 +109,6 @@ const ShowTicketReport = async ({
|
||||||
model: Message,
|
model: Message,
|
||||||
required: true,
|
required: true,
|
||||||
separate: true,
|
separate: true,
|
||||||
// where: where_clause_msg ,
|
|
||||||
|
|
||||||
attributes: [
|
attributes: [
|
||||||
"body",
|
"body",
|
||||||
|
|
|
@ -300,6 +300,8 @@ const Report = () => {
|
||||||
|
|
||||||
const [reportTypeList,] = useState(reportOptType)
|
const [reportTypeList,] = useState(reportOptType)
|
||||||
const [reportType, setReportType] = useState('1')
|
const [reportType, setReportType] = useState('1')
|
||||||
|
const [firstLoad, setFirstLoad] = useState(true);
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch({ type: "RESET" })
|
dispatch({ type: "RESET" })
|
||||||
|
@ -309,6 +311,14 @@ const Report = () => {
|
||||||
}, [searchParam, profile])
|
}, [searchParam, profile])
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (firstLoad) {
|
||||||
|
setFirstLoad(false)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}, [firstLoad]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
//setLoading(true);
|
//setLoading(true);
|
||||||
|
|
||||||
|
@ -340,16 +350,14 @@ const Report = () => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
||||||
//setLoading(true);
|
//setLoading(true);
|
||||||
|
if (firstLoad) return
|
||||||
|
|
||||||
const delayDebounceFn = setTimeout(() => {
|
const delayDebounceFn = setTimeout(() => {
|
||||||
|
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const fetchQueries = async () => {
|
const fetchQueries = async () => {
|
||||||
try {
|
try {
|
||||||
if (reportOption === '1') {
|
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 })
|
const { data } = await api.get("/reports/", { params: { userId, startDate, endDate, pageNumber: pageNumberTickets, createdOrUpdated: selectedValue, queueId }, userQueues: userA.queues })
|
||||||
|
|
||||||
let ticketsQueue = data.tickets
|
let ticketsQueue = data.tickets
|
||||||
|
|
Loading…
Reference in New Issue