'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const profileSchema = require('../../schemas/profiles') const params = require('../../schemas/params') const pluginConfig = { handlerType: 'profile', docs: { description: 'Returns previously scored profiles', notes: 'returns from the MatchQueue Table', }, opts: { tags: ['api'], auth: { strategy: 'default_jwt' }, cors: true, }, } const responseSchemas = { response: Joi.array().items( Joi.alternatives().try(Joi.number(), profileSchema.single), ), error: errorSchema.single, } const validators = { params: params.profileId, query: params.profileInclude, } module.exports = { method: 'GET', path: '/{profile_id}/queue', options: { ...pluginConfig.docs, ...pluginConfig.opts, handler: async function (request, h) { const { profile_id } = request.params const { include_profile } = request.query const { profileService, matchQueueService } = request.server.services() const queue = await matchQueueService.getQueue(profile_id) const queueIds = queue.map(entry => entry.target_id) // console.log('queueIds', queueIds) const res = { ok: true, handler: pluginConfig.handlerType, data: queueIds, } // 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), // ) if (include_profile) { res.data = await profileService.getProfilesFor( queueIds, 'participant', false, ) } try { return h.response(res).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'), }, }, }, }