You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

patch-queue.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const errorSchema = require('../../schemas/errors')
  5. const pluginConfig = {
  6. handlerType: 'profile',
  7. docs: {
  8. description: 'Updates match queue in place',
  9. notes: 'Updates in place and does not delete from table',
  10. },
  11. }
  12. const responseSchemas = {
  13. response: Joi.array().items(
  14. Joi.alternatives().try(
  15. Joi.number(),
  16. Joi.object({
  17. // ORIGINAL
  18. profile_id: Joi.number(),
  19. user_id: Joi.number(),
  20. // Added user_name, user_media to account for updated CompleteProfile
  21. user_name: Joi.string(),
  22. user_media: Joi.string(),
  23. responses: Joi.array().items(),
  24. user_type: Joi.any(),
  25. }),
  26. ),
  27. ),
  28. error: errorSchema.single
  29. }
  30. const validators = {
  31. params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
  32. query: Joi.object({ include_profile: Joi.bool(), reinsert: Joi.boolean() }),
  33. }
  34. module.exports = {
  35. method: 'PATCH',
  36. path: '/{profile_id}/queue/{target_id}/delete',
  37. options: {
  38. ...pluginConfig.docs,
  39. tags: ['api'],
  40. /** Protect this route with authentication? */
  41. auth: false,
  42. cors: true,
  43. handler: async function (request, h) {
  44. const { profile_id, target_id } = request.params
  45. const { include_profile, reinsert } = request.query
  46. const { profileService, matchQueueService } =
  47. request.server.services()
  48. // console.log('reinsert', reinsert)
  49. const updatedQueue = await matchQueueService.markAsDeleted(
  50. profile_id,
  51. target_id,
  52. reinsert,
  53. )
  54. const queueIds = updatedQueue.map(entry => entry.target_id)
  55. const res = {
  56. ok: true,
  57. handler: pluginConfig.handlerType,
  58. data: queueIds,
  59. }
  60. if (include_profile) {
  61. res.data = await profileService.getProfilesFor(queueIds)
  62. }
  63. try {
  64. return h.response(res).code(200)
  65. } catch (err) {
  66. return h
  67. .response({
  68. ok: false,
  69. handler: pluginConfig.handlerType,
  70. data: { error: `${err}` },
  71. })
  72. .code(409)
  73. }
  74. },
  75. /** Validate based on validators object */
  76. validate: {
  77. ...validators,
  78. failAction: 'log',
  79. },
  80. /** Validate the server response */
  81. response: {
  82. status: {
  83. 200: apiSchema.single.append({
  84. data: responseSchemas.response,
  85. }),
  86. 409: apiSchema.single.append({
  87. data: responseSchemas.error,
  88. })
  89. },
  90. },
  91. },
  92. }