feat: new connections page to handle multiple whats

This commit is contained in:
canove
2020-09-05 14:41:55 -03:00
parent 8786c7ca5e
commit b8ff993e6f
9 changed files with 335 additions and 119 deletions

View File

@@ -2,6 +2,12 @@ const Whatsapp = require("../models/Whatsapp");
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) => {
const dbSession = await Whatsapp.findAll();
return res.status(200).json(dbSession);
};
exports.show = async (req, res) => { exports.show = async (req, res) => {
const { sessionId } = req.params; const { sessionId } = req.params;
const dbSession = await Whatsapp.findByPk(sessionId); const dbSession = await Whatsapp.findByPk(sessionId);
@@ -13,33 +19,25 @@ exports.show = async (req, res) => {
return res.status(200).json(dbSession); return res.status(200).json(dbSession);
}; };
exports.delete = async (req, res) => { exports.update = async (req, res) => {
const wbot = getWbot();
const io = getIO();
const { sessionId } = req.params; const { sessionId } = req.params;
const dbSession = await Whatsapp.findByPk(sessionId); const dbSession = await Whatsapp.findByPk(sessionId);
if (!dbSession) { if (!dbSession) {
return res.status(404).json({ message: "Session not found" }); return res.status(404).json({ message: "Session not found" });
} }
await dbSession.update({ session: "", status: "pending" }); const wbot = getWbot(dbSession.id);
const io = getIO();
await dbSession.update(req.body);
wbot.logout(); wbot.logout();
io.emit("session", { io.emit("session", {
action: "logout", action: "update",
session: dbSession, session: dbSession,
}); });
return res.status(200).json({ message: "session disconnected" }); return res.status(200).json({ message: "session disconnected" });
}; };
// exports.getContacts = async (req, res, next) => {
// const io = getIO();
// const wbot = getWbot();
// const phoneContacts = await wbot.getContacts();
// return res.status(200).json(phoneContacts);
// };

View File

@@ -7,6 +7,7 @@ let sessions = [];
module.exports = { module.exports = {
init: async dbSession => { init: async dbSession => {
const io = getIO();
const sessionName = dbSession.name; const sessionName = dbSession.name;
let sessionCfg; let sessionCfg;
@@ -14,43 +15,64 @@ module.exports = {
sessionCfg = JSON.parse(dbSession.session); sessionCfg = JSON.parse(dbSession.session);
} }
const sessionIndex = sessions.findIndex(s => s.id === dbSession.id);
if (sessionIndex !== -1) {
sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1);
}
const wbot = new Client({ const wbot = new Client({
session: sessionCfg, session: sessionCfg,
restartOnAuthFail: true, restartOnAuthFail: true,
}); });
wbot.initialize(); wbot.initialize();
wbot.on("qr", async qr => { wbot.on("qr", async qr => {
console.log("Session:", sessionName); console.log("Session:", sessionName);
qrCode.generate(qr, { small: true }); qrCode.generate(qr, { small: true });
await dbSession.update({ id: 1, qrcode: qr, status: "disconnected" });
getIO().emit("session", { await dbSession.update({ id: 1, qrcode: qr, status: "qrcode" });
io.emit("session", {
action: "update", action: "update",
qr: qr,
session: dbSession, session: dbSession,
}); });
}); });
wbot.on("authenticated", async session => { wbot.on("authenticated", async session => {
console.log("Session:", sessionName, "AUTHENTICATED"); console.log("Session:", sessionName, "AUTHENTICATED");
await dbSession.update({ await dbSession.update({
session: JSON.stringify(session), session: JSON.stringify(session),
status: "authenticated", status: "authenticated",
}); });
getIO().emit("session", {
action: "authentication", io.emit("session", {
action: "update",
session: dbSession, session: dbSession,
}); });
}); });
wbot.on("auth_failure", async msg => { wbot.on("auth_failure", async msg => {
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg); console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
await dbSession.update({ session: "" }); await dbSession.update({ session: "" });
}); });
wbot.on("ready", async () => { wbot.on("ready", async () => {
console.log("Session:", sessionName, "READY"); console.log("Session:", sessionName, "READY");
await dbSession.update({ await dbSession.update({
status: "CONNECTED", status: "CONNECTED",
qrcode: "", qrcode: "",
}); });
io.emit("session", {
action: "update",
session: dbSession,
});
// const chats = await wbot.getChats(); // pega as mensagens nao lidas (recebidas quando o bot estava offline) // const chats = await wbot.getChats(); // pega as mensagens nao lidas (recebidas quando o bot estava offline)
// let unreadMessages; // todo > salvar isso no DB pra mostrar no frontend // let unreadMessages; // todo > salvar isso no DB pra mostrar no frontend
// for (let chat of chats) { // for (let chat of chats) {
@@ -62,16 +84,19 @@ module.exports = {
// } // }
// console.log(unreadMessages); // console.log(unreadMessages);
// wbot.sendPresenceAvailable(); wbot.sendPresenceAvailable();
}); });
wbot.name = sessionName; wbot.id = dbSession.id;
sessions.push(wbot); sessions.push(wbot);
return null; return null;
}, },
getWbot: sessionName => { getWbot: sessionId => {
const sessionIndex = sessions.findIndex(s => s.name === sessionName); console.log(sessionId);
console.log(sessions.map(session => session.id));
const sessionIndex = sessions.findIndex(s => s.id === sessionId);
if (sessionIndex === -1) { if (sessionIndex === -1) {
throw new Error("This Wbot session is not initialized"); throw new Error("This Wbot session is not initialized");

View File

@@ -5,19 +5,18 @@ const WhatsAppSessionController = require("../../controllers/WhatsAppSessionCont
const routes = express.Router(); const routes = express.Router();
routes.get("/whatsapp/session/", isAuth, WhatsAppSessionController.index);
routes.get( routes.get(
"/whatsapp/session/:sessionId", "/whatsapp/session/:sessionId",
isAuth, isAuth,
WhatsAppSessionController.show WhatsAppSessionController.show
); );
routes.delete( routes.put(
"/whatsapp/session/:sessionId", "/whatsapp/session/:sessionId",
isAuth, isAuth,
WhatsAppSessionController.delete WhatsAppSessionController.update
); );
// fetch contacts in user cellphone, not in use
// routes.get("/whatsapp/contacts", isAuth, WhatsappController.getContacts);
module.exports = routes; module.exports = routes;

View File

@@ -133,7 +133,7 @@ const handleMessage = async (msg, ticket, contact) => {
}; };
const wbotMessageListener = dbSession => { const wbotMessageListener = dbSession => {
const wbot = getWbot(dbSession.name); const wbot = getWbot(dbSession.id);
const io = getIO(); const io = getIO();
wbot.on("message_create", async msg => { wbot.on("message_create", async msg => {

View File

@@ -7,11 +7,12 @@ const { getWbot, init } = require("../libs/wbot");
const wbotMonitor = dbSession => { const wbotMonitor = dbSession => {
const io = getIO(); const io = getIO();
const wbot = getWbot(dbSession.name); const sessionName = dbSession.name;
const wbot = getWbot(dbSession.id);
try { try {
wbot.on("change_state", async newState => { wbot.on("change_state", async newState => {
console.log("monitor", newState); console.log("Monitor session:", sessionName, newState);
try { try {
await dbSession.update({ status: newState }); await dbSession.update({ status: newState });
} catch (err) { } catch (err) {
@@ -27,7 +28,9 @@ const wbotMonitor = dbSession => {
wbot.on("change_battery", async batteryInfo => { wbot.on("change_battery", async batteryInfo => {
const { battery, plugged } = batteryInfo; const { battery, plugged } = batteryInfo;
console.log(`Battery: ${battery}% - Charging? ${plugged}`); console.log(
`Battery session: ${sessionName} ${battery}% - Charging? ${plugged}`
);
try { try {
await dbSession.update({ battery, plugged }); await dbSession.update({ battery, plugged });
@@ -43,7 +46,7 @@ const wbotMonitor = dbSession => {
}); });
wbot.on("disconnected", async reason => { wbot.on("disconnected", async reason => {
console.log("disconnected", reason); console.log("Disconnected session:", sessionName, reason);
try { try {
await dbSession.update({ status: "disconnected" }); await dbSession.update({ status: "disconnected" });
} catch (err) { } catch (err) {
@@ -52,7 +55,7 @@ const wbotMonitor = dbSession => {
} }
io.emit("session", { io.emit("session", {
action: "logout", action: "update",
session: dbSession, session: dbSession,
}); });
@@ -60,7 +63,7 @@ const wbotMonitor = dbSession => {
() => () =>
init(dbSession) init(dbSession)
.then(() => { .then(() => {
wbotMessageListener(); wbotMessageListener(dbSession);
wbotMonitor(dbSession); wbotMonitor(dbSession);
}) })
.catch(err => { .catch(err => {

View File

@@ -1,18 +0,0 @@
import React from "react";
import QRCode from "qrcode.react";
import Typography from "@material-ui/core/Typography";
import { i18n } from "../../translate/i18n";
const Qrcode = ({ qrCode }) => {
return (
<div>
<Typography color="primary" gutterBottom>
{i18n.t("qrCode.message")}
</Typography>
{qrCode ? <QRCode value={qrCode} size={256} /> : <span>loading</span>}
</div>
);
};
export default Qrcode;

View File

@@ -0,0 +1,37 @@
import React from "react";
import QRCode from "qrcode.react";
import {
Dialog,
DialogContent,
Paper,
Typography,
DialogTitle,
} from "@material-ui/core";
import { i18n } from "../../translate/i18n";
const QrcodeModal = ({ open, onClose, session }) => {
if (session) {
return (
<Dialog open={open} onClose={onClose} maxWidth="lg" scroll="paper">
<DialogTitle>{session.name}</DialogTitle>
<DialogContent>
<Paper elevation={0}>
<Typography color="primary" gutterBottom>
{i18n.t("qrCode.message")}
</Typography>
{session.qrcode ? (
<QRCode value={session.qrcode} size={256} />
) : (
<span>loading</span>
)}
</Paper>
</DialogContent>
</Dialog>
);
} else {
return null;
}
};
export default QrcodeModal;

View File

@@ -10,7 +10,7 @@ import api from "../../services/api";
const SessionInfo = ({ session }) => { const SessionInfo = ({ session }) => {
const handleDisconectSession = async () => { const handleDisconectSession = async () => {
try { try {
await api.delete("/whatsapp/session/1"); await api.put(`/whatsapp/session/${session.id}`);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
if (err.response && err.response.data && err.response.data.error) { if (err.response && err.response.data && err.response.data.error) {
@@ -27,7 +27,7 @@ const SessionInfo = ({ session }) => {
<Typography variant="body2" gutterBottom> <Typography variant="body2" gutterBottom>
{`${i18n.t("sessionInfo.updatedAt")}`}{" "} {`${i18n.t("sessionInfo.updatedAt")}`}{" "}
{session.updatedAt && {session.updatedAt &&
format(parseISO(session.updatedAt), "dd/mm/yy HH:mm")} format(parseISO(session.updatedAt), "dd/MM/yy HH:mm")}
</Typography> </Typography>
<Button <Button
color="primary" color="primary"

View File

@@ -1,50 +1,109 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect, useReducer } from "react";
import { useHistory } from "react-router-dom";
import api from "../../services/api";
import openSocket from "socket.io-client"; import openSocket from "socket.io-client";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { format, parseISO } from "date-fns";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import {
Button,
TableBody,
TableRow,
TableCell,
IconButton,
Table,
TableHead,
Paper,
} from "@material-ui/core";
import { WifiOff, DeleteOutline } from "@material-ui/icons";
import Paper from "@material-ui/core/Paper"; import MainContainer from "../../components/MainContainer";
import SessionInfo from "../../components/SessionInfo"; import MainHeader from "../../components/MainHeader";
import Qrcode from "../../components/Qrcode"; import MainHeaderButtonsWrapper from "../../components/MainHeaderButtonsWrapper";
import Title from "../../components/Title";
import TableRowSkeleton from "../../components/TableRowSkeleton";
import api from "../../services/api";
import QrcodeModal from "../../components/QrcodeModal";
const reducer = (state, action) => {
if (action.type === "LOAD_SESSIONS") {
const sessions = action.payload;
return [...sessions];
}
if (action.type === "UPDATE_SESSIONS") {
const session = action.payload;
const sessionIndex = state.findIndex(s => s.id === session.id);
if (sessionIndex !== -1) {
state[sessionIndex] = session;
return [...state];
} else {
return [session, ...state];
}
}
if (action.type === "DELETE_SESSION") {
const sessionId = action.payload;
const sessionIndex = state.findIndex(s => s.id === sessionId);
if (sessionIndex !== -1) {
state.splice(sessionIndex, 1);
}
return [...state];
}
if (action.type === "RESET") {
return [];
}
};
const useStyles = makeStyles(theme => ({ const useStyles = makeStyles(theme => ({
root: { // root: {
display: "flex", // display: "flex",
alignItems: "center", // alignItems: "center",
justifyContent: "center", // justifyContent: "center",
padding: theme.spacing(4), // padding: theme.spacing(4),
// },
mainPaper: {
flex: 1,
padding: theme.spacing(1),
overflowY: "scroll",
...theme.scrollbarStyles,
}, },
paper: { // paper: {
padding: theme.spacing(2), // padding: theme.spacing(2),
display: "flex", // margin: theme.spacing(1),
width: 400, // display: "flex",
overflow: "auto", // width: 400,
flexDirection: "column", // height: 270,
alignItems: "center", // overflow: "auto",
justifyContent: "center", // flexDirection: "column",
}, // alignItems: "center",
fixedHeight: { // justifyContent: "center",
height: 640, // },
},
// fixedHeight: {
// height: 640,
// },
})); }));
const WhatsAuth = () => { const WhatsAuth = () => {
const classes = useStyles(); const classes = useStyles();
const history = useHistory();
const [qrCode, setQrCode] = useState(""); const [sessions, dispatch] = useReducer(reducer, []);
const [session, setSession] = useState({});
const [qrModalOpen, setQrModalOpen] = useState(false);
const [selectedSession, setSelectedSession] = useState(null);
useEffect(() => { useEffect(() => {
const fetchSession = async () => { const fetchSession = async () => {
try { try {
const { data } = await api.get("/whatsapp/session/1"); const { data } = await api.get("/whatsapp/session/");
setQrCode(data.qrcode); dispatch({ type: "LOAD_SESSIONS", payload: data });
setSession(data);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
if (err.response && err.response.data && err.response.data.error) { if (err.response && err.response.data && err.response.data.error) {
@@ -60,48 +119,161 @@ const WhatsAuth = () => {
socket.on("session", data => { socket.on("session", data => {
if (data.action === "update") { if (data.action === "update") {
setQrCode(data.qr); dispatch({ type: "UPDATE_SESSIONS", payload: data.session });
setSession(data.session);
}
});
socket.on("session", data => {
if (data.action === "update") {
setSession(data.session);
}
});
socket.on("session", data => {
if (data.action === "authentication") {
setQrCode("");
setSession(data.session);
history.push("/tickets");
}
});
socket.on("session", data => {
if (data.action === "logout") {
setSession(data.session);
} }
}); });
return () => { return () => {
socket.disconnect(); socket.disconnect();
}; };
}, [history]); }, []);
const handleDisconnectSession = async sessionId => {
try {
await api.put(`/whatsapp/session/${sessionId}`, {
session: "",
status: "pending",
});
} catch (err) {
console.log(err);
if (err.response && err.response.data && err.response.data.error) {
toast.error(err.response.data.error);
}
}
};
console.log(sessions);
const handleOpenQrMoral = session => {
setQrModalOpen(true);
setSelectedSession(session);
};
const handleCloseQrModal = () => {
setQrModalOpen(false);
setSelectedSession(null);
};
return ( return (
<div className={classes.root}> <MainContainer>
{session && session.status === "disconnected" ? ( <QrcodeModal
<Paper className={classes.paper}> open={qrModalOpen}
<Qrcode qrCode={qrCode} /> onClose={handleCloseQrModal}
</Paper> aria-labelledby="form-dialog-title"
) : ( session={selectedSession}
<Paper className={classes.paper}> />
<SessionInfo session={session} /> {/* <Dialog
</Paper> open={qrModalOpen}
)} onClose={handleCloseQrModal}
</div> maxWidth="lg"
scroll="paper"
>
<DialogTitle>Qr Code</DialogTitle>
<DialogContent>
<Paper className={classes.paper}>
<Qrcode qrCode={selectedSession && selectedSession.qrcode} />
</Paper>
</DialogContent>
</Dialog> */}
<MainHeader>
<Title>Connections</Title>
<MainHeaderButtonsWrapper>
<Button
variant="contained"
color="primary"
// onClick={handleOpenUserModal}
>
Add Whatsapp
</Button>
</MainHeaderButtonsWrapper>
</MainHeader>
<Paper
className={classes.mainPaper}
variant="outlined"
// onScroll={handleScroll}
>
<Table size="small">
<TableHead>
<TableRow>
<TableCell align="center">Name</TableCell>
<TableCell align="center">Status</TableCell>
<TableCell align="center">Last update</TableCell>
<TableCell align="center">Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{false ? (
<TableRowSkeleton />
) : (
<>
{sessions &&
sessions.length > 0 &&
sessions.map(session => (
<TableRow key={session.id}>
<TableCell align="center">{session.name}</TableCell>
<TableCell align="center">
{session.status === "qrcode" ? (
<Button
size="small"
variant="contained"
color="primary"
onClick={() => handleOpenQrMoral(session)}
>
QR CODE
</Button>
) : (
session.status
)}
</TableCell>
<TableCell align="center">
{format(parseISO(session.updatedAt), "dd/MM/yy HH:mm")}
</TableCell>
<TableCell align="center">
<IconButton
size="small"
onClick={() => handleDisconnectSession(session.id)}
>
<WifiOff />
</IconButton>
<IconButton
size="small"
onClick={e => {
// setConfirmModalOpen(true);
// setDeletingUser(user);
}}
>
<DeleteOutline />
</IconButton>
</TableCell>
</TableRow>
))}
</>
)}
</TableBody>
</Table>
</Paper>
{/* <div className={classes.root}>
{sessions &&
sessions.length > 0 &&
sessions.map(session => {
if (session.status === "disconnected")
return (
<Paper className={classes.paper}>
<Qrcode qrCode={qrCode} />
</Paper>
);
else {
return (
<Paper className={classes.paper}>
<SessionInfo session={session} />
</Paper>
);
}
})}
</div> */}
</MainContainer>
); );
}; };