feat(ura): Added option to return to the previous submenu and main menu

Fixed URAs context to prevent users from receiving information from a menu that is not currently in the context.
feat-scaling-ticket-remote-creation
adriano 2024-03-15 11:59:32 -03:00
parent 75b14becc3
commit 0cc027252d
2 changed files with 125 additions and 45 deletions

View File

@ -6,6 +6,7 @@ type WhatsappData = {
contactId: string; contactId: string;
identifier: string; identifier: string;
value?: string; value?: string;
history?: string;
}; };
type getData = { type getData = {
@ -28,10 +29,10 @@ export async function getSimple(key: string) {
export async function get({ key, value, parse }: getData) { 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 (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 (val.includes(value)) {
if (parse) return JSON.parse(val); if (parse) return JSON.parse(val);
return val; return val;
@ -98,7 +99,8 @@ export async function createObject({
whatsappId, whatsappId,
contactId, contactId,
identifier, identifier,
value value,
history = ""
}: WhatsappData) { }: WhatsappData) {
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`; const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
const result = await redis.hmset( const result = await redis.hmset(
@ -110,7 +112,9 @@ export async function createObject({
"identifier", "identifier",
identifier, identifier,
"value", "value",
value value,
"history",
history
); );
await redis.expire(key, 300); await redis.expire(key, 300);
@ -138,7 +142,8 @@ export async function findObject(
"whatsappId", "whatsappId",
"contactId", "contactId",
"identifier", "identifier",
"value" "value",
"history"
); );
return result; return result;
} }

View File

@ -376,7 +376,8 @@ const verifyQueue = async (
whatsappId: `${ticket.whatsappId}`, whatsappId: `${ticket.whatsappId}`,
contactId: `${ticket.contactId}`, contactId: `${ticket.contactId}`,
identifier: "ura", identifier: "ura",
value: data[1].id value: data[1].id,
history: `|${data[1].id}`
}); });
botSendMessage(ticket, data[1].value); botSendMessage(ticket, data[1].value);
@ -527,9 +528,7 @@ const transferTicket = async (
ticket: Ticket, ticket: Ticket,
sendGreetingMessage?: boolean sendGreetingMessage?: boolean
) => { ) => {
const botInfo = await BotIsOnQueue("botqueue"); const botInfo = await BotIsOnQueue("botqueue");
console.log("kkkkkkkkkkkkkkkkkkkkk queueName: ", queueName);
const queuesWhatsGreetingMessage = await queuesOutBot( const queuesWhatsGreetingMessage = await queuesOutBot(
wbot, wbot,
@ -995,7 +994,8 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
whatsappId, whatsappId,
contactId, contactId,
identifier: "ura", identifier: "ura",
value: data[1].id value: data[1].id,
history: `|${data[1].id}`
}); });
} }
@ -1005,7 +1005,7 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
if ( if (
lastId && lastId &&
lastId.length == 4 && (lastId.length == 4 || lastId.length == 5) &&
lastId[3] && lastId[3] &&
lastId[3].trim().length > 0 lastId[3].trim().length > 0
) { ) {
@ -1015,23 +1015,49 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
o.value.toLowerCase() == userTyped.toLowerCase() o.value.toLowerCase() == userTyped.toLowerCase()
); );
// TEST DEL if (!option && userTyped != "0" && userTyped != "#") {
console.log("OPTION: ", option);
if (!option && userTyped != "0") {
if (!existSubMenu()) { if (!existSubMenu()) {
const response = await mainOptionsMenu(userTyped); const response = await mainOptionsMenu(userTyped);
if (response) return response; if (response) return response;
else { else {
console.log("kkkkkkkkkkkkkkkkkkk"); let uraOptionSelected = await findObject(
await createObject({
whatsappId, whatsappId,
contactId, contactId,
identifier: "ura", "ura"
value: data[1].id );
});
uraOptionSelected = uraOptionSelected[4].split("|");
return data[1]; if (uraOptionSelected.length == 1) {
await createObject({
whatsappId,
contactId,
identifier: "ura",
value: data[1].id,
history: `|${data[1].id}`
});
return data[1];
} else if (uraOptionSelected.length > 1) {
const id = uraOptionSelected[uraOptionSelected.length - 1];
console.log(" ID FROM THE MENU/SUBMENU: ", id);
const history = await historyUra(whatsappId, contactId, id);
await createObject({
whatsappId,
contactId,
identifier: "ura",
value: id,
history
});
let response: any = data.find((o: any) => o.id == id);
return response;
}
} }
} }
} }
@ -1039,19 +1065,16 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
if (option) { if (option) {
let response: any = data.find((o: any) => o.idmaster == option.id); let response: any = data.find((o: any) => o.idmaster == option.id);
console.log(" RESPONSE OPTION: ", response, " | OPTION: ", option);
console.log( let history: any = await historyUra(whatsappId, contactId, response.id);
"RRRRRRRRRRRRRRRRRRRRRRRRRRRRR response: ",
response,
" | option: ",
option
);
await createObject({ await createObject({
whatsappId, whatsappId,
contactId, contactId,
identifier: "ura", identifier: "ura",
value: response.id value: response.id,
history
}); });
return response; return response;
@ -1060,26 +1083,42 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
whatsappId, whatsappId,
contactId, contactId,
identifier: "ura", identifier: "ura",
value: data[1].id value: data[1].id,
history: `|${data[1].id}`
}); });
return data[1]; return data[1];
} else { } else if (userTyped == "#") {
console.log("INVALID SEARCH"); let uraOptionSelected = await findObject(whatsappId, contactId, "ura");
let response = await existSubMenu(); uraOptionSelected = uraOptionSelected[4].split("|").filter(Boolean);
if (response) return response;
return { let id = uraOptionSelected[0];
value: data.find((o: any) => o.id == lastId[3])?.value
};
// return { let history = `|${uraOptionSelected[0]}`;
// value: `Você digitou uma opçao inválida!\n\n${
// data.find((o: any) => o.id == lastId[3])?.value if (uraOptionSelected.length > 1) {
// }\n\nDigite 0 para voltar ao menu ` const idRemove = uraOptionSelected[uraOptionSelected.length - 1];
// };
} history = await historyUra(whatsappId, contactId, idRemove, true);
const lstIds = history.split("|").filter(Boolean);
id = lstIds[lstIds.length - 1];
}
await createObject({
whatsappId,
contactId,
identifier: "ura",
value: id,
history
});
let response: any = data.find((o: any) => o.id == id);
return response;
}
} }
function existSubMenu() { function existSubMenu() {
@ -1091,18 +1130,29 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
} }
async function mainOptionsMenu(userTyped: any) { async function mainOptionsMenu(userTyped: any) {
let currentMenu = await findObject(whatsappId, contactId, "ura");
const menuValues = data
.filter((m: any) => m.idmaster == currentMenu[3])
.map((m: any) => m.value);
let menuOption = data.find( let menuOption = data.find(
(o: any) => o.value.toLowerCase() == userTyped.toLowerCase() (o: any) =>
o.value.toLowerCase() == userTyped.toLowerCase() &&
menuValues.includes(userTyped.toLowerCase())
); );
console.log("============> menuOption OPTION: ", menuOption);
if (menuOption) { if (menuOption) {
let response = data.find((o: any) => o.idmaster == menuOption.id); let response = data.find((o: any) => o.idmaster == menuOption.id);
if (response) { if (response) {
let history = await historyUra(whatsappId, contactId, response.id);
await createObject({ await createObject({
whatsappId, whatsappId,
contactId, contactId,
identifier: "ura", identifier: "ura",
value: response.id value: response.id,
history
}); });
return response; return response;
@ -1211,6 +1261,31 @@ export {
mediaTypeWhatsappOfficial, mediaTypeWhatsappOfficial,
botSendMessage botSendMessage
}; };
async function historyUra(
whatsappId: any,
contactId: any,
id: any,
remove?: boolean
) {
let uraOptionSelected = await findObject(whatsappId, contactId, "ura");
let history = "";
console.log("SELECED OPTION uraOptionSelected: ", uraOptionSelected);
if (remove) {
return uraOptionSelected[4]?.replace(`|${id}`, "");
}
if (uraOptionSelected && uraOptionSelected.length == 5) {
if (!uraOptionSelected[4]?.includes(`${id}`))
history += `${uraOptionSelected[4]}|${id}`;
else history = `${uraOptionSelected[4]}`;
} else {
history = `|${id}`;
}
return history;
}
async function whatsappInfo(whatsappId: string | number) { async function whatsappInfo(whatsappId: string | number) {
return await Whatsapp.findByPk(whatsappId); return await Whatsapp.findByPk(whatsappId);
} }