import { Op } from "sequelize";
import { sub, subHours } from "date-fns";
import Message from "../models/Message";
import Ticket from "../models/Ticket";

async function checkLastClientMsg24hs(ticket: Ticket) {
  return await Message.findAll({
    attributes: ["createdAt", "body"],
    where: {
      contactId: ticket.contactId,
      phoneNumberId: ticket.phoneNumberId,
      fromMe: false,
      createdAt: {
        [Op.between]: [+subHours(new Date(), 24), +new Date()]
      }
    },
    order: [["createdAt", "DESC"]],
    limit: 1
  });
}

export default checkLastClientMsg24hs;
