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

View File

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

View File

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

View File

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