| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 'use strict'
-
- const Joi = require('joi')
- const apiSchema = require('../../schemas/api')
- const errorSchema = require('../../schemas/errors')
-
- 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(),
- Joi.object({
- profile_id: Joi.number(),
- user_id: Joi.number(),
- user_name: Joi.string(),
- responses: Joi.array().items(),
- user_media: Joi.string(),
- user_type: Joi.any(),
- user: Joi.object()
- }),
- )
- ),
- error: errorSchema.single
- }
-
- const validators = {
- params: Joi.object({ profile_id: Joi.number() }),
- query: Joi.object({ include_profile: Joi.bool() }),
- }
-
- 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 { 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)
- }
- 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,
- }),
- 409: apiSchema.single.append({
- data: responseSchemas.error,
- })
- },
- },
- },
- }
|