From 0f8a33cc0ca33236da6c2b28d2d5859b22573409 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Wed, 1 Feb 2023 16:33:57 +0100 Subject: [PATCH] refactor(cli): :zap: flowDynamic --- __test__/05-case.test.js | 2 +- packages/bot/core/core.class.js | 40 ++++++++++++++--------------- packages/database/src/mock/index.js | 5 +++- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/__test__/05-case.test.js b/__test__/05-case.test.js index 6ca65d0..16b11f5 100644 --- a/__test__/05-case.test.js +++ b/__test__/05-case.test.js @@ -54,7 +54,7 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => { async (ctx, { flowDynamic, fallBack }) => { if (ctx.body !== '18') { await delay(50) - return fallBack(false, 'Ups creo que no eres mayor de edad') + return fallBack('Ups creo que no eres mayor de edad') } return flowDynamic('Bien tu edad es correcta!') } diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index 9b329cc..68fd4d5 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -118,19 +118,11 @@ class CoreClass { // 📄 Finalizar flujo const endFlow = async (message = null) => { - prevMsg = null endFlowFlag = true if (message) this.sendProviderAndSave(from, createCtxMessage(message)) clearQueue() - return - } - - // 📄 Continuar con el siguiente flujo - const continueFlow = async () => { - const cotinueMessage = - this.flowClass.find(refToContinue?.ref, true) || [] - sendFlow(cotinueMessage, from, { continue: true }) + sendFlow([]) return } @@ -138,10 +130,10 @@ class CoreClass { const sendFlow = async ( messageToSend, numberOrId, - options = { continue: false } + options = { prev: prevMsg } ) => { - if (!options.continue && prevMsg?.options?.capture) - await cbEveryCtx(prevMsg?.ref) + if (options.prev?.options?.capture) + await cbEveryCtx(options.prev?.ref) const queue = [] for (const ctxMessage of messageToSend) { @@ -160,9 +152,8 @@ class CoreClass { } // 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje - const fallBack = async (next = false, message = null) => { + const fallBack = async (message = null) => { QueuePrincipal.queue = [] - if (next) return continueFlow() return this.sendProviderAndSave(from, { ...prevMsg, answer: @@ -171,7 +162,7 @@ class CoreClass { : message?.body ?? prevMsg.answer, options: { ...prevMsg.options, - buttons: message?.buttons ?? prevMsg.options?.buttons, + buttons: prevMsg.options?.buttons, }, }) } @@ -185,12 +176,19 @@ class CoreClass { const parseListMsg = listMsg.map((opt, index) => createCtxMessage(opt, index) ) + const currentPrev = await this.databaseClass.getPrevByNumber(from) if (endFlowFlag) return for (const msg of parseListMsg) { await this.sendProviderAndSave(from, msg) } - return continueFlow() + + const nextFlow = await this.flowClass.find(refToContinue?.ref, true) + const filterNextFlow = nextFlow.filter( + (msg) => msg.refSerialize !== currentPrev?.refSerialize + ) + + return sendFlow(filterNextFlow, from, { prev: undefined }) } // 📄 Se encarga de revisar si el contexto del mensaje tiene callback o fallback @@ -206,7 +204,6 @@ class CoreClass { fallBack, flowDynamic, endFlow, - continueFlow, }) } @@ -246,10 +243,11 @@ class CoreClass { */ sendProviderAndSave = (numberOrId, ctxMessage) => { const { answer } = ctxMessage - return Promise.all([ - this.providerClass.sendMessage(numberOrId, answer, ctxMessage), - this.databaseClass.save({ ...ctxMessage, from: numberOrId }), - ]) + return this.providerClass + .sendMessage(numberOrId, answer, ctxMessage) + .then(() => + this.databaseClass.save({ ...ctxMessage, from: numberOrId }) + ) } /** diff --git a/packages/database/src/mock/index.js b/packages/database/src/mock/index.js index 4c07c03..2ab91a0 100644 --- a/packages/database/src/mock/index.js +++ b/packages/database/src/mock/index.js @@ -10,7 +10,10 @@ class MockDatabase { constructor() {} getPrevByNumber = (from) => { - const history = this.listHistory.slice().reverse() + const history = this.listHistory + .slice() + .reverse() + .filter((i) => !!i.keyword) return history.find((a) => a.from === from) }