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-01-25 14:24:05 +00:00
|
|
|
|
|
|
|
//Report by user, startDate, endDate
|
|
|
|
const ShowTicketReport = async (id: string | number, startDate: string, endDate: string): Promise<Ticket[]> => {
|
2022-01-25 21:54:46 +00:00
|
|
|
|
|
|
|
let where_clause = {}
|
2022-01-15 18:32:46 +00:00
|
|
|
|
2022-01-25 21:54:46 +00:00
|
|
|
if(id=='0'){
|
|
|
|
where_clause = {
|
2022-01-25 14:24:05 +00:00
|
|
|
createdAt: {
|
|
|
|
[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 = {
|
|
|
|
userid: id,
|
|
|
|
createdAt: {
|
|
|
|
[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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ticket = await Ticket.findAll({
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-25 21:54:46 +00:00
|
|
|
where: where_clause ,
|
2022-01-26 02:05:48 +00:00
|
|
|
//attributes: ['id', 'status', 'createdAt', 'updatedAt'],
|
|
|
|
|
|
|
|
attributes: ['id', 'status', [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-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-01-25 14:24:05 +00:00
|
|
|
],
|
2022-01-15 18:32:46 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
/*//Report by user
|
|
|
|
const ShowTicketReport = async (id: string | number): Promise<Ticket[]> => {
|
|
|
|
|
|
|
|
const ticket = await Ticket.findAll({
|
|
|
|
where: {userid: id} ,
|
|
|
|
attributes: ['id', 'status', 'createdAt', 'updatedAt'],
|
2022-01-15 18:32:46 +00:00
|
|
|
include: [
|
|
|
|
{
|
2022-01-25 14:24:05 +00:00
|
|
|
model: Message,
|
|
|
|
required:true,
|
|
|
|
separate: true,
|
|
|
|
attributes: ['body', 'read', 'mediaType','fromMe', 'mediaUrl','createdAt'],
|
|
|
|
order: [
|
|
|
|
['createdAt', 'ASC']
|
|
|
|
]
|
2022-01-15 18:32:46 +00:00
|
|
|
},
|
2022-01-25 14:24:05 +00:00
|
|
|
{
|
|
|
|
model: Contact,
|
|
|
|
attributes: ['name', 'number']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: User,
|
|
|
|
attributes: ['name', 'email']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: Queue,
|
|
|
|
attributes: ['name']
|
|
|
|
},
|
2022-01-15 18:32:46 +00:00
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
],
|
|
|
|
|
2022-01-15 18:32:46 +00:00
|
|
|
}); */
|
|
|
|
|
|
|
|
if (!ticket) {
|
|
|
|
throw new AppError("ERR_NO_TICKET_FOUND", 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticket;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Report all
|
|
|
|
/*const ShowTicketReport = async (): Promise<Message[]> => {
|
|
|
|
|
|
|
|
const ticket = await Message.findAll({
|
|
|
|
//raw: true,
|
|
|
|
attributes: ['body', 'createdAt', 'ticketId'],
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Ticket,
|
|
|
|
attributes: ['contactid', 'userid', 'queueid', 'status'],
|
|
|
|
include:[
|
|
|
|
{
|
|
|
|
model: Contact,
|
|
|
|
attributes: ['name', 'number']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: User,
|
|
|
|
attributes: ['name', 'email']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: Queue,
|
|
|
|
attributes: ['name']
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
],
|
|
|
|
order:
|
|
|
|
['ticketId', 'createdAt']
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!ticket) {
|
|
|
|
throw new AppError("ERR_NO_TICKET_FOUND", 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticket;
|
|
|
|
};*/
|
|
|
|
|
|
|
|
export default ShowTicketReport;
|