diff --git a/backend/src/controllers/WhatsAppController.ts b/backend/src/controllers/WhatsAppController.ts index 5759109..13097d3 100644 --- a/backend/src/controllers/WhatsAppController.ts +++ b/backend/src/controllers/WhatsAppController.ts @@ -9,13 +9,9 @@ import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsServi import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService"; import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService"; -interface QueueData { - id: number; - optionNumber: number; -} interface WhatsappData { name: string; - queuesData: QueueData[]; + queueIds: number[]; greetingMessage?: string; status?: string; isDefault?: boolean; @@ -33,7 +29,7 @@ export const store = async (req: Request, res: Response): Promise => { status, isDefault, greetingMessage, - queuesData + queueIds }: WhatsappData = req.body; const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({ @@ -41,7 +37,7 @@ export const store = async (req: Request, res: Response): Promise => { status, isDefault, greetingMessage, - queuesData + queueIds }); // StartWhatsAppSession(whatsapp); diff --git a/backend/src/database/migrations/20210108174594-associate-whatsapp-queue.ts b/backend/src/database/migrations/20210108174594-associate-whatsapp-queue.ts index 0ea50f0..0e08f71 100644 --- a/backend/src/database/migrations/20210108174594-associate-whatsapp-queue.ts +++ b/backend/src/database/migrations/20210108174594-associate-whatsapp-queue.ts @@ -3,9 +3,6 @@ import { QueryInterface, DataTypes } from "sequelize"; module.exports = { up: (queryInterface: QueryInterface) => { return queryInterface.createTable("WhatsappQueues", { - optionNumber: { - type: DataTypes.INTEGER - }, whatsappId: { type: DataTypes.INTEGER, primaryKey: true diff --git a/backend/src/models/WhatsappQueue.ts b/backend/src/models/WhatsappQueue.ts index 7886618..b68aaa0 100644 --- a/backend/src/models/WhatsappQueue.ts +++ b/backend/src/models/WhatsappQueue.ts @@ -12,9 +12,6 @@ import Whatsapp from "./Whatsapp"; @Table class WhatsappQueue extends Model { - @Column - optionNumber: number; - @ForeignKey(() => Whatsapp) @Column whatsappId: number; diff --git a/backend/src/services/QueueService/AssociateWhatsappQueue.ts b/backend/src/services/QueueService/AssociateWhatsappQueue.ts index 45a5a84..5f840f7 100644 --- a/backend/src/services/QueueService/AssociateWhatsappQueue.ts +++ b/backend/src/services/QueueService/AssociateWhatsappQueue.ts @@ -1,32 +1,12 @@ import Whatsapp from "../../models/Whatsapp"; -import WhatsappQueue from "../../models/WhatsappQueue"; - -interface QueueData { - id: number; - optionNumber: number; -} const AssociateWhatsappQueue = async ( whatsapp: Whatsapp, - queuesData: QueueData[] + queueIds: number[] ): Promise => { - const queueIds = queuesData.map(({ id }) => id); - await whatsapp.$set("queues", queueIds); - /* eslint-disable no-restricted-syntax */ - /* eslint-disable no-await-in-loop */ - for (const queueData of queuesData) { - await WhatsappQueue.update( - { optionNumber: queueData.optionNumber }, - { - where: { - whatsappId: whatsapp.id, - queueId: queueData.id - } - } - ); - } + await whatsapp.reload(); }; export default AssociateWhatsappQueue; diff --git a/backend/src/services/QueueService/ListQueuesService.ts b/backend/src/services/QueueService/ListQueuesService.ts index 9a9a3a1..204d9a1 100644 --- a/backend/src/services/QueueService/ListQueuesService.ts +++ b/backend/src/services/QueueService/ListQueuesService.ts @@ -1,7 +1,7 @@ import Queue from "../../models/Queue"; const ListQueuesService = async (): Promise => { - const queues = await Queue.findAll(); + const queues = await Queue.findAll({ order: [["name", "ASC"]] }); return queues; }; diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index 76f8904..3f97218 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -133,12 +133,10 @@ const verifyQueue = async ( ticket: Ticket, contact: Contact ) => { - const { whatsappQueues, greetingMessage } = await ShowWhatsAppService( - wbot.id! - ); + const { queues, greetingMessage } = await ShowWhatsAppService(wbot.id!); - if (whatsappQueues.length === 1) { - await ticket.$set("queue", whatsappQueues[0].queue); + if (queues.length === 1) { + await ticket.$set("queue", queues[0]); // TODO sendTicketQueueUpdate to frontend return; @@ -146,14 +144,12 @@ const verifyQueue = async ( const selectedOption = msg.body[0]; - const validOption = whatsappQueues.find( - q => q.optionNumber === +selectedOption - ); + const validOption = queues[+selectedOption - 1]; if (validOption) { - await ticket.$set("queue", validOption.queue); + await ticket.$set("queue", validOption); - const body = `\u200e ${validOption.queue.greetingMessage}`; + const body = `\u200e ${validOption.greetingMessage}`; const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body); @@ -163,8 +159,8 @@ const verifyQueue = async ( } else { let options = ""; - whatsappQueues.forEach(whatsQueue => { - options += `*${whatsQueue.optionNumber}* - ${whatsQueue.queue.name}\n`; + queues.forEach((queue, index) => { + options += `*${index + 1}* - ${queue.name}\n`; }); const body = `\u200e ${greetingMessage}\n\n${options}`; diff --git a/backend/src/services/WhatsappService/CreateWhatsAppService.ts b/backend/src/services/WhatsappService/CreateWhatsAppService.ts index f1375e5..b2ced1e 100644 --- a/backend/src/services/WhatsappService/CreateWhatsAppService.ts +++ b/backend/src/services/WhatsappService/CreateWhatsAppService.ts @@ -4,13 +4,9 @@ import AppError from "../../errors/AppError"; import Whatsapp from "../../models/Whatsapp"; import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue"; -interface QueueData { - id: number; - optionNumber: number; -} interface Request { name: string; - queuesData: QueueData[]; + queueIds?: number[]; greetingMessage?: string; status?: string; isDefault?: boolean; @@ -24,7 +20,7 @@ interface Response { const CreateWhatsAppService = async ({ name, status = "OPENING", - queuesData = [], + queueIds = [], greetingMessage, isDefault = false }: Request): Promise => { @@ -71,6 +67,10 @@ const CreateWhatsAppService = async ({ } } + if (queueIds.length > 1 && !greetingMessage) { + throw new AppError("ERR_WAPP_GREETING_REQUIRED"); + } + const whatsapp = await Whatsapp.create( { name, @@ -81,9 +81,7 @@ const CreateWhatsAppService = async ({ { include: ["queues"] } ); - await AssociateWhatsappQueue(whatsapp, queuesData); - - await whatsapp.reload(); + await AssociateWhatsappQueue(whatsapp, queueIds); return { whatsapp, oldDefaultWhatsapp }; }; diff --git a/backend/src/services/WhatsappService/ListWhatsAppsService.ts b/backend/src/services/WhatsappService/ListWhatsAppsService.ts index 0ac127c..3d29c2c 100644 --- a/backend/src/services/WhatsappService/ListWhatsAppsService.ts +++ b/backend/src/services/WhatsappService/ListWhatsAppsService.ts @@ -1,24 +1,15 @@ import Queue from "../../models/Queue"; import Whatsapp from "../../models/Whatsapp"; -import WhatsappQueue from "../../models/WhatsappQueue"; const ListWhatsAppsService = async (): Promise => { const whatsapps = await Whatsapp.findAll({ include: [ { - model: WhatsappQueue, - as: "whatsappQueues", - attributes: ["optionNumber"], - include: [ - { - model: Queue, - as: "queue", - attributes: ["id", "name", "color", "greetingMessage"] - } - ] + model: Queue, + as: "queues", + attributes: ["id", "name", "color", "greetingMessage"] } - ], - order: [["whatsappQueues", "optionNumber", "ASC"]] + ] }); return whatsapps; diff --git a/backend/src/services/WhatsappService/ShowWhatsAppService.ts b/backend/src/services/WhatsappService/ShowWhatsAppService.ts index 5643032..235ef17 100644 --- a/backend/src/services/WhatsappService/ShowWhatsAppService.ts +++ b/backend/src/services/WhatsappService/ShowWhatsAppService.ts @@ -1,25 +1,17 @@ import Whatsapp from "../../models/Whatsapp"; import AppError from "../../errors/AppError"; import Queue from "../../models/Queue"; -import WhatsappQueue from "../../models/WhatsappQueue"; const ShowWhatsAppService = async (id: string | number): Promise => { const whatsapp = await Whatsapp.findByPk(id, { include: [ { - model: WhatsappQueue, - as: "whatsappQueues", - attributes: ["optionNumber"], - include: [ - { - model: Queue, - as: "queue", - attributes: ["id", "name", "color", "greetingMessage"] - } - ] + model: Queue, + as: "queues", + attributes: ["id", "name", "color", "greetingMessage"] } ], - order: [["whatsappQueues", "optionNumber", "ASC"]] + order: [["queues", "name", "ASC"]] }); if (!whatsapp) { diff --git a/backend/src/services/WhatsappService/UpdateWhatsAppService.ts b/backend/src/services/WhatsappService/UpdateWhatsAppService.ts index 417d8b2..d7a155c 100644 --- a/backend/src/services/WhatsappService/UpdateWhatsAppService.ts +++ b/backend/src/services/WhatsappService/UpdateWhatsAppService.ts @@ -6,17 +6,13 @@ import Whatsapp from "../../models/Whatsapp"; import ShowWhatsAppService from "./ShowWhatsAppService"; import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue"; -interface QueueData { - id: number; - optionNumber: number; -} interface WhatsappData { name?: string; status?: string; session?: string; isDefault?: boolean; greetingMessage?: string; - queuesData?: QueueData[]; + queueIds?: number[]; } interface Request { @@ -34,7 +30,7 @@ const UpdateWhatsAppService = async ({ whatsappId }: Request): Promise => { const schema = Yup.object().shape({ - name: Yup.string().min(2), + name: Yup.string().min(2).required(), isDefault: Yup.boolean() }); @@ -44,7 +40,7 @@ const UpdateWhatsAppService = async ({ isDefault, session, greetingMessage, - queuesData = [] + queueIds = [] } = whatsappData; try { @@ -53,6 +49,10 @@ const UpdateWhatsAppService = async ({ throw new AppError(err.message); } + if (queueIds.length > 1 && !greetingMessage) { + throw new AppError("ERR_WAPP_GREETING_REQUIRED"); + } + let oldDefaultWhatsapp: Whatsapp | null = null; if (isDefault) { @@ -74,9 +74,7 @@ const UpdateWhatsAppService = async ({ isDefault }); - await AssociateWhatsappQueue(whatsapp, queuesData); - - await whatsapp.reload(); + await AssociateWhatsappQueue(whatsapp, queueIds); return { whatsapp, oldDefaultWhatsapp }; }; diff --git a/frontend/src/components/WhatsAppModal/index.js b/frontend/src/components/WhatsAppModal/index.js index edead91..c525ab2 100644 --- a/frontend/src/components/WhatsAppModal/index.js +++ b/frontend/src/components/WhatsAppModal/index.js @@ -75,7 +75,7 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => { const { data } = await api.get(`whatsapp/${whatsAppId}`); setWhatsApp(data); - const whatsQueueIds = data.whatsappQueues?.map(q => q.queue.id); + const whatsQueueIds = data.queues?.map(queue => queue.id); setSelectedQueueIds(whatsQueueIds); } catch (err) { toastError(err); @@ -94,10 +94,10 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => { await api.post("/whatsapp", whatsappData); } toast.success(i18n.t("whatsappModal.success")); + handleClose(); } catch (err) { toastError(err); } - handleClose(); }; const handleClose = () => {