online_notifications_controller.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright (C) 2012-2023 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, 200)
  44. if response_expand?
  45. list = []
  46. online_notifications.each do |item|
  47. list.push item.attributes_with_association_names
  48. end
  49. render json: list, status: :ok
  50. return
  51. end
  52. if response_full?
  53. assets = {}
  54. item_ids = []
  55. online_notifications.each do |item|
  56. item_ids.push item['id']
  57. assets = item.assets(assets)
  58. end
  59. render json: {
  60. record_ids: item_ids,
  61. assets: assets,
  62. }, status: :ok
  63. return
  64. end
  65. all = []
  66. online_notifications.each do |item|
  67. all.push item.attributes_with_association_ids
  68. end
  69. render json: all, status: :ok
  70. end
  71. =begin
  72. Resource:
  73. GET /api/v1/online_notifications/#{id}
  74. Response:
  75. {
  76. "id": 123,
  77. "o_id": 628,
  78. "object": "Ticket",
  79. "type": "escalation",
  80. "seen": true,
  81. "updated_at": "2016-08-16T07:55:42.119Z",
  82. "created_at": "2016-08-16T07:55:42.119Z"
  83. }
  84. Test:
  85. curl http://localhost/api/v1/online_notifications/#{id} -v -u #{login}:#{password}
  86. =end
  87. def show
  88. model_show_render(OnlineNotification, params)
  89. end
  90. =begin
  91. Resource:
  92. PUT /api/v1/online_notifications/{id}
  93. Payload:
  94. {
  95. "id": 123,
  96. "o_id": 628,
  97. "object": "Ticket",
  98. "type": "escalation",
  99. "seen": true,
  100. "updated_at": "2016-08-16T07:55:42.119Z",
  101. "created_at": "2016-08-16T07:55:42.119Z"
  102. }
  103. Response:
  104. {
  105. "id": 123,
  106. "o_id": 628,
  107. "object": "Ticket",
  108. "type": "escalation",
  109. "seen": true,
  110. "updated_at": "2016-08-16T07:55:42.119Z",
  111. "created_at": "2016-08-16T07:55:42.119Z"
  112. }
  113. Test:
  114. 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"}'
  115. =end
  116. def update
  117. model_update_render(OnlineNotification, params)
  118. end
  119. =begin
  120. Resource:
  121. DELETE /api/v1/online_notifications/{id}.json
  122. Response:
  123. {}
  124. Test:
  125. curl http://localhost/api/v1/online_notifications/{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  126. =end
  127. def destroy
  128. model_destroy_render(OnlineNotification, params)
  129. end
  130. =begin
  131. Resource:
  132. PUT /api/v1/online_notifications/mark_all_as_read
  133. Payload:
  134. {}
  135. Response:
  136. {}
  137. Test:
  138. curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login}:#{password} -X POST -d '{}'
  139. =end
  140. def mark_all_as_read
  141. notifications = OnlineNotification.list(current_user, 200)
  142. notifications.each do |notification|
  143. next if notification['seen']
  144. OnlineNotification.find(notification['id']).update!(seen: true)
  145. end
  146. render json: {}, status: :ok
  147. end
  148. end