Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

patch-queue.js 2.8KB

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