'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const profileSchema = require('../../schemas/profiles') const pluginConfig = { handlerType: 'profile', docs: { description: 'Returns previously scored profiles', notes: 'returns from the MatchQueue Table', }, } const responseSchemas = { response: Joi.array().items( Joi.alternatives().try(Joi.number(), profileSchema.single), ), error: errorSchema.single, } const validators = { params: Joi.object({ profile_id: Joi.string(), }), query: Joi.object({ include_profile: Joi.bool(), limit: Joi.number(), offset: Joi.number(), }), } module.exports = { method: 'GET', path: '/{profile_id}/queue', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, cors: true, handler: async function (request, h) { const { profile_id } = request.params const { limit, offset } = request.query const { profileService, matchQueueService } = request.server.services() const queue = await matchQueueService.getQueue( profile_id, limit, offset, ) const queueIds = queue.map(entry => entry.target_id) // HELP: I think there's an issue here // queueIds spits out the queue profiles in the correct order // ~However~ when it goes through getProfilesFor // it comes back in literal database order regardless of is_deleted status // console.log( // 'include_profile results', // await profileService.getProfilesFor(queueIds), // ) try { return h .response({ ok: true, handler: pluginConfig.handlerType, data: await profileService.getProfilesFor( queueIds, 'participant', ), }) .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('match_queue_res'), 409: apiSchema.single .append({ data: responseSchemas.error, }) .label('error_single_res'), }, }, }, }