feat: reopen same ticket when last ticket < 2h

This commit is contained in:
canove
2020-07-30 09:38:00 -03:00
parent b5599ce054
commit 83491b5882
3 changed files with 27 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
const path = require("path");
const fs = require("fs");
const { Op } = require("sequelize");
const { parseISO, subHours } = require("date-fns");
const Contact = require("../models/Contact");
const Ticket = require("../models/Ticket");
@@ -28,16 +29,36 @@ const verifyContact = async (msgContact, profilePicUrl) => {
};
const verifyTicket = async contact => {
const [ticket] = await Ticket.findOrCreate({
let ticket = await Ticket.findOne({
where: {
status: {
[Op.or]: ["open", "pending"],
},
contactId: contact.id,
},
defaults: { contactId: contact.id, status: "pending" },
});
if (!ticket) {
ticket = await Ticket.findOne({
where: {
createdAt: { [Op.between]: [subHours(new Date(), 2), new Date()] },
contactId: contact.id,
},
order: [["createdAt", "DESC"]],
});
if (ticket) {
await ticket.update({ status: "open" });
}
}
if (!ticket) {
ticket = await Ticket.create({
contactId: contact.id,
status: "pending",
});
}
return ticket;
};

View File

@@ -221,7 +221,7 @@ const MessagesList = () => {
const classes = useStyles();
const token = localStorage.getItem("token");
const userId = localStorage.getItem("userId");
const userId = +localStorage.getItem("userId");
const [loading, setLoading] = useState(true);
const [contact, setContact] = useState({});

View File

@@ -288,6 +288,7 @@ const TicketsList = () => {
let aux = [...prevState];
aux[ticketIndex].unreadMessages++;
aux[ticketIndex].lastMessage = data.message.body;
aux[ticketIndex].status = data.ticket.status;
aux.unshift(aux.splice(ticketIndex, 1)[0]);
return aux;
} else {
@@ -488,6 +489,8 @@ const TicketsList = () => {
}
};
console.log(tickets);
return (
<Paper elevation={0} variant="outlined" className={classes.contactsWrapper}>
<NewTicketModal