Skip to content

Commit

Permalink
fix(cli): ⚡ endflow
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Jan 28, 2023
1 parent f201c50 commit 1c66f17
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
111 changes: 111 additions & 0 deletions __test__/06-case.test.js
Original file line number Diff line number Diff line change
@@ -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))
}
11 changes: 8 additions & 3 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ class CoreClass {
}

// 📄 Finalizar flujo
const endFlow = async () => {
const endFlow = async (message = null) => {
prevMsg = null
endFlowFlag = true
clearQueue()
if (message)
this.sendProviderAndSave(from, {
...prevMsg,
answer: message ?? prevMsg.answer,
})
return
}

Expand Down Expand Up @@ -199,7 +204,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),
Expand All @@ -212,7 +217,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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/bot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bot-whatsapp/bot",
"version": "0.0.84-alpha.0",
"version": "0.0.86-alpha.0",
"description": "",
"main": "./lib/bundle.bot.cjs",
"scripts": {
Expand Down

0 comments on commit 1c66f17

Please sign in to comment.