mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
toJson
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const { generateRef } = require('../utils')
|
||||
|
||||
const { toJson } = require('./toJson')
|
||||
/**
|
||||
*
|
||||
* @param answer string
|
||||
@@ -14,7 +14,15 @@ const addAnswer = (inCtx) => (answer, options) => {
|
||||
|
||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
||||
const ctxAnswer = () => {
|
||||
const ref = generateRef()
|
||||
const ref = `ans_${generateRef()}`
|
||||
|
||||
const json = [].concat(inCtx.json).concat([
|
||||
{
|
||||
ref,
|
||||
keyword: lastCtx.ref,
|
||||
answer,
|
||||
},
|
||||
])
|
||||
/**
|
||||
* Se guarda en db
|
||||
*/
|
||||
@@ -24,7 +32,7 @@ const addAnswer = (inCtx) => (answer, options) => {
|
||||
keyword: {},
|
||||
}
|
||||
|
||||
return { ...lastCtx, ref, answer, options }
|
||||
return { ...lastCtx, ref, answer, json, options }
|
||||
}
|
||||
|
||||
const ctx = ctxAnswer()
|
||||
@@ -33,6 +41,7 @@ const addAnswer = (inCtx) => (answer, options) => {
|
||||
ctx,
|
||||
ref: ctx.ref,
|
||||
addAnswer: addAnswer(ctx),
|
||||
toJson: toJson(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ const { addAnswer } = require('./addAnswer')
|
||||
* @param {*} message `string | string[]`
|
||||
* @param {*} options {sensitive:boolean} default
|
||||
*/
|
||||
const addKeyword = (message, options) => {
|
||||
const addKeyword = (keyword, options) => {
|
||||
/**
|
||||
* Esta funcion deberia parsear y validar las opciones
|
||||
* del keyword
|
||||
@@ -25,13 +25,19 @@ const addKeyword = (message, options) => {
|
||||
}
|
||||
|
||||
const ctxAddKeyword = () => {
|
||||
const ref = generateRef()
|
||||
const ref = `key_${generateRef()}`
|
||||
const options = parseOptions()
|
||||
const json = [
|
||||
{
|
||||
ref,
|
||||
keyword,
|
||||
},
|
||||
]
|
||||
/**
|
||||
* Se guarda en db
|
||||
*/
|
||||
|
||||
return { ref, keyword: message, options }
|
||||
return { ref, keyword, options, json }
|
||||
}
|
||||
|
||||
const ctx = ctxAddKeyword()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const { addAnswer } = require('./addAnswer')
|
||||
const { addKeyword } = require('./addKeyword')
|
||||
const { toJson } = require('./toJson')
|
||||
|
||||
module.exports = { addAnswer, addKeyword }
|
||||
module.exports = { addAnswer, addKeyword, toJson }
|
||||
|
||||
6
packages/io/methods/toJson.js
Normal file
6
packages/io/methods/toJson.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const toJson = (inCtx) => () => {
|
||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
||||
return lastCtx.json
|
||||
}
|
||||
|
||||
module.exports = { toJson }
|
||||
@@ -75,4 +75,20 @@ test('Debere probar error las addAnswer', () => {
|
||||
assert.is(MAIN_CTX.ctx.options.answer.buttons.length, 0)
|
||||
})
|
||||
|
||||
test('Obtener toJson', () => {
|
||||
const [ctxA, ctxB, ctxC] = addKeyword('hola')
|
||||
.addAnswer('pera!')
|
||||
.addAnswer('chao')
|
||||
.toJson()
|
||||
|
||||
assert.is(ctxA.keyword, 'hola')
|
||||
assert.match(ctxA.ref, /^key_/)
|
||||
|
||||
assert.is(ctxB.answer, 'pera!')
|
||||
assert.match(ctxB.ref, /^ans_/)
|
||||
|
||||
assert.is(ctxC.answer, 'chao')
|
||||
assert.match(ctxC.ref, /^ans_/)
|
||||
})
|
||||
|
||||
test.run()
|
||||
|
||||
Reference in New Issue
Block a user