import Ticket from "../../models/Ticket"; import AppError from "../../errors/AppError"; import { Op, where } from "sequelize"; import { Sequelize } from "sequelize"; import { format } from "date-fns"; import ptBR from 'date-fns/locale/pt-BR'; import { splitDateTime } from "../../helpers/SplitDateTime"; const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) const CountTicketService = async (status: string, date?: string): Promise => { let where_clause = {} if (date) { where_clause = { createdAt: { [Op.gte]: date + ' 00:00:00.000000', [Op.lte]: date + ' 23:59:59.999999' } } } else { // where_clause = { // createdAt: { // [Op.gte]: dateToday.fullDate + ' 00:00:00.000000', // [Op.lte]: dateToday.fullDate + ' 23:59:59.999999' // } // } } where_clause = { ...where_clause, status: status } const ticket = await Ticket.findAll({ where: where_clause, raw: true, attributes: [[Sequelize.fn("COUNT", Sequelize.col("Ticket.status")), "count"]], }); if (!ticket) { throw new AppError("ERR_NO_TICKET_FOUND", 404); } return ticket[0]; }; export default CountTicketService;