Compare commits
No commits in common. "d25d296498d0a74413da6209894150622dc24287" and "5abab2ef05fe5d7dadd05db00b4bd73ee58c86ab" have entirely different histories.
d25d296498
...
5abab2ef05
|
@ -68,13 +68,6 @@ import ListWhatsAppsForQueueService from "../services/WhatsappService/ListWhatsA
|
||||||
import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber";
|
import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber";
|
||||||
import Whatsapp from "../models/Whatsapp";
|
import Whatsapp from "../models/Whatsapp";
|
||||||
import AppError from "../errors/AppError";
|
import AppError from "../errors/AppError";
|
||||||
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
|
||||||
import FindOrCreateTicketService from "../services/TicketServices/FindOrCreateTicketService";
|
|
||||||
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
|
||||||
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
|
||||||
import CreateContactService from "../services/ContactServices/CreateContactService";
|
|
||||||
import { botSendMessage } from "../services/WbotServices/wbotMessageListener";
|
|
||||||
import WhatsappQueue from "../models/WhatsappQueue";
|
|
||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const {
|
const {
|
||||||
|
@ -113,128 +106,6 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
return res.status(200).json({ tickets, count, hasMore });
|
return res.status(200).json({ tickets, count, hasMore });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const remoteTicketCreation = async (
|
|
||||||
req: Request,
|
|
||||||
res: Response
|
|
||||||
): Promise<Response> => {
|
|
||||||
const { contact_from, contact_to, msg, contact_name }: any = req.body;
|
|
||||||
|
|
||||||
const validate = ["contact_from", "contact_to", "msg"];
|
|
||||||
|
|
||||||
for (let prop of validate) {
|
|
||||||
if (!req.body[prop])
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.json({ error: `Property '${prop}' is undefined.` });
|
|
||||||
}
|
|
||||||
|
|
||||||
const whatsapp = await Whatsapp.findOne({
|
|
||||||
where: { number: contact_from, status: "CONNECTED" }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (whatsapp) {
|
|
||||||
const { id: whatsappId, number, status } = whatsapp;
|
|
||||||
|
|
||||||
const queue: any = await WhatsappQueue.findOne({
|
|
||||||
where: { whatsappId },
|
|
||||||
attributes: ["queueId"]
|
|
||||||
});
|
|
||||||
|
|
||||||
const { queueId } = queue;
|
|
||||||
|
|
||||||
// const validNumber = await CheckIsValidContact(contact_to, true);
|
|
||||||
const validNumber = contact_to;
|
|
||||||
|
|
||||||
if (validNumber) {
|
|
||||||
let contact = await Contact.findOne({ where: { number: validNumber } });
|
|
||||||
|
|
||||||
if (!contact) {
|
|
||||||
// const profilePicUrl = await GetProfilePicUrl(validNumber);
|
|
||||||
|
|
||||||
contact = await CreateContactService({
|
|
||||||
name: contact_name ? contact_name : contact_to,
|
|
||||||
number: validNumber
|
|
||||||
// profilePicUrl
|
|
||||||
});
|
|
||||||
|
|
||||||
const io = getIO();
|
|
||||||
io.emit("contact", {
|
|
||||||
action: "create",
|
|
||||||
contact
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id: contactId } = contact;
|
|
||||||
|
|
||||||
const botInfo = await BotIsOnQueue("botqueue");
|
|
||||||
|
|
||||||
let ticket = await Ticket.findOne({
|
|
||||||
where: {
|
|
||||||
[Op.or]: [
|
|
||||||
{ contactId, status: "queueChoice" },
|
|
||||||
{ contactId, status: "open", userId: botInfo.userIdBot }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (getSettingValue("whatsaAppCloudApi")?.value == "enabled") {
|
|
||||||
if (ticket) {
|
|
||||||
await UpdateTicketService({
|
|
||||||
ticketData: { status: "closed" },
|
|
||||||
ticketId: ticket.id
|
|
||||||
});
|
|
||||||
ticket = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (ticket) {
|
|
||||||
await UpdateTicketService({
|
|
||||||
ticketData: { status: "closed" },
|
|
||||||
ticketId: ticket.id
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ticket) {
|
|
||||||
ticket = await FindOrCreateTicketService(
|
|
||||||
contact,
|
|
||||||
whatsappId,
|
|
||||||
0,
|
|
||||||
undefined,
|
|
||||||
queueId
|
|
||||||
);
|
|
||||||
botSendMessage(ticket, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
const io = getIO();
|
|
||||||
io.to(ticket.status).emit("ticket", {
|
|
||||||
action: "update",
|
|
||||||
ticket
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 200 | MSG: success`
|
|
||||||
);
|
|
||||||
return res.status(200).json({ msg: "success" });
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 500 | MSG: The number ${contact_to} does not exist on WhatsApp`
|
|
||||||
);
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({ msg: `The number ${contact_to} does not exist on WhatsApp` });
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 500 | MSG: Whatsapp number ${contact_from} disconnected or it doesn't exist in omnihit`
|
|
||||||
);
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({
|
|
||||||
msg: `Whatsapp number ${contact_from} disconnected or it doesn't exist in omnihit`
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { contactId, status, userId, msg, queueId, whatsappId }: TicketData =
|
const { contactId, status, userId, msg, queueId, whatsappId }: TicketData =
|
||||||
req.body;
|
req.body;
|
||||||
|
@ -555,3 +426,10 @@ export const remove = async (
|
||||||
|
|
||||||
return res.status(200).json({ message: "ticket deleted" });
|
return res.status(200).json({ message: "ticket deleted" });
|
||||||
};
|
};
|
||||||
|
// export async function setMessageAsRead(ticket: Ticket) {
|
||||||
|
// const wbot_url = await getWbot(ticket.whatsappId);
|
||||||
|
|
||||||
|
// console.log('wbot_url: ', wbot_url, ' | ticket.contact.number: ', ticket.contact.number);
|
||||||
|
|
||||||
|
// await endPointQuery(`${wbot_url}/api/sendSeen`, { number: `${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us` });
|
||||||
|
// }
|
||||||
|
|
|
@ -13,23 +13,16 @@ interface TokenPayload {
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAuth = (req: Request, res: Response, next: NextFunction): void => {
|
const isAuth = (req: Request, res: Response, next: NextFunction): void => {
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
|
|
||||||
if (!authHeader) {
|
if (!authHeader) {
|
||||||
throw new AppError("ERR_SESSION_EXPIRED", 401);
|
throw new AppError("ERR_SESSION_EXPIRED", 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [, token] = authHeader.split(" ");
|
const [, token] = authHeader.split(" ");
|
||||||
|
|
||||||
if (
|
|
||||||
req.originalUrl == "/tickets/remote/create" &&
|
|
||||||
token === process.env.TOKEN_REMOTE_TICKET_CREATION
|
|
||||||
) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const decoded = verify(token, authConfig.secret);
|
const decoded = verify(token, authConfig.secret);
|
||||||
|
|
||||||
const { id, profile } = decoded as TokenPayload;
|
const { id, profile } = decoded as TokenPayload;
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,6 @@ ticketRoutes.get("/tickets", isAuth, TicketController.index);
|
||||||
|
|
||||||
ticketRoutes.get("/tickets/:ticketId", isAuth, TicketController.show);
|
ticketRoutes.get("/tickets/:ticketId", isAuth, TicketController.show);
|
||||||
|
|
||||||
ticketRoutes.post(
|
|
||||||
"/tickets/remote/create", isAuth,
|
|
||||||
TicketController.remoteTicketCreation
|
|
||||||
);
|
|
||||||
|
|
||||||
ticketRoutes.post("/tickets", isAuth, TicketController.store);
|
ticketRoutes.post("/tickets", isAuth, TicketController.store);
|
||||||
|
|
||||||
ticketRoutes.put("/tickets/:ticketId", isAuth, TicketController.update);
|
ticketRoutes.put("/tickets/:ticketId", isAuth, TicketController.update);
|
||||||
|
|
|
@ -13,12 +13,15 @@ const FindOrCreateTicketService = async (
|
||||||
contact: Contact,
|
contact: Contact,
|
||||||
whatsappId: number,
|
whatsappId: number,
|
||||||
unreadMessages: number,
|
unreadMessages: number,
|
||||||
groupContact?: Contact,
|
groupContact?: Contact,
|
||||||
queueId?: number | string
|
|
||||||
): Promise<Ticket> => {
|
): Promise<Ticket> => {
|
||||||
try {
|
try {
|
||||||
let ticket;
|
let ticket;
|
||||||
|
|
||||||
|
// else if (getSettingValue("whatsaAppCloudApi")?.value == "enabled") {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
if (getSettingValue("oneContactChatWithManyWhats")?.value == "enabled") {
|
if (getSettingValue("oneContactChatWithManyWhats")?.value == "enabled") {
|
||||||
let whats = await ListWhatsAppsNumber(whatsappId);
|
let whats = await ListWhatsAppsNumber(whatsappId);
|
||||||
|
|
||||||
|
@ -42,8 +45,7 @@ const FindOrCreateTicketService = async (
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const { queues, greetingMessage, phoneNumberId } =
|
const { queues, greetingMessage, phoneNumberId } = await ShowWhatsAppService(whatsappId);
|
||||||
await ShowWhatsAppService(whatsappId);
|
|
||||||
|
|
||||||
const botInfo = { isOnQueue: false };
|
const botInfo = { isOnQueue: false };
|
||||||
|
|
||||||
|
@ -95,7 +97,7 @@ const FindOrCreateTicketService = async (
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
let status = "pending";
|
let status = "pending";
|
||||||
|
|
||||||
if (queues.length > 1 && !botInfo.isOnQueue && !queueId) {
|
if (queues.length > 1 && !botInfo.isOnQueue) {
|
||||||
status = "queueChoice";
|
status = "queueChoice";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +105,6 @@ const FindOrCreateTicketService = async (
|
||||||
contactId: groupContact ? groupContact.id : contact.id,
|
contactId: groupContact ? groupContact.id : contact.id,
|
||||||
status: status,
|
status: status,
|
||||||
isGroup: !!groupContact,
|
isGroup: !!groupContact,
|
||||||
queueId,
|
|
||||||
unreadMessages,
|
unreadMessages,
|
||||||
whatsappId,
|
whatsappId,
|
||||||
phoneNumberId
|
phoneNumberId
|
||||||
|
|
|
@ -3,7 +3,7 @@ import endPointQuery from "../../helpers/EndPointQuery";
|
||||||
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
||||||
import { getWbot } from "../../libs/wbot";
|
import { getWbot } from "../../libs/wbot";
|
||||||
|
|
||||||
const CheckIsValidContact = async (number: string, ignoreThrow?:boolean): Promise<any> => {
|
const CheckIsValidContact = async (number: string): Promise<any> => {
|
||||||
|
|
||||||
const defaultWhatsapp = await GetDefaultWhatsApp({});
|
const defaultWhatsapp = await GetDefaultWhatsApp({});
|
||||||
|
|
||||||
|
@ -11,8 +11,6 @@ const CheckIsValidContact = async (number: string, ignoreThrow?:boolean): Promis
|
||||||
|
|
||||||
const isValidNumber = await endPointQuery(`${wbot_url}/api/validate`, { mobile: `${number}`, })
|
const isValidNumber = await endPointQuery(`${wbot_url}/api/validate`, { mobile: `${number}`, })
|
||||||
|
|
||||||
if(ignoreThrow) return isValidNumber?.data?.number;
|
|
||||||
|
|
||||||
// console.log('isValidNumber.data.number: ', isValidNumber.data.number)
|
// console.log('isValidNumber.data.number: ', isValidNumber.data.number)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1134,8 +1134,7 @@ export {
|
||||||
verifyMediaMessage,
|
verifyMediaMessage,
|
||||||
verifyContact,
|
verifyContact,
|
||||||
isValidMsg,
|
isValidMsg,
|
||||||
mediaTypeWhatsappOfficial,
|
mediaTypeWhatsappOfficial
|
||||||
botSendMessage
|
|
||||||
};
|
};
|
||||||
async function whatsappInfo(whatsappId: string | number) {
|
async function whatsappInfo(whatsappId: string | number) {
|
||||||
return await Whatsapp.findByPk(whatsappId);
|
return await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
Loading…
Reference in New Issue