tickets_controller.rb 15 KB

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