| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 'use strict'
-
- const Joi = require('joi')
-
- const pluginConfig = {
- handlerType: 'profile',
- docs: {
- description: 'Updates match queue in place',
- notes: 'Updates in place and does not delete from table',
- },
- }
-
- const responseSchemas = {
- responses: 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: Joi.object({
- error: Joi.string(),
- }),
- }
-
- 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: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.responses,
- }),
- 409: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.error,
- }),
- },
- },
- },
- }
|