Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

patch-queue.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. profile_id: Joi.number(),
  18. user_id: Joi.number(),
  19. user_name: Joi.string(),
  20. responses: Joi.array().items(),
  21. tags: Joi.array().items(),
  22. user_media: Joi.string(),
  23. user_type: Joi.any(),
  24. }),
  25. ),
  26. ),
  27. error: errorSchema.single
  28. }
  29. const validators = {
  30. params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
  31. query: Joi.object({ include_profile: Joi.bool(), reinsert: Joi.boolean() }),
  32. }
  33. module.exports = {
  34. method: 'PATCH',
  35. path: '/{profile_id}/queue/{target_id}/delete',
  36. options: {
  37. ...pluginConfig.docs,
  38. tags: ['api'],
  39. /** Protect this route with authentication? */
  40. auth: false,
  41. cors: true,
  42. handler: async function (request, h) {
  43. const { profile_id, target_id } = request.params
  44. const { include_profile, reinsert } = request.query
  45. const { profileService, matchQueueService } =
  46. request.server.services()
  47. // console.log('reinsert', reinsert)
  48. const updatedQueue = await matchQueueService.markAsDeleted(
  49. profile_id,
  50. target_id,
  51. reinsert,
  52. )
  53. const queueIds = updatedQueue.map(entry => entry.target_id)
  54. const res = {
  55. ok: true,
  56. handler: pluginConfig.handlerType,
  57. data: queueIds,
  58. }
  59. if (include_profile) {
  60. res.data = await profileService.getProfilesFor(queueIds)
  61. }
  62. try {
  63. return h.response(res).code(200)
  64. } catch (err) {
  65. return h
  66. .response({
  67. ok: false,
  68. handler: pluginConfig.handlerType,
  69. data: { error: `${err}` },
  70. })
  71. .code(409)
  72. }
  73. },
  74. /** Validate based on validators object */
  75. validate: {
  76. ...validators,
  77. failAction: 'log',
  78. },
  79. /** Validate the server response */
  80. response: {
  81. status: {
  82. 200: apiSchema.single.append({
  83. data: responseSchemas.response,
  84. }),
  85. 409: apiSchema.single.append({
  86. data: responseSchemas.error,
  87. })
  88. },
  89. },
  90. },
  91. }