This commit is contained in:
Leifer Mendez
2022-11-02 21:10:18 +01:00
parent 33797ce9de
commit 0a9e14c460
5 changed files with 45 additions and 7 deletions

View File

@@ -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),
}
}

View File

@@ -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()

View File

@@ -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 }

View File

@@ -0,0 +1,6 @@
const toJson = (inCtx) => () => {
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
return lastCtx.json
}
module.exports = { toJson }

View File

@@ -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()