選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

patch-queue.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Joi.number().optional(),
  18. profileSchema.single.optional(),
  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. 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:
  51. include_profile == true
  52. ? await profileService.getProfilesFor(queueIds)
  53. : 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
  76. .append({
  77. data: responseSchemas.response,
  78. })
  79. .label('match_queue_res'),
  80. 409: apiSchema.single
  81. .append({
  82. data: responseSchemas.error,
  83. })
  84. .label('error_single_res'),
  85. },
  86. },
  87. },
  88. }