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

patch-queue.js 2.7KB

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