'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const pluginConfig = { handlerType: 'profile', docs: { description: 'Updates match queue in place', notes: 'Updates in place and does not delete from table', }, } const responseSchemas = { response: Joi.array().items( Joi.alternatives().try( Joi.number(), Joi.object({ // ORIGINAL profile_id: Joi.number(), user_id: Joi.number(), // Added user_name, user_media to account for updated CompleteProfile user_name: Joi.string(), user_media: Joi.string(), responses: Joi.array().items(), user_type: Joi.any(), }), ), ), error: errorSchema.single } const validators = { params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }), query: Joi.object({ include_profile: Joi.bool(), reinsert: Joi.boolean() }), } module.exports = { method: 'PATCH', path: '/{profile_id}/queue/{target_id}/delete', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, cors: true, handler: async function (request, h) { const { profile_id, target_id } = request.params const { include_profile, reinsert } = request.query const { profileService, matchQueueService } = request.server.services() console.log('reinsert', reinsert) const updatedQueue = await matchQueueService.markAsDeleted( profile_id, target_id, reinsert, ) const queueIds = updatedQueue.map(entry => entry.target_id) const res = { ok: true, handler: pluginConfig.handlerType, data: 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, }) }, }, }, }