You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 pluginConfig = {
  7. handlerType: 'profile',
  8. docs: {
  9. description: 'Returns a single profile with tags',
  10. notes: 'returns from the Profiles Table',
  11. },
  12. }
  13. const responseSchemas = {
  14. profile: profileSchema.single,
  15. error: errorSchema.single,
  16. }
  17. const validators = {
  18. params: Joi.object({ profile_id: Joi.number() }),
  19. }
  20. module.exports = {
  21. method: 'GET',
  22. path: '/{profile_id}',
  23. options: {
  24. ...pluginConfig.docs,
  25. tags: ['api'],
  26. /** Protect this route with authentication? */
  27. auth: false,
  28. cors: true,
  29. handler: async function (request, h) {
  30. const { profile_id } = request.params
  31. const { profileService } = request.server.services()
  32. const res = {
  33. ok: true,
  34. handler: pluginConfig.handlerType,
  35. data: null,
  36. }
  37. res.data = await profileService.getProfile(profile_id)
  38. try {
  39. return h.response(res).code(200)
  40. } catch (err) {
  41. return h
  42. .response({
  43. ok: false,
  44. handler: pluginConfig.handlerType,
  45. data: { error: `${err}` },
  46. })
  47. .code(409)
  48. }
  49. },
  50. /** Validate based on validators object */
  51. validate: {
  52. ...validators,
  53. failAction: 'log',
  54. },
  55. /** Validate the server response */
  56. response: {
  57. status: {
  58. 200: apiSchema.single.append({
  59. data: responseSchemas.profile,
  60. }),
  61. 409: apiSchema.single.append({
  62. data: responseSchemas.error,
  63. }),
  64. },
  65. },
  66. },
  67. }