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
parent
75b14becc3
commit
0cc027252d
|
@ -6,6 +6,7 @@ type WhatsappData = {
|
|||
contactId: string;
|
||||
identifier: string;
|
||||
value?: string;
|
||||
history?: string;
|
||||
};
|
||||
|
||||
type getData = {
|
||||
|
@ -98,7 +99,8 @@ export async function createObject({
|
|||
whatsappId,
|
||||
contactId,
|
||||
identifier,
|
||||
value
|
||||
value,
|
||||
history = ""
|
||||
}: WhatsappData) {
|
||||
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
||||
const result = await redis.hmset(
|
||||
|
@ -110,7 +112,9 @@ export async function createObject({
|
|||
"identifier",
|
||||
identifier,
|
||||
"value",
|
||||
value
|
||||
value,
|
||||
"history",
|
||||
history
|
||||
);
|
||||
|
||||
await redis.expire(key, 300);
|
||||
|
@ -138,7 +142,8 @@ export async function findObject(
|
|||
"whatsappId",
|
||||
"contactId",
|
||||
"identifier",
|
||||
"value"
|
||||
"value",
|
||||
"history"
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -376,7 +376,8 @@ const verifyQueue = async (
|
|||
whatsappId: `${ticket.whatsappId}`,
|
||||
contactId: `${ticket.contactId}`,
|
||||
identifier: "ura",
|
||||
value: data[1].id
|
||||
value: data[1].id,
|
||||
history: `|${data[1].id}`
|
||||
});
|
||||
|
||||
botSendMessage(ticket, data[1].value);
|
||||
|
@ -529,8 +530,6 @@ const transferTicket = async (
|
|||
) => {
|
||||
const botInfo = await BotIsOnQueue("botqueue");
|
||||
|
||||
console.log("kkkkkkkkkkkkkkkkkkkkk queueName: ", queueName);
|
||||
|
||||
const queuesWhatsGreetingMessage = await queuesOutBot(
|
||||
wbot,
|
||||
botInfo.botQueueId
|
||||
|
@ -995,7 +994,8 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
|
|||
whatsappId,
|
||||
contactId,
|
||||
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 (
|
||||
lastId &&
|
||||
lastId.length == 4 &&
|
||||
(lastId.length == 4 || lastId.length == 5) &&
|
||||
lastId[3] &&
|
||||
lastId[3].trim().length > 0
|
||||
) {
|
||||
|
@ -1015,23 +1015,49 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
|
|||
o.value.toLowerCase() == userTyped.toLowerCase()
|
||||
);
|
||||
|
||||
// TEST DEL
|
||||
console.log("OPTION: ", option);
|
||||
|
||||
if (!option && userTyped != "0") {
|
||||
if (!option && userTyped != "0" && userTyped != "#") {
|
||||
if (!existSubMenu()) {
|
||||
|
||||
const response = await mainOptionsMenu(userTyped);
|
||||
if (response) return response;
|
||||
else {
|
||||
console.log("kkkkkkkkkkkkkkkkkkk");
|
||||
let uraOptionSelected = await findObject(
|
||||
whatsappId,
|
||||
contactId,
|
||||
"ura"
|
||||
);
|
||||
|
||||
uraOptionSelected = uraOptionSelected[4].split("|");
|
||||
|
||||
if (uraOptionSelected.length == 1) {
|
||||
await createObject({
|
||||
whatsappId,
|
||||
contactId,
|
||||
identifier: "ura",
|
||||
value: data[1].id
|
||||
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) {
|
||||
let response: any = data.find((o: any) => o.idmaster == option.id);
|
||||
console.log(" RESPONSE OPTION: ", response, " | OPTION: ", option);
|
||||
|
||||
console.log(
|
||||
"RRRRRRRRRRRRRRRRRRRRRRRRRRRRR response: ",
|
||||
response,
|
||||
" | option: ",
|
||||
option
|
||||
);
|
||||
let history: any = await historyUra(whatsappId, contactId, response.id);
|
||||
|
||||
await createObject({
|
||||
whatsappId,
|
||||
contactId,
|
||||
identifier: "ura",
|
||||
value: response.id
|
||||
value: response.id,
|
||||
history
|
||||
});
|
||||
|
||||
return response;
|
||||
|
@ -1060,25 +1083,41 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => {
|
|||
whatsappId,
|
||||
contactId,
|
||||
identifier: "ura",
|
||||
value: data[1].id
|
||||
value: data[1].id,
|
||||
history: `|${data[1].id}`
|
||||
});
|
||||
|
||||
return data[1];
|
||||
} else {
|
||||
console.log("INVALID SEARCH");
|
||||
} else if (userTyped == "#") {
|
||||
let uraOptionSelected = await findObject(whatsappId, contactId, "ura");
|
||||
|
||||
let response = await existSubMenu();
|
||||
if (response) return response;
|
||||
uraOptionSelected = uraOptionSelected[4].split("|").filter(Boolean);
|
||||
|
||||
return {
|
||||
value: data.find((o: any) => o.id == lastId[3])?.value
|
||||
};
|
||||
let id = uraOptionSelected[0];
|
||||
|
||||
// return {
|
||||
// value: `Você digitou uma opçao inválida!\n\n${
|
||||
// data.find((o: any) => o.id == lastId[3])?.value
|
||||
// }\n\nDigite 0 para voltar ao menu `
|
||||
// };
|
||||
let history = `|${uraOptionSelected[0]}`;
|
||||
|
||||
if (uraOptionSelected.length > 1) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1091,18 +1130,29 @@ const menu = async (userTyped: string, whatsappId: any, contactId: 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(
|
||||
(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) {
|
||||
let response = data.find((o: any) => o.idmaster == menuOption.id);
|
||||
if (response) {
|
||||
let history = await historyUra(whatsappId, contactId, response.id);
|
||||
|
||||
await createObject({
|
||||
whatsappId,
|
||||
contactId,
|
||||
identifier: "ura",
|
||||
value: response.id
|
||||
value: response.id,
|
||||
history
|
||||
});
|
||||
|
||||
return response;
|
||||
|
@ -1211,6 +1261,31 @@ export {
|
|||
mediaTypeWhatsappOfficial,
|
||||
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) {
|
||||
return await Whatsapp.findByPk(whatsappId);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue