Skip to content

Commit

Permalink
Inclusão de whatsappId em api/messages/send
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh authored May 18, 2022
1 parent 297f9fc commit 4f98ba2
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions backend/src/controllers/ApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
);

Expand All @@ -59,6 +77,7 @@ const createContact = async (newContact: string) => {

export const index = async (req: Request, res: Response): Promise<Response> => {
const newContact: ContactData = req.body;
const { whatsappId }: WhatsappData = req.body;
const { body, quotedMsg }: MessageData = req.body;
const medias = req.files as Express.Multer.File[];

Expand All @@ -76,7 +95,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
throw new AppError(err.message);
}

const contactAndTicket = await createContact(newContact.number);
const contactAndTicket = await createContact(whatsappId, newContact.number);

if (medias) {
await Promise.all(
Expand All @@ -89,4 +108,4 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
}

return res.send();
};
};

0 comments on commit 4f98ba2

Please sign in to comment.