diff --git a/backend/src/controllers/HitController.ts b/backend/src/controllers/HitController.ts index 509bc94..5c53f7b 100644 --- a/backend/src/controllers/HitController.ts +++ b/backend/src/controllers/HitController.ts @@ -1,4 +1,18 @@ -import { Request, Response } from "express"; +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"; // type IndexQuery = { @@ -7,20 +21,58 @@ import { Request, Response } from "express"; export const hit = async (req: Request, res: Response): Promise => { -// const { -// centro_custo, -// } = req.body as IndexQuery; + // 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'){ - console.log('fffffffffffffffffff') - } - else{ - res.status(401).json({ "message": "Token Inválido!" }); - } + + if (req.headers["auth"] === '0424bd59b807674191e7d77572075f33') { + + let contact = await ContactByCustomField(req.body['centro_custo']) + + // console.log('--------------> contact: ', contact) + + if (contact) { + + try { + + const botInfo = await BotIsOnQueue('botqueue') + let ticket = await ShowTicketServiceByContactId(contact['contact.id']) + + if (ticket.id && ticket.status == 'pending') { + + await SendWhatsAppMessage({ body: 'Resposta da operadora', ticket }); + + } + 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 SendWhatsAppMessage({ body: 'Ola isso é um teste', ticket }); + + } + + } 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" }); }; \ No newline at end of file diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 9987e26..ec481e8 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -91,9 +91,8 @@ export const index = async (req: Request, res: Response): Promise => { export const store = async (req: Request, res: Response): Promise => { const { contactId, status, userId }: TicketData = req.body; - - // test del + let ticket = await Ticket.findOne({ where: { contactId, status: 'queueChoice' } }); if (ticket) { @@ -108,8 +107,7 @@ export const store = async (req: Request, res: Response): Promise => { io.to(ticket.status).emit("ticket", { action: "update", ticket - }); - // + }); // const ticket = await CreateTicketService({ contactId, status, userId }); diff --git a/backend/src/services/HitServices/ShowContactByCustomFieldValueService.ts b/backend/src/services/HitServices/ShowContactByCustomFieldValueService.ts new file mode 100644 index 0000000..859ad1a --- /dev/null +++ b/backend/src/services/HitServices/ShowContactByCustomFieldValueService.ts @@ -0,0 +1,27 @@ + +import Contact from "../../models/Contact"; +import ContactCustomField from "../../models/ContactCustomField"; + +const ContactByCustomField = async (value: string | number): Promise => { + + const contact = await ContactCustomField.findOne({ + where: { value }, + raw: true, + attributes: ['id', 'value', 'contactId'], + + include: [ + { + model: Contact, + required: true, + attributes: ['id', 'name', 'number'], + + }, + + ], + + }); + + return contact; +}; + +export default ContactByCustomField; diff --git a/backend/src/services/TicketServices/ListTicketsService.ts b/backend/src/services/TicketServices/ListTicketsService.ts index 421361d..0185ef8 100644 --- a/backend/src/services/TicketServices/ListTicketsService.ts +++ b/backend/src/services/TicketServices/ListTicketsService.ts @@ -15,9 +15,7 @@ const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss import ListTicketServiceCache from "./ListTicketServiceCache" -import { searchTicketCache, loadTicketsCache } from '../../helpers/TicketCache' - - +import { searchTicketCache, loadTicketsCache } from '../../helpers/TicketCache' interface Request { @@ -60,6 +58,9 @@ const ListTicketsService = async ({ } + + + if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) { try { diff --git a/backend/src/services/TicketServices/ShowTicketServiceByContactId.ts b/backend/src/services/TicketServices/ShowTicketServiceByContactId.ts new file mode 100644 index 0000000..b340d9a --- /dev/null +++ b/backend/src/services/TicketServices/ShowTicketServiceByContactId.ts @@ -0,0 +1,45 @@ +import Ticket from "../../models/Ticket"; +import AppError from "../../errors/AppError"; +import Contact from "../../models/Contact"; +import User from "../../models/User"; +import Queue from "../../models/Queue"; + +import { Op } from "sequelize"; + +const ShowTicketServiceByContactId = async (contactId: string | number): Promise => { + + const ticket = await Ticket.findOne({ + + where:{ contactId: contactId, [Op.or]: [ { status: 'open' }, { status: 'pending' }] }, + + include: [ + { + model: Contact, + as: "contact", + attributes: ["id", "name", "number", "profilePicUrl", "useDialogflow", "useQueues"], + include: ["extraInfo"] + }, + { + model: User, + as: "user", + attributes: ["id", "name"] + }, + { + model: Queue, + as: "queue", + attributes: ["id", "name", "color"], + include: ["dialogflow"] + } + ] + }); + + if (!ticket) { + + return new Ticket + + } + + return ticket; +}; + +export default ShowTicketServiceByContactId; diff --git a/backend/src/services/WbotServices/CheckIsValidContact.ts b/backend/src/services/WbotServices/CheckIsValidContact.ts index daa56bf..4434ec9 100644 --- a/backend/src/services/WbotServices/CheckIsValidContact.ts +++ b/backend/src/services/WbotServices/CheckIsValidContact.ts @@ -12,7 +12,7 @@ const CheckIsValidContact = async (number: string): Promise => { if (!isValidNumber) { throw new AppError("invalidNumber"); } - } catch (err) { + } catch (err:any) { if (err.message === "invalidNumber") { throw new AppError("ERR_WAPP_INVALID_CONTACT"); } diff --git a/backend/src/services/WbotServices/SendWhatsAppMessage.ts b/backend/src/services/WbotServices/SendWhatsAppMessage.ts index 9b42891..ad07a2e 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMessage.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMessage.ts @@ -43,7 +43,7 @@ const SendWhatsAppMessage = async ({ var timetaken = `########################################${timestamp}| TicketId: ${ticket.id} => Time taken to send the message`; - console.time(timetaken) + console.time(timetaken) let quotedMsgSerializedId: string | undefined;