tickets_controller.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketsController < ApplicationController
  3. include CreatesTicketArticles
  4. include ClonesTicketArticleAttachments
  5. include TicketStats
  6. prepend_before_action :authentication_check
  7. # GET /api/v1/tickets
  8. def index
  9. offset = 0
  10. per_page = 100
  11. if params[:page] && params[:per_page]
  12. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  13. per_page = params[:per_page].to_i
  14. end
  15. if per_page > 100
  16. per_page = 100
  17. end
  18. access_condition = Ticket.access_condition(current_user, 'read')
  19. tickets = Ticket.where(access_condition).order(id: 'ASC').offset(offset).limit(per_page)
  20. if params[:expand]
  21. list = []
  22. tickets.each do |ticket|
  23. list.push ticket.attributes_with_association_names
  24. end
  25. render json: list, status: :ok
  26. return
  27. end
  28. if params[:full]
  29. assets = {}
  30. item_ids = []
  31. tickets.each do |item|
  32. item_ids.push item.id
  33. assets = item.assets(assets)
  34. end
  35. render json: {
  36. record_ids: item_ids,
  37. assets: assets,
  38. }, status: :ok
  39. return
  40. end
  41. render json: tickets
  42. end
  43. # GET /api/v1/tickets/1
  44. def show
  45. ticket = Ticket.find(params[:id])
  46. access!(ticket, 'read')
  47. if params[:expand]
  48. result = ticket.attributes_with_association_names
  49. render json: result, status: :ok
  50. return
  51. end
  52. if params[:full]
  53. full = Ticket.full(params[:id])
  54. render json: full
  55. return
  56. end
  57. if params[:all]
  58. render json: ticket_all(ticket)
  59. return
  60. end
  61. render json: ticket
  62. end
  63. # POST /api/v1/tickets
  64. def create
  65. clean_params = Ticket.association_name_to_id_convert(params)
  66. # overwrite params
  67. if !current_user.permissions?('ticket.agent')
  68. %i[owner owner_id customer customer_id organization organization_id preferences].each do |key|
  69. clean_params.delete(key)
  70. end
  71. clean_params[:customer_id] = current_user.id
  72. end
  73. # try to create customer if needed
  74. if clean_params[:customer_id] && clean_params[:customer_id] =~ /^guess:(.+?)$/
  75. email = $1
  76. if email !~ /@/ || email =~ /(>|<|\||\!|"|§|'|\$|%|&|\(|\)|\?|\s)/
  77. render json: { error: 'Invalid email of customer' }, status: :unprocessable_entity
  78. return
  79. end
  80. customer = User.find_by(email: email.downcase)
  81. if !customer
  82. role_ids = Role.signup_role_ids
  83. customer = User.create(
  84. firstname: '',
  85. lastname: '',
  86. email: email,
  87. password: '',
  88. active: true,
  89. role_ids: role_ids,
  90. )
  91. end
  92. clean_params[:customer_id] = customer.id
  93. end
  94. clean_params = Ticket.param_cleanup(clean_params, true)
  95. ticket = Ticket.new(clean_params)
  96. # check if article is given
  97. if !params[:article]
  98. render json: { error: 'article hash is missing' }, status: :unprocessable_entity
  99. return
  100. end
  101. # create ticket
  102. ticket.save!
  103. ticket.with_lock do
  104. # create tags if given
  105. if params[:tags].present?
  106. tags = params[:tags].split(/,/)
  107. tags.each do |tag|
  108. ticket.tag_add(tag)
  109. end
  110. end
  111. # create article if given
  112. if params[:article]
  113. article_create(ticket, params[:article])
  114. end
  115. end
  116. # create links (e. g. in case of ticket split)
  117. # links: {
  118. # Ticket: {
  119. # parent: [ticket_id1, ticket_id2, ...]
  120. # normal: [ticket_id1, ticket_id2, ...]
  121. # child: [ticket_id1, ticket_id2, ...]
  122. # },
  123. # }
  124. if params[:links].present?
  125. link = params[:links].permit!.to_h
  126. raise Exceptions::UnprocessableEntity, 'Invalid link structure' if !link.is_a? Hash
  127. link.each do |target_object, link_types_with_object_ids|
  128. raise Exceptions::UnprocessableEntity, 'Invalid link structure (Object)' if !link_types_with_object_ids.is_a? Hash
  129. link_types_with_object_ids.each do |link_type, object_ids|
  130. raise Exceptions::UnprocessableEntity, 'Invalid link structure (Object->LinkType)' if !object_ids.is_a? Array
  131. object_ids.each do |local_object_id|
  132. link = Link.add(
  133. link_type: link_type,
  134. link_object_target: target_object,
  135. link_object_target_value: local_object_id,
  136. link_object_source: 'Ticket',
  137. link_object_source_value: ticket.id,
  138. )
  139. end
  140. end
  141. end
  142. end
  143. if params[:expand]
  144. result = ticket.reload.attributes_with_association_names
  145. render json: result, status: :created
  146. return
  147. end
  148. if params[:all]
  149. render json: ticket_all(ticket.reload)
  150. return
  151. end
  152. render json: ticket.reload, status: :created
  153. end
  154. # PUT /api/v1/tickets/1
  155. def update
  156. ticket = Ticket.find(params[:id])
  157. access!(ticket, 'change')
  158. clean_params = Ticket.association_name_to_id_convert(params)
  159. clean_params = Ticket.param_cleanup(clean_params, true)
  160. # overwrite params
  161. if !current_user.permissions?('ticket.agent')
  162. %i[owner owner_id customer customer_id organization organization_id preferences].each do |key|
  163. clean_params.delete(key)
  164. end
  165. end
  166. ticket.with_lock do
  167. ticket.update!(clean_params)
  168. if params[:article]
  169. article_create(ticket, params[:article])
  170. end
  171. end
  172. if params[:expand]
  173. result = ticket.reload.attributes_with_association_names
  174. render json: result, status: :ok
  175. return
  176. end
  177. if params[:all]
  178. render json: ticket_all(ticket.reload)
  179. return
  180. end
  181. render json: ticket.reload, status: :ok
  182. end
  183. # DELETE /api/v1/tickets/1
  184. def destroy
  185. ticket = Ticket.find(params[:id])
  186. access!(ticket, 'delete')
  187. raise Exceptions::NotAuthorized, 'Not authorized (admin permission required)!' if !current_user.permissions?('admin')
  188. ticket.destroy!
  189. head :ok
  190. end
  191. # GET /api/v1/ticket_customer
  192. # GET /api/v1/tickets_customer
  193. def ticket_customer
  194. # return result
  195. result = Ticket::ScreenOptions.list_by_customer(
  196. customer_id: params[:customer_id],
  197. limit: 15,
  198. )
  199. render json: result
  200. end
  201. # GET /api/v1/ticket_history/1
  202. def ticket_history
  203. # get ticket data
  204. ticket = Ticket.find(params[:id])
  205. access!(ticket, 'read')
  206. # get history of ticket
  207. history = ticket.history_get(true)
  208. # return result
  209. render json: history
  210. end
  211. # GET /api/v1/ticket_related/1
  212. def ticket_related
  213. ticket = Ticket.find(params[:ticket_id])
  214. assets = ticket.assets({})
  215. # open tickets by customer
  216. access_condition = Ticket.access_condition(current_user, 'read')
  217. ticket_lists = Ticket
  218. .where(
  219. customer_id: ticket.customer_id,
  220. state_id: Ticket::State.by_category(:open)
  221. )
  222. .where(access_condition)
  223. .where('id != ?', [ ticket.id ])
  224. .order('created_at DESC')
  225. .limit(6)
  226. # if we do not have open related tickets, search for any tickets
  227. if ticket_lists.blank?
  228. ticket_lists = Ticket
  229. .where(
  230. customer_id: ticket.customer_id,
  231. ).where.not(
  232. state_id: Ticket::State.by_category(:merged)
  233. )
  234. .where(access_condition)
  235. .where('id != ?', [ ticket.id ])
  236. .order('created_at DESC')
  237. .limit(6)
  238. end
  239. # get related assets
  240. ticket_ids_by_customer = []
  241. ticket_lists.each do |ticket_list|
  242. ticket_ids_by_customer.push ticket_list.id
  243. assets = ticket_list.assets(assets)
  244. end
  245. ticket_ids_recent_viewed = []
  246. recent_views = RecentView.list(current_user, 8, 'Ticket').delete_if { |object| object['o_id'] == ticket.id }
  247. recent_views.each do |recent_view|
  248. next if recent_view['object'] != 'Ticket'
  249. ticket_ids_recent_viewed.push recent_view['o_id']
  250. recent_view_ticket = Ticket.find(recent_view['o_id'])
  251. next if recent_view_ticket.state.state_type.name == 'merged'
  252. assets = recent_view_ticket.assets(assets)
  253. end
  254. # return result
  255. render json: {
  256. assets: assets,
  257. ticket_ids_by_customer: ticket_ids_by_customer,
  258. ticket_ids_recent_viewed: ticket_ids_recent_viewed,
  259. }
  260. end
  261. # GET /api/v1/ticket_merge/1/1
  262. def ticket_merge
  263. # check master ticket
  264. ticket_master = Ticket.find_by(number: params[:master_ticket_number])
  265. if !ticket_master
  266. render json: {
  267. result: 'failed',
  268. message: 'No such master ticket number!',
  269. }
  270. return
  271. end
  272. access!(ticket_master, 'full')
  273. # check slave ticket
  274. ticket_slave = Ticket.find_by(id: params[:slave_ticket_id])
  275. if !ticket_slave
  276. render json: {
  277. result: 'failed',
  278. message: 'No such slave ticket!',
  279. }
  280. return
  281. end
  282. access!(ticket_slave, 'full')
  283. # merge ticket
  284. ticket_slave.merge_to(
  285. ticket_id: ticket_master.id,
  286. created_by_id: current_user.id,
  287. )
  288. # return result
  289. render json: {
  290. result: 'success',
  291. master_ticket: ticket_master.attributes,
  292. slave_ticket: ticket_slave.attributes,
  293. }
  294. end
  295. # GET /api/v1/ticket_split
  296. def ticket_split
  297. ticket = Ticket.find(params[:ticket_id])
  298. access!(ticket, 'read')
  299. assets = ticket.assets({})
  300. article = Ticket::Article.find(params[:article_id])
  301. access!(article.ticket, 'read')
  302. assets = article.assets(assets)
  303. render json: {
  304. assets: assets,
  305. attachments: article_attachments_clone(article),
  306. }
  307. end
  308. # GET /api/v1/ticket_create
  309. def ticket_create
  310. # get attributes to update
  311. attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
  312. current_user: current_user,
  313. )
  314. render json: attributes_to_change
  315. end
  316. # GET /api/v1/tickets/search
  317. def search
  318. # permit nested conditions
  319. if params[:condition]
  320. params.require(:condition).permit!
  321. end
  322. # set limit for pagination if needed
  323. if params[:page] && params[:per_page]
  324. params[:limit] = params[:page].to_i * params[:per_page].to_i
  325. end
  326. if params[:limit] && params[:limit].to_i > 100
  327. params[:limit] = 100
  328. end
  329. query = params[:query]
  330. if query.respond_to?(:permit!)
  331. query = query.permit!.to_h
  332. end
  333. # build result list
  334. tickets = Ticket.search(
  335. query: query,
  336. condition: params[:condition].to_h,
  337. limit: params[:limit],
  338. current_user: current_user,
  339. )
  340. # do pagination if needed
  341. if params[:page] && params[:per_page]
  342. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  343. tickets = tickets[offset, params[:per_page].to_i] || []
  344. end
  345. if params[:expand]
  346. list = []
  347. tickets.each do |ticket|
  348. list.push ticket.attributes_with_association_names
  349. end
  350. render json: list, status: :ok
  351. return
  352. end
  353. assets = {}
  354. ticket_result = []
  355. tickets.each do |ticket|
  356. ticket_result.push ticket.id
  357. assets = ticket.assets(assets)
  358. end
  359. # return result
  360. render json: {
  361. tickets: ticket_result,
  362. tickets_count: tickets.count,
  363. assets: assets,
  364. }
  365. end
  366. # GET /api/v1/tickets/selector
  367. def selector
  368. permission_check('admin.*')
  369. ticket_count, tickets = Ticket.selectors(params[:condition], 6)
  370. assets = {}
  371. ticket_ids = []
  372. tickets&.each do |ticket|
  373. ticket_ids.push ticket.id
  374. assets = ticket.assets(assets)
  375. end
  376. # return result
  377. render json: {
  378. ticket_ids: ticket_ids,
  379. ticket_count: ticket_count || 0,
  380. assets: assets,
  381. }
  382. end
  383. # GET /api/v1/ticket_stats
  384. def stats
  385. if !params[:user_id] && !params[:organization_id]
  386. raise 'Need user_id or organization_id as param'
  387. end
  388. # lookup open user tickets
  389. limit = 100
  390. assets = {}
  391. access_condition = Ticket.access_condition(current_user, 'read')
  392. user_tickets = {}
  393. if params[:user_id]
  394. user = User.lookup(id: params[:user_id])
  395. if !user
  396. raise "No such user with id #{params[:user_id]}"
  397. end
  398. conditions = {
  399. closed_ids: {
  400. 'ticket.state_id' => {
  401. operator: 'is',
  402. value: Ticket::State.by_category(:closed).pluck(:id),
  403. },
  404. 'ticket.customer_id' => {
  405. operator: 'is',
  406. value: user.id,
  407. },
  408. },
  409. open_ids: {
  410. 'ticket.state_id' => {
  411. operator: 'is',
  412. value: Ticket::State.by_category(:open).pluck(:id),
  413. },
  414. 'ticket.customer_id' => {
  415. operator: 'is',
  416. value: user.id,
  417. },
  418. },
  419. }
  420. conditions.each do |key, local_condition|
  421. user_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
  422. end
  423. # generate stats by user
  424. condition = {
  425. 'tickets.customer_id' => user.id,
  426. }
  427. user_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
  428. end
  429. # lookup open org tickets
  430. org_tickets = {}
  431. if params[:organization_id].present?
  432. organization = Organization.lookup(id: params[:organization_id])
  433. if !organization
  434. raise "No such organization with id #{params[:organization_id]}"
  435. end
  436. conditions = {
  437. closed_ids: {
  438. 'ticket.state_id' => {
  439. operator: 'is',
  440. value: Ticket::State.by_category(:closed).pluck(:id),
  441. },
  442. 'ticket.organization_id' => {
  443. operator: 'is',
  444. value: organization.id,
  445. },
  446. },
  447. open_ids: {
  448. 'ticket.state_id' => {
  449. operator: 'is',
  450. value: Ticket::State.by_category(:open).pluck(:id),
  451. },
  452. 'ticket.organization_id' => {
  453. operator: 'is',
  454. value: organization.id,
  455. },
  456. },
  457. }
  458. conditions.each do |key, local_condition|
  459. org_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
  460. end
  461. # generate stats by org
  462. condition = {
  463. 'tickets.organization_id' => organization.id,
  464. }
  465. org_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
  466. end
  467. # return result
  468. render json: {
  469. user: user_tickets,
  470. organization: org_tickets,
  471. assets: assets,
  472. }
  473. end
  474. private
  475. def ticket_all(ticket)
  476. # get attributes to update
  477. attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
  478. current_user: current_user,
  479. ticket: ticket
  480. )
  481. # get related users
  482. assets = attributes_to_change[:assets]
  483. assets = ticket.assets(assets)
  484. # get related users
  485. article_ids = []
  486. ticket.articles.each do |article|
  487. # ignore internal article if customer is requesting
  488. next if article.internal == true && current_user.permissions?('ticket.customer')
  489. article_ids.push article.id
  490. assets = article.assets(assets)
  491. end
  492. # get links
  493. links = Link.list(
  494. link_object: 'Ticket',
  495. link_object_value: ticket.id,
  496. )
  497. link_list = []
  498. links.each do |item|
  499. link_list.push item
  500. if item['link_object'] == 'Ticket'
  501. linked_ticket = Ticket.lookup(id: item['link_object_value'])
  502. assets = linked_ticket.assets(assets)
  503. end
  504. end
  505. # get tags
  506. tags = ticket.tag_list
  507. # return result
  508. {
  509. ticket_id: ticket.id,
  510. ticket_article_ids: article_ids,
  511. assets: assets,
  512. links: link_list,
  513. tags: tags,
  514. form_meta: attributes_to_change[:form_meta],
  515. }
  516. end
  517. end