'use strict' const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const surveyResponseSchema = require('../../schemas/responses') const pluginConfig = { handlerType: 'match', docs: { description: 'matches', notes: 'Match everyone', }, } const validators = {} const responseSchemas = { response: surveyResponseSchema.list, error: errorSchema.single, } module.exports = { method: 'GET', path: '/match', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, handler: async function (request, h) { const { matchService, matchQueueService } = request.server.services() const allQueues = await matchQueueService.getAllQueues() const matched = await matchService.calcMatches(allQueues) try { if (!allQueues) { throw new RangeError('Unable to match profiles') } return h .response({ ok: true, handler: pluginConfig.handlerType, data: matched, }) .code(200) } catch (err) { return h .response({ ok: false, handler: pluginConfig.handlerType, data: { error: `${err}` }, }) .code(409) } }, /** Validate based on validators object */ validate: { ...validators, failAction: 'log', }, /** Validate the server response */ response: { status: { 200: apiSchema.single .append({ data: responseSchemas.response, }) .label('response_list_res'), 409: apiSchema.single .append({ data: responseSchemas.error, }) .label('error_single_res'), }, }, }, }