templates_controller.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class TemplatesController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  4. =begin
  5. Format:
  6. JSON
  7. Example:
  8. {
  9. "id": 1,
  10. "name": "some template",
  11. "user_id": null,
  12. "options": {
  13. "ticket.title": {
  14. "value": "some title"
  15. },
  16. "ticket.customer_id": {
  17. "value": "2",
  18. "value_completion": "Nicole Braun <nicole.braun@zammad.org>"
  19. }
  20. },
  21. "updated_at": "2012-09-14T17:51:53Z",
  22. "created_at": "2012-09-14T17:51:53Z",
  23. "updated_by_id": 2,
  24. "created_by_id": 2
  25. }
  26. =end
  27. =begin
  28. Resource:
  29. GET /api/v1/templates.json
  30. Response:
  31. [
  32. {
  33. "id": 1,
  34. "name": "some_name1",
  35. ...
  36. },
  37. {
  38. "id": 2,
  39. "name": "some_name2",
  40. ...
  41. }
  42. ]
  43. Test:
  44. curl http://localhost/api/v1/templates.json -v -u #{login}:#{password}
  45. =end
  46. def index
  47. model_index_render(policy_scope(Template), params)
  48. end
  49. =begin
  50. Resource:
  51. GET /api/v1/templates/#{id}.json
  52. Response:
  53. {
  54. "id": 1,
  55. "name": "name_1",
  56. ...
  57. }
  58. Test:
  59. curl http://localhost/api/v1/templates/#{id}.json -v -u #{login}:#{password}
  60. =end
  61. def show
  62. model_show_render(policy_scope(Template), params)
  63. end
  64. =begin
  65. Resource:
  66. POST /api/v1/templates.json
  67. Payload:
  68. {
  69. "name": "some name",
  70. "options": {
  71. "ticket.title": {
  72. "value": "some title"
  73. },
  74. "ticket.customer_id": {
  75. "value": "2",
  76. "value_completion": "Nicole Braun <nicole.braun@zammad.org>"
  77. }
  78. }
  79. }
  80. Response:
  81. {
  82. "id": 1,
  83. "name": "some_name",
  84. ...
  85. }
  86. Test:
  87. curl http://localhost/api/v1/templates.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"name": "some_name", "options": {"ticket.title": {"value": "some title"},"ticket.customer_id": {"value": "2", "value_completion": "Nicole Braun <nicole.braun@zammad.org>"}}}'
  88. =end
  89. def create
  90. template_create_render(params)
  91. end
  92. =begin
  93. Resource:
  94. PUT /api/v1/templates/{id}.json
  95. Payload:
  96. {
  97. "name": "some name",
  98. "options": {
  99. "ticket.title": {
  100. "value": "some title"
  101. },
  102. "ticket.customer_id": {
  103. "value": "2",
  104. "value_completion": "Nicole Braun <nicole.braun@zammad.org>"
  105. }
  106. }
  107. }
  108. Response:
  109. {
  110. "id": 1,
  111. "name": "some_name",
  112. ...
  113. }
  114. Test:
  115. curl http://localhost/api/v1/templates/1.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name", "options": {"ticket.title": {"value": "some title"},"ticket.customer_id": {"value": "2", "value_completion": "Nicole Braun <nicole.braun@zammad.org>"}}}'
  116. =end
  117. def update
  118. template_update_render(params)
  119. end
  120. =begin
  121. Resource:
  122. DELETE /api/v1/templates/{id}.json
  123. Response:
  124. {}
  125. Test:
  126. curl http://localhost/api/v1/templates.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  127. =end
  128. def destroy
  129. model_destroy_render(Template, params)
  130. end
  131. private
  132. def old_options?(options)
  133. has_new_options = false
  134. options.each_key do |key|
  135. if key.starts_with?(%r{(ticket|article)\.})
  136. has_new_options = true
  137. end
  138. break if has_new_options
  139. end
  140. !has_new_options
  141. end
  142. def migrate_options(options)
  143. old_options = options.clone
  144. new_options = {}
  145. article_attribute_list = %w[body form_id]
  146. # Implements a compatibility layer for templates, by converting `options` to a newer format:
  147. # options: {
  148. # 'ticket.field_1': { value: 'value_1' },
  149. # 'ticket.field_2': { value: 'value_2', value_completion: 'value_completion_2' },
  150. # }
  151. old_options.each do |key, value|
  152. new_key = "ticket.#{key}"
  153. if article_attribute_list.include?(key)
  154. new_key = "article.#{key}"
  155. end
  156. new_options[new_key] = { value: value }
  157. if old_options.key?("#{key}_completion")
  158. new_options[new_key]['value_completion'] = old_options["#{key}_completion"]
  159. old_options.delete("#{key}_completion")
  160. end
  161. end
  162. new_options
  163. end
  164. def template_prepare_params(params)
  165. clean_params = Template.association_name_to_id_convert(params)
  166. clean_params = Template.param_cleanup(clean_params, true)
  167. if Template.included_modules.include?(ChecksCoreWorkflow)
  168. clean_params[:screen] = 'create'
  169. end
  170. if old_options?(clean_params[:options])
  171. clean_params[:options] = migrate_options(clean_params[:options])
  172. ActiveSupport::Deprecation.warn 'Usage of the old format for template options with unprefixed keys and simple values is deprecated.'
  173. end
  174. clean_params
  175. end
  176. def template_create_render(params)
  177. clean_params = template_prepare_params(params)
  178. template = Template.new(clean_params)
  179. template.associations_from_param(params)
  180. template.save!
  181. if response_expand?
  182. render json: template.attributes_with_association_names, status: :created
  183. return
  184. end
  185. if response_full?
  186. render json: template.class.full(template.id), status: :created
  187. return
  188. end
  189. model_create_render_item(template)
  190. end
  191. def template_update_render(params)
  192. template = Template.find(params[:id])
  193. clean_params = template_prepare_params(params)
  194. template.with_lock do
  195. template.associations_from_param(params)
  196. template.update!(clean_params)
  197. end
  198. if response_expand?
  199. render json: template.attributes_with_association_names, status: :ok
  200. return
  201. end
  202. if response_full?
  203. render json: template.class.full(template.id), status: :ok
  204. return
  205. end
  206. model_update_render_item(template)
  207. end
  208. end