Feat: Added import contacts function on frontend

This commit is contained in:
canove
2020-07-28 10:45:35 -03:00
parent 915ee712dc
commit 124297a849
11 changed files with 128 additions and 141 deletions

View File

@@ -1,67 +0,0 @@
const Contact = require("../models/Contact");
const Message = require("../models/Message");
const Sequelize = require("sequelize");
const { getIO } = require("../libs/socket");
const { getWbot } = require("../libs/wbot");
exports.index = async (req, res) => {
const { searchParam = "" } = req.query;
const lowerSerachParam = searchParam.toLowerCase();
const whereCondition = {
name: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
"%" + lowerSerachParam + "%"
),
};
//todo >> add contact number to search where condition
const contacts = await Contact.findAll({
where: whereCondition,
attributes: {
include: [
[
Sequelize.literal(`(
SELECT COUNT(*)
FROM messages AS message
WHERE
message.contactId = contact.id
AND
message.read = 0
)`),
"unreadMessages",
],
],
},
order: [["updatedAt", "DESC"]],
});
return res.json(contacts);
};
exports.store = async (req, res) => {
const wbot = getWbot();
const io = getIO();
const { number, name } = req.body;
const result = await wbot.isRegisteredUser(`55${number}@c.us`);
if (!result) {
return res
.status(400)
.json({ error: "The suplied number is not a valid Whatsapp number" });
}
const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
const contact = await Contact.create({
name,
number: `55${number}`,
profilePicUrl,
});
res.status(200).json(contact);
};

View File

