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.

index.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const params = require('../../schemas/params')
  2. const pluginConfig = {
  3. handlerType: 'notifictaion',
  4. docs: {
  5. description: 'subscribe',
  6. notes: 'Subscribe to notifications based on profile_id',
  7. },
  8. opts: {
  9. tags: ['api'],
  10. auth: { strategy: 'default_jwt' },
  11. cors: true,
  12. },
  13. }
  14. const validators = {
  15. params: params.profileId,
  16. }
  17. module.exports = {
  18. method: 'GET',
  19. path: '/{profile_id}/subscribe',
  20. options: {
  21. ...pluginConfig.docs,
  22. ...pluginConfig.opts,
  23. handler: async (request, h) => {
  24. const { profile_id } = request.params
  25. /**
  26. * Write the initial stream
  27. * !: this must remain open for notifications to work
  28. */
  29. return request.server.methods.notify(
  30. `${profile_id}.stonk`,
  31. {
  32. profile_id,
  33. name: 'BDGRS',
  34. price: (500 + Math.floor(Math.random() * 100)).toString(),
  35. order: Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL',
  36. type: 'info',
  37. },
  38. h,
  39. true,
  40. )
  41. },
  42. /** Validate based on validators object */
  43. validate: {
  44. ...validators,
  45. failAction: 'log',
  46. },
  47. },
  48. }