diff --git a/backend/src/controllers/QueueController.ts b/backend/src/controllers/QueueController.ts index eeb3703..8f5aa81 100644 --- a/backend/src/controllers/QueueController.ts +++ b/backend/src/controllers/QueueController.ts @@ -71,9 +71,9 @@ export const listQueues = async ( }; export const store = async (req: Request, res: Response): Promise => { - const { name, color, greetingMessage } = req.body; + const { name, color, greetingMessage, cc } = req.body; - const queue = await CreateQueueService({ name, color, greetingMessage }); + const queue = await CreateQueueService({ name, color, greetingMessage, cc }); const io = getIO(); io.emit("queue", { diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 0a60271..fd6c193 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -77,6 +77,7 @@ import { botSendMessage } from "../services/WbotServices/wbotMessageListener"; import WhatsappQueue from "../models/WhatsappQueue"; import { get } from "../helpers/RedisClient"; import CountStatusChatEndService from "../services/StatusChatEndService/CountStatusChatEndService"; +import Queue from "../models/Queue"; export const index = async (req: Request, res: Response): Promise => { const { @@ -119,14 +120,15 @@ export const remoteTicketCreation = async ( req: Request, res: Response ): Promise => { - let { queueId, contact_from, contact_to, msg, contact_name }: any = req.body; + let { queueId, contact_from, cc, contact_to, msg, contact_name }: any = + req.body; let whatsappId: any; - if (!queueId && !contact_from) { - return res - .status(400) - .json({ error: `Property 'queueId' or 'contact_from' is required.` }); + if (!queueId && !contact_from && !cc) { + return res.status(400).json({ + error: `Property 'queueId' or 'contact_from' or 'cc' is required.` + }); } const validate = ["contact_to", "msg"]; @@ -182,6 +184,25 @@ export const remoteTicketCreation = async ( queueId = queues[0].id; whatsappId = id; + } else if (cc) { + const queue = await Queue.findOne({ where: { cc } }); + if (!queue) { + return res.status(404).json({ + msg: `Queue with cc ${cc} not found! ` + }); + } + + queueId = queue.id; + + const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED"); + + if ( whatsapps.length === 0) { + return res.status(500).json({ + msg: `No WhatsApp found for this cc ${cc} or the WhatsApp number is disconnected! ` + }); + } + + whatsappId = whatsapps[0].id; } // const validNumber = await CheckIsValidContact(contact_to, true); diff --git a/backend/src/database/migrations/20240403202639-add-cc-to-Queues.ts b/backend/src/database/migrations/20240403202639-add-cc-to-Queues.ts new file mode 100644 index 0000000..60cf4ff --- /dev/null +++ b/backend/src/database/migrations/20240403202639-add-cc-to-Queues.ts @@ -0,0 +1,14 @@ +import { QueryInterface, DataTypes } from "sequelize"; + +module.exports = { + up: (queryInterface: QueryInterface) => { + return queryInterface.addColumn("Queues", "cc", { + type: DataTypes.STRING, + allowNull: true + }); + }, + + down: (queryInterface: QueryInterface) => { + return queryInterface.removeColumn("Queues", "cc"); + } +}; diff --git a/backend/src/models/Queue.ts b/backend/src/models/Queue.ts index c5c06d9..45ae22f 100644 --- a/backend/src/models/Queue.ts +++ b/backend/src/models/Queue.ts @@ -36,6 +36,9 @@ class Queue extends Model { @Column greetingMessage: string; + @Column + cc: string; + @CreatedAt createdAt: Date; diff --git a/backend/src/services/QueueService/CreateQueueService.ts b/backend/src/services/QueueService/CreateQueueService.ts index 528d1b1..d13b5cc 100644 --- a/backend/src/services/QueueService/CreateQueueService.ts +++ b/backend/src/services/QueueService/CreateQueueService.ts @@ -7,6 +7,7 @@ interface QueueData { name: string; color: string; greetingMessage?: string; + cc?: string; } const CreateQueueService = async (queueData: QueueData): Promise => { diff --git a/backend/src/services/QueueService/UpdateQueueService.ts b/backend/src/services/QueueService/UpdateQueueService.ts index 59a6077..6f78880 100644 --- a/backend/src/services/QueueService/UpdateQueueService.ts +++ b/backend/src/services/QueueService/UpdateQueueService.ts @@ -3,12 +3,13 @@ import * as Yup from "yup"; import AppError from "../../errors/AppError"; import Queue from "../../models/Queue"; import ShowQueueService from "./ShowQueueService"; -import { set } from "../../helpers/RedisClient" +import { set } from "../../helpers/RedisClient"; interface QueueData { name?: string; color?: string; greetingMessage?: string; + cc?: string; } const UpdateQueueService = async ( diff --git a/backend/src/services/WhatsappService/ListWhatsAppsForQueueService.ts b/backend/src/services/WhatsappService/ListWhatsAppsForQueueService.ts index 836ef9e..63d6c91 100644 --- a/backend/src/services/WhatsappService/ListWhatsAppsForQueueService.ts +++ b/backend/src/services/WhatsappService/ListWhatsAppsForQueueService.ts @@ -10,9 +10,9 @@ const ListWhatsAppsForQueueService = async ( queueId: number | string, status?: string ): Promise => { - let distinctWhatsapps: any; + let distinctWhatsapps: any[] = []; - if (status) { + if (status) { distinctWhatsapps = await sequelize.query( `SELECT w.id, w.number, w.status, wq.whatsappId, wq.queueId FROM Whatsapps w JOIN WhatsappQueues wq ON w.id = wq.whatsappId AND wq.queueId = ${queueId} AND w.status = '${status}' diff --git a/frontend/src/components/QueueModal/index.js b/frontend/src/components/QueueModal/index.js index fa5caa9..3baaa77 100644 --- a/frontend/src/components/QueueModal/index.js +++ b/frontend/src/components/QueueModal/index.js @@ -70,6 +70,7 @@ const QueueModal = ({ open, onClose, queueId }) => { name: "", color: "", greetingMessage: "", + cc:"" }; const [colorPickerModalOpen, setColorPickerModalOpen] = useState(false); @@ -94,6 +95,7 @@ const QueueModal = ({ open, onClose, queueId }) => { name: "", color: "", greetingMessage: "", + cc: "" }); }; }, [queueId, open]); @@ -213,6 +215,19 @@ const QueueModal = ({ open, onClose, queueId }) => { margin="dense" /> +
+ +