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

patch-queue.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  14. const responseSchemas = {
  15. response: Joi.array().items(
  16. Joi.alternatives().try(
  17. params.profileId,
  18. profileSchema.single,
  19. ),
  20. ),
  21. error: errorSchema.single
  22. }
  23. const validators = {
  24. params: params.profileId.append({ target_id: Joi.number() }),
  25. query: params.profileInclude.append({ reinsert: Joi.boolean() }),
  26. }
  27. module.exports = {
  28. method: 'PATCH',
  29. path: '/{profile_id}/queue/{target_id}/delete',
  30. options: {
  31. ...pluginConfig.docs,
  32. tags: ['api'],
  33. /** Protect this route with authentication? */
  34. auth: false,
  35. cors: true,
  36. handler: async function (request, h) {
  37. const { profile_id, target_id } = request.params
  38. const { include_profile, reinsert } = request.query
  39. const { profileService, matchQueueService } =
  40. request.server.services()
  41. // console.log('reinsert', reinsert)
  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
  60. .response({
  61. ok: false,
  62. handler: pluginConfig.handlerType,
  63. data: { error: `${err}` },
  64. })
  65. .code(409)
  66. }
  67. },
  68. /** Validate based on validators object */
  69. validate: {
  70. ...validators,
  71. failAction: 'log',
  72. },
  73. /** Validate the server response */
  74. response: {
  75. status: {
  76. 200: apiSchema.single.append({
  77. data: responseSchemas.response,
  78. }).label('match_queue_res'),
  79. 409: apiSchema.single.append({
  80. data: responseSchemas.error,
  81. }).label('error_single_res')
  82. },
  83. },
  84. },
  85. }