| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 'use strict'
-
- const Joi = require('joi')
- const apiSchema = require('../../schemas/api')
- const surveyResponseSchema = require('../../schemas/responses')
- const params = require('../../schemas/params')
-
- const pluginConfig = {
- handlerType: 'survey',
- docs: {
- description: 'Get responses to questions',
- notes: 'Returns a list of all survey responses for a user',
- },
- }
-
- /** Validator functions by request method */
- const validators = {
- /** Validate the header (cookie check) */
- // headers: true,
- /** Validate the route params (/active/{thing}) */
- params: params.userId,
- /** Validate the route query (/active/{thing}?limit=10&offset=10) */
- // query: true,
- /** Validate the incoming payload (POST method) */
- // payload: true,
- }
- const responseSchemas = {
- response: surveyResponseSchema.keys,
- }
-
- module.exports = {
- method: 'GET',
- path: '/questions',
- options: {
- ...pluginConfig.docs,
- tags: ['api'],
- /** Protect this route with authentication? */
- auth: false,
-
- handler: async function (request, h) {
- const { responseService } = request.services()
- const responseKeys = await responseService.getResponseKeys()
- try {
- return {
- ok: true,
- handler: pluginConfig.handlerType,
- data: responseKeys,
- }
- } catch (err) {
- return {
- ok: false,
- handler: pluginConfig.handlerType,
- data: { error: err },
- }
- }
- },
-
- /** Validate based on validators object */
- validate: { ...validators, failAction: 'log' },
-
- /** Validate the server response */
- response: {
- schema: apiSchema.single
- .append({
- data: responseSchemas.response,
- })
- .label('response_list_res'),
- failAction: 'log',
- },
- },
- }
|