projeto-hit/backend/src/services/UserServices/ShowUserServiceReport.ts

121 lines
2.7 KiB
TypeScript

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";
import { Op, where } from "sequelize";
import { Sequelize } from "sequelize";
import moment from 'moment';
import { startOfDay, endOfDay, parseISO, getDate } from "date-fns";
import { string } from "yup/lib/locale";
import Whatsapp from "../../models/Whatsapp";
import UserOnlineTime from "../../models/UserOnlineTime";
interface Request {
startDate: string;
endDate: string;
userId?: string;
ticketStatus?: string;
}
//Report by user, startDate, endDate
const ShowUserServiceReport = async ({
startDate,
endDate,
userId="",
ticketStatus=""
}: Request): Promise<Object[]> => {
// Para contornar a gambiarra do ShowTicketReport
userId=='0' ? userId='' : userId = userId
let where_clause = {}
let objQuery: Object[] = []
if (ticketStatus.length>0) {
if (userId.length>0) {
where_clause = {
userId: userId,
status: ticketStatus,
createdAt: {
[Op.gte]: startDate + ' 00:00:00.000000',
[Op.lte]: endDate + ' 23:59:59.999999'
},
}
}
else {
where_clause = {
status: ticketStatus,
createdAt: {
[Op.gte]: startDate + ' 00:00:00.000000',
[Op.lte]: endDate + ' 23:59:59.999999'
},
}
}
objQuery = await Ticket.findAll({
where: where_clause,
raw: true,
attributes: ['userId', [Sequelize.fn("COUNT", Sequelize.col("Ticket.status")), "count"]],
group: ['userId']
});
}
else {
if (userId.length>0) {
where_clause = {
userId: userId,
createdAt: {
[Op.gte]: startDate + ' 00:00:00.000000',
[Op.lte]: endDate + ' 23:59:59.999999'
},
}
}
else {
where_clause = {
createdAt: {
[Op.gte]: startDate + ' 00:00:00.000000',
[Op.lte]: endDate + ' 23:59:59.999999'
},
}
}
objQuery = await UserOnlineTime.findAll({
where: where_clause,
raw: true,
// attributes: ['userId', [Sequelize.literal(`( SELECT SUM(time_to_sec(TIME(onlineTime)) ))`), 'sum']],
attributes: ['userId', [Sequelize.literal(`( SELECT SEC_TO_TIME(SUM(time_to_sec(TIME(onlineTime)) )))`), 'sum']],
group: ['userId']
});
}
// console.log('>>>>>>>>>>>>>> objQuery: ', objQuery)
if (!objQuery) {
throw new AppError("ERR_NO_OBJ_QUERY_FOUND", 404);
}
return objQuery;
};
export default ShowUserServiceReport;