Compare commits

..

No commits in common. "5366f9943b6da445947a35f90df857411c91ac27" and "279c4697ddcf97a0f382f0fe5d1c82cec4dbffb9" have entirely different histories.

14 changed files with 96 additions and 186 deletions

View File

@ -21,7 +21,6 @@ import sendWhatsAppMessageOfficialAPI from "../helpers/sendWhatsAppMessageOffici
import Whatsapp from "../models/Whatsapp"; import Whatsapp from "../models/Whatsapp";
import checkLastClientMsg24hs from "../helpers/CheckLastClientMsg24hs"; import checkLastClientMsg24hs from "../helpers/CheckLastClientMsg24hs";
import AppError from "../errors/AppError"; import AppError from "../errors/AppError";
import { get } from "../helpers/RedisClient";
type IndexQuery = { type IndexQuery = {
pageNumber: string; pageNumber: string;
@ -36,7 +35,7 @@ type MessageData = {
params: any; params: any;
}; };
export const index = async (req: Request, res: Response): Promise<Response> => { export const index = async (req: Request, res: Response): Promise<Response> => {
const { ticketId } = req.params; const { ticketId } = req.params;
const { pageNumber } = req.query as IndexQuery; const { pageNumber } = req.query as IndexQuery;
@ -98,8 +97,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
} }
const name = params.find((p: any) => p?.template_name); const name = params.find((p: any) => p?.template_name);
const { language }: any = const { language }: any = params?.find((p: any) => p?.language) || 'pt_BR'
params?.find((p: any) => p?.language) || "pt_BR";
const { template_name } = name; const { template_name } = name;

View File

@ -5,12 +5,14 @@ import DeleteQueueService from "../services/QueueService/DeleteQueueService";
import ListQueuesService from "../services/QueueService/ListQueuesService"; import ListQueuesService from "../services/QueueService/ListQueuesService";
import ShowQueueService from "../services/QueueService/ShowQueueService"; import ShowQueueService from "../services/QueueService/ShowQueueService";
import UpdateQueueService from "../services/QueueService/UpdateQueueService"; import UpdateQueueService from "../services/QueueService/UpdateQueueService";
import Queue from "../models/Queue"; import Queue from "../models/Queue"
import AppError from "../errors/AppError"; import AppError from "../errors/AppError"
import { del, get, set } from "../helpers/RedisClient"; import { get, set } from "../helpers/RedisClient";
export const index = async (req: Request, res: Response): Promise<Response> => { export const index = async (req: Request, res: Response): Promise<Response> => {
const queues = await ListQueuesService(); const queues = await ListQueuesService();
return res.status(200).json(queues); return res.status(200).json(queues);
}; };
@ -123,7 +125,7 @@ export const customization = async (
await set("ura", ura); await set("ura", ura);
const _ura = await get({ key: "ura", parse: true }); const _ura = await get("ura");
console.log("_URA: ", _ura); console.log("_URA: ", _ura);
return res.status(200).json({ new_queues }); return res.status(200).json({ new_queues });
@ -162,8 +164,6 @@ export const remove = async (
await DeleteQueueService(queueId); await DeleteQueueService(queueId);
await del(`queue:${queueId}`);
const io = getIO(); const io = getIO();
io.emit("queue", { io.emit("queue", {
action: "delete", action: "delete",

View File

@ -75,9 +75,8 @@ import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
import CreateContactService from "../services/ContactServices/CreateContactService"; import CreateContactService from "../services/ContactServices/CreateContactService";
import { botSendMessage } from "../services/WbotServices/wbotMessageListener"; import { botSendMessage } from "../services/WbotServices/wbotMessageListener";
import WhatsappQueue from "../models/WhatsappQueue"; import WhatsappQueue from "../models/WhatsappQueue";
import { get } from "../helpers/RedisClient"
export const index = async (req: Request, res: Response): Promise<Response> => { export const index = async (req: Request, res: Response): Promise<Response> => {
const { const {
pageNumber, pageNumber,
status, status,

View File

@ -230,10 +230,7 @@ export const weebhook = async (
req.body.entry[0].changes[0].value.metadata.display_phone_number; req.body.entry[0].changes[0].value.metadata.display_phone_number;
let type = message.type; let type = message.type;
const contact_to_exist = await get({ const contact_to_exist = await get("whatsapp:*", `${contact_to}`);
key: "whatsapp:*",
value: `${contact_to}`
});
if (contact_to_exist == null) { if (contact_to_exist == null) {
console.log( console.log(
@ -411,15 +408,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}` client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}`
}); });
} else { } else {
await set( await set(`whatsapp:${whatsapp.id}`, `${number}`);
`whatsapp:${whatsapp.id}`,
JSON.stringify({
number: whatsapp?.number,
id: whatsapp?.id,
greetingMessage: whatsapp?.greetingMessage,
phoneNumberId: whatsapp?.phoneNumberId
})
);
} }
const io = getIO(); const io = getIO();
@ -495,15 +484,7 @@ export const update = async (
client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}` client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}`
}); });
} else { } else {
await set( await set(`whatsapp:${whatsapp.id}`, `${number}`);
`whatsapp:${whatsapp.id}`,
JSON.stringify({
number: whatsapp?.number,
id: whatsapp?.id,
greetingMessage: whatsapp?.greetingMessage,
phoneNumberId: whatsapp?.phoneNumberId
})
);
} }
const io = getIO(); const io = getIO();

View File

@ -1,15 +0,0 @@
import { QueryInterface, DataTypes } from "sequelize";
module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Messages", "fromAgent", {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
});
},
down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Messages", "fromAgent");
}
};

