almost work it

This commit is contained in:
Leifer Mendez
2022-11-22 21:45:00 +01:00
parent c40c0c54bd
commit 368bf29e63
4 changed files with 52 additions and 27 deletions

View File

@@ -50,7 +50,13 @@ class CoreClass {
handleMsg = async (messageInComming) => {
const { body, from } = messageInComming
let msgToSend = []
//Consultamos mensaje previo en DB
const prevMsg = await this.databaseClass.getPrevByNumber(from)
//Consultamos for refSerializada en el flow actual
const refToContinue = this.flowClass.findBySerialize(
prevMsg?.refSerialize
)
if (prevMsg?.ref) {
const ctxByNumber = toCtx({
@@ -61,26 +67,32 @@ class CoreClass {
this.databaseClass.save(ctxByNumber)
}
if (prevMsg?.refSerialize && prevMsg?.options?.capture) {
const refToContinue = this.flowClass.findBySerialize(
prevMsg.refSerialize
)
//Si se tiene un callback se ejecuta
if (refToContinue && prevMsg?.options?.callback) {
const indexFlow = this.flowClass.findIndexByRef(refToContinue?.ref)
this.flowClass.allCallbacks[indexFlow].callback(messageInComming)
}
if (refToContinue && prevMsg?.options?.callback) {
const indexFlow = this.flowClass.findIndexByRef(
refToContinue?.ref
)
//Si se tiene anidaciones de flows, si tienes anidados obligatoriamente capture:true
if (prevMsg?.options?.nested?.length) {
const nestedRef = prevMsg.options.nested
const flowStandalone = nestedRef.map((f) => ({
...this.flowClass.findBySerialize(f),
}))
this.flowClass.allCallbacks[indexFlow].callback(
messageInComming
)
}
msgToSend = this.flowClass.find(body, false, flowStandalone) || []
this.sendFlow(msgToSend, from)
return
}
//Consultamos si se espera respuesta por parte de cliente "Ejemplo: Dime tu nombre"
if (!prevMsg?.options?.nested?.length && prevMsg?.options?.capture) {
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
} else {
msgToSend = this.flowClass.find(body) || []
}
if (Array.isArray(msgToSend)) this.sendFlow(msgToSend, from)
this.sendFlow(msgToSend, from)
}
sendProviderAndSave = (numberOrId, ctxMessage) => {

View File

@@ -1,4 +1,4 @@
const { toSerialize } = require('./methods')
const { toSerialize } = require('./methods/toSerialize')
class FlowClass {
allCallbacks = []
@@ -20,16 +20,13 @@ class FlowClass {
this.flowSerialize = toSerialize(mergeToJsonSerialize)
}
find = (keyOrWord, symbol = false) => {
find = (keyOrWord, symbol = false, overFlow = null) => {
let capture = false
let messages = []
let refSymbol = null
overFlow = overFlow ?? this.flowSerialize
const findIn = (
keyOrWord,
symbol = false,
flow = this.flowSerialize
) => {
const findIn = (keyOrWord, symbol = false, flow = overFlow) => {
capture = refSymbol?.options?.capture || false
if (capture) return messages

View File

@@ -1,5 +1,6 @@
const { generateRef } = require('../../utils/hash')
const { toJson } = require('./toJson')
const { toSerialize } = require('./toSerialize')
/**
*
* @param answer string
@@ -8,7 +9,11 @@ const { toJson } = require('./toJson')
*/
const addAnswer =
(inCtx) =>
(answer, options, cb = null) => {
(answer, options, cb = null, nested = []) => {
/**
* Todas las opciones referentes a el mensaje en concreto options:{}
* @returns
*/
const getAnswerOptions = () => ({
media:
typeof options?.media === 'string' ? `${options?.media}` : null,
@@ -21,17 +26,28 @@ const addAnswer =
typeof options?.child === 'string' ? `${options?.child}` : null,
})
const getNested = () => ({
nested: Array.isArray(nested) ? nested : [],
})
const callback =
typeof cb === 'function'
? cb
: () => console.log('Callback no definida')
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
/**
* Esta funcion se encarga de mapear y transformar todo antes
* de retornar
* @returns
*/
const ctxAnswer = () => {
const ref = `ans_${generateRef()}`
const callback =
typeof cb === 'function'
? cb
: () => console.log('Callback no definida')
const options = {
...getAnswerOptions(),
...getNested(),
keyword: {},
callback: !!cb,
}

View File

@@ -3,7 +3,7 @@ const { generateRefSerialize } = require('../../utils/hash')
/**
* Crear referencia serializada
* @param {*} flowJson
* @returns
* @returns array[]
*/
const toSerialize = (flowJson) => {
if (!Array.isArray(flowJson)) throw new Error('Esto debe ser un ARRAY')