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

score.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'score',
  5. docs: {
  6. description: 'scores',
  7. notes: 'A list of profile scores',
  8. },
  9. }
  10. const validators = {
  11. /** Validate the header (cookie check) */
  12. // headers: true,
  13. /** Validate the route params (/active/{thing}) */
  14. params: Joi.object({
  15. profile_id: Joi.number(),
  16. }),
  17. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  18. // query: true,
  19. /** Validate the incoming payload (POST method) */
  20. // payload: true,
  21. }
  22. const responseSchemas = {
  23. response: Joi.array().items(),
  24. error: Joi.object({
  25. error: Joi.string(),
  26. }),
  27. }
  28. module.exports = {
  29. method: 'GET',
  30. path: '/{profile_id}/score',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. handler: async function (request, h) {
  37. const { profileService } = request.services()
  38. const profileId = request.params.profile_id
  39. const profiles = await profileService.scoreProfilesFor(profileId)
  40. try {
  41. if(!profiles){
  42. throw new RangeError('Unable to score profiles')
  43. }
  44. return h.response({
  45. ok: true,
  46. handler: pluginConfig.handlerType,
  47. data: profiles,
  48. }).code(200)
  49. } catch (err) {
  50. return h.response({
  51. ok: false,
  52. handler: pluginConfig.handlerType,
  53. data: { error: `${err}` },
  54. }).code(409)
  55. }
  56. },
  57. /** Validate based on validators object */
  58. validate: {
  59. ...validators,
  60. failAction: 'log',
  61. },
  62. /** Validate the server response */
  63. response: {
  64. status: {
  65. 200: Joi.object({
  66. ok: Joi.bool(),
  67. handler: Joi.string(),
  68. data: responseSchemas.response,
  69. }),
  70. 409: Joi.object({
  71. ok: Joi.bool(),
  72. handler: Joi.string(),
  73. data: responseSchemas.error,
  74. }),
  75. }
  76. },
  77. },
  78. }