online_notifications_controller.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class OnlineNotificationsController < ApplicationController
  3. prepend_before_action -> { authorize! }, only: %i[show update destroy]
  4. prepend_before_action :authentication_check
  5. =begin
  6. Format:
  7. JSON
  8. Example:
  9. {
  10. "id": 123,
  11. "o_id": 628,
  12. "object": "Ticket",
  13. "type": "escalation",
  14. "seen": true,
  15. "updated_at": "2016-08-16T07:55:42.119Z",
  16. "created_at": "2016-08-16T07:55:42.119Z"
  17. }
  18. =end
  19. =begin
  20. Resource:
  21. GET /api/v1/online_notifications
  22. Response:
  23. [
  24. {
  25. "id": 1,
  26. "object": "Ticket",
  27. "type": "escalation",
  28. "seen": true,
  29. ...
  30. },
  31. {
  32. "id": 2,
  33. "object": "Ticket",
  34. "type": "escalation",
  35. "seen": false,
  36. ...
  37. }
  38. ]
  39. Test:
  40. curl http://localhost/api/v1/online_notifications -v -u #{login}:#{password}
  41. =end
  42. def index
  43. online_notifications = OnlineNotification.list(current_user)
  44. if response_expand?
  45. list = online_notifications.map(&:attributes_with_association_names)
  46. render json: list, status: :ok
  47. return
  48. end
  49. if response_full?
  50. assets = {}
  51. item_ids = []
  52. online_notifications.each do |item|
  53. item_ids.push item['id']
  54. assets = item.assets(assets)
  55. end
  56. render json: {
  57. record_ids: item_ids,
  58. assets: assets,
  59. }, status: :ok
  60. return
  61. end
  62. all = online_notifications.map(&:attributes_with_association_ids)
  63. render json: all, status: :ok
  64. end
  65. =begin
  66. Resource:
  67. GET /api/v1/online_notifications/#{id}
  68. Response:
  69. {
  70. "id": 123,
  71. "o_id": 628,
  72. "object": "Ticket",
  73. "type": "escalation",
  74. "seen": true,
  75. "updated_at": "2016-08-16T07:55:42.119Z",
  76. "created_at": "2016-08-16T07:55:42.119Z"
  77. }
  78. Test:
  79. curl http://localhost/api/v1/online_notifications/#{id} -v -u #{login}:#{password}
  80. =end
  81. def show
  82. model_show_render(OnlineNotification, params)
  83. end
  84. =begin
  85. Resource:
  86. PUT /api/v1/online_notifications/{id}
  87. Payload:
  88. {
  89. "id": 123,
  90. "o_id": 628,
  91. "object": "Ticket",
  92. "type": "escalation",
  93. "seen": true,
  94. "updated_at": "2016-08-16T07:55:42.119Z",
  95. "created_at": "2016-08-16T07:55:42.119Z"
  96. }
  97. Response:
  98. {
  99. "id": 123,
  100. "o_id": 628,
  101. "object": "Ticket",
  102. "type": "escalation",
  103. "seen": true,
  104. "updated_at": "2016-08-16T07:55:42.119Z",
  105. "created_at": "2016-08-16T07:55:42.119Z"
  106. }
  107. Test:
  108. curl http://localhost/api/v1/online_notifications -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}'
  109. =end
  110. def update
  111. model_update_render(OnlineNotification, params)
  112. end
  113. =begin
  114. Resource:
  115. DELETE /api/v1/online_notifications/{id}.json
  116. Response:
  117. {}
  118. Test:
  119. curl http://localhost/api/v1/online_notifications/{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  120. =end
  121. def destroy
  122. model_destroy_render(OnlineNotification, params)
  123. end
  124. =begin
  125. Resource:
  126. PUT /api/v1/online_notifications/mark_all_as_read
  127. Payload:
  128. {}
  129. Response:
  130. {}
  131. Test:
  132. curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login}:#{password} -X POST -d '{}'
  133. =end
  134. def mark_all_as_read
  135. OnlineNotification
  136. .list(current_user, limit: nil, access: 'ignore')
  137. .where(seen: false)
  138. .in_batches
  139. .each_record do |notification|
  140. notification.update!(seen: true)
  141. end
  142. render json: {}, status: :ok
  143. end
  144. end