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

patch-queue.js 2.8KB

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