diff --git a/backend/src/controllers/ContactController.js b/backend/src/controllers/ContactController.js index a50ba0b..807d00a 100644 --- a/backend/src/controllers/ContactController.js +++ b/backend/src/controllers/ContactController.js @@ -37,7 +37,7 @@ exports.index = async (req, res) => { exports.store = async (req, res) => { // const wbot = getWbot(); const io = getIO(); - // const { number, name } = req.body; + const newContact = req.body; // const result = await wbot.isRegisteredUser(`55${number}@c.us`); @@ -48,7 +48,9 @@ exports.store = async (req, res) => { // } // const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`); - const contact = await Contact.create(req.body); + const contact = await Contact.create(newContact, { + include: "extraInfo", + }); io.emit("contact", { action: "create", @@ -64,7 +66,7 @@ exports.show = async (req, res) => { const { id, name, number, email, extraInfo } = await Contact.findByPk( contactId, { - include: [{ model: ContactCustomField, as: "extraInfo" }], + include: "extraInfo", } ); @@ -80,15 +82,39 @@ exports.show = async (req, res) => { exports.update = async (req, res) => { const io = getIO(); + const updatedContact = req.body; + const { contactId } = req.params; - const contact = await Contact.findByPk(contactId); + const contact = await Contact.findByPk(contactId, { + include: "extraInfo", + }); if (!contact) { return res.status(400).json({ error: "No contact found with this ID" }); } - await contact.update(req.body); + if (updatedContact.extraInfo) { + await Promise.all( + updatedContact.extraInfo.map(async info => { + await ContactCustomField.upsert({ ...info, contactId: contact.id }); + }) + ); + + await Promise.all( + contact.extraInfo.map(async oldInfo => { + let stillExists = updatedContact.extraInfo.findIndex( + info => info.id === oldInfo.id + ); + + if (stillExists === -1) { + await ContactCustomField.destroy({ where: { id: oldInfo.id } }); + } + }) + ); + } + + await contact.update(updatedContact); io.emit("contact", { action: "update", diff --git a/frontend/src/pages/Chat/components/TicketsList/TicketsList.js b/frontend/src/pages/Chat/components/TicketsList/TicketsList.js index dbff5e1..2f8ef0b 100644 --- a/frontend/src/pages/Chat/components/TicketsList/TicketsList.js +++ b/frontend/src/pages/Chat/components/TicketsList/TicketsList.js @@ -14,14 +14,11 @@ import ListItemText from "@material-ui/core/ListItemText"; import ListItemAvatar from "@material-ui/core/ListItemAvatar"; import Typography from "@material-ui/core/Typography"; import Avatar from "@material-ui/core/Avatar"; -// import AddIcon from "@material-ui/icons/Add"; import Divider from "@material-ui/core/Divider"; import Badge from "@material-ui/core/Badge"; import SearchIcon from "@material-ui/icons/Search"; import InputBase from "@material-ui/core/InputBase"; import Button from "@material-ui/core/Button"; -// import Fab from "@material-ui/core/Fab"; -// import AddContactModal from "../AddContact/AddContactModal"; import Tabs from "@material-ui/core/Tabs"; import Tab from "@material-ui/core/Tab"; @@ -84,7 +81,7 @@ const useStyles = makeStyles(theme => ({ // flexShrink: 0, // -webkitBoxAlign: "center", alignItems: "center", - fontWeight: 600, + fontWeight: 500, fontSize: "16px", height: "56px", // backgroundColor: "#eee", @@ -294,16 +291,18 @@ const TicketsList = () => { ticket => ticket.id === data.ticket.id ); - if (ticketIndex !== -1) { + if (ticketIndex === -1) { + return [data.ticket, ...prevState]; + } else { let aux = [...prevState]; aux[ticketIndex] = data.ticket; return aux; - } else { - return [data.ticket, ...prevState]; } }); }; + console.log(tickets); + const showDesktopNotification = data => { const options = { body: `${data.message.body} - ${format(new Date(), "HH:mm")}`, @@ -350,6 +349,7 @@ const TicketsList = () => { } catch (err) { alert(err); } + history.push(`/chat/${ticketId}`); }; const countTickets = (status, userId) => { @@ -365,7 +365,7 @@ const TicketsList = () => { const viewTickets = tickets.map(ticket => { if ( (ticket.status === status && ticket.userId === userId) || - ticket.status === "closed" + (ticket.status === "closed" && status === "closed") ) return (