commit
390772eb91
|
@ -37,9 +37,6 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
const { body, quotedMsg }: MessageData = req.body;
|
const { body, quotedMsg }: MessageData = req.body;
|
||||||
const medias = req.files as Express.Multer.File[];
|
const medias = req.files as Express.Multer.File[];
|
||||||
|
|
|
@ -44,7 +44,7 @@ const restart = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
|
||||||
const whatsapp = await ShowWhatsAppService(whatsappId);
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||||
|
|
||||||
await restartWhatsSession(whatsapp)
|
restartWhatsSession(whatsapp, true)
|
||||||
|
|
||||||
return res.status(200).json({ message: "Starting session." });
|
return res.status(200).json({ message: "Starting session." });
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,7 @@ const fsPromises = require("fs/promises");
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
|
||||||
// Restart session
|
// Restart session
|
||||||
export const restartWhatsSession = async (whatsapp: Whatsapp) => {
|
export const restartWhatsSession = async (whatsapp: Whatsapp, backupSession: boolean = false) => {
|
||||||
|
|
||||||
console.log('RESTARTING THE whatsapp.id: ', whatsapp.id)
|
console.log('RESTARTING THE whatsapp.id: ', whatsapp.id)
|
||||||
|
|
||||||
|
@ -34,6 +34,6 @@ export const restartWhatsSession = async (whatsapp: Whatsapp) => {
|
||||||
|
|
||||||
console.log('RESTARTING SESSION...')
|
console.log('RESTARTING SESSION...')
|
||||||
|
|
||||||
await StartWhatsAppSession(whatsapp);
|
await StartWhatsAppSession(whatsapp, backupSession);
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,17 @@
|
||||||
|
import path from "path";
|
||||||
import { getIO } from "../libs/socket";
|
import { getIO } from "../libs/socket";
|
||||||
import Message from "../models/Message";
|
import Message from "../models/Message";
|
||||||
import Ticket from "../models/Ticket";
|
import Ticket from "../models/Ticket";
|
||||||
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||||
import { logger } from "../utils/logger";
|
import { logger } from "../utils/logger";
|
||||||
import GetTicketWbot from "./GetTicketWbot";
|
import GetTicketWbot from "./GetTicketWbot";
|
||||||
|
import fs from 'fs';
|
||||||
|
import { restartWhatsSession } from "./RestartWhatsSession";
|
||||||
|
import { Session } from "@sentry/types";
|
||||||
|
import { splitDateTime } from "./SplitDateTime";
|
||||||
|
|
||||||
|
import { format } from "date-fns";
|
||||||
|
import ptBR from 'date-fns/locale/pt-BR';
|
||||||
|
|
||||||
const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
||||||
await Message.update(
|
await Message.update(
|
||||||
|
@ -17,15 +26,44 @@ const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
||||||
|
|
||||||
await ticket.update({ unreadMessages: 0 });
|
await ticket.update({ unreadMessages: 0 });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const wbot = await GetTicketWbot(ticket);
|
|
||||||
await wbot.sendSeen(
|
const wbot = await GetTicketWbot(ticket);
|
||||||
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`
|
|
||||||
);
|
// test del
|
||||||
|
// throw new Error('Throw makes it go boom!')
|
||||||
|
//
|
||||||
|
|
||||||
|
await wbot.sendSeen(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Could not mark messages as read. Maybe whatsapp session disconnected? Err: ${err}`
|
`Could not mark messages as read. Maybe whatsapp session disconnected? Err: ${err}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//Solução para contornar erro de sessão
|
||||||
|
if ((`${err}`).includes("Evaluation failed: r") && ticket.whatsappId) {
|
||||||
|
|
||||||
|
const sourcePath = path.join(__dirname,`../../.wwebjs_auth/sessions`)
|
||||||
|
|
||||||
|
const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
|
||||||
|
|
||||||
|
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
|
||||||
|
|
||||||
|
if (whatsapp && whatsapp.status == 'CONNECTED') {
|
||||||
|
|
||||||
|
console.log('SetTicketMessagesAsRead.ts - ENTROU NO RESTORE...')
|
||||||
|
|
||||||
|
let timestamp = Math.floor(Date.now() / 1000)
|
||||||
|
|
||||||
|
fs.writeFile(`${sourcePath}/${timestamp}_SetTicketMessagesAsRead.txt`, `Whatsapp id: ${whatsapp.id} \nDate: ${dateToday.fullDate} ${dateToday.fullTime} \nFile: SetTicketMessagesAsRead.ts \nError: ${err}`, (error)=>{});
|
||||||
|
|
||||||
|
await restartWhatsSession(whatsapp)
|
||||||
|
|
||||||
|
console.log('...PASSOU O RESTORE - SetTicketMessagesAsRead.ts ')
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
|
@ -10,6 +10,7 @@ const fs = require('fs')
|
||||||
import { copyFolder } from "../helpers/CopyFolder";
|
import { copyFolder } from "../helpers/CopyFolder";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { number } from "yup";
|
import { number } from "yup";
|
||||||
|
import { removeDir } from "../helpers/DeleteDirectory";
|
||||||
|
|
||||||
interface Session extends Client {
|
interface Session extends Client {
|
||||||
id?: number;
|
id?: number;
|
||||||
|
@ -43,7 +44,7 @@ const syncUnreadMessages = async (wbot: Session) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean = false): Promise<Session> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
@ -59,23 +60,6 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||||
puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || undefined },
|
puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || undefined },
|
||||||
});
|
});
|
||||||
|
|
||||||
//OPÇÃO DEFAULT NAO MD
|
|
||||||
// const wbot: Session = new Client({session: sessionCfg,
|
|
||||||
// puppeteer: {executablePath: process.env.CHROME_BIN || undefined
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
//OPÇÃO MD ANTIGO COM ERRO
|
|
||||||
// const io = getIO();
|
|
||||||
// const sessionName = whatsapp.name;
|
|
||||||
// const SESSION_FILE_PATH = './session.json'
|
|
||||||
// let sessionCfg
|
|
||||||
// if(fs.existsSync(SESSION_FILE_PATH)){
|
|
||||||
// sessionCfg = require(SESSION_FILE_PATH)
|
|
||||||
// }
|
|
||||||
// const wbot: Session = new Client({ puppeteer: { headless: true }, clientId: 'bd_'+whatsapp.id})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
wbot.initialize();
|
wbot.initialize();
|
||||||
|
|
||||||
|
@ -167,11 +151,15 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||||
|
|
||||||
const whatsIndex = backupSession.findIndex((id: number) => id === +whatsapp.id);
|
const whatsIndex = backupSession.findIndex((id: number) => id === +whatsapp.id);
|
||||||
|
|
||||||
console.log(' whatsIndex: ', whatsIndex)
|
console.log(' whatsIndex: ', whatsIndex)
|
||||||
|
|
||||||
if (whatsIndex !== -1) {
|
|
||||||
|
|
||||||
backupSession.splice(whatsIndex, 1);
|
if (whatsIndex !== -1 || backupSessionRestore) {
|
||||||
|
|
||||||
|
if (whatsIndex !== -1) {
|
||||||
|
backupSession.splice(whatsIndex, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
|
|
||||||
|
@ -179,6 +167,9 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||||
const destPath = path.join(__dirname, `../../.wwebjs_auth/sessions`, `session-bd_${whatsapp.id}`)
|
const destPath = path.join(__dirname, `../../.wwebjs_auth/sessions`, `session-bd_${whatsapp.id}`)
|
||||||
|
|
||||||
if (fs.existsSync(path.join(__dirname, `../../.wwebjs_auth/sessions`))) {
|
if (fs.existsSync(path.join(__dirname, `../../.wwebjs_auth/sessions`))) {
|
||||||
|
|
||||||
|
await removeDir(destPath)
|
||||||
|
|
||||||
// copy the good session for backup dir
|
// copy the good session for backup dir
|
||||||
copyFolder(sourcePath, destPath)
|
copyFolder(sourcePath, destPath)
|
||||||
}
|
}
|
||||||
|
@ -189,9 +180,9 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||||
console.log(` COPIOU backup whatsapp.id ---------------------------------->${whatsapp.id}`)
|
console.log(` COPIOU backup whatsapp.id ---------------------------------->${whatsapp.id}`)
|
||||||
|
|
||||||
|
|
||||||
}, 30000);
|
}, 55000);
|
||||||
|
|
||||||
console.log(' PASSOU NO TIMEOUT!')
|
console.log(' PASSOU NO TIMEOUT whatsapp.id: ',whatsapp.id)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { getIO } from "../../libs/socket";
|
||||||
import wbotMonitor from "./wbotMonitor";
|
import wbotMonitor from "./wbotMonitor";
|
||||||
import { logger } from "../../utils/logger";
|
import { logger } from "../../utils/logger";
|
||||||
|
|
||||||
export const StartWhatsAppSession = async (whatsapp: Whatsapp): Promise<void> => {
|
export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise<void> => {
|
||||||
await whatsapp.update({ status: "OPENING" });
|
await whatsapp.update({ status: "OPENING" });
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
@ -15,7 +15,7 @@ export const StartWhatsAppSession = async (whatsapp: Whatsapp): Promise<void> =>
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const wbot = await initWbot(whatsapp);
|
const wbot = await initWbot(whatsapp, backupSession);
|
||||||
wbotMessageListener(wbot);
|
wbotMessageListener(wbot);
|
||||||
wbotMonitor(wbot, whatsapp);
|
wbotMonitor(wbot, whatsapp);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -283,7 +283,7 @@ const reducer = (state, action) => {
|
||||||
state[messageIndex] = newMessage;
|
state[messageIndex] = newMessage;
|
||||||
} else {
|
} else {
|
||||||
state.push(newMessage);
|
state.push(newMessage);
|
||||||
console.log(' TESTANDO NOVA MENSAGEM: ', newMessage)
|
// console.log(' TESTANDO NOVA MENSAGEM: ', newMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
return [...state];
|
return [...state];
|
||||||
|
@ -365,7 +365,7 @@ const MessagesList = ({ ticketId, isGroup }) => {
|
||||||
if (data.action === "create") {
|
if (data.action === "create") {
|
||||||
dispatch({ type: "ADD_MESSAGE", payload: data.message });
|
dispatch({ type: "ADD_MESSAGE", payload: data.message });
|
||||||
|
|
||||||
console.log('* NOVA MENSAGEM CAP: ', data.message)
|
// console.log('* NOVA MENSAGEM CAP: ', data.message)
|
||||||
|
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ const NotificationsPopOver = () => {
|
||||||
|
|
||||||
const { handleLogout } = useContext(AuthContext);
|
const { handleLogout } = useContext(AuthContext);
|
||||||
|
|
||||||
const [lastRef] = useState(+history.location.pathname.split("/")[2])
|
// const [lastRef] = useState(+history.location.pathname.split("/")[2])
|
||||||
|
|
||||||
// console.log('ticketIdRef: ',ticketIdRef, ' | lastRef: ',lastRef)
|
// console.log('ticketIdRef: ',ticketIdRef, ' | lastRef: ',lastRef)
|
||||||
|
|
||||||
|
@ -168,12 +168,16 @@ const NotificationsPopOver = () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("appMessage", data => {
|
socket.on("appMessage", data => {
|
||||||
|
|
||||||
|
// console.log('******************* DATA: ', data)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
data.action === "create" &&
|
data.action === "create" &&
|
||||||
!data.message.read &&
|
!data.message.read &&
|
||||||
(data.ticket.userId === user?.id || !data.ticket.userId)
|
(data.ticket.userId === user?.id || !data.ticket.userId)
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
// console.log('entrou.............')
|
||||||
|
|
||||||
|
|
||||||
setNotifications(prevState => {
|
setNotifications(prevState => {
|
||||||
|
@ -194,6 +198,7 @@ const NotificationsPopOver = () => {
|
||||||
return [data.ticket, ...prevState];
|
return [data.ticket, ...prevState];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const shouldNotNotificate = (data.message.ticketId === ticketIdRef.current && document.visibilityState === "visible") ||
|
const shouldNotNotificate = (data.message.ticketId === ticketIdRef.current && document.visibilityState === "visible") ||
|
||||||
(data.ticket.userId && data.ticket.userId !== user?.id) ||
|
(data.ticket.userId && data.ticket.userId !== user?.id) ||
|
||||||
|
@ -201,8 +206,7 @@ const NotificationsPopOver = () => {
|
||||||
|
|
||||||
if (shouldNotNotificate) return;
|
if (shouldNotNotificate) return;
|
||||||
|
|
||||||
//console.log('PASSOU!!!!!!!')
|
//console.log('PASSOU!!!!!!!')
|
||||||
|
|
||||||
|
|
||||||
handleNotifications(data);
|
handleNotifications(data);
|
||||||
}
|
}
|
||||||
|
@ -289,8 +293,8 @@ const NotificationsPopOver = () => {
|
||||||
onClose={handleClickAway}
|
onClose={handleClickAway}
|
||||||
>
|
>
|
||||||
<List dense className={classes.tabContainer}>
|
<List dense className={classes.tabContainer}>
|
||||||
{notifications.length === 0 ? (
|
{notifications.length === 0 ? (
|
||||||
<ListItem>
|
<ListItem>
|
||||||
<ListItemText>{i18n.t("notifications.noTickets")}</ListItemText>
|
<ListItemText>{i18n.t("notifications.noTickets")}</ListItemText>
|
||||||
</ListItem>
|
</ListItem>
|
||||||
) : (
|
) : (
|
||||||
|
|
|
@ -221,6 +221,14 @@ const TicketListItem = ({ ticket }) => {
|
||||||
badge: classes.badgeStyle,
|
badge: classes.badgeStyle,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{/* <Badge
|
||||||
|
className={classes.newMessagesCount}
|
||||||
|
badgeContent={ticket.unreadMessages}
|
||||||
|
classes={{
|
||||||
|
badge: classes.badgeStyle,
|
||||||
|
}}
|
||||||
|
/> */}
|
||||||
</span>
|
</span>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -73,10 +73,13 @@ const useStyles = makeStyles(theme => ({
|
||||||
|
|
||||||
const reducer = (state, action) => {
|
const reducer = (state, action) => {
|
||||||
if (action.type === "LOAD_TICKETS") {
|
if (action.type === "LOAD_TICKETS") {
|
||||||
const newTickets = action.payload;
|
const newTickets = action.payload;
|
||||||
|
|
||||||
|
|
||||||
newTickets.forEach(ticket => {
|
newTickets.forEach(ticket => {
|
||||||
|
|
||||||
|
// console.log('* ticket.unreadMessages: ',ticket.unreadMessages)
|
||||||
|
|
||||||
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
||||||
if (ticketIndex !== -1) {
|
if (ticketIndex !== -1) {
|
||||||
state[ticketIndex] = ticket;
|
state[ticketIndex] = ticket;
|
||||||
|
@ -105,6 +108,8 @@ const reducer = (state, action) => {
|
||||||
if (action.type === "UPDATE_TICKET") {
|
if (action.type === "UPDATE_TICKET") {
|
||||||
const ticket = action.payload;
|
const ticket = action.payload;
|
||||||
|
|
||||||
|
// console.log('++++++++++++ UPDATE_TICKET: ',ticket)
|
||||||
|
|
||||||
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
||||||
if (ticketIndex !== -1) {
|
if (ticketIndex !== -1) {
|
||||||
state[ticketIndex] = ticket;
|
state[ticketIndex] = ticket;
|
||||||
|
@ -116,12 +121,26 @@ const reducer = (state, action) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action.type === "UPDATE_TICKET_UNREAD_MESSAGES") {
|
if (action.type === "UPDATE_TICKET_UNREAD_MESSAGES") {
|
||||||
const ticket = action.payload;
|
|
||||||
|
const message = action.payload.message
|
||||||
|
|
||||||
|
const ticket = action.payload.ticket;
|
||||||
|
|
||||||
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
||||||
|
|
||||||
if (ticketIndex !== -1) {
|
if (ticketIndex !== -1) {
|
||||||
|
|
||||||
|
// console.log('>>>>>> ticketIndex: ', ticketIndex)
|
||||||
|
|
||||||
|
// console.log('&&&&&&& UPDATE_TICKET_UNREAD_MESSAGES ticket: ',ticket, ' |\n MESSAGE: ', message)
|
||||||
|
|
||||||
|
if(!message.fromMe){
|
||||||
|
ticket.unreadMessages +=1
|
||||||
|
}
|
||||||
|
|
||||||
state[ticketIndex] = ticket;
|
state[ticketIndex] = ticket;
|
||||||
state.unshift(state.splice(ticketIndex, 1)[0]);
|
state.unshift(state.splice(ticketIndex, 1)[0]);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
state.unshift(ticket);
|
state.unshift(ticket);
|
||||||
}
|
}
|
||||||
|
@ -227,9 +246,13 @@ const TicketsList = (props) => {
|
||||||
|
|
||||||
socket.on("appMessage", data => {
|
socket.on("appMessage", data => {
|
||||||
if (data.action === "create" && shouldUpdateTicket(data.ticket)) {
|
if (data.action === "create" && shouldUpdateTicket(data.ticket)) {
|
||||||
|
|
||||||
|
// console.log('((((((((((((((((((( DATA.MESSAGE: ', data.message)
|
||||||
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: "UPDATE_TICKET_UNREAD_MESSAGES",
|
type: "UPDATE_TICKET_UNREAD_MESSAGES",
|
||||||
payload: data.ticket,
|
// payload: data.ticket,
|
||||||
|
payload: data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue