open more than one ticket per user as long as it is using another whatsapp

This commit is contained in:
Ricardo Paes
2022-02-23 11:21:11 -03:00
parent afb5d68602
commit acd628028e
4 changed files with 13 additions and 7 deletions

View File

@@ -2,9 +2,12 @@ import { Op } from "sequelize";
import AppError from "../errors/AppError";
import Ticket from "../models/Ticket";
const CheckContactOpenTickets = async (contactId: number): Promise<void> => {
const CheckContactOpenTickets = async (
contactId: number,
whatsappId: number
): Promise<void> => {
const ticket = await Ticket.findOne({
where: { contactId, status: { [Op.or]: ["open", "pending"] } }
where: { contactId, whatsappId, status: { [Op.or]: ["open", "pending"] } }
});
if (ticket) {

View File

@@ -17,7 +17,7 @@ const CreateTicketService = async ({
}: Request): Promise<Ticket> => {
const defaultWhatsapp = await GetDefaultWhatsApp(userId);
await CheckContactOpenTickets(contactId);
await CheckContactOpenTickets(contactId, defaultWhatsapp.id);
const { isGroup } = await ShowContactService(contactId);

View File

@@ -15,7 +15,8 @@ const FindOrCreateTicketService = async (
status: {
[Op.or]: ["open", "pending"]
},
contactId: groupContact ? groupContact.id : contact.id
contactId: groupContact ? groupContact.id : contact.id,
whatsappId: whatsappId
}
});
@@ -26,7 +27,8 @@ const FindOrCreateTicketService = async (
if (!ticket && groupContact) {
ticket = await Ticket.findOne({
where: {
contactId: groupContact.id
contactId: groupContact.id,
whatsappId: whatsappId
},
order: [["updatedAt", "DESC"]]
});
@@ -46,7 +48,8 @@ const FindOrCreateTicketService = async (
updatedAt: {
[Op.between]: [+subHours(new Date(), 2), +new Date()]
},
contactId: contact.id
contactId: contact.id,
whatsappId: whatsappId
},
order: [["updatedAt", "DESC"]]
});

View File

@@ -36,7 +36,7 @@ const UpdateTicketService = async ({
const oldUserId = ticket.user?.id;
if (oldStatus === "closed") {
await CheckContactOpenTickets(ticket.contact.id);
await CheckContactOpenTickets(ticket.contact.id, ticket.whatsappId);
}
await ticket.update({