@@ -1,29 +1,31 @@
const Sequelize = require("sequelize"); const Sequelize = require("sequelize");
const { Op } = require("sequelize");
const Contact = require("../models/Contact"); const Contact = require("../models/Contact");
const ContactCustomField = require("../models/ContactCustomField"); const ContactCustomField = require("../models/ContactCustomField");
// const Message = require("../models/Message");
// const Sequelize = require("sequelize");
const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
// const { getWbot } = require("../libs/wbot"); const { getWbot } = require("../libs/wbot");
exports.index = async (req, res) => { exports.index = async (req, res) => {
const { searchParam = "", pageNumber = 1, rowsPerPage = 10 } = req.query; const { searchParam = "", pageNumber = 1, rowsPerPage = 10 } = req.query;
const whereCondition = { const whereCondition = {
name: Sequelize.where( [Op.or]: [
Sequelize.fn("LOWER", Sequelize.col("name")), {
"LIKE", name: Sequelize.where(
"%" + searchParam.toLowerCase() + "%" Sequelize.fn("LOWER", Sequelize.col("name")),
), "LIKE",
"%" + searchParam.toLowerCase() + "%"
),
},
{ number: { [Op.like]: `%${searchParam}%` } },
],
}; };
let limit = +rowsPerPage; let limit = +rowsPerPage;
let offset = limit * (pageNumber - 1); let offset = limit * (pageNumber - 1);
//todo >> add contact number to search where condition
const { count, rows: contacts } = await Contact.findAndCountAll({ const { count, rows: contacts } = await Contact.findAndCountAll({
where: whereCondition, where: whereCondition,
limit, limit,
@@ -35,22 +37,27 @@ 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 newContact = req.body; const newContact = req.body;
// const result = await wbot.isRegisteredUser(`55${number}@c.us`); const result = await wbot.isRegisteredUser(`${newContact.number}@c.us`);
// if (!result) { if (!result) {
// return res return res
// .status(400) .status(400)
// .json({ error: "The suplied number is not a valid Whatsapp number" }); .json({ error: "The suplied number is not a valid Whatsapp number" });
// } }
// const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`); const profilePicUrl = await wbot.getProfilePicUrl(
`${newContact.number}@c.us`
);
const contact = await Contact.create(newContact, { const contact = await Contact.create(
include: "extraInfo", { ...newContact, profilePicUrl },
}); {
include: "extraInfo",
}
);
io.emit("contact", { io.emit("contact", {
action: "create", action: "create",

View File

@@ -0,0 +1,18 @@
const Contact = require("../models/Contact");
const { getIO } = require("../libs/socket");
const { getWbot, init } = require("../libs/wbot");
exports.store = async (req, res, next) => {
const io = getIO();
const wbot = getWbot();
const phoneContacts = await wbot.getContacts();
await Promise.all(
phoneContacts.map(async ({ number, name }) => {
await Contact.create({ number, name });
})
);
return res.status(200).json({ message: "contacts imported" });
};

View File

@@ -5,7 +5,7 @@ class Contact extends Sequelize.Model {
super.init( super.init(
{ {
name: { type: Sequelize.STRING }, name: { type: Sequelize.STRING },
number: { type: Sequelize.STRING }, number: { type: Sequelize.STRING, allowNull: false, unique: true },
email: { type: Sequelize.STRING, allowNull: false, defaultValue: "" }, email: { type: Sequelize.STRING, allowNull: false, defaultValue: "" },
profilePicUrl: { type: Sequelize.STRING }, profilePicUrl: { type: Sequelize.STRING },
}, },

View File

@@ -2,9 +2,12 @@ const express = require("express");
const isAuth = require("../middleware/is-auth"); const isAuth = require("../middleware/is-auth");
const ContactController = require("../controllers/ContactController"); const ContactController = require("../controllers/ContactController");
const ImportPhoneContactsController = require("../controllers/ImportPhoneContactsController");
const routes = express.Router(); const routes = express.Router();
routes.post("/contacts/import", isAuth, ImportPhoneContactsController.store);
routes.get("/contacts", isAuth, ContactController.index); routes.get("/contacts", isAuth, ContactController.index);
routes.get("/contacts/:contactId", isAuth, ContactController.show); routes.get("/contacts/:contactId", isAuth, ContactController.show);

View File

@@ -28,7 +28,7 @@ const verifyContact = async (msgContact, profilePicUrl) => {
}; };
const verifyTicket = async contact => { const verifyTicket = async contact => {
const [ticket, created] = await Ticket.findOrCreate({ const [ticket] = await Ticket.findOrCreate({
where: { where: {
status: { status: {
[Op.or]: ["open", "pending"], [Op.or]: ["open", "pending"],

View File

@@ -151,7 +151,7 @@ const ContactDrawer = ({ open, handleDrawerClose, contact, loading }) => {
</Paper> </Paper>
<Paper square variant="outlined" className={classes.contactDetails}> <Paper square variant="outlined" className={classes.contactDetails}>
<ContactModal <ContactModal
modalOpen={modalOpen} open={modalOpen}
onClose={e => setModalOpen(false)} onClose={e => setModalOpen(false)}
aria-labelledby="form-dialog-title" aria-labelledby="form-dialog-title"
contactId={contact.id} contactId={contact.id}

View File

@@ -52,19 +52,13 @@ const useStyles = makeStyles(theme => ({
}, },
})); }));
const ContactModal = ({ modalOpen, onClose, contactId }) => { const ContactModal = ({ open, onClose, contactId }) => {
const classes = useStyles(); const classes = useStyles();
const initialState = { const initialState = {
name: "", name: "",
number: "", number: "",
email: "", email: "",
extraInfo: [
{
name: "",
value: "",
},
],
}; };
const [contact, setContact] = useState(initialState); const [contact, setContact] = useState(initialState);
@@ -77,7 +71,7 @@ const ContactModal = ({ modalOpen, onClose, contactId }) => {
}; };
fetchContact(); fetchContact();
}, [contactId, modalOpen]); }, [contactId, open]);
const handleClose = () => { const handleClose = () => {
onClose(); onClose();
@@ -100,7 +94,7 @@ const ContactModal = ({ modalOpen, onClose, contactId }) => {
return ( return (
<div className={classes.root}> <div className={classes.root}>
<Dialog <Dialog
open={modalOpen} open={open}
onClose={handleClose} onClose={handleClose}
maxWidth="lg" maxWidth="lg"
scroll="paper" scroll="paper"
@@ -125,7 +119,7 @@ const ContactModal = ({ modalOpen, onClose, contactId }) => {
handleSubmit, handleSubmit,
isSubmitting, isSubmitting,
}) => ( }) => (
<> <form onSubmit={handleSubmit}>
<DialogTitle id="form-dialog-title"> <DialogTitle id="form-dialog-title">
{contactId ? "Editar contato" : "Adicionar contato"} {contactId ? "Editar contato" : "Adicionar contato"}
</DialogTitle> </DialogTitle>
@@ -148,7 +142,7 @@ const ContactModal = ({ modalOpen, onClose, contactId }) => {
name="number" name="number"
value={values.number || ""} value={values.number || ""}
onChange={handleChange} onChange={handleChange}
placeholder="Ex: 13912344321" placeholder="Ex: 5513912344321"
variant="outlined" variant="outlined"
margin="dense" margin="dense"
required required
@@ -234,7 +228,7 @@ const ContactModal = ({ modalOpen, onClose, contactId }) => {
Cancelar Cancelar
</Button> </Button>
<Button <Button
onClick={handleSubmit} type="submit"
color="primary" color="primary"
disabled={isSubmitting} disabled={isSubmitting}
variant="contained" variant="contained"
@@ -249,7 +243,7 @@ const ContactModal = ({ modalOpen, onClose, contactId }) => {
)} )}
</Button> </Button>
</DialogActions> </DialogActions>
</> </form>
)} )}
</Formik> </Formik>
</Dialog> </Dialog>

View File

@@ -9,7 +9,6 @@ import DialogContent from "@material-ui/core/DialogContent";
import DialogTitle from "@material-ui/core/DialogTitle"; import DialogTitle from "@material-ui/core/DialogTitle";
import Autocomplete from "@material-ui/lab/Autocomplete"; import Autocomplete from "@material-ui/lab/Autocomplete";
import CircularProgress from "@material-ui/core/CircularProgress"; import CircularProgress from "@material-ui/core/CircularProgress";
import FormControl from "@material-ui/core/FormControl";
import { green } from "@material-ui/core/colors"; import { green } from "@material-ui/core/colors";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
@@ -43,25 +42,33 @@ const NewTicketModal = ({ modalOpen, onClose, contactId }) => {
const [options, setOptions] = useState([]); const [options, setOptions] = useState([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [searchParam, setSearchParam] = useState("");
const [selectedContact, setSelectedContact] = useState(null); const [selectedContact, setSelectedContact] = useState(null);
useEffect(() => { useEffect(() => {
if (!modalOpen || searchParam.length < 3) return;
setLoading(true); setLoading(true);
const fetchContacts = async () => { const delayDebounceFn = setTimeout(() => {
try { const fetchContacts = async () => {
const res = await api.get("contacts"); try {
setOptions(res.data.contacts); const res = await api.get("contacts", {
} catch (err) { params: { searchParam, rowsPerPage: 20 },
alert(err); });
} setOptions(res.data.contacts);
}; setLoading(false);
} catch (err) {
alert(err);
}
};
fetchContacts(); fetchContacts();
setLoading(false); }, 1000);
}, []); return () => clearTimeout(delayDebounceFn);
}, [searchParam, modalOpen]);
const handleClose = () => { const handleClose = () => {
onClose(); onClose();
setSearchParam("");
setSelectedContact(null); setSelectedContact(null);
}; };
@@ -82,6 +89,8 @@ const NewTicketModal = ({ modalOpen, onClose, contactId }) => {
handleClose(); handleClose();
}; };
console.log(options);
return ( return (
<div className={classes.root}> <div className={classes.root}>
<Dialog <Dialog
@@ -96,7 +105,7 @@ const NewTicketModal = ({ modalOpen, onClose, contactId }) => {
<Autocomplete <Autocomplete
id="asynchronous-demo" id="asynchronous-demo"
style={{ width: 300 }} style={{ width: 300 }}
getOptionLabel={option => option.name} getOptionLabel={option => `${option.name} - ${option.number}`}
onChange={(e, newValue) => { onChange={(e, newValue) => {
setSelectedContact(newValue); setSelectedContact(newValue);
}} }}
@@ -105,9 +114,11 @@ const NewTicketModal = ({ modalOpen, onClose, contactId }) => {
renderInput={params => ( renderInput={params => (
<TextField <TextField
{...params} {...params}
label="Selecione o contato" label="Digite para pesquisar o contato"
variant="outlined" variant="outlined"
required required
autoFocus
onChange={e => setSearchParam(e.target.value)}
id="my-input" id="my-input"
InputProps={{ InputProps={{
...params.InputProps, ...params.InputProps,

View File

@@ -211,8 +211,7 @@ const TicketsList = () => {
const [loading, setLoading] = useState(); const [loading, setLoading] = useState();
const [searchParam, setSearchParam] = useState(""); const [searchParam, setSearchParam] = useState("");
const [tab, setTab] = useState("open"); const [tab, setTab] = useState("open");
const [newTicketModalOpen, setNewTicketModalOpen] = useState(false);
const [newTicketModalOpen, setNewTicketModalOpen] = useState(true);
useEffect(() => { useEffect(() => {
if (!("Notification" in window)) { if (!("Notification" in window)) {

View File

@@ -70,9 +70,6 @@ const useStyles = makeStyles(theme => ({
const Contacts = () => { const Contacts = () => {
const classes = useStyles(); const classes = useStyles();
const token = localStorage.getItem("token");
// const userId = localStorage.getItem("userId");
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10); const [rowsPerPage, setRowsPerPage] = useState(10);
@@ -80,9 +77,7 @@ const Contacts = () => {
const [searchParam, setSearchParam] = useState(""); const [searchParam, setSearchParam] = useState("");
const [contacts, setContacts] = useState([]); const [contacts, setContacts] = useState([]);
const [selectedContactId, setSelectedContactId] = useState(null); const [selectedContactId, setSelectedContactId] = useState(null);
const [contactModalOpen, setContactModalOpen] = useState(false);
const [modalOpen, setModalOpen] = useState(false);
const [deletingContact, setDeletingContact] = useState(null); const [deletingContact, setDeletingContact] = useState(null);
const [confirmOpen, setConfirmOpen] = useState(false); const [confirmOpen, setConfirmOpen] = useState(false);
@@ -105,7 +100,7 @@ const Contacts = () => {
fetchContacts(); fetchContacts();
}, 1000); }, 1000);
return () => clearTimeout(delayDebounceFn); return () => clearTimeout(delayDebounceFn);
}, [searchParam, page, token, rowsPerPage]); }, [searchParam, page, rowsPerPage]);
useEffect(() => { useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL); const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
@@ -163,19 +158,19 @@ const Contacts = () => {
setSearchParam(event.target.value.toLowerCase()); setSearchParam(event.target.value.toLowerCase());
}; };
const handleClickOpen = () => { const handleOpenContactModal = () => {
setSelectedContactId(null); setSelectedContactId(null);
setModalOpen(true); setContactModalOpen(true);
}; };
const handleClose = () => { const handleCloseContactModal = () => {
setSelectedContactId(null); setSelectedContactId(null);
setModalOpen(false); setContactModalOpen(false);
}; };
const hadleEditContact = contactId => { const hadleEditContact = contactId => {
setSelectedContactId(contactId); setSelectedContactId(contactId);
setModalOpen(true); setContactModalOpen(true);
}; };
const handleDeleteContact = async contactId => { const handleDeleteContact = async contactId => {
@@ -185,24 +180,43 @@ const Contacts = () => {
alert(err); alert(err);
} }
setDeletingContact(null); setDeletingContact(null);
setSearchParam("");
setPage(0);
};
const handleimportContact = async () => {
try {
await api.post("/contacts/import");
} catch (err) {
console.log(err);
}
}; };
return ( return (
<Container className={classes.mainContainer}> <Container className={classes.mainContainer}>
<ContactModal <ContactModal
modalOpen={modalOpen} open={contactModalOpen}
onClose={handleClose} onClose={handleCloseContactModal}
aria-labelledby="form-dialog-title" aria-labelledby="form-dialog-title"
contactId={selectedContactId} contactId={selectedContactId}
></ContactModal> ></ContactModal>
<ConfirmationModal <ConfirmationModal
title={deletingContact && `Deletar ${deletingContact.name}`} title={
deletingContact
? `Deletar ${deletingContact.name}?`
: `Importar contatos`
}
open={confirmOpen} open={confirmOpen}
setOpen={setConfirmOpen} setOpen={setConfirmOpen}
onConfirm={e => handleDeleteContact(deletingContact.id)} onConfirm={e =>
deletingContact
? handleDeleteContact(deletingContact.id)
: handleimportContact()
}
> >
Tem certeza que deseja deletar este contato? Todos os tickets {deletingContact
relacionados serão perdidos. ? "Tem certeza que deseja deletar este contato? Todos os tickets relacionados serão perdidos."
: "Deseja importas todos os contatos do telefone? Essa função é experimental, você terá que recarregar a página após a importação "}
</ConfirmationModal> </ConfirmationModal>
<div className={classes.contactsHeader}> <div className={classes.contactsHeader}>
<Typography variant="h5" gutterBottom> <Typography variant="h5" gutterBottom>
@@ -223,10 +237,18 @@ const Contacts = () => {
), ),
}} }}
/> />
<Button variant="contained" color="primary"> <Button
variant="contained"
color="primary"
onClick={e => setConfirmOpen(true)}
>
Importar contatos Importar contatos
</Button> </Button>
<Button variant="contained" color="primary" onClick={handleClickOpen}> <Button
variant="contained"
color="primary"
onClick={handleOpenContactModal}
>
Adicionar contato Adicionar contato
</Button> </Button>
</div> </div>
@@ -237,7 +259,7 @@ const Contacts = () => {
<TableRow> <TableRow>
<TableCell padding="checkbox" /> <TableCell padding="checkbox" />
<TableCell>Nome</TableCell> <TableCell>Nome</TableCell>
<TableCell>Telefone</TableCell> <TableCell>Whatsapp</TableCell>
<TableCell>Email</TableCell> <TableCell>Email</TableCell>
<TableCell align="right">Ações</TableCell> <TableCell align="right">Ações</TableCell>
</TableRow> </TableRow>