Alteração finalizada para que um mesmo numero de cliente seja atendido por multiplos numeros na interface omnihit

adriano 2023-06-24 05:16:04 -03:00
parent 1b79ce1a55
commit 34a0f72ac6
8 changed files with 78 additions and 59 deletions

View File

@ -22,6 +22,7 @@ import format from 'date-fns/format';
import ListTicketsServiceCache from "../services/TicketServices/ListTicketServiceCache";
import { searchTicketCache, loadTicketsCache, } from '../helpers/TicketCache'
import { Op } from "sequelize";
@ -41,6 +42,7 @@ interface TicketData {
status: string;
queueId: number;
userId: number;
whatsappId?: string | number
msg?: string,
}
@ -53,6 +55,11 @@ import CountTicketService from "../services/TicketServices/CountTicketService";
import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";
import ShowUserService from "../services/UserServices/ShowUserService";
import axios from "axios";
import User from "../models/User";
import CheckContactOpenTickets from "../helpers/CheckContactOpenTickets";
import QueuesByUser from "../services/UserServices/ShowQueuesByUser";
import GetDefaultWhatsApp from "../helpers/GetDefaultWhatsApp";
import whatsappQueueMatchingUserQueue from "../helpers/whatsappQueueMatchingUserQueue";
export const index = async (req: Request, res: Response): Promise<Response> => {
@ -176,10 +183,6 @@ export const update = async (req: Request, res: Response): Promise<Response> =>
});
///////////////////////////////
//
if (scheduleData.farewellMessage) {
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
@ -218,13 +221,23 @@ export const update = async (req: Request, res: Response): Promise<Response> =>
}
else {
const ticketData: TicketData = req.body;
let ticketData: TicketData = req.body;
//ticketData: { status: 'open', userId: 4 } , ticketId
const defaultWhatsapp: any = await GetDefaultWhatsApp(ticketData.userId);
const _ticket: any = await Ticket.findByPk(ticketId)
if (defaultWhatsapp && ticketData.status != 'open') {
await CheckContactOpenTickets(_ticket.dataValues.contactId, defaultWhatsapp.dataValues.id)
}
ticketData.whatsappId = defaultWhatsapp.dataValues.id
const { ticket } = await UpdateTicketService({
ticketData,
ticketId
ticketId,
});
if (ticketData.userId) {

View File

@ -5,27 +5,22 @@ import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber
const CheckContactOpenTickets = async (contactId: number | string, whatsappId: number | string): Promise<void> => {
// let whats = await ListWhatsAppsNumber(whatsappId)
// console.log('----> whats: ', whats)
let whats = await ListWhatsAppsNumber(whatsappId)
const ticket = await Ticket.findOne({
// where: { contactId, whatsappId, status: { [Op.or]: ["open", "pending"] } }
where: {
[Op.and]: [
{ contactId: contactId },
{ whatsappId: whatsappId },
{ whatsappId: { [Op.in]: whats.whatsapps.map((w: any) => w.id) }, },
{ status: { [Op.or]: ["open", "pending"] } },
]
}
});
console.log('>>>>>>>>>>>>>> ticket: ', (JSON.parse(JSON.stringify(ticket))))
if (ticket) {
throw new AppError("ERR_OTHER_OPEN_TICKET");
}
};

View File

@ -44,18 +44,11 @@ const CreateTicketService = async ({
const defaultWhatsapp = await GetDefaultWhatsApp(userId);
// console.log('defaultWhatsapp: ',defaultWhatsapp)
// console.log('JSON.parse(JSON.stringify(defaultWhatsapp)): ',JSON.parse(JSON.stringify(defaultWhatsapp)))
const user = await User.findByPk(userId, { raw: true, })
console.log('user.profile: ', user?.profile)
const matchingQueue = await whatsappQueueMatchingUserQueue(userId, defaultWhatsapp, user?.profile);
console.log('matchingQueue: ', matchingQueue)
const queueId = matchingQueue ? matchingQueue.queueId : undefined
console.log('contactId, defaultWhatsapp.id: ',contactId, defaultWhatsapp.id)
const queueId = matchingQueue ? matchingQueue.queueId : undefined
await CheckContactOpenTickets(contactId, defaultWhatsapp.id);

View File

@ -6,6 +6,7 @@ import Ticket from "../../models/Ticket";
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
import ShowTicketService from "./ShowTicketService";
import AppError from "../../errors/AppError";
import ListWhatsAppsNumber from "../WhatsappService/ListWhatsAppsNumber";
const FindOrCreateTicketService = async (
@ -15,6 +16,9 @@ const FindOrCreateTicketService = async (
groupContact?: Contact
): Promise<Ticket> => {
let whats = await ListWhatsAppsNumber(whatsappId)
try {
let ticket = await Ticket.findOne({
where: {
@ -22,7 +26,7 @@ const FindOrCreateTicketService = async (
[Op.or]: ["open", "pending", "queueChoice"]
},
contactId: groupContact ? groupContact.id : contact.id,
whatsappId: whatsappId
whatsappId: { [Op.in]: whats.whatsapps.map((w: any) => w.id) },
}
});

View File

@ -9,6 +9,7 @@ import ShowTicketService from "./ShowTicketService";
import { createOrUpdateTicketCache } from '../../helpers/TicketCache'
import AppError from "../../errors/AppError";
import sendWhatsAppMessageSocket from "../../helpers/SendWhatsappMessageSocket";
import { number } from "yup";
var flatten = require('flat')
@ -18,6 +19,7 @@ interface TicketData {
userId?: number;
queueId?: number;
statusChatEnd?: string;
whatsappId?: string | number;
}
interface Request {
@ -40,10 +42,9 @@ const UpdateTicketService = async ({
try {
const { status, userId, queueId, statusChatEnd } = ticketData;
const { status, userId, queueId, statusChatEnd, whatsappId } = ticketData;
const ticket = await ShowTicketService(ticketId);
// await SetTicketMessagesAsRead(ticket);
const oldStatus = ticket.status;
const oldUserId = ticket.user?.id;
@ -56,7 +57,8 @@ const UpdateTicketService = async ({
status,
queueId,
userId,
statusChatEnd
statusChatEnd,
whatsappId
});
await ticket.reload();

View File

@ -7,27 +7,34 @@ const sequelize = new Sequelize(dbConfig);
const { QueryTypes } = require('sequelize');
interface Request {
profile: string;
profile?: string;
userId?: string | number;
}
const QueuesByUser = async ({ profile, userId }: Request): Promise<any[]> => {
let queueByUsersInfo = []
let queueByUsersInfo: any = []
if (userId) {
if (userId && profile) {
// CONSULTANDO FILAS PELO ID DO USUARIO
queueByUsersInfo = await sequelize.query(`select UserQueues.userId, UserQueues.queueId, Users.name,
Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
UserQueues.queueId = Queues.id and UserQueues.userId = Users.id and Users.id = '${userId}' and Users.profile = '${profile}' order by userId, queueId;`, { type: QueryTypes.SELECT });
} else {
} else if (profile) {
// CONSULTANDO FILAS PELO USUARIO
queueByUsersInfo = await sequelize.query(`select UserQueues.userId, UserQueues.queueId, Users.name,
Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
UserQueues.queueId = Queues.id and UserQueues.userId = Users.id and Users.profile = '${profile}' order by userId, queueId;`, { type: QueryTypes.SELECT });
}
else if (userId) {
queueByUsersInfo = await sequelize.query(`select UserQueues.userId, UserQueues.queueId, Users.name,
Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
UserQueues.queueId = Queues.id and UserQueues.userId = Users.id and Users.id = '${userId}' order by userId, queueId;`, { type: QueryTypes.SELECT });
}
return queueByUsersInfo;

View File

@ -68,14 +68,20 @@ const SendWhatsAppMessage = async ({
console.log('ticket.whatsappIdticket.whatsappIdticket.whatsappIdticket: ', ticket.whatsappId)
if (!ticket.whatsappId) {
const defaultWhatsapp: any = await GetDefaultWhatsApp(ticket.userId);
await ticket.update({ whatsappId: +defaultWhatsapp.id });
}
if (!listWhatsapp) {
listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED')
}
if (listWhatsapp.whatsapp && listWhatsapp.whatsapp.status != 'CONNECTED' && listWhatsapp.whatsapps.length > 0) {
// console.log('kkkkkkkkkkkkkkkkkkkkkkkkkkkk: ', listWhatsapp.whatsapps[0].id)
await ticket.update({ whatsappId: + listWhatsapp.whatsapps[0].id });
let _ticket = await Ticket.findByPk(listWhatsapp.whatsapps[0].id)

View File

@ -523,8 +523,6 @@ const handleMessage = async (
// groupContact = await verifyContact(msgGroupContact);
// }
const whatsapp = await ShowWhatsAppService(wbot.id!);
// const whatsapp = await ShowWhatsAppService(46);
@ -894,9 +892,10 @@ const handleMessage = async (
// }
//
} catch (err) {
} catch (err: any) {
Sentry.captureException(err);
logger.error(`Error handling whatsapp message: Err: ${err}`);
console.log('xxxxxxxxxxxxxxxxxx error: ', err)
}
};