Skip to content

Commit

Permalink
fix incorrectly asking for ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Aug 22, 2024
1 parent dfa600a commit 26142ed
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import UserRating from "../../models/UserRating";
import SendWhatsAppMessage from "./SendWhatsAppMessage";
import Queue from "../../models/Queue";
import QueueOption from "../../models/QueueOption";
import FindOrCreateATicketTrakingService from "../TicketServices/FindOrCreateATicketTrakingService";
import VerifyCurrentSchedule, { ScheduleResult } from "../CompanyService/VerifyCurrentSchedule";
import Campaign from "../../models/Campaign";
import CampaignShipping from "../../models/CampaignShipping";
Expand Down Expand Up @@ -1420,51 +1419,75 @@ const handleMessage = async (
return;
}

const ticketTraking = await FindOrCreateATicketTrakingService({
ticketId: ticket.id,
companyId,
whatsappId: whatsapp?.id
});
if (!msg.key.fromMe) {
const userRatingEnabled = await GetCompanySetting(companyId, "userRating", "") === "enabled"

try {
if (!msg.key.fromMe) {
/**
* Tratamento para avaliação do atendente
*/
const ticketTracking = userRatingEnabled && await TicketTraking.findOne({
where: {
whatsappId: whatsapp.id,
rated: false,
ratingAt: { [Op.not]: null }
},
include: [{
model: Ticket,
where: {
status: "closed",
contactId: contact.id,
},
include: [
{
model: Contact
},
{
model: User
},
{
model: Queue
}
]
}]
});

// dev Ricardo: insistir a responder avaliação
const rate = Number(bodyMessage);
if (ticketTracking) {
try {
/**
* Tratamento para avaliação do atendente
*/

if ((ticket?.lastMessage.includes("_Insatisfeito_") || ticket?.lastMessage.includes("Por favor avalie nosso atendimento.")) && (!Number.isFinite(rate))) {
const debouncedSentMessage = debounce(
async () => {
await wbot.sendMessage(
`${ticket.contact.number}@${ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: "Por favor avalie nosso atendimento."
}
);
},
1000,
ticket.id
);
debouncedSentMessage();
return;
}
// dev Ricardo
// insistir a responder avaliação
const rate = Number(bodyMessage);

if (ticketTraking !== null && verifyRating(ticketTraking)) {
handleRating(msg, ticket, ticketTraking);
return;
if (!Number.isFinite(rate)) {
const debouncedSentMessage = debounce(
async () => {
await wbot.sendMessage(
`${ticketTracking.ticket.contact.number}@${ticketTracking.ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: "\u200e\n*Por favor avalie nosso atendimento com uma nota de 1 a 5*"
}
);
},
1000,
ticketTracking.ticket.id
);
debouncedSentMessage();
return;
}
// dev Ricardo

if (verifyRating(ticketTracking)) {
handleRating(msg, ticketTracking.ticket, ticketTracking);
return;
}
} catch (e) {
Sentry.captureException(e);
console.log(e);
}

}
} catch (e) {
Sentry.captureException(e);
console.log(e);
}


if (messageMedia) {
await verifyMediaMessage(msg, ticket, contact, wbot);
} else if (msg.message?.editedMessage?.message?.protocolMessage?.editedMessage) {
Expand Down Expand Up @@ -1563,18 +1586,6 @@ const handleMessage = async (
console.log(e);
}

try {
if (!msg.key.fromMe) {
if (ticketTraking !== null && verifyRating(ticketTraking)) {
handleRating(msg, ticket, ticketTraking);
return;
}
}
} catch (e) {
Sentry.captureException(e);
console.log(e);
}

if (
!ticket.queue &&
!isGroup &&
Expand Down

0 comments on commit 26142ed

Please sign in to comment.