Skip to content

Commit

Permalink
fix: ⚡ delay after gotoFlow #877
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Nov 17, 2023
1 parent 6edf373 commit 1eda39a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
28 changes: 28 additions & 0 deletions __test__/0.0.2-goto-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,32 @@ testSuite(`Debe de continuar el el encadenamiento con procesos async`, async ({
assert.is(undefined, history[96])
})

//Issue https://github.com/codigoencasa/bot-whatsapp/issues/877
testSuite(`Debe respectar el delay del node previo`, async ({ database, provider }) => {
const flowPing = addKeyword(['hi']).addAction(async (_, { flowDynamic, gotoFlow }) => {
await flowDynamic('Buenas ping debe espera 1segundo')
return gotoFlow(flowBye)
})

const flowBye = addKeyword('ping').addAnswer(`Pong con delay 1 segundo`, { delay: 1000 })

await createBot({
database,
flow: createFlow([flowPing, flowBye]),
provider,
})

await provider.delaySendMessage(0, 'message', {
from: '000',
body: 'hi',
})

await delay(2000)
const history = database.listHistory.map((item) => item.answer)
assert.is('__call_action__', history[0])
assert.is('Buenas ping debe espera 1segundo', history[1])
assert.is('Pong con delay 1 segundo', history[2])
assert.is(undefined, history[3])
})

testSuite.run()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build": "pnpm run cli:rollup && pnpm run bot:rollup && pnpm run provider:rollup && pnpm run database:rollup && pnpm run contexts:rollup && pnpm run create-bot-whatsapp:rollup && pnpm run portal:rollup && pnpm run eslint-plugin:rollup",
"copy.lib": "node ./scripts/move.js",
"test.unit": "node ./node_modules/uvu/bin.js packages test",
"test.e2e": "node ./node_modules/uvu/bin.js __test__",
"test.e2e": "node ./node_modules/uvu/bin.js __test__ ",
"test.coverage": "node ./node_modules/c8/bin/c8.js npm run test.unit",
"test": "npm run test.coverage",
"cli": "node ./packages/cli/bin/cli.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class CoreClass extends EventEmitter {
async (flowInstance, step = 0) => {
const promises = []
flag.gotoFlow = true

if (!flowInstance?.toJson) {
printer([
`[POSSIBLE_CIRCULAR_DEPENDENCY]: Se ha detectado una dependencia circular.`,
Expand All @@ -302,6 +303,8 @@ class CoreClass extends EventEmitter {
return
}

await delay(flowInstance?.ctx?.options?.delay ?? 0)

const flowTree = flowInstance.toJson()

const flowParentId = flowTree[step]
Expand All @@ -316,6 +319,7 @@ class CoreClass extends EventEmitter {
// Enviar el mensaje al proveedor y guardarlo
await this.sendProviderAndSave(from, ctxMessage).then(() => promises.push(ctxMessage))
}

await endFlow(flag)(promises)

return
Expand Down

0 comments on commit 1eda39a

Please sign in to comment.