refactor(cli): flowDynamic

This commit is contained in:
Leifer Mendez
2023-02-01 16:33:57 +01:00
parent f7d90efc2f
commit 0f8a33cc0c
3 changed files with 24 additions and 23 deletions

View File

@@ -54,7 +54,7 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
async (ctx, { flowDynamic, fallBack }) => { async (ctx, { flowDynamic, fallBack }) => {
if (ctx.body !== '18') { if (ctx.body !== '18') {
await delay(50) 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!') return flowDynamic('Bien tu edad es correcta!')
} }

View File

@@ -118,19 +118,11 @@ class CoreClass {
// 📄 Finalizar flujo // 📄 Finalizar flujo
const endFlow = async (message = null) => { const endFlow = async (message = null) => {
prevMsg = null
endFlowFlag = true endFlowFlag = true
if (message) if (message)
this.sendProviderAndSave(from, createCtxMessage(message)) this.sendProviderAndSave(from, createCtxMessage(message))
clearQueue() clearQueue()
return sendFlow([])
}
// 📄 Continuar con el siguiente flujo
const continueFlow = async () => {
const cotinueMessage =
this.flowClass.find(refToContinue?.ref, true) || []
sendFlow(cotinueMessage, from, { continue: true })
return return
} }
@@ -138,10 +130,10 @@ class CoreClass {
const sendFlow = async ( const sendFlow = async (
messageToSend, messageToSend,
numberOrId, numberOrId,
options = { continue: false } options = { prev: prevMsg }
) => { ) => {
if (!options.continue && prevMsg?.options?.capture) if (options.prev?.options?.capture)
await cbEveryCtx(prevMsg?.ref) await cbEveryCtx(options.prev?.ref)
const queue = [] const queue = []
for (const ctxMessage of messageToSend) { for (const ctxMessage of messageToSend) {
@@ -160,9 +152,8 @@ class CoreClass {
} }
// 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje // 📄 [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 = [] QueuePrincipal.queue = []
if (next) return continueFlow()
return this.sendProviderAndSave(from, { return this.sendProviderAndSave(from, {
...prevMsg, ...prevMsg,
answer: answer:
@@ -171,7 +162,7 @@ class CoreClass {
: message?.body ?? prevMsg.answer, : message?.body ?? prevMsg.answer,
options: { options: {
...prevMsg.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) => const parseListMsg = listMsg.map((opt, index) =>
createCtxMessage(opt, index) createCtxMessage(opt, index)
) )
const currentPrev = await this.databaseClass.getPrevByNumber(from)
if (endFlowFlag) return if (endFlowFlag) return
for (const msg of parseListMsg) { for (const msg of parseListMsg) {
await this.sendProviderAndSave(from, msg) 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 // 📄 Se encarga de revisar si el contexto del mensaje tiene callback o fallback
@@ -206,7 +204,6 @@ class CoreClass {
fallBack, fallBack,
flowDynamic, flowDynamic,
endFlow, endFlow,
continueFlow,
}) })
} }
@@ -246,10 +243,11 @@ class CoreClass {
*/ */
sendProviderAndSave = (numberOrId, ctxMessage) => { sendProviderAndSave = (numberOrId, ctxMessage) => {
const { answer } = ctxMessage const { answer } = ctxMessage
return Promise.all([ return this.providerClass
this.providerClass.sendMessage(numberOrId, answer, ctxMessage), .sendMessage(numberOrId, answer, ctxMessage)
this.databaseClass.save({ ...ctxMessage, from: numberOrId }), .then(() =>
]) this.databaseClass.save({ ...ctxMessage, from: numberOrId })
)
} }
/** /**

View File

@@ -10,7 +10,10 @@ class MockDatabase {
constructor() {} constructor() {}
getPrevByNumber = (from) => { 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) return history.find((a) => a.from === from)
} }