mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 04:39:20 +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 GetDefaultWhatsApp from "../helpers/GetDefaultWhatsApp";
|
||||||
import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
|
import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
|
||||||
import Message from "../models/Message";
|
import Message from "../models/Message";
|
||||||
|
import Whatsapp from "../models/Whatsapp";
|
||||||
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
||||||
import FindOrCreateTicketService from "../services/TicketServices/FindOrCreateTicketService";
|
import FindOrCreateTicketService from "../services/TicketServices/FindOrCreateTicketService";
|
||||||
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
||||||
@@ -13,6 +14,10 @@ import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
|||||||
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
|
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
|
||||||
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
||||||
|
|
||||||
|
type WhatsappData = {
|
||||||
|
whatsappId: number;
|
||||||
|
}
|
||||||
|
|
||||||
type MessageData = {
|
type MessageData = {
|
||||||
body: string;
|
body: string;
|
||||||
fromMe: boolean;
|
fromMe: boolean;
|
||||||
@@ -25,7 +30,7 @@ interface ContactData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const createContact = async (
|
const createContact = async (
|
||||||
userId: number,
|
whatsappId: number | undefined,
|
||||||
newContact: string
|
newContact: string
|
||||||
) => {
|
) => {
|
||||||
await CheckIsValidContact(newContact);
|
await CheckIsValidContact(newContact);
|
||||||
@@ -45,11 +50,21 @@ const createContact = async (
|
|||||||
|
|
||||||
const contact = await CreateOrUpdateContactService(contactData);
|
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(
|
const createTicket = await FindOrCreateTicketService(
|
||||||
contact,
|
contact,
|
||||||
defaultWhatsapp.id,
|
whatsapp.id,
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -62,6 +77,7 @@ const createContact = async (
|
|||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const newContact: ContactData = req.body;
|
const newContact: ContactData = req.body;
|
||||||
|
const { whatsappId }: WhatsappData = req.body;
|
||||||
const { body, quotedMsg }: MessageData = req.body;
|
const { body, quotedMsg }: MessageData = req.body;
|
||||||
const medias = req.files as Express.Multer.File[];
|
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);
|
throw new AppError(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const userId:number = parseInt(req.user.id);
|
const contactAndTicket = await createContact(whatsappId, newContact.number);
|
||||||
const contactAndTicket = await createContact(userId, newContact.number);
|
|
||||||
|
|
||||||
if (medias) {
|
if (medias) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
|
|||||||
Reference in New Issue
Block a user