From 4f98ba2ccd231ec9b72ae44704fcb3761e86ba6c Mon Sep 17 00:00:00 2001 From: Robson Tenorio Date: Wed, 18 May 2022 20:50:37 -0300 Subject: [PATCH] =?UTF-8?q?Inclus=C3=A3o=20de=20whatsappId=20em=20api/mess?= =?UTF-8?q?ages/send?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/ApiController.ts | 29 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/backend/src/controllers/ApiController.ts b/backend/src/controllers/ApiController.ts index 33fb7df2..b5803de7 100644 --- a/backend/src/controllers/ApiController.ts +++ b/backend/src/controllers/ApiController.ts @@ -4,6 +4,7 @@ import AppError from "../errors/AppError"; import GetDefaultWhatsApp from "../helpers/GetDefaultWhatsApp"; import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead"; import Message from "../models/Message"; +import Whatsapp from "../models/Whatsapp"; import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService"; import FindOrCreateTicketService from "../services/TicketServices/FindOrCreateTicketService"; import ShowTicketService from "../services/TicketServices/ShowTicketService"; @@ -13,6 +14,10 @@ import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl"; import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia"; import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage"; +type WhatsappData = { + whatsappId: number; +} + type MessageData = { body: string; fromMe: boolean; @@ -24,7 +29,10 @@ interface ContactData { number: string; } -const createContact = async (newContact: string) => { +const createContact = async ( + whatsappId: number | undefined, + newContact: string +) => { await CheckIsValidContact(newContact); const validNumber: any = await CheckContactNumber(newContact); @@ -42,11 +50,21 @@ const createContact = async (newContact: string) => { const contact = await CreateOrUpdateContactService(contactData); - const defaultWhatsapp = await GetDefaultWhatsApp(); + let whatsapp:Whatsapp | null; + + if(whatsappId === undefined) { + whatsapp = await GetDefaultWhatsApp(); + } else { + whatsapp = await Whatsapp.findByPk(whatsappId); + + if(whatsapp === null) { + throw new AppError(`whatsapp #${whatsappId} not found`); + } + } const createTicket = await FindOrCreateTicketService( contact, - defaultWhatsapp.id, + whatsapp.id, 1 ); @@ -59,6 +77,7 @@ const createContact = async (newContact: string) => { export const index = async (req: Request, res: Response): Promise => { const newContact: ContactData = req.body; + const { whatsappId }: WhatsappData = req.body; const { body, quotedMsg }: MessageData = req.body; const medias = req.files as Express.Multer.File[]; @@ -76,7 +95,7 @@ export const index = async (req: Request, res: Response): Promise => { throw new AppError(err.message); } - const contactAndTicket = await createContact(newContact.number); + const contactAndTicket = await createContact(whatsappId, newContact.number); if (medias) { await Promise.all( @@ -89,4 +108,4 @@ export const index = async (req: Request, res: Response): Promise => { } return res.send(); -}; +}; \ No newline at end of file