Skip to content

Commit

Permalink
fix(#45): ajuste no módulo de campanha para exibir as sessões correta…
Browse files Browse the repository at this point in the history
…mente.

fix: implementado checagem de "auth" para evitar erros no cliclo de vida da autenticação do socket.
  • Loading branch information
ldurans committed Nov 14, 2022
1 parent 66912d5 commit 007c016
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 165 deletions.
2 changes: 1 addition & 1 deletion backend/src/controllers/CampaignController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const startCampaign = async (
campaignId,
tenantId,
options: {
delay: 15000
delay: 2000
}
});

Expand Down
7 changes: 2 additions & 5 deletions backend/src/jobs/SendMessageWhatsappCampaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/libs/socketChat/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
72 changes: 49 additions & 23 deletions backend/src/services/CampaignServices/StartCampaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions backend/src/services/ChatFlowServices/BuildSendMessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 007c016

Please sign in to comment.