improvement: block open another ticket from same contact when already a open / pending ticket

This commit is contained in:
canove
2020-09-27 09:17:10 -03:00
parent 856308e093
commit 11f429329b
2 changed files with 20 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import { Op } from "sequelize";
import AppError from "../errors/AppError";
import Ticket from "../models/Ticket";
const CheckContactOpenTickets = async (contactId: number): Promise<void> => {
const ticket = await Ticket.findOne({
where: { contactId, status: { [Op.or]: ["open", "pending"] } }
});
if (ticket) {
throw new AppError(
"There's already an open or pending ticket for this contact."
);
}
};
export default CheckContactOpenTickets;

View File

@@ -1,4 +1,5 @@
import AppError from "../../errors/AppError";
import CheckContactOpenTickets from "../../helpers/CheckContactOpenTickets";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import Ticket from "../../models/Ticket";
@@ -19,6 +20,8 @@ const CreateTicketService = async ({
throw new AppError("No default WhatsApp found. Check Connection page.");
}
await CheckContactOpenTickets(contactId);
const { id }: Ticket = await defaultWhatsapp.$create("ticket", {
contactId,
status,