dappros/ethora

View on GitHub
bots/questionnaire/handlers/question.js

Summary

Maintainability
A
1 hr
Test Coverage
import {sendMessage, userSteps} from "../actions.js";
import messages from "../config/messages.js";
import {getListAnswers} from "../controllers/answers.js";
import {getListQuestions} from "../controllers/questions.js";

export const questionHandler = (data) => {
    getListQuestions().then(result => {
        let questions = result;
        getListAnswers(data.receiver).then(result => {
            if(questions.length === result.length){
                return sendMessage(
                    data,
                    messages.bot.answeredAllQuestions,
                    'message',
                    false,
                    0,
                );
            }
            const lastAnswerIndex = result.length;
            userSteps('setStep', data.receiver, 2);
            return sendMessage(
                data,
                questions[lastAnswerIndex].question,
                'message',
                false,
                0,
            );
        }).catch(error => {
            console.log('Error | getListQuestions: ', error);
            return sendMessage(
                data,
                messages.errors.requestError,
                'message',
                false,
                0,
            );
        });
    }).catch(error => {
        console.log('Error | getListQuestions: ', error);
        return sendMessage(
            data,
            messages.errors.requestError,
            'message',
            false,
            0,
        );
    });
}