From 007c016a616b38be768f4049ac84fb508b2f3153 Mon Sep 17 00:00:00 2001 From: Durans Date: Mon, 14 Nov 2022 18:50:36 -0300 Subject: [PATCH] =?UTF-8?q?fix(#45):=20ajuste=20no=20m=C3=B3dulo=20de=20ca?= =?UTF-8?q?mpanha=20para=20exibir=20as=20sess=C3=B5es=20corretamente.=20fi?= =?UTF-8?q?x:=20implementado=20checagem=20de=20"auth"=20para=20evitar=20er?= =?UTF-8?q?ros=20no=20cliclo=20de=20vida=20da=20autentica=C3=A7=C3=A3o=20d?= =?UTF-8?q?o=20socket.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/CampaignController.ts | 2 +- .../src/jobs/SendMessageWhatsappCampaign.ts | 7 +- backend/src/libs/socketChat/Chat.ts | 2 + .../CampaignServices/StartCampaignService.ts | 72 ++++--- .../BuildSendMessageService.ts | 28 +-- .../src/pages/campanhas/ContatosCampanha.vue | 182 ++++++------------ .../src/pages/campanhas/ModalCampanha.vue | 2 +- frontend/src/pages/contatos/Index.vue | 2 +- 8 files changed, 132 insertions(+), 165 deletions(-) diff --git a/backend/src/controllers/CampaignController.ts b/backend/src/controllers/CampaignController.ts index 67497510..0258b2ce 100644 --- a/backend/src/controllers/CampaignController.ts +++ b/backend/src/controllers/CampaignController.ts @@ -141,7 +141,7 @@ export const startCampaign = async ( campaignId, tenantId, options: { - delay: 15000 + delay: 2000 } }); diff --git a/backend/src/jobs/SendMessageWhatsappCampaign.ts b/backend/src/jobs/SendMessageWhatsappCampaign.ts index 323bc0dd..721a2bf1 100644 --- a/backend/src/jobs/SendMessageWhatsappCampaign.ts +++ b/backend/src/jobs/SendMessageWhatsappCampaign.ts @@ -22,11 +22,8 @@ export default { const wbot = getWbot(data.whatsappId); let message = {} as WbotMessage; if (data.mediaUrl) { - const customPath = join(__dirname, "..", "..", "..", "public"); - const mediaPath = join( - process.env.PATH_OFFLINE_MEDIA || customPath, - data.mediaName - ); + const customPath = join(__dirname, "..", "..", "public"); + const mediaPath = join(customPath, data.mediaName); const newMedia = MessageMedia.fromFilePath(mediaPath); message = await wbot.sendMessage(`${data.number}@c.us`, newMedia, { sendAudioAsVoice: true, diff --git a/backend/src/libs/socketChat/Chat.ts b/backend/src/libs/socketChat/Chat.ts index 2245c2e8..d265e1f9 100644 --- a/backend/src/libs/socketChat/Chat.ts +++ b/backend/src/libs/socketChat/Chat.ts @@ -447,6 +447,8 @@ events.getOpenChatWindows = (socket: Socket) => { }; function register(socket: Socket): void { + if (!socket?.user?.tenantId) return; + events.onSetUserIdle(socket); events.onSetUserActive(socket); events.onUpdateUsers(socket); diff --git a/backend/src/services/CampaignServices/StartCampaignService.ts b/backend/src/services/CampaignServices/StartCampaignService.ts index 6655095b..a02b8981 100644 --- a/backend/src/services/CampaignServices/StartCampaignService.ts +++ b/backend/src/services/CampaignServices/StartCampaignService.ts @@ -8,7 +8,10 @@ import { getDay, addDays, differenceInSeconds, - startOfDay + startOfDay, + isAfter, + isBefore, + differenceInDays } from "date-fns"; import { zonedTimeToUtc } from "date-fns-tz"; import Campaign from "../../models/Campaign"; @@ -66,34 +69,57 @@ const mountMessageData = ( const nextDayHoursValid = (date: Date) => { let dateVerify = date; - const isValidHour = isWithinInterval(dateVerify, { - start: parse("08:00", "HH:mm", new Date()), - end: parse("18:00", "HH:mm", new Date()) - }); + const dateNow = new Date(); + const diffDays = differenceInDays(dateVerify, new Date()); + // se dia for menor que o atual + if (diffDays < 0) { + dateVerify = addDays(dateVerify, diffDays * -1); + } - if (!isValidHour) { - dateVerify = setHours(dateVerify, 8); + // se a hora for menor que a atual ao programar a campanha + if (dateVerify.getTime() < dateNow.getTime()) { + dateVerify = setMinutes( + setHours(dateVerify, dateNow.getHours()), + dateNow.getMinutes() + ); } - // Se a data da programação for mario que a - // data atual, pular para o proximo dia. - if (dateVerify.getTime() < new Date().getTime()) { - dateVerify = addDays(dateVerify, 1); + const start = parse("08:00", "HH:mm", dateVerify); + const end = parse("20:00", "HH:mm", dateVerify); + + const isValidHour = isWithinInterval(dateVerify, { start, end }); + + const isDateBefore = isBefore(start, dateVerify); + const isDateAfter = isAfter(end, dateVerify); + + // fora do intervalo e menor que a hora inicial + if (!isValidHour && isDateBefore) { + dateVerify = setMinutes(setHours(dateVerify, 8), 30); } - const isValidDay = getDay(dateVerify) !== 0 && getDay(dateVerify) !== 6; - - if (!isValidDay) { - // Se for domingo add 1 dia para segunda - if (getDay(dateVerify) === 0) { - dateVerify = addDays(dateVerify, 1); - } - // SE for Sabado add 2 dias para segunda - if (getDay(dateVerify) === 6) { - dateVerify = addDays(dateVerify, 2); - } + // fora do intervalo, maior que a hora final e no mesmo dia + if (!isValidHour && isDateAfter && diffDays === 0) { + dateVerify = addDays(setHours(dateVerify, 8), 1); } + // fora do intervalo, maior que a hora final e dia diferente + if (!isValidHour && isDateAfter && diffDays > 0) { + dateVerify = setHours(dateVerify, 8); + } + + // const isValidDay = getDay(dateVerify) !== 0 && getDay(dateVerify) !== 6; + + // if (!isValidDay) { + // // Se for domingo add 1 dia para segunda + // if (getDay(dateVerify) === 0) { + // dateVerify = addDays(dateVerify, 1); + // } + // // SE for Sabado add 2 dias para segunda + // if (getDay(dateVerify) === 6) { + // dateVerify = addDays(dateVerify, 2); + // } + // } + return dateVerify; }; @@ -132,7 +158,7 @@ const StartCampaignService = async ({ throw new AppError("ERR_CAMPAIGN_CONTACTS_NOT_EXISTS", 404); } - const timeDelay = options?.delay || 15000; + const timeDelay = options?.delay || 5000; let dateDelay = setHours( setMinutes(zonedTimeToUtc(campaign.start, "America/Sao_Paulo"), 30), 8 diff --git a/backend/src/services/ChatFlowServices/BuildSendMessageService.ts b/backend/src/services/ChatFlowServices/BuildSendMessageService.ts index f9cb4001..34fac268 100644 --- a/backend/src/services/ChatFlowServices/BuildSendMessageService.ts +++ b/backend/src/services/ChatFlowServices/BuildSendMessageService.ts @@ -111,13 +111,13 @@ const BuildSendMessageService = async ({ lastMessageAt: new Date().getTime() }); - global.rabbitWhatsapp.publishInQueue( - `whatsapp::${tenantId}`, - JSON.stringify({ - ...messageCreated.toJSON(), - contact: ticket.contact.toJSON() - }) - ); + // global.rabbitWhatsapp.publishInQueue( + // `whatsapp::${tenantId}`, + // JSON.stringify({ + // ...messageCreated.toJSON(), + // contact: ticket.contact.toJSON() + // }) + // ); socketEmit({ tenantId, @@ -157,13 +157,13 @@ const BuildSendMessageService = async ({ answered: true }); - global.rabbitWhatsapp.publishInQueue( - `whatsapp::${tenantId}`, - JSON.stringify({ - ...messageCreated.toJSON(), - contact: ticket.contact.toJSON() - }) - ); + // global.rabbitWhatsapp.publishInQueue( + // `whatsapp::${tenantId}`, + // JSON.stringify({ + // ...messageCreated.toJSON(), + // contact: ticket.contact.toJSON() + // }) + // ); socketEmit({ tenantId, diff --git a/frontend/src/pages/campanhas/ContatosCampanha.vue b/frontend/src/pages/campanhas/ContatosCampanha.vue index 15aab378..004e065c 100644 --- a/frontend/src/pages/campanhas/ContatosCampanha.vue +++ b/frontend/src/pages/campanhas/ContatosCampanha.vue @@ -1,11 +1,9 @@