View File

@ -4,8 +4,9 @@ const fs = require("fs");
import ListUsersService from "../services/UserServices/ListUsersService"; import ListUsersService from "../services/UserServices/ListUsersService";
import { get } from "./RedisClient"; import { get } from "./RedisClient";
const _botIsOnQueue = async (botName: string) => { const _botIsOnQueue = async (botName: string) => {
const botInfo = await get({ key: "botInfo", parse: true });
const botInfo = await get("botInfo");
if ( if (
botInfo && botInfo &&
@ -18,8 +19,8 @@ const _botIsOnQueue = async (botName: string) => {
botQueueId: botInfo.queueId, botQueueId: botInfo.queueId,
isOnQueue: botInfo.botIsOnQueue isOnQueue: botInfo.botIsOnQueue
}; };
} }
return { userIdBot: null, botQueueId: null, isOnQueue: false }; return { userIdBot: null, botQueueId: null, isOnQueue: false };
}; };
export default _botIsOnQueue; export default _botIsOnQueue;

View File

@ -8,12 +8,6 @@ type WhatsappData = {
value?: string; value?: string;
}; };
type getData = {
key: string;
value?: string;
parse?: boolean;
};
export async function set(key: string, value: string | object) { export async function set(key: string, value: string | object) {
if (typeof value == "object") await redis.set(key, JSON.stringify(value)); if (typeof value == "object") await redis.set(key, JSON.stringify(value));
else { else {
@ -21,30 +15,21 @@ export async function set(key: string, value: string | object) {
} }
} }
export async function getSimple(key: string) { export async function get(key: string, value?: string) {
const value: any = await redis.get(key);
return value;
}
export async function get({ key, value, parse }: getData) {
if (key.includes("*")) { if (key.includes("*")) {
const keys = await redis.keys(key); const keys = await redis.keys(key);
// If there are keys, delete them
if (keys.length > 0) { if (keys.length > 0) {
for (const key of keys) { for (const key of keys) {
const val = await redis.get(key); const val = await redis.get(key);
if (val.includes(value)) { if (value == val) return value;
if (parse) return JSON.parse(val);
return val;
}
} }
} }
return null; return null;
} else { } else {
const value: any = await redis.get(key); const value: any = await redis.get(key);
return JSON.parse(value);
if (parse) return JSON.parse(value);
return value;
} }
} }

View File

@ -31,10 +31,6 @@ class Message extends Model<Message> {
@Column @Column
fromMe: boolean; fromMe: boolean;
@Default(false)
@Column
fromAgent: boolean;
@Column(DataType.TEXT) @Column(DataType.TEXT)
body: string; body: string;

View File

@ -27,7 +27,6 @@ import { clearAllKeys, get, set } from "./helpers/RedisClient";
import ShowUserService from "./services/UserServices/ShowUserService"; import ShowUserService from "./services/UserServices/ShowUserService";
import { json } from "sequelize"; import { json } from "sequelize";
import { setBotInfo } from "./helpers/SetBotInfo"; import { setBotInfo } from "./helpers/SetBotInfo";
import Queue from "./models/Queue";
const server = app.listen(process.env.PORT, () => { const server = app.listen(process.env.PORT, () => {
logger.info(`Server started on port: ${process.env.PORT}`); logger.info(`Server started on port: ${process.env.PORT}`);
@ -48,7 +47,7 @@ gracefulShutdown(server);
(async () => { (async () => {
console.log("os.tmpdir(): ", os.tmpdir()); console.log("os.tmpdir(): ", os.tmpdir());
await clearAllKeys("user:*", "whatsapp:*", "queue:*"); await clearAllKeys("user:*", "whatsapp:*");
const users = await User.findAll(); const users = await User.findAll();
@ -63,37 +62,23 @@ gracefulShutdown(server);
await set(`user:${id}`, { id, name }); await set(`user:${id}`, { id, name });
} }
// const queues = await Queue.findAll();
// for (const queue of queues) {
// const { id, greetingMessage, name } = queue;
// await set(`queue:${id}`, { id, name, greetingMessage });
// }
loadSettings(); loadSettings();
let whatsapps: any = await Whatsapp.findAll({ let whatsapps: any = await Whatsapp.findAll({
attributes: ["id", "url", "phoneNumberId", "number", "greetingMessage"] attributes: ["id", "url", "phoneNumberId", "number"]
}); });
if (whatsapps && whatsapps.length > 0) { if (whatsapps && whatsapps.length > 0) {
for (let i = 0; i < whatsapps.length; i++) { for (let i = 0; i < whatsapps.length; i++) {
try { try {
const { phoneNumberId, id, greetingMessage } = whatsapps[i]; const { phoneNumberId } = whatsapps[i];
if (phoneNumberId) { if (phoneNumberId) {
await set( await set(
`whatsapp:${whatsapps[i].dataValues.id}`, `whatsapp:${whatsapps[i].dataValues.id}`,
JSON.stringify({ `${whatsapps[i].dataValues.number}`
number: whatsapps[i].dataValues.number,
id,
greetingMessage,
phoneNumberId
})
); );
}
if (phoneNumberId) {
continue; continue;
} }

View File

@ -13,7 +13,6 @@ interface MessageData {
read?: boolean; read?: boolean;
mediaType?: string; mediaType?: string;
mediaUrl?: string; mediaUrl?: string;
fromAgent?: boolean;
} }
interface Request { interface Request {
messageData: MessageData; messageData: MessageData;

View File

@ -1,7 +1,6 @@
import * as Yup from "yup"; import * as Yup from "yup";
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import Queue from "../../models/Queue"; import Queue from "../../models/Queue";
import { set } from "../../helpers/RedisClient";
interface QueueData { interface QueueData {
name: string; name: string;
@ -10,67 +9,68 @@ interface QueueData {
} }
const CreateQueueService = async (queueData: QueueData): Promise<Queue> => { const CreateQueueService = async (queueData: QueueData): Promise<Queue> => {
try { try {
const { color, name } = queueData; const { color, name } = queueData;
const queueSchema = Yup.object().shape({ const queueSchema = Yup.object().shape({
name: Yup.string() name: Yup.string()
.min(2, "ERR_QUEUE_INVALID_NAME") .min(2, "ERR_QUEUE_INVALID_NAME")
.required("ERR_QUEUE_INVALID_NAME") .required("ERR_QUEUE_INVALID_NAME")
.test( .test(
"Check-unique-name", "Check-unique-name",
"ERR_QUEUE_NAME_ALREADY_EXISTS", "ERR_QUEUE_NAME_ALREADY_EXISTS",
async value => { async value => {
if (value) {
const queueWithSameName = await Queue.findOne({
where: { name: value }
});
return !queueWithSameName;
}
return false;
}
),
color: Yup.string()
.required("ERR_QUEUE_INVALID_COLOR")
.test("Check-color", "ERR_QUEUE_INVALID_COLOR", async value => {
if (value) { if (value) {
const colorTestRegex = /^#[0-9a-f]{3,6}$/i; const queueWithSameName = await Queue.findOne({
return colorTestRegex.test(value); where: { name: value }
});
return !queueWithSameName;
} }
return false; return false;
}) }
.test( ),
"Check-color-exists", color: Yup.string()
"ERR_QUEUE_COLOR_ALREADY_EXISTS", .required("ERR_QUEUE_INVALID_COLOR")
async value => { .test("Check-color", "ERR_QUEUE_INVALID_COLOR", async value => {
if (value) { if (value) {
const queueWithSameColor = await Queue.findOne({ const colorTestRegex = /^#[0-9a-f]{3,6}$/i;
where: { color: value } return colorTestRegex.test(value);
}); }
return !queueWithSameColor; return false;
} })
return false; .test(
"Check-color-exists",
"ERR_QUEUE_COLOR_ALREADY_EXISTS",
async value => {
if (value) {
const queueWithSameColor = await Queue.findOne({
where: { color: value }
});
return !queueWithSameColor;
} }
) return false;
}); }
)
});
try { try {
await queueSchema.validate({ color, name }); await queueSchema.validate({ color, name });
} catch (err: any) { } catch (err: any) {
throw new AppError(err.message); throw new AppError(err.message);
} }
const queue = await Queue.create(queueData); const queue = await Queue.create(queueData);
// const { id, greetingMessage } = queue; return queue;
// await set(`queue:${id}`, { id, name, greetingMessage });
return queue;
} catch (error: any) { } catch (error: any) {
console.error("===> Error on CreateQueueService.ts file: \n", error); console.error('===> Error on CreateQueueService.ts file: \n', error)
throw new AppError(error.message); throw new AppError(error.message);
} }
}; };
export default CreateQueueService; export default CreateQueueService;

View File

@ -3,7 +3,6 @@ import * as Yup from "yup";
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import Queue from "../../models/Queue"; import Queue from "../../models/Queue";
import ShowQueueService from "./ShowQueueService"; import ShowQueueService from "./ShowQueueService";
import { set } from "../../helpers/RedisClient"
interface QueueData { interface QueueData {
name?: string; name?: string;
@ -15,7 +14,9 @@ const UpdateQueueService = async (
queueId: number | string, queueId: number | string,
queueData: QueueData queueData: QueueData
): Promise<Queue> => { ): Promise<Queue> => {
try { try {
const { color, name } = queueData; const { color, name } = queueData;
const queueSchema = Yup.object().shape({ const queueSchema = Yup.object().shape({
@ -29,7 +30,7 @@ const UpdateQueueService = async (
const queueWithSameName = await Queue.findOne({ const queueWithSameName = await Queue.findOne({
where: { name: value, id: { [Op.not]: queueId } } where: { name: value, id: { [Op.not]: queueId } }
}); });
return !queueWithSameName; return !queueWithSameName;
} }
return true; return true;
@ -58,25 +59,24 @@ const UpdateQueueService = async (
} }
) )
}); });
try { try {
await queueSchema.validate({ color, name }); await queueSchema.validate({ color, name });
} catch (err: any) { } catch (err: any) {
throw new AppError(err.message); throw new AppError(err.message);
} }
const queue = await ShowQueueService(queueId); const queue = await ShowQueueService(queueId);
await queue.update(queueData); await queue.update(queueData);
// const { id, greetingMessage } = queue;
// await set(`queue:${id}`, { id, name, greetingMessage });
return queue; return queue;
} catch (error: any) { } catch (error: any) {
console.error("===> Error on UpdateQueueService.ts file: \n", error); console.error('===> Error on UpdateQueueService.ts file: \n', error)
throw new AppError(error.message); throw new AppError(error.message);
} }
}; };
export default UpdateQueueService; export default UpdateQueueService;

View File

@ -29,7 +29,9 @@ const CheckIsValidContact = async (
try { try {
let _status: any; let _status: any;
if (!isValidNumber) { if (!isValidNumber) {
console.log('kkkkkkkkkkkkkkkkkkkkkkkkkkkkk ')
const { data, status } = await axios.post( const { data, status } = await axios.post(
`${process.env.WHATS_NUMBER_VALIDATOR_URL}/api/validate`, `${process.env.WHATS_NUMBER_VALIDATOR_URL}/api/validate`,

View File

@ -92,8 +92,7 @@ import {
createObject, createObject,
findByContain, findByContain,
findObject, findObject,
get, get
getSimple
} from "../../helpers/RedisClient"; } from "../../helpers/RedisClient";
import FindOrCreateTicketServiceBot from "../TicketServices/FindOrCreateTicketServiceBot"; import FindOrCreateTicketServiceBot from "../TicketServices/FindOrCreateTicketServiceBot";
import ShowTicketService from "../TicketServices/ShowTicketService"; import ShowTicketService from "../TicketServices/ShowTicketService";
@ -176,14 +175,9 @@ const verifyMediaMessage = async (
mediaUrl: media.filename, mediaUrl: media.filename,
mediaType: media.mimetype.split("/")[0], mediaType: media.mimetype.split("/")[0],
quotedMsgId: quotedMsg, quotedMsgId: quotedMsg,
phoneNumberId: msg?.phoneNumberId, phoneNumberId: msg?.phoneNumberId
fromAgent: false
}; };
if (msg?.fromMe) {
messageData = { ...messageData, fromAgent: true };
}
if (!ticket?.phoneNumberId) { if (!ticket?.phoneNumberId) {
if (!media.filename) { if (!media.filename) {
const ext = media.mimetype.split("/")[1].split(";")[0]; const ext = media.mimetype.split("/")[1].split(";")[0];
@ -286,33 +280,18 @@ const verifyMessage = async (
contact: Contact, contact: Contact,
quotedMsg?: any quotedMsg?: any
) => { ) => {
let messageData = { const messageData = {
id: msg.id.id, id: msg.id.id,
ticketId: ticket.id, ticketId: ticket.id,
contactId: msg.fromMe ? undefined : contact.id, contactId: msg.fromMe ? undefined : contact.id,
body: msg.body, body: msg.body,
fromMe: msg.fromMe, fromMe: msg.fromMe,
fromAgent: false,
mediaType: msg.type, mediaType: msg.type,
read: msg.fromMe, read: msg.fromMe,
quotedMsgId: quotedMsg, quotedMsgId: quotedMsg,
phoneNumberId: msg?.phoneNumberId phoneNumberId: msg?.phoneNumberId
}; };
if (msg?.fromMe) {
const botInfo = await BotIsOnQueue("botqueue");
if (botInfo.isOnQueue) {
const ura: any = await get({ key: "ura" });
if (ura && !ura.includes(JSON.stringify(msg?.body))) {
messageData = { ...messageData, fromAgent: true };
}
} else if (msg?.body?.trim().length > 0 && !/\u200e/.test(msg.body[0])) {
messageData = { ...messageData, fromAgent: true };
}
}
await ticket.update({ lastMessage: msg.body }); await ticket.update({ lastMessage: msg.body });
await CreateMessageService({ messageData }); await CreateMessageService({ messageData });
@ -370,7 +349,7 @@ const verifyQueue = async (
ticketId: ticket.id ticketId: ticket.id
}); });
const data = await get({ key: "ura", parse: true }); const data = await get("ura");
await createObject({ await createObject({
whatsappId: `${ticket.whatsappId}`, whatsappId: `${ticket.whatsappId}`,
@ -986,7 +965,7 @@ const handleMessage = async (
const menu = async (userTyped: string, whatsappId: any, contactId: any) => { const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
let lastId = await findObject(whatsappId, contactId, "ura"); let lastId = await findObject(whatsappId, contactId, "ura");
const data: any = await get({ key: "ura", parse: true }); const data: any = await get("ura");
console.log("lastId[0]: ", lastId[0]); console.log("lastId[0]: ", lastId[0]);