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 CheckContactOpenTickets from "../../helpers/CheckContactOpenTickets";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import Ticket from "../../models/Ticket"; import Ticket from "../../models/Ticket";
import User from "../../models/User";
import ShowContactService from "../ContactServices/ShowContactService"; import ShowContactService from "../ContactServices/ShowContactService";
interface Request { interface Request {
contactId: number; contactId: number;
status: string; status: string;
userId: number; userId: number;
queueId ?: number;
} }
const CreateTicketService = async ({ const CreateTicketService = async ({
contactId, contactId,
status, status,
userId userId,
queueId
}: Request): Promise<Ticket> => { }: Request): Promise<Ticket> => {
const defaultWhatsapp = await GetDefaultWhatsApp(userId); const defaultWhatsapp = await GetDefaultWhatsApp(userId);
@@ -21,11 +24,17 @@ const CreateTicketService = async ({
const { isGroup } = await ShowContactService(contactId); 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", { const { id }: Ticket = await defaultWhatsapp.$create("ticket", {
contactId, contactId,
status, status,
isGroup, isGroup,
userId userId,
queueId
}); });
const ticket = await Ticket.findByPk(id, { include: ["contact"] }); const ticket = await Ticket.findByPk(id, { include: ["contact"] });