Atualização para a aplicação receber informação do centro de custo para envio da mensagem para o cliente

pull/20/head
adriano 2022-12-29 12:10:36 -03:00
parent 2d02a69b19
commit db898bc804
7 changed files with 143 additions and 20 deletions

View File

@ -1,4 +1,18 @@
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,17 +21,55 @@ import { Request, Response } from "express";
export const hit = async (req: Request, res: Response): Promise<Response> => {
// 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')
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{
else {
res.status(401).json({ "message": "Token Inválido!" });
}

View File

@ -93,7 +93,6 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
const { contactId, status, userId }: TicketData = req.body;
// test del
let ticket = await Ticket.findOne({ where: { contactId, status: 'queueChoice' } });
if (ticket) {
@ -109,7 +108,6 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
action: "update",
ticket
});
//
// const ticket = await CreateTicketService({ contactId, status, userId });

View File

@ -0,0 +1,27 @@
import Contact from "../../models/Contact";
import ContactCustomField from "../../models/ContactCustomField";
const ContactByCustomField = async (value: string | number): Promise<any> => {
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;

View File

@ -18,8 +18,6 @@ import ListTicketServiceCache from "./ListTicketServiceCache"
import { searchTicketCache, loadTicketsCache } from '../../helpers/TicketCache'
interface Request {
searchParam?: string;
pageNumber?: string;
@ -60,6 +58,9 @@ const ListTicketsService = async ({
}
if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) {
try {

View File

@ -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<Ticket> => {
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;

View File

@ -12,7 +12,7 @@ const CheckIsValidContact = async (number: string): Promise<void> => {
if (!isValidNumber) {
throw new AppError("invalidNumber");
}
} catch (err) {
} catch (err:any) {
if (err.message === "invalidNumber") {
throw new AppError("ERR_WAPP_INVALID_CONTACT");
}