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

patch-queue.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 params = require('../../schemas/params')
  7. const pluginConfig = {
  8. handlerType: 'profile',
  9. docs: {
  10. description: 'Updates match queue in place',
  11. notes: 'Updates in place and does not delete from table',
  12. },
  13. opts: {
  14. tags: ['api'],
  15. auth:
  16. process.env.USE_AUTH != 'true'
  17. ? false
  18. : { strategy: 'default_jwt' },
  19. cors: true,
  20. },
  21. }
  22. const responseSchemas = {
  23. response: Joi.array().items(
  24. Joi.alternatives().try(
  25. Joi.number().optional(),
  26. profileSchema.single.optional(),
  27. ),
  28. ),
  29. error: errorSchema.single,
  30. }
  31. const validators = {
  32. params: params.profileId.append({ target_id: Joi.number() }),
  33. query: params.profileInclude.append({ reinsert: Joi.boolean() }),
  34. }
  35. module.exports = {
  36. method: 'PATCH',
  37. path: '/{profile_id}/queue/{target_id}/delete',
  38. options: {
  39. ...pluginConfig.docs,
  40. ...pluginConfig.opts,
  41. handler: async function (request, h) {
  42. const { profile_id, target_id } = request.params
  43. const { include_profile, reinsert } = request.query
  44. const { profileService, matchQueueService } =
  45. request.server.services()
  46. const updatedQueue = await matchQueueService.markAsDeleted(
  47. profile_id,
  48. target_id,
  49. reinsert,
  50. )
  51. const queueIds = updatedQueue.map(entry => entry.target_id)
  52. const res = {
  53. ok: true,
  54. handler: pluginConfig.handlerType,
  55. data:
  56. include_profile == true
  57. ? await profileService.getProfilesFor(queueIds)
  58. : queueIds,
  59. }
  60. try {
  61. return h.response(res).code(200)
  62. } catch (err) {
  63. return h
  64. .response({
  65. ok: false,
  66. handler: pluginConfig.handlerType,
  67. data: { error: `${err}` },
  68. })
  69. .code(409)
  70. }
  71. },
  72. /** Validate based on validators object */
  73. validate: {
  74. ...validators,
  75. failAction: 'log',
  76. },
  77. /** Validate the server response */
  78. response: {
  79. status: {
  80. 200: apiSchema.single
  81. .append({
  82. data: responseSchemas.response,
  83. })
  84. .label('match_queue_res'),
  85. 409: apiSchema.single
  86. .append({
  87. data: responseSchemas.error,
  88. })
  89. .label('error_single_res'),
  90. },
  91. },
  92. },
  93. }