diff --git a/backend/src/helpers/RedisClient.ts b/backend/src/helpers/RedisClient.ts index 7154d15..6fd90b2 100644 --- a/backend/src/helpers/RedisClient.ts +++ b/backend/src/helpers/RedisClient.ts @@ -6,6 +6,7 @@ type WhatsappData = { contactId: string; identifier: string; value?: string; + history?: string; }; type getData = { @@ -28,10 +29,10 @@ export async function getSimple(key: string) { export async function get({ key, value, parse }: getData) { if (key.includes("*")) { - const keys = await redis.keys(key); + const keys = await redis.keys(key); if (keys.length > 0) { for (const key of keys) { - const val = await redis.get(key); + const val = await redis.get(key); if (val.includes(value)) { if (parse) return JSON.parse(val); return val; @@ -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; } diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index f4e006a..d5da2bf 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -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); @@ -527,9 +528,7 @@ const transferTicket = async ( ticket: Ticket, sendGreetingMessage?: boolean ) => { - const botInfo = await BotIsOnQueue("botqueue"); - - console.log("kkkkkkkkkkkkkkkkkkkkk queueName: ", queueName); + const botInfo = await BotIsOnQueue("botqueue"); const queuesWhatsGreetingMessage = await queuesOutBot( wbot, @@ -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"); - await createObject({ + let uraOptionSelected = await findObject( whatsappId, contactId, - identifier: "ura", - value: data[1].id - }); + "ura" + ); + + 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) { 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,26 +1083,42 @@ 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; + } } function existSubMenu() { @@ -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); }