Merge pull request #574 from codigoencasa/fix/end-flow

Fix/end flow
This commit is contained in:
Leifer Mendez
2023-01-29 14:07:29 +01:00
committed by GitHub
3 changed files with 148 additions and 25 deletions

111
__test__/06-case.test.js Normal file
View File

@@ -0,0 +1,111 @@
const { test } = require('uvu')
const assert = require('uvu/assert')
const MOCK_DB = require('../packages/database/src/mock')
const PROVIDER_DB = require('../packages/provider/src/mock')
const {
addKeyword,
createBot,
createFlow,
createProvider,
} = require('../packages/bot/index')
/**
* Falsear peticion async
* @param {*} fakeData
* @returns
*/
const fakeHTTP = async (fakeData = []) => {
await delay(5)
const data = fakeData.map((u, i) => ({ body: `${i + 1} ${u}` }))
return Promise.resolve(data)
}
test(`[Caso - 06] Finalizar Flujo (endFlow)`, async () => {
const MOCK_VALUES = [
'¿CUal es tu email?',
'Continuamos....',
'¿Cual es tu edad?',
]
const provider = createProvider(PROVIDER_DB)
const database = new MOCK_DB()
const flujoPrincipal = addKeyword(['hola'])
.addAnswer(
MOCK_VALUES[0],
{
capture: true,
},
async (ctx, { flowDynamic, fallBack }) => {
const validation = ctx.body.includes('@')
if (validation) {
const getDataFromApi = await fakeHTTP([
'Gracias por tu email se ha validado de manera correcta',
])
return flowDynamic(getDataFromApi)
}
return fallBack(validation)
}
)
.addAnswer(MOCK_VALUES[1], null, async (_, { endFlow }) => {
return endFlow()
})
.addAnswer(
MOCK_VALUES[2],
{ capture: true },
async (ctx, { flowDynamic, fallBack }) => {
if (ctx.body !== '18') {
await delay(50)
return fallBack(false, 'Ups creo que no eres mayor de edad')
}
return flowDynamic('Bien tu edad es correcta!')
}
)
.addAnswer('Puedes pasar')
createBot({
database,
flow: createFlow([flujoPrincipal]),
provider,
})
provider.delaySendMessage(0, 'message', {
from: '000',
body: 'hola',
})
provider.delaySendMessage(10, 'message', {
from: '000',
body: 'this is not email value',
})
provider.delaySendMessage(20, 'message', {
from: '000',
body: 'test@test.com',
})
provider.delaySendMessage(90, 'message', {
from: '000',
body: '20',
})
await delay(1200)
const getHistory = database.listHistory.map((i) => i.answer)
assert.is(MOCK_VALUES[0], getHistory[0])
assert.is('this is not email value', getHistory[1])
assert.is(MOCK_VALUES[0], getHistory[2])
assert.is('test@test.com', getHistory[3])
assert.is(
'1 Gracias por tu email se ha validado de manera correcta',
getHistory[4]
)
assert.is(MOCK_VALUES[1], getHistory[5])
assert.is('20', getHistory[6])
assert.is(undefined, getHistory[7])
})
test.run()
function delay(ms) {
return new Promise((res) => setTimeout(res, ms))
}

View File

@@ -91,6 +91,25 @@ class CoreClass {
this.databaseClass.save(ctxByNumber)
}
// 📄 Crar CTX de mensaje (uso private)
const createCtxMessage = (payload = {}, index = 0) => {
const body =
typeof payload === 'string'
? payload
: payload?.body ?? payload?.answer
const media = payload?.media ?? null
const buttons = payload?.buttons ?? []
const capture = payload?.capture ?? false
return toCtx({
body,
from,
keyword: null,
index,
options: { media, buttons, capture },
})
}
// 📄 Limpiar cola de procesos
const clearQueue = () => {
QueuePrincipal.pendingPromise = false
@@ -98,9 +117,11 @@ class CoreClass {
}
// 📄 Finalizar flujo
const endFlow = async () => {
const endFlow = async (message = null) => {
prevMsg = null
endFlowFlag = true
if (message)
this.sendProviderAndSave(from, createCtxMessage(message))
clearQueue()
return
}
@@ -144,35 +165,26 @@ class CoreClass {
if (next) return continueFlow()
return this.sendProviderAndSave(from, {
...prevMsg,
answer: message ?? prevMsg.answer,
answer:
typeof message === 'string'
? message
: message?.body ?? prevMsg.answer,
options: {
...prevMsg.options,
buttons: message?.buttons ?? prevMsg.options?.buttons,
},
})
}
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes
// para evitar bloque de whatsapp
const flowDynamic = async (
listMsg = [],
optListMsg = { limit: 5, fallback: false }
) => {
const flowDynamic = async (listMsg = []) => {
if (!Array.isArray(listMsg)) listMsg = [listMsg]
fallBackFlag = optListMsg.fallback
const parseListMsg = listMsg
.map((opt, index) => {
const body = typeof opt === 'string' ? opt : opt.body
const media = opt?.media ?? null
const buttons = opt?.buttons ?? []
return toCtx({
body,
from,
keyword: null,
index,
options: { media, buttons },
})
})
.slice(0, optListMsg.limit)
const parseListMsg = listMsg.map((opt, index) =>
createCtxMessage(opt, index)
)
if (endFlowFlag) return
for (const msg of parseListMsg) {
@@ -199,7 +211,7 @@ class CoreClass {
}
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
if (prevMsg?.options?.nested?.length) {
if (!endFlowFlag && prevMsg?.options?.nested?.length) {
const nestedRef = prevMsg.options.nested
const flowStandalone = nestedRef.map((f) => ({
...nestedRef.find((r) => r.refSerialize === f.refSerialize),
@@ -212,7 +224,7 @@ class CoreClass {
}
// 📄🤘(tiene return) Si el mensaje previo implementa capture
if (!prevMsg?.options?.nested?.length) {
if (!endFlowFlag && !prevMsg?.options?.nested?.length) {
const typeCapture = typeof prevMsg?.options?.capture
if (typeCapture === 'boolean' && fallBackFlag) {

View File

@@ -1,6 +1,6 @@
{
"name": "@bot-whatsapp/bot",
"version": "0.0.84-alpha.0",
"version": "0.0.91-alpha.0",
"description": "",
"main": "./lib/bundle.bot.cjs",
"scripts": {