fix(bot): working callback Phase 1

This commit is contained in:
Leifer Mendez
2022-12-30 14:18:22 +01:00
parent c62af73c16
commit 952ce86ffa
4 changed files with 36 additions and 43 deletions

View File

@@ -115,24 +115,16 @@ class CoreClass {
// 📄 Se encarga de revisar si el contexto del mensaje tiene callback y ejecutarlo // 📄 Se encarga de revisar si el contexto del mensaje tiene callback y ejecutarlo
const cbEveryCtx = (inRef) => { const cbEveryCtx = (inRef) => {
const indexFlow = this.flowClass.findIndexByRef(inRef) this.flowClass.allCallbacks[inRef](messageCtxInComming, {
this.flowClass.allCallbacks[indexFlow].callback( fallBack,
messageCtxInComming, flowDynamic,
{ })
fallBack,
flowDynamic,
}
)
} }
// 📄 [options: callback]: Si se tiene un callback se ejecuta // 📄 [options: callback]: Si se tiene un callback se ejecuta
if (!fallBackFlag) { if (!fallBackFlag) {
if (refToContinue && prevMsg?.options?.callback) { for (const ite of this.flowClass.find(body)) {
cbEveryCtx(refToContinue?.ref) cbEveryCtx(ite?.ref)
} else {
for (const ite of this.flowClass.find(body)) {
cbEveryCtx(ite?.ref)
}
} }
} }

View File

@@ -1,4 +1,5 @@
const { toSerialize } = require('./methods/toSerialize') const { toSerialize } = require('./methods/toSerialize')
const { flatObject } = require('../utils/flattener')
class FlowClass { class FlowClass {
allCallbacks = [] allCallbacks = []
@@ -8,7 +9,8 @@ class FlowClass {
if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY') if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY')
this.flowRaw = _flow this.flowRaw = _flow
this.allCallbacks = this.parseCallBacks(this.flowRaw) this.allCallbacks = flatObject(_flow)
console.log('[🙌🙌🙌]', this.allCallbacks)
const mergeToJsonSerialize = Object.keys(_flow) const mergeToJsonSerialize = Object.keys(_flow)
.map((indexObjectFlow) => _flow[indexObjectFlow].toJson()) .map((indexObjectFlow) => _flow[indexObjectFlow].toJson())
@@ -17,16 +19,6 @@ class FlowClass {
this.flowSerialize = toSerialize(mergeToJsonSerialize) this.flowSerialize = toSerialize(mergeToJsonSerialize)
} }
/**
* Buscar y aplanar todos los callbacks
* @param {*} inFlow
*/
parseCallBacks = (inFlow) =>
inFlow
.map((cbIn) => cbIn.ctx.callbacks)
.flat(2)
.map((c, i) => ({ callback: c?.callback, index: i }))
find = (keyOrWord, symbol = false, overFlow = null) => { find = (keyOrWord, symbol = false, overFlow = null) => {
keyOrWord = `${keyOrWord}` keyOrWord = `${keyOrWord}`
let capture = false let capture = false

View File

@@ -1,3 +1,4 @@
const { flatObject } = require('../../utils/flattener')
const { generateRef } = require('../../utils/hash') const { generateRef } = require('../../utils/hash')
const { toJson } = require('./toJson') const { toJson } = require('./toJson')
/** /**
@@ -35,24 +36,7 @@ const addAnswer =
* Esta funcion aplana y busca los callback anidados de los hijos * Esta funcion aplana y busca los callback anidados de los hijos
* @returns * @returns
*/ */
const getCbFromNested = () => { const getCbFromNested = () => flatObject(nested)
const cbNestedList = Array.isArray(nested) ? nested : []
const cbNestedObj = cbNestedList.map(({ ctx }) => ctx?.callbacks)
const queueCb = cbNestedObj.reduce((acc, current) => {
const getKeys = Object.keys(current)
const parse = getKeys.map((icb, i) => ({
[icb]: Object.values(current)[i],
}))
return [...acc, ...parse]
}, [])
const flatObj = {}
for (const iteration of queueCb) {
const [keyCb] = Object.keys(iteration)
flatObj[keyCb] = iteration[keyCb]
}
return flatObj
}
const callback = typeof cb === 'function' ? cb : () => null const callback = typeof cb === 'function' ? cb : () => null

View File

@@ -0,0 +1,25 @@
const flatObject = (listArray = []) => {
const cbNestedList = Array.isArray(listArray) ? listArray : []
if (!listArray.length) return {}
const cbNestedObj = cbNestedList
.map(({ ctx }) => ctx?.callbacks)
.filter((i) => !!i)
const queueCb = cbNestedObj.reduce((acc, current) => {
const getKeys = Object.keys(current)
const parse = getKeys.map((icb, i) => ({
[icb]: Object.values(current)[i],
}))
return [...acc, ...parse]
}, [])
const flatObj = {}
for (const iteration of queueCb) {
const [keyCb] = Object.keys(iteration)
flatObj[keyCb] = iteration[keyCb]
}
return flatObj
}
module.exports = { flatObject }