2022-01-06 01:26:15 +00:00
|
|
|
import AppError from "../../errors/AppError";
|
|
|
|
import CheckContactOpenTickets from "../../helpers/CheckContactOpenTickets";
|
|
|
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
|
|
|
import Ticket from "../../models/Ticket";
|
|
|
|
import ShowContactService from "../ContactServices/ShowContactService";
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
import { getIO } from "../../libs/socket";
|
|
|
|
import ShowUserServiceReport from "../UserServices/ShowUserServiceReport";
|
|
|
|
|
|
|
|
import format from 'date-fns/format';
|
|
|
|
import ptBR from 'date-fns/locale/pt-BR';
|
|
|
|
import { splitDateTime } from "../../helpers/SplitDateTime";
|
|
|
|
import TicketEmiterSumOpenClosedByUser from "../../helpers/OnlineReporEmiterInfoByUser";
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
import { createOrUpdateTicketCache } from '../../helpers/TicketCache'
|
2023-06-02 18:45:14 +00:00
|
|
|
import ShowQueuesByUser from "../UserServices/ShowQueuesByUser";
|
|
|
|
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
|
|
|
|
import Whatsapp from "../../models/Whatsapp";
|
|
|
|
import whatsappQueueMatchingUserQueue from "../../helpers/whatsappQueueMatchingUserQueue";
|
2023-06-03 10:45:47 +00:00
|
|
|
import User from "../../models/User";
|
2023-06-06 17:59:06 +00:00
|
|
|
import sendWhatsAppMessageSocket from "../../helpers/SendWhatsappMessageSocket";
|
2022-10-25 14:16:36 +00:00
|
|
|
let flatten = require('flat')
|
2023-04-20 18:48:42 +00:00
|
|
|
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface Request {
|
|
|
|
contactId: number;
|
|
|
|
status: string;
|
|
|
|
userId: number;
|
2023-06-06 17:59:06 +00:00
|
|
|
msg?: string
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const CreateTicketService = async ({
|
|
|
|
contactId,
|
|
|
|
status,
|
2023-06-06 17:59:06 +00:00
|
|
|
userId,
|
|
|
|
msg = ''
|
2022-01-06 01:26:15 +00:00
|
|
|
}: Request): Promise<Ticket> => {
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
try {
|
|
|
|
|
2023-06-02 18:45:14 +00:00
|
|
|
console.log('Create contact service........')
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
const defaultWhatsapp = await GetDefaultWhatsApp(userId);
|
|
|
|
|
2023-06-03 10:45:47 +00:00
|
|
|
|
|
|
|
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)
|
2023-06-02 18:45:14 +00:00
|
|
|
const queueId = matchingQueue ? matchingQueue.queueId : undefined
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
await CheckContactOpenTickets(contactId);
|
|
|
|
|
|
|
|
const { isGroup } = await ShowContactService(contactId);
|
|
|
|
|
|
|
|
const { id }: Ticket = await defaultWhatsapp.$create("ticket", {
|
|
|
|
contactId,
|
|
|
|
status,
|
|
|
|
isGroup,
|
2023-06-02 18:45:14 +00:00
|
|
|
userId,
|
|
|
|
queueId
|
2023-04-20 18:48:42 +00:00
|
|
|
});
|
|
|
|
|
2023-06-02 18:45:14 +00:00
|
|
|
console.log('TICKET CREATED!')
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
const ticket = await Ticket.findByPk(id, { include: ["contact"] });
|
|
|
|
|
|
|
|
if (!ticket) {
|
|
|
|
throw new AppError("ERR_CREATING_TICKET");
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log('CONTACT ticket.id: ', ticket.id)
|
|
|
|
|
2023-06-06 17:59:06 +00:00
|
|
|
// console.log('>>>>>>>>>>>>>>>>> msg: ', msg)
|
|
|
|
|
|
|
|
if (msg.length > 0) {
|
|
|
|
sendWhatsAppMessageSocket(ticket, msg)
|
|
|
|
}
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
|
|
|
|
try {
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
let jsonString = JSON.stringify(ticket); //convert to string to remove the sequelize specific meta data
|
|
|
|
let ticket_obj = JSON.parse(jsonString); //to make plain json
|
|
|
|
delete ticket_obj['contact']['extraInfo']
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
ticket_obj = flatten(ticket_obj)
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
await createOrUpdateTicketCache(`ticket:${ticket.id}`, ticket_obj)
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on UpdateTicketService.ts on createTicketCache from user: ', error)
|
|
|
|
}
|
2022-10-25 14:16:36 +00:00
|
|
|
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
TicketEmiterSumOpenClosedByUser(userId.toString(), dateToday.fullDate, dateToday.fullDate)
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("ticketStatus", {
|
|
|
|
action: "update",
|
|
|
|
ticketStatus: { ticketId: ticket.id, status: ticket.status }
|
|
|
|
});
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
return ticket;
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
console.error('===> Error on CreateTicketService.ts file: \n', error)
|
|
|
|
throw new AppError(error.message);
|
|
|
|
}
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CreateTicketService;
|
2023-06-02 18:45:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|