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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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:Joi.object({
  19. max_distance: Joi.number(),
  20. unit: Joi.string(),
  21. }),
  22. /** Validate the incoming payload (POST method) */
  23. // payload: true,
  24. }
  25. const responseSchemas = {
  26. response: Joi.array().items(),
  27. error: Joi.object({
  28. error: Joi.string(),
  29. }),
  30. }
  31. module.exports = {
  32. method: 'GET',
  33. path: '/{profile_id}/score',
  34. options: {
  35. ...pluginConfig.docs,
  36. tags: ['api'],
  37. /** Protect this route with authentication? */
  38. auth: false,
  39. handler: async function (request, h) {
  40. const { profileService } = request.services()
  41. const profileId = request.params.profile_id
  42. const maxDistanceMiles = request.query.max_distance
  43. const distanceUnit = request.query.unit ? request.query.unit : 'mile'
  44. const profiles = await profileService.scoreProfilesFor(profileId, maxDistanceMiles, distanceUnit)
  45. try {
  46. if(!profiles){
  47. throw new RangeError('Unable to score profiles')
  48. }
  49. return h.response({
  50. ok: true,
  51. handler: pluginConfig.handlerType,
  52. data: profiles,
  53. }).code(200)
  54. } catch (err) {
  55. return h.response({
  56. ok: false,
  57. handler: pluginConfig.handlerType,
  58. data: { error: `${err}` },
  59. }).code(409)
  60. }
  61. },
  62. /** Validate based on validators object */
  63. validate: {
  64. ...validators,
  65. failAction: 'log',
  66. },
  67. /** Validate the server response */
  68. response: {
  69. status: {
  70. 200: Joi.object({
  71. ok: Joi.bool(),
  72. handler: Joi.string(),
  73. data: responseSchemas.response,
  74. }),
  75. 409: Joi.object({
  76. ok: Joi.bool(),
  77. handler: Joi.string(),
  78. data: responseSchemas.error,
  79. }),
  80. }
  81. },
  82. },
  83. }