Feat: Contacts custom fields working

This commit is contained in:
canove
2020-07-24 16:35:08 -03:00
parent e266765caa
commit 9292c2c27d
2 changed files with 39 additions and 13 deletions

View File

@@ -37,7 +37,7 @@ exports.index = async (req, res) => {
exports.store = async (req, res) => { exports.store = async (req, res) => {
// const wbot = getWbot(); // const wbot = getWbot();
const io = getIO(); const io = getIO();
// const { number, name } = req.body; const newContact = req.body;
// const result = await wbot.isRegisteredUser(`55${number}@c.us`); // 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 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", { io.emit("contact", {
action: "create", action: "create",
@@ -64,7 +66,7 @@ exports.show = async (req, res) => {
const { id, name, number, email, extraInfo } = await Contact.findByPk( const { id, name, number, email, extraInfo } = await Contact.findByPk(
contactId, contactId,
{ {
include: [{ model: ContactCustomField, as: "extraInfo" }], include: "extraInfo",
} }
); );
@@ -80,15 +82,39 @@ exports.show = async (req, res) => {
exports.update = async (req, res) => { exports.update = async (req, res) => {
const io = getIO(); const io = getIO();
const updatedContact = req.body;
const { contactId } = req.params; const { contactId } = req.params;
const contact = await Contact.findByPk(contactId); const contact = await Contact.findByPk(contactId, {
include: "extraInfo",
});
if (!contact) { if (!contact) {
return res.status(400).json({ error: "No contact found with this ID" }); 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", { io.emit("contact", {
action: "update", action: "update",

View File

@@ -14,14 +14,11 @@ import ListItemText from "@material-ui/core/ListItemText";
import ListItemAvatar from "@material-ui/core/ListItemAvatar"; import ListItemAvatar from "@material-ui/core/ListItemAvatar";
import Typography from "@material-ui/core/Typography"; import Typography from "@material-ui/core/Typography";
import Avatar from "@material-ui/core/Avatar"; import Avatar from "@material-ui/core/Avatar";
// import AddIcon from "@material-ui/icons/Add";
import Divider from "@material-ui/core/Divider"; import Divider from "@material-ui/core/Divider";
import Badge from "@material-ui/core/Badge"; import Badge from "@material-ui/core/Badge";
import SearchIcon from "@material-ui/icons/Search"; import SearchIcon from "@material-ui/icons/Search";
import InputBase from "@material-ui/core/InputBase"; import InputBase from "@material-ui/core/InputBase";
import Button from "@material-ui/core/Button"; 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 Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab"; import Tab from "@material-ui/core/Tab";
@@ -84,7 +81,7 @@ const useStyles = makeStyles(theme => ({
// flexShrink: 0, // flexShrink: 0,
// -webkitBoxAlign: "center", // -webkitBoxAlign: "center",
alignItems: "center", alignItems: "center",
fontWeight: 600, fontWeight: 500,
fontSize: "16px", fontSize: "16px",
height: "56px", height: "56px",
// backgroundColor: "#eee", // backgroundColor: "#eee",
@@ -294,16 +291,18 @@ const TicketsList = () => {
ticket => ticket.id === data.ticket.id ticket => ticket.id === data.ticket.id
); );
if (ticketIndex !== -1) { if (ticketIndex === -1) {
return [data.ticket, ...prevState];
} else {
let aux = [...prevState]; let aux = [...prevState];
aux[ticketIndex] = data.ticket; aux[ticketIndex] = data.ticket;
return aux; return aux;
} else {
return [data.ticket, ...prevState];
} }
}); });
}; };
console.log(tickets);
const showDesktopNotification = data => { const showDesktopNotification = data => {
const options = { const options = {
body: `${data.message.body} - ${format(new Date(), "HH:mm")}`, body: `${data.message.body} - ${format(new Date(), "HH:mm")}`,
@@ -350,6 +349,7 @@ const TicketsList = () => {
} catch (err) { } catch (err) {
alert(err); alert(err);
} }
history.push(`/chat/${ticketId}`);
}; };
const countTickets = (status, userId) => { const countTickets = (status, userId) => {
@@ -365,7 +365,7 @@ const TicketsList = () => {
const viewTickets = tickets.map(ticket => { const viewTickets = tickets.map(ticket => {
if ( if (
(ticket.status === status && ticket.userId === userId) || (ticket.status === status && ticket.userId === userId) ||
ticket.status === "closed" (ticket.status === "closed" && status === "closed")
) )
return ( return (
<React.Fragment key={ticket.id}> <React.Fragment key={ticket.id}>