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

patch-queue.js 2.7KB

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