Compare commits
No commits in common. "5366f9943b6da445947a35f90df857411c91ac27" and "279c4697ddcf97a0f382f0fe5d1c82cec4dbffb9" have entirely different histories.
5366f9943b
...
279c4697dd
|
@ -21,7 +21,6 @@ import sendWhatsAppMessageOfficialAPI from "../helpers/sendWhatsAppMessageOffici
|
|||
import Whatsapp from "../models/Whatsapp";
|
||||
import checkLastClientMsg24hs from "../helpers/CheckLastClientMsg24hs";
|
||||
import AppError from "../errors/AppError";
|
||||
import { get } from "../helpers/RedisClient";
|
||||
|
||||
type IndexQuery = {
|
||||
pageNumber: string;
|
||||
|
@ -98,8 +97,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||
}
|
||||
|
||||
const name = params.find((p: any) => p?.template_name);
|
||||
const { language }: any =
|
||||
params?.find((p: any) => p?.language) || "pt_BR";
|
||||
const { language }: any = params?.find((p: any) => p?.language) || 'pt_BR'
|
||||
|
||||
const { template_name } = name;
|
||||
|
||||
|
|
|
@ -5,9 +5,11 @@ import DeleteQueueService from "../services/QueueService/DeleteQueueService";
|
|||
import ListQueuesService from "../services/QueueService/ListQueuesService";
|
||||
import ShowQueueService from "../services/QueueService/ShowQueueService";
|
||||
import UpdateQueueService from "../services/QueueService/UpdateQueueService";
|
||||
import Queue from "../models/Queue";
|
||||
import AppError from "../errors/AppError";
|
||||
import { del, get, set } from "../helpers/RedisClient";
|
||||
import Queue from "../models/Queue"
|
||||
import AppError from "../errors/AppError"
|
||||
import { get, set } from "../helpers/RedisClient";
|
||||
|
||||
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
const queues = await ListQueuesService();
|
||||
|
@ -123,7 +125,7 @@ export const customization = async (
|
|||
|
||||
await set("ura", ura);
|
||||
|
||||
const _ura = await get({ key: "ura", parse: true });
|
||||
const _ura = await get("ura");
|
||||
console.log("_URA: ", _ura);
|
||||
|
||||
return res.status(200).json({ new_queues });
|
||||
|
@ -162,8 +164,6 @@ export const remove = async (
|
|||
|
||||
await DeleteQueueService(queueId);
|
||||
|
||||
await del(`queue:${queueId}`);
|
||||
|
||||
const io = getIO();
|
||||
io.emit("queue", {
|
||||
action: "delete",
|
||||
|
|
|
@ -75,7 +75,6 @@ import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
|||
import CreateContactService from "../services/ContactServices/CreateContactService";
|
||||
import { botSendMessage } from "../services/WbotServices/wbotMessageListener";
|
||||
import WhatsappQueue from "../models/WhatsappQueue";
|
||||
import { get } from "../helpers/RedisClient"
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
const {
|
||||
|
|
|
@ -230,10 +230,7 @@ export const weebhook = async (
|
|||
req.body.entry[0].changes[0].value.metadata.display_phone_number;
|
||||
let type = message.type;
|
||||
|
||||
const contact_to_exist = await get({
|
||||
key: "whatsapp:*",
|
||||
value: `${contact_to}`
|
||||
});
|
||||
const contact_to_exist = await get("whatsapp:*", `${contact_to}`);
|
||||
|
||||
if (contact_to_exist == null) {
|
||||
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}`
|
||||
});
|
||||
} else {
|
||||
await set(
|
||||
`whatsapp:${whatsapp.id}`,
|
||||
JSON.stringify({
|
||||
number: whatsapp?.number,
|
||||
id: whatsapp?.id,
|
||||
greetingMessage: whatsapp?.greetingMessage,
|
||||
phoneNumberId: whatsapp?.phoneNumberId
|
||||
})
|
||||
);
|
||||
await set(`whatsapp:${whatsapp.id}`, `${number}`);
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
|
@ -495,15 +484,7 @@ export const update = async (
|
|||
client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}`
|
||||
});
|
||||
} else {
|
||||
await set(
|
||||
`whatsapp:${whatsapp.id}`,
|
||||
JSON.stringify({
|
||||
number: whatsapp?.number,
|
||||
id: whatsapp?.id,
|
||||
greetingMessage: whatsapp?.greetingMessage,
|
||||
phoneNumberId: whatsapp?.phoneNumberId
|
||||
})
|
||||
);
|
||||
await set(`whatsapp:${whatsapp.id}`, `${number}`);
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
};
|
|
@ -5,7 +5,8 @@ import ListUsersService from "../services/UserServices/ListUsersService";
|
|||
import { get } from "./RedisClient";
|
||||
|
||||
const _botIsOnQueue = async (botName: string) => {
|
||||
const botInfo = await get({ key: "botInfo", parse: true });
|
||||
|
||||
const botInfo = await get("botInfo");
|
||||
|
||||
if (
|
||||
botInfo &&
|
||||
|
|
|
@ -8,12 +8,6 @@ type WhatsappData = {
|
|||
value?: string;
|
||||
};
|
||||
|
||||
type getData = {
|
||||
key: string;
|
||||
value?: string;
|
||||
parse?: boolean;
|
||||
};
|
||||
|
||||
export async function set(key: string, value: string | object) {
|
||||
if (typeof value == "object") await redis.set(key, JSON.stringify(value));
|
||||
else {
|
||||
|
@ -21,30 +15,21 @@ export async function set(key: string, value: string | object) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function getSimple(key: string) {
|
||||
const value: any = await redis.get(key);
|
||||
return value;
|
||||
}
|
||||
|
||||
export async function get({ key, value, parse }: getData) {
|
||||
export async function get(key: string, value?: string) {
|
||||
if (key.includes("*")) {
|
||||
const keys = await redis.keys(key);
|
||||
|
||||
// If there are keys, delete them
|
||||
if (keys.length > 0) {
|
||||
for (const key of keys) {
|
||||
const val = await redis.get(key);
|
||||
if (val.includes(value)) {
|
||||
if (parse) return JSON.parse(val);
|
||||
return val;
|
||||
}
|
||||
if (value == val) return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
const value: any = await redis.get(key);
|
||||
|
||||
if (parse) return JSON.parse(value);
|
||||
|
||||
return value;
|
||||
return JSON.parse(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,10 +31,6 @@ class Message extends Model<Message> {
|
|||
@Column
|
||||
fromMe: boolean;
|
||||
|
||||
@Default(false)
|
||||
@Column
|
||||
fromAgent: boolean;
|
||||
|
||||
@Column(DataType.TEXT)
|
||||
body: string;
|
||||
|
||||
|
|
|
@ -27,7 +27,6 @@ import { clearAllKeys, get, set } from "./helpers/RedisClient";
|
|||
import ShowUserService from "./services/UserServices/ShowUserService";
|
||||
import { json } from "sequelize";
|
||||
import { setBotInfo } from "./helpers/SetBotInfo";
|
||||
import Queue from "./models/Queue";
|
||||
|
||||
const server = app.listen(process.env.PORT, () => {
|
||||
logger.info(`Server started on port: ${process.env.PORT}`);
|
||||
|
@ -48,7 +47,7 @@ gracefulShutdown(server);
|
|||
(async () => {
|
||||
console.log("os.tmpdir(): ", os.tmpdir());
|
||||
|
||||
await clearAllKeys("user:*", "whatsapp:*", "queue:*");
|
||||
await clearAllKeys("user:*", "whatsapp:*");
|
||||
|
||||
const users = await User.findAll();
|
||||
|
||||
|
@ -63,37 +62,23 @@ gracefulShutdown(server);
|
|||
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();
|
||||
|
||||
let whatsapps: any = await Whatsapp.findAll({
|
||||
attributes: ["id", "url", "phoneNumberId", "number", "greetingMessage"]
|
||||
attributes: ["id", "url", "phoneNumberId", "number"]
|
||||
});
|
||||
|
||||
if (whatsapps && whatsapps.length > 0) {
|
||||
for (let i = 0; i < whatsapps.length; i++) {
|
||||
try {
|
||||
const { phoneNumberId, id, greetingMessage } = whatsapps[i];
|
||||
const { phoneNumberId } = whatsapps[i];
|
||||
|
||||
if (phoneNumberId) {
|
||||
await set(
|
||||
`whatsapp:${whatsapps[i].dataValues.id}`,
|
||||
JSON.stringify({
|
||||
number: whatsapps[i].dataValues.number,
|
||||
id,
|
||||
greetingMessage,
|
||||
phoneNumberId
|
||||
})
|
||||
`${whatsapps[i].dataValues.number}`
|
||||
);
|
||||
}
|
||||
|
||||
if (phoneNumberId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ interface MessageData {
|
|||
read?: boolean;
|
||||
mediaType?: string;
|
||||
mediaUrl?: string;
|
||||
fromAgent?: boolean;
|
||||
}
|
||||
interface Request {
|
||||
messageData: MessageData;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import * as Yup from "yup";
|
||||
import AppError from "../../errors/AppError";
|
||||
import Queue from "../../models/Queue";
|
||||
import { set } from "../../helpers/RedisClient";
|
||||
|
||||
interface QueueData {
|
||||
name: string;
|
||||
|
@ -10,67 +9,68 @@ interface QueueData {
|
|||
}
|
||||
|
||||
const CreateQueueService = async (queueData: QueueData): Promise<Queue> => {
|
||||
|
||||
try {
|
||||
|
||||
const { color, name } = queueData;
|
||||
|
||||
const queueSchema = Yup.object().shape({
|
||||
name: Yup.string()
|
||||
.min(2, "ERR_QUEUE_INVALID_NAME")
|
||||
.required("ERR_QUEUE_INVALID_NAME")
|
||||
.test(
|
||||
"Check-unique-name",
|
||||
"ERR_QUEUE_NAME_ALREADY_EXISTS",
|
||||
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 => {
|
||||
const queueSchema = Yup.object().shape({
|
||||
name: Yup.string()
|
||||
.min(2, "ERR_QUEUE_INVALID_NAME")
|
||||
.required("ERR_QUEUE_INVALID_NAME")
|
||||
.test(
|
||||
"Check-unique-name",
|
||||
"ERR_QUEUE_NAME_ALREADY_EXISTS",
|
||||
async value => {
|
||||
if (value) {
|
||||
const colorTestRegex = /^#[0-9a-f]{3,6}$/i;
|
||||
return colorTestRegex.test(value);
|
||||
const queueWithSameName = await Queue.findOne({
|
||||
where: { name: value }
|
||||
});
|
||||
|
||||
return !queueWithSameName;
|
||||
}
|
||||
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;
|
||||
}
|
||||
),
|
||||
color: Yup.string()
|
||||
.required("ERR_QUEUE_INVALID_COLOR")
|
||||
.test("Check-color", "ERR_QUEUE_INVALID_COLOR", async value => {
|
||||
if (value) {
|
||||
const colorTestRegex = /^#[0-9a-f]{3,6}$/i;
|
||||
return colorTestRegex.test(value);
|
||||
}
|
||||
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 {
|
||||
await queueSchema.validate({ color, name });
|
||||
} catch (err: any) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
try {
|
||||
await queueSchema.validate({ color, name });
|
||||
} catch (err: any) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
const queue = await Queue.create(queueData);
|
||||
const queue = await Queue.create(queueData);
|
||||
|
||||
// const { id, greetingMessage } = queue;
|
||||
// await set(`queue:${id}`, { id, name, greetingMessage });
|
||||
return queue;
|
||||
|
||||
return queue;
|
||||
} 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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export default CreateQueueService;
|
||||
|
|
|
@ -3,7 +3,6 @@ import * as Yup from "yup";
|
|||
import AppError from "../../errors/AppError";
|
||||
import Queue from "../../models/Queue";
|
||||
import ShowQueueService from "./ShowQueueService";
|
||||
import { set } from "../../helpers/RedisClient"
|
||||
|
||||
interface QueueData {
|
||||
name?: string;
|
||||
|
@ -15,7 +14,9 @@ const UpdateQueueService = async (
|
|||
queueId: number | string,
|
||||
queueData: QueueData
|
||||
): Promise<Queue> => {
|
||||
|
||||
try {
|
||||
|
||||
const { color, name } = queueData;
|
||||
|
||||
const queueSchema = Yup.object().shape({
|
||||
|
@ -69,14 +70,13 @@ const UpdateQueueService = async (
|
|||
|
||||
await queue.update(queueData);
|
||||
|
||||
// const { id, greetingMessage } = queue;
|
||||
// await set(`queue:${id}`, { id, name, greetingMessage });
|
||||
|
||||
return queue;
|
||||
|
||||
} 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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export default UpdateQueueService;
|
||||
|
|
|
@ -31,6 +31,8 @@ const CheckIsValidContact = async (
|
|||
|
||||
if (!isValidNumber) {
|
||||
|
||||
console.log('kkkkkkkkkkkkkkkkkkkkkkkkkkkkk ')
|
||||
|
||||
const { data, status } = await axios.post(
|
||||
`${process.env.WHATS_NUMBER_VALIDATOR_URL}/api/validate`,
|
||||
{ mobile: number },
|
||||
|
|
|
@ -92,8 +92,7 @@ import {
|
|||
createObject,
|
||||
findByContain,
|
||||
findObject,
|
||||
get,
|
||||
getSimple
|
||||
get
|
||||
} from "../../helpers/RedisClient";
|
||||
import FindOrCreateTicketServiceBot from "../TicketServices/FindOrCreateTicketServiceBot";
|
||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||
|
@ -176,14 +175,9 @@ const verifyMediaMessage = async (
|
|||
mediaUrl: media.filename,
|
||||
mediaType: media.mimetype.split("/")[0],
|
||||
quotedMsgId: quotedMsg,
|
||||
phoneNumberId: msg?.phoneNumberId,
|
||||
fromAgent: false
|
||||
phoneNumberId: msg?.phoneNumberId
|
||||
};
|
||||
|
||||
if (msg?.fromMe) {
|
||||
messageData = { ...messageData, fromAgent: true };
|
||||
}
|
||||
|
||||
if (!ticket?.phoneNumberId) {
|
||||
if (!media.filename) {
|
||||
const ext = media.mimetype.split("/")[1].split(";")[0];
|
||||
|
@ -286,33 +280,18 @@ const verifyMessage = async (
|
|||
contact: Contact,
|
||||
quotedMsg?: any
|
||||
) => {
|
||||
let messageData = {
|
||||
const messageData = {
|
||||
id: msg.id.id,
|
||||
ticketId: ticket.id,
|
||||
contactId: msg.fromMe ? undefined : contact.id,
|
||||
body: msg.body,
|
||||
fromMe: msg.fromMe,
|
||||
fromAgent: false,
|
||||
mediaType: msg.type,
|
||||
read: msg.fromMe,
|
||||
quotedMsgId: quotedMsg,
|
||||
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 CreateMessageService({ messageData });
|
||||
|
@ -370,7 +349,7 @@ const verifyQueue = async (
|
|||
ticketId: ticket.id
|
||||
});
|
||||
|
||||
const data = await get({ key: "ura", parse: true });
|
||||
const data = await get("ura");
|
||||
|
||||
await createObject({
|
||||
whatsappId: `${ticket.whatsappId}`,
|
||||
|
@ -986,7 +965,7 @@ const handleMessage = async (
|
|||
|
||||
const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
|
||||
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]);
|
||||
|
||||
|
|
Loading…
Reference in New Issue