2022-12-29 15:10:36 +00:00
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" ;
2023-01-02 19:32:21 +00:00
import hitPortalMonitoring from "../helpers/HitPortalMonitoring" ;
2022-12-12 20:07:51 +00:00
// type IndexQuery = {
// centro_custo: string;
// };
2023-01-02 20:54:18 +00:00
2022-12-12 20:07:51 +00:00
export const hit = async ( req : Request , res : Response ) : Promise < Response > = > {
2022-12-29 15:10:36 +00:00
// const {
// centro_custo,
// } = req.body as IndexQuery;
2022-12-12 20:07:51 +00:00
console . log ( 'req.boy: ' , req . body )
2022-12-29 15:10:36 +00:00
console . log ( 'req.boy: ' , req . body [ 'centro_custo' ] )
2023-01-02 19:32:21 +00:00
if ( req . headers [ "auth" ] === '0424bd59b807674191e7d77572075f33' ) {
2022-12-29 15:10:36 +00:00
let contact = await ContactByCustomField ( req . body [ 'centro_custo' ] )
2023-01-02 19:32:21 +00:00
console . log ( '--------------> contact: ' , contact )
2022-12-29 15:10:36 +00:00
if ( contact ) {
2023-01-02 19:32:21 +00:00
try {
2023-01-02 20:54:18 +00:00
let response = await hitPortalMonitoring ( req . body [ 'centro_custo' ] )
2023-01-02 19:32:21 +00:00
2023-01-02 20:54:18 +00:00
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" } ) ;
}
2022-12-29 15:10:36 +00:00
const botInfo = await BotIsOnQueue ( 'botqueue' )
2023-01-02 19:32:21 +00:00
2022-12-29 15:10:36 +00:00
let ticket = await ShowTicketServiceByContactId ( contact [ 'contact.id' ] )
if ( ticket . id && ticket . status == 'pending' ) {
2023-01-02 20:54:18 +00:00
await sendMessageHitMonitoring ( response , ticket , contact ) ;
2023-01-02 19:32:21 +00:00
2022-12-29 15:10:36 +00:00
}
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 } ) ;
2023-01-02 20:54:18 +00:00
await sendMessageHitMonitoring ( response , ticket , contact ) ;
2023-01-02 19:32:21 +00:00
}
2022-12-29 15:10:36 +00:00
} catch ( error ) {
console . log ( ` Error on try sending the message monitor: ` , error )
}
}
2022-12-12 20:07:51 +00:00
}
2022-12-29 15:10:36 +00:00
else {
res . status ( 401 ) . json ( { "message" : "Token Inválido!" } ) ;
2022-12-12 20:07:51 +00:00
}
2022-12-29 15:10:36 +00:00
2022-12-12 20:07:51 +00:00
return res . status ( 200 ) . json ( { "message" : "Ok" } ) ;
2023-01-02 19:32:21 +00:00
} ;
2023-01-02 20:54:18 +00:00
async function sendMessageHitMonitoring ( response : any , ticket : Ticket , contact : any ) {
2023-01-02 19:32:21 +00:00
2023-01-02 20:54:18 +00:00
if ( response && response . length > 0 ) {
2023-01-02 19:32:21 +00:00
2023-01-02 20:54:18 +00:00
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 } ) ;
}
2023-01-02 19:32:21 +00:00
}