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
|
|
|
|
|
|
|
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;
|
|
|
|
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,
|
|
|
|
pageNumber = "1"
|
|
|
|
}: Request): Promise<Response> => {
|
2022-01-25 21:54:46 +00:00
|
|
|
|
|
|
|
let where_clause = {}
|
2022-01-15 18:32:46 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
if(userId=='0'){
|
2022-01-25 21:54:46 +00:00
|
|
|
where_clause = {
|
2022-10-02 06:22:44 +00:00
|
|
|
updatedAt: {
|
2022-01-25 14:24:05 +00:00
|
|
|
[Op.gte]: startDate+' 00:00:00.000000',
|
|
|
|
[Op.lte]: endDate +' 23:59:59.999999'
|
2022-01-25 21:54:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
where_clause = {
|
2022-07-28 17:55:23 +00:00
|
|
|
userid: userId,
|
2022-10-02 06:22:44 +00:00
|
|
|
updatedAt: {
|
2022-01-25 21:54:46 +00:00
|
|
|
[Op.gte]: startDate+' 00:00:00.000000',
|
|
|
|
[Op.lte]: endDate +' 23:59:59.999999'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
const limit = 40;
|
|
|
|
const offset = limit * (+pageNumber - 1);
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
const {count, rows: tickets} = await Ticket.findAndCountAll({
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-25 21:54:46 +00:00
|
|
|
where: where_clause ,
|
2022-07-28 17:55:23 +00:00
|
|
|
limit,
|
|
|
|
offset,
|
2022-01-26 02:05:48 +00:00
|
|
|
//attributes: ['id', 'status', 'createdAt', 'updatedAt'],
|
|
|
|
|
2022-05-19 21:29:38 +00:00
|
|
|
attributes: ['id', 'status', 'statusChatEnd', [Sequelize.fn("DATE_FORMAT",Sequelize.col("Ticket.createdAt"),"%d/%m/%Y %H:%i:%s"),"createdAt"],
|
2022-01-26 02:05:48 +00:00
|
|
|
[Sequelize.fn("DATE_FORMAT",Sequelize.col("Ticket.updatedAt"),"%d/%m/%Y %H:%i:%s"),"updatedAt"]],
|
|
|
|
|
2022-01-15 18:32:46 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Message,
|
|
|
|
required:true,
|
|
|
|
separate: true,
|
2022-01-26 02:05:48 +00:00
|
|
|
|
|
|
|
// attributes: ['body', 'read', 'mediaType','fromMe', 'mediaUrl','createdAt'],
|
|
|
|
|
|
|
|
attributes: ['body', 'read', 'mediaType','fromMe', 'mediaUrl', [Sequelize.fn("DATE_FORMAT",Sequelize.col("createdAt"),"%d/%m/%Y %H:%i:%s"),"createdAt"]],
|
|
|
|
|
2022-01-15 18:32:46 +00:00
|
|
|
order: [
|
|
|
|
['createdAt', 'ASC']
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2022-01-15 19:00:55 +00:00
|
|
|
model: Contact,
|
2022-01-15 18:32:46 +00:00
|
|
|
attributes: ['name', 'number']
|
|
|
|
},
|
|
|
|
{
|
2022-01-15 19:00:55 +00:00
|
|
|
model: User,
|
2022-01-15 18:32:46 +00:00
|
|
|
attributes: ['name', 'email']
|
|
|
|
},
|
|
|
|
{
|
2022-01-15 19:00:55 +00:00
|
|
|
model: Queue,
|
2022-01-15 18:32:46 +00:00
|
|
|
attributes: ['name']
|
|
|
|
},
|
2022-04-17 21:02:15 +00:00
|
|
|
{
|
|
|
|
model: Whatsapp,
|
|
|
|
attributes: ['name']
|
|
|
|
},
|
2022-01-25 14:24:05 +00:00
|
|
|
],
|
2022-07-28 17:55:23 +00:00
|
|
|
|
|
|
|
order: [
|
|
|
|
['id', 'ASC']
|
|
|
|
]
|
2022-01-15 18:32:46 +00:00
|
|
|
|
2022-01-27 00:33:16 +00:00
|
|
|
});
|
2022-07-28 17:55:23 +00:00
|
|
|
|
|
|
|
const hasMore = count > offset + tickets.length;
|
2022-01-27 00:33:16 +00:00
|
|
|
|
2022-01-15 18:32:46 +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);
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
return {tickets, count, hasMore};
|
2022-01-27 00:33:16 +00:00
|
|
|
};
|
2022-01-15 18:32:46 +00:00
|
|
|
|
|
|
|
export default ShowTicketReport;
|