101 lines
3.3 KiB
TypeScript
101 lines
3.3 KiB
TypeScript
import { Request, Response } from "express";
|
|
import BotIsOnQueue from "../helpers/BotIsOnQueue";
|
|
import GetDefaultWhatsApp from "../helpers/GetDefaultWhatsApp";
|
|
import { getIO } from "../libs/socket";
|
|
import { getWbot } from "../libs/wbot";
|
|
import Ticket from "../models/Ticket";
|
|
import ContactByCustomField from "../services/HitServices/ShowContactByCustomFieldValueService";
|
|
import ShowQueueService from "../services/QueueService/ShowQueueService";
|
|
import CreateTicketService from "../services/TicketServices/CreateTicketService";
|
|
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
|
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
|
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
|
|
|
import { Op, where, Sequelize } from "sequelize";
|
|
import ShowTicketServiceByContactId from "../services/TicketServices/ShowTicketServiceByContactId";
|
|
import hitPortalMonitoring from "../helpers/HitPortalMonitoring";
|
|
|
|
|
|
// type IndexQuery = {
|
|
// centro_custo: string;
|
|
// };
|
|
|
|
export const hit = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
// const {
|
|
// centro_custo,
|
|
// } = req.body as IndexQuery;
|
|
|
|
console.log('req.boy: ', req.body)
|
|
console.log('req.boy: ', req.body['centro_custo'])
|
|
|
|
|
|
if (req.headers["auth"] === '0424bd59b807674191e7d77572075f33') {
|
|
|
|
let contact = await ContactByCustomField(req.body['centro_custo'])
|
|
|
|
console.log('--------------> contact: ', contact)
|
|
|
|
|
|
if (contact) {
|
|
|
|
try {
|
|
|
|
let response = await hitPortalMonitoring(req.body['centro_custo'])
|
|
|
|
if (!response || response.length == 0) {
|
|
console.log('Empty result from hit portal monitoring. Centro_de_custo: ',req.body['centro_custo'])
|
|
return res.status(200).json({ "message": "Ok" });
|
|
}
|
|
|
|
const botInfo = await BotIsOnQueue('botqueue')
|
|
|
|
let ticket = await ShowTicketServiceByContactId(contact['contact.id'])
|
|
|
|
if (ticket.id && ticket.status == 'pending') {
|
|
|
|
await sendMessageHitMonitoring(response, ticket, contact);
|
|
|
|
}
|
|
else if (!ticket.id) {
|
|
|
|
ticket = await CreateTicketService({ contactId: contact['contact.id'], status: 'open', userId: botInfo.userIdBot });
|
|
|
|
console.log('botInfo.botQueueId: ', botInfo.botQueueId)
|
|
|
|
await UpdateTicketService({ ticketData: { queueId: botInfo.botQueueId }, ticketId: ticket.id });
|
|
|
|
await sendMessageHitMonitoring(response, ticket, contact);
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error on try sending the message monitor: `, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
else {
|
|
res.status(401).json({ "message": "Token Inválido!" });
|
|
}
|
|
|
|
|
|
return res.status(200).json({ "message": "Ok" });
|
|
};
|
|
|
|
async function sendMessageHitMonitoring(response: any, ticket: Ticket, contact: any) {
|
|
|
|
if (response && response.length > 0) {
|
|
|
|
console.log('MESSAGE WILL BE SENT!')
|
|
|
|
await SendWhatsAppMessage({ body: `*Olá. Somos a TI Espaçolaser.*\nIdentificamos em nossos monitoramentos que há um problema na internet da sua loja ${contact['contact.name']} e já estamos resolvendo. Abaixo seguem informações sobre o incidente para que possam acompanhar.\n\n*Situação do chamado na Operadora*\n\n*Incidente*:\n\n ${response[0].header}\n${response[0].body}`, ticket });
|
|
|
|
}
|
|
|
|
}
|