mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
✨ Add parameter whatsappId in api/messages/send to choose
This commit is contained in:
@@ -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;
|
||||
@@ -25,7 +30,7 @@ interface ContactData {
|
||||
}
|
||||
|
||||
const createContact = async (
|
||||
userId: number,
|
||||
whatsappId: number | undefined,
|
||||
newContact: string
|
||||
) => {
|
||||
await CheckIsValidContact(newContact);
|
||||
@@ -45,11 +50,21 @@ const createContact = async (
|
||||
|
||||
const contact = await CreateOrUpdateContactService(contactData);
|
||||
|
||||
const defaultWhatsapp = await GetDefaultWhatsApp(userId);
|
||||
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
|
||||
);
|
||||
|
||||
@@ -62,6 +77,7 @@ const createContact = async (
|
||||
|
||||
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[];
|
||||
|
||||
@@ -79,8 +95,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
const userId:number = parseInt(req.user.id);
|
||||
const contactAndTicket = await createContact(userId, newContact.number);
|
||||
const contactAndTicket = await createContact(whatsappId, newContact.number);
|
||||
|
||||
if (medias) {
|
||||
await Promise.all(
|
||||
|
||||
Reference in New Issue
Block a user