When starting ticket set queue automatically if user is only one linked

This commit is contained in:
Ricardo Paes
2022-02-24 08:07:24 -03:00
parent a9739f3a65
commit e7252c1273

View File

@@ -2,18 +2,21 @@ import AppError from "../../errors/AppError";
import CheckContactOpenTickets from "../../helpers/CheckContactOpenTickets";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import Ticket from "../../models/Ticket";
import User from "../../models/User";
import ShowContactService from "../ContactServices/ShowContactService";
interface Request {
contactId: number;
status: string;
userId: number;
queueId ?: number;
}
const CreateTicketService = async ({
contactId,
status,
userId
userId,
queueId
}: Request): Promise<Ticket> => {
const defaultWhatsapp = await GetDefaultWhatsApp(userId);
@@ -21,11 +24,17 @@ const CreateTicketService = async ({
const { isGroup } = await ShowContactService(contactId);
if(queueId === undefined) {
const user = await User.findByPk(userId, { include: ["queues"]});
queueId = user?.queues.length === 1 ? user.queues[0].id : undefined;
}
const { id }: Ticket = await defaultWhatsapp.$create("ticket", {
contactId,
status,
isGroup,
userId
userId,
queueId
});
const ticket = await Ticket.findByPk(id, { include: ["contact"] });