tickets_controller.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketsController < ApplicationController
  3. before_action :authentication_check
  4. # GET /api/v1/tickets
  5. def index
  6. offset = 0
  7. per_page = 100
  8. if params[:page] && params[:per_page]
  9. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  10. per_page = params[:per_page].to_i
  11. end
  12. if per_page > 100
  13. per_page = 100
  14. end
  15. access_condition = Ticket.access_condition(current_user)
  16. tickets = Ticket.where(access_condition).order(id: 'ASC').offset(offset).limit(per_page)
  17. if params[:expand]
  18. list = []
  19. tickets.each { |ticket|
  20. list.push ticket.attributes_with_relation_names
  21. }
  22. render json: list, status: :ok
  23. return
  24. end
  25. if params[:full]
  26. assets = {}
  27. item_ids = []
  28. tickets.each { |item|
  29. item_ids.push item.id
  30. assets = item.assets(assets)
  31. }
  32. render json: {
  33. record_ids: item_ids,
  34. assets: assets,
  35. }, status: :ok
  36. return
  37. end
  38. render json: tickets
  39. end
  40. # GET /api/v1/tickets/1
  41. def show
  42. # permission check
  43. ticket = Ticket.find(params[:id])
  44. ticket_permission(ticket)
  45. if params[:expand]
  46. result = ticket.attributes_with_relation_names
  47. render json: result, status: :ok
  48. return
  49. end
  50. if params[:full]
  51. full = Ticket.full(params[:id])
  52. render json: full
  53. return
  54. end
  55. if params[:all]
  56. render json: ticket_all(ticket)
  57. return
  58. end
  59. render json: ticket
  60. end
  61. # POST /api/v1/tickets
  62. def create
  63. clean_params = Ticket.param_association_lookup(params)
  64. clean_params = Ticket.param_cleanup(clean_params, true)
  65. # overwrite params
  66. if !current_user.permissions?('ticket.agent')
  67. [:owner, :owner_id, :customer, :customer_id, :organization, :organization_id, :preferences].each { |key|
  68. clean_params.delete(key)
  69. }
  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)
  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. ticket = Ticket.new(clean_params)
  94. # check if article is given
  95. if !params[:article]
  96. render json: { error: 'article hash is missing' }, status: :unprocessable_entity
  97. return
  98. end
  99. # create ticket
  100. ticket.save!
  101. ticket.with_lock do
  102. # create tags if given
  103. if params[:tags] && !params[:tags].empty?
  104. tags = params[:tags].split(/,/)
  105. tags.each { |tag|
  106. Tag.tag_add(
  107. object: 'Ticket',
  108. o_id: ticket.id,
  109. item: tag,
  110. created_by_id: current_user.id,
  111. )
  112. }
  113. end
  114. # create article if given
  115. if params[:article]
  116. article_create(ticket, params[:article])
  117. end
  118. end
  119. # create links (e. g. in case of ticket split)
  120. # links: {
  121. # Ticket: {
  122. # parent: [ticket_id1, ticket_id2, ...]
  123. # normal: [ticket_id1, ticket_id2, ...]
  124. # child: [ticket_id1, ticket_id2, ...]
  125. # },
  126. # }
  127. if params[:links]
  128. raise 'Invalid link structure' if params[:links].to_h.class != Hash
  129. params[:links].each { |target_object, link_types_with_object_ids|
  130. raise 'Invalid link structure (Object)' if link_types_with_object_ids.to_h.class != Hash
  131. link_types_with_object_ids.each { |link_type, object_ids|
  132. raise 'Invalid link structure (Object->LinkType)' if object_ids.class != Array
  133. object_ids.each { |local_object_id|
  134. link = Link.add(
  135. link_type: link_type,
  136. link_object_target: target_object,
  137. link_object_target_value: local_object_id,
  138. link_object_source: 'Ticket',
  139. link_object_source_value: ticket.id,
  140. )
  141. }
  142. }
  143. }
  144. end
  145. if params[:expand]
  146. result = ticket.reload.attributes_with_relation_names
  147. render json: result, status: :created
  148. return
  149. end
  150. if params[:all]
  151. render json: ticket_all(ticket.reload)
  152. return
  153. end
  154. render json: ticket.reload, status: :created
  155. end
  156. # PUT /api/v1/tickets/1
  157. def update
  158. # permission check
  159. ticket = Ticket.find(params[:id])
  160. ticket_permission(ticket)
  161. clean_params = Ticket.param_association_lookup(params)
  162. clean_params = Ticket.param_cleanup(clean_params, true)
  163. # overwrite params
  164. if !current_user.permissions?('ticket.agent')
  165. [:owner, :owner_id, :customer, :customer_id, :organization, :organization_id, :preferences].each { |key|
  166. clean_params.delete(key)
  167. }
  168. end
  169. ticket.with_lock do
  170. ticket.update_attributes!(clean_params)
  171. if params[:article]
  172. article_create(ticket, params[:article])
  173. end
  174. end
  175. if params[:expand]
  176. result = ticket.reload.attributes_with_relation_names
  177. render json: result, status: :ok
  178. return
  179. end
  180. if params[:all]
  181. render json: ticket_all(ticket.reload)
  182. return
  183. end
  184. render json: ticket.reload, status: :ok
  185. end
  186. # DELETE /api/v1/tickets/1
  187. def destroy
  188. # permission check
  189. ticket = Ticket.find(params[:id])
  190. ticket_permission(ticket)
  191. raise Exceptions::NotAuthorized, 'Not authorized (admin permission required)!' if !current_user.permissions?('admin')
  192. ticket.destroy!
  193. head :ok
  194. end
  195. # GET /api/v1/ticket_customer
  196. # GET /api/v1/tickets_customer
  197. def ticket_customer
  198. # return result
  199. result = Ticket::ScreenOptions.list_by_customer(
  200. customer_id: params[:customer_id],
  201. limit: 15,
  202. )
  203. render json: result
  204. end
  205. # GET /api/v1/ticket_history/1
  206. def ticket_history
  207. # get ticket data
  208. ticket = Ticket.find(params[:id])
  209. # permission check
  210. ticket_permission(ticket)
  211. # get history of ticket
  212. history = ticket.history_get(true)
  213. # return result
  214. render json: history
  215. end
  216. # GET /api/v1/ticket_related/1
  217. def ticket_related
  218. ticket = Ticket.find(params[:ticket_id])
  219. assets = ticket.assets({})
  220. # open tickets by customer
  221. access_condition = Ticket.access_condition(current_user)
  222. ticket_lists = Ticket
  223. .where(
  224. customer_id: ticket.customer_id,
  225. state_id: Ticket::State.by_category('open')
  226. )
  227. .where(access_condition)
  228. .where('id != ?', [ ticket.id ])
  229. .order('created_at DESC')
  230. .limit(6)
  231. # if we do not have open related tickets, search for any tickets
  232. if ticket_lists.empty?
  233. ticket_lists = Ticket
  234. .where(
  235. customer_id: ticket.customer_id,
  236. )
  237. .where(access_condition)
  238. .where('id != ?', [ ticket.id ])
  239. .order('created_at DESC')
  240. .limit(6)
  241. end
  242. # get related assets
  243. ticket_ids_by_customer = []
  244. ticket_lists.each { |ticket_list|
  245. ticket_ids_by_customer.push ticket_list.id
  246. assets = ticket_list.assets(assets)
  247. }
  248. ticket_ids_recent_viewed = []
  249. recent_views = RecentView.list(current_user, 8, 'Ticket')
  250. recent_views.each { |recent_view|
  251. next if recent_view['object'] != 'Ticket'
  252. ticket_ids_recent_viewed.push recent_view['o_id']
  253. recent_view_ticket = Ticket.find(recent_view['o_id'])
  254. assets = recent_view_ticket.assets(assets)
  255. }
  256. # return result
  257. render json: {
  258. assets: assets,
  259. ticket_ids_by_customer: ticket_ids_by_customer,
  260. ticket_ids_recent_viewed: ticket_ids_recent_viewed,
  261. }
  262. end
  263. # GET /api/v1/ticket_merge/1/1
  264. def ticket_merge
  265. # check master ticket
  266. ticket_master = Ticket.find_by(number: params[:master_ticket_number])
  267. if !ticket_master
  268. render json: {
  269. result: 'faild',
  270. message: 'No such master ticket number!',
  271. }
  272. return
  273. end
  274. # permission check
  275. ticket_permission(ticket_master)
  276. # check slave ticket
  277. ticket_slave = Ticket.find_by(id: params[:slave_ticket_id])
  278. if !ticket_slave
  279. render json: {
  280. result: 'faild',
  281. message: 'No such slave ticket!',
  282. }
  283. return
  284. end
  285. # permission check
  286. ticket_permission(ticket_slave)
  287. # check diffetent ticket ids
  288. if ticket_slave.id == ticket_master.id
  289. render json: {
  290. result: 'faild',
  291. message: 'Can\'t merge ticket with it self!',
  292. }
  293. return
  294. end
  295. # merge ticket
  296. ticket_slave.merge_to(
  297. ticket_id: ticket_master.id,
  298. created_by_id: current_user.id,
  299. )
  300. # return result
  301. render json: {
  302. result: 'success',
  303. master_ticket: ticket_master.attributes,
  304. slave_ticket: ticket_slave.attributes,
  305. }
  306. end
  307. # GET /api/v1/ticket_split
  308. def ticket_split
  309. # permission check
  310. ticket = Ticket.find(params[:ticket_id])
  311. ticket_permission(ticket)
  312. assets = ticket.assets({})
  313. # get related articles
  314. article = Ticket::Article.find(params[:article_id])
  315. assets = article.assets(assets)
  316. render json: {
  317. assets: assets
  318. }
  319. end
  320. # GET /api/v1/ticket_create
  321. def ticket_create
  322. # get attributes to update
  323. attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
  324. user: current_user,
  325. )
  326. render json: attributes_to_change
  327. end
  328. # GET /api/v1/tickets/search
  329. def search
  330. # permit nested conditions
  331. if params[:condition]
  332. params.require(:condition).permit!
  333. end
  334. # set limit for pagination if needed
  335. if params[:page] && params[:per_page]
  336. params[:limit] = params[:page].to_i * params[:per_page].to_i
  337. end
  338. if params[:limit] && params[:limit].to_i > 100
  339. params[:limit].to_i = 100
  340. end
  341. # build result list
  342. tickets = Ticket.search(
  343. limit: params[:limit],
  344. query: params[:query],
  345. condition: params[:condition],
  346. current_user: current_user,
  347. )
  348. # do pagination if needed
  349. if params[:page] && params[:per_page]
  350. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  351. tickets = tickets.slice(offset, params[:per_page].to_i) || []
  352. end
  353. if params[:expand]
  354. list = []
  355. tickets.each { |ticket|
  356. list.push ticket.attributes_with_relation_names
  357. }
  358. render json: list, status: :ok
  359. return
  360. end
  361. assets = {}
  362. ticket_result = []
  363. tickets.each do |ticket|
  364. ticket_result.push ticket.id
  365. assets = ticket.assets(assets)
  366. end
  367. # return result
  368. render json: {
  369. tickets: ticket_result,
  370. tickets_count: tickets.count,
  371. assets: assets,
  372. }
  373. end
  374. # GET /api/v1/tickets/selector
  375. def selector
  376. permission_check('admin.*')
  377. ticket_count, tickets = Ticket.selectors(params[:condition], 6)
  378. assets = {}
  379. ticket_ids = []
  380. if tickets
  381. tickets.each do |ticket|
  382. ticket_ids.push ticket.id
  383. assets = ticket.assets(assets)
  384. end
  385. end
  386. # return result
  387. render json: {
  388. ticket_ids: ticket_ids,
  389. ticket_count: ticket_count || 0,
  390. assets: assets,
  391. }
  392. end
  393. # GET /api/v1/ticket_stats
  394. def stats
  395. if !params[:user_id] && !params[:organization_id]
  396. raise 'Need user_id or organization_id as param'
  397. end
  398. # permission check
  399. #ticket_permission(ticket)
  400. # lookup open user tickets
  401. limit = 100
  402. assets = {}
  403. access_condition = Ticket.access_condition(current_user)
  404. now = Time.zone.now
  405. user_tickets_open_ids = []
  406. user_tickets_closed_ids = []
  407. user_ticket_volume_by_year = []
  408. if params[:user_id]
  409. user = User.lookup(id: params[:user_id])
  410. condition = {
  411. 'ticket.state_id' => {
  412. operator: 'is',
  413. value: Ticket::State.by_category('open').map(&:id),
  414. },
  415. 'ticket.customer_id' => {
  416. operator: 'is',
  417. value: user.id,
  418. },
  419. }
  420. user_tickets_open = Ticket.search(
  421. limit: limit,
  422. condition: condition,
  423. current_user: current_user,
  424. )
  425. user_tickets_open_ids = assets_of_tickets(user_tickets_open, assets)
  426. # lookup closed user tickets
  427. condition = {
  428. 'ticket.state_id' => {
  429. operator: 'is',
  430. value: Ticket::State.by_category('closed').map(&:id),
  431. },
  432. 'ticket.customer_id' => {
  433. operator: 'is',
  434. value: user.id,
  435. },
  436. }
  437. user_tickets_closed = Ticket.search(
  438. limit: limit,
  439. condition: condition,
  440. current_user: current_user,
  441. )
  442. user_tickets_closed_ids = assets_of_tickets(user_tickets_closed, assets)
  443. # generate stats by user
  444. (0..11).each { |month_back|
  445. date_to_check = now - month_back.month
  446. date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
  447. date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
  448. condition = {
  449. 'tickets.customer_id' => user.id,
  450. }
  451. # created
  452. created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end )
  453. .where(access_condition)
  454. .where(condition)
  455. .count
  456. # closed
  457. closed = Ticket.where('close_at > ? AND close_at < ?', date_start, date_end )
  458. .where(access_condition)
  459. .where(condition)
  460. .count
  461. data = {
  462. month: date_to_check.month,
  463. year: date_to_check.year,
  464. text: Date::MONTHNAMES[date_to_check.month],
  465. created: created,
  466. closed: closed,
  467. }
  468. user_ticket_volume_by_year.push data
  469. }
  470. end
  471. # lookup open org tickets
  472. org_tickets_open_ids = []
  473. org_tickets_closed_ids = []
  474. org_ticket_volume_by_year = []
  475. if params[:organization_id] && !params[:organization_id].empty?
  476. condition = {
  477. 'ticket.state_id' => {
  478. operator: 'is',
  479. value: Ticket::State.by_category('open').map(&:id),
  480. },
  481. 'ticket.organization_id' => {
  482. operator: 'is',
  483. value: params[:organization_id],
  484. },
  485. }
  486. org_tickets_open = Ticket.search(
  487. limit: limit,
  488. condition: condition,
  489. current_user: current_user,
  490. )
  491. org_tickets_open_ids = assets_of_tickets(org_tickets_open, assets)
  492. # lookup closed org tickets
  493. condition = {
  494. 'ticket.state_id' => {
  495. operator: 'is',
  496. value: Ticket::State.by_category('closed').map(&:id),
  497. },
  498. 'ticket.organization_id' => {
  499. operator: 'is',
  500. value: params[:organization_id],
  501. },
  502. }
  503. org_tickets_closed = Ticket.search(
  504. limit: limit,
  505. condition: condition,
  506. current_user: current_user,
  507. )
  508. org_tickets_closed_ids = assets_of_tickets(org_tickets_closed, assets)
  509. # generate stats by org
  510. (0..11).each { |month_back|
  511. date_to_check = now - month_back.month
  512. date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
  513. date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
  514. condition = {
  515. 'tickets.organization_id' => params[:organization_id],
  516. }
  517. # created
  518. created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end ).where(condition).count
  519. # closed
  520. closed = Ticket.where('close_at > ? AND close_at < ?', date_start, date_end ).where(condition).count
  521. data = {
  522. month: date_to_check.month,
  523. year: date_to_check.year,
  524. text: Date::MONTHNAMES[date_to_check.month],
  525. created: created,
  526. closed: closed,
  527. }
  528. org_ticket_volume_by_year.push data
  529. }
  530. end
  531. # return result
  532. render json: {
  533. user_tickets_open_ids: user_tickets_open_ids,
  534. user_tickets_closed_ids: user_tickets_closed_ids,
  535. org_tickets_open_ids: org_tickets_open_ids,
  536. org_tickets_closed_ids: org_tickets_closed_ids,
  537. user_ticket_volume_by_year: user_ticket_volume_by_year,
  538. org_ticket_volume_by_year: org_ticket_volume_by_year,
  539. assets: assets,
  540. }
  541. end
  542. private
  543. def assets_of_tickets(tickets, assets)
  544. ticket_ids = []
  545. tickets.each do |ticket|
  546. ticket_ids.push ticket.id
  547. assets = ticket.assets(assets)
  548. end
  549. ticket_ids
  550. end
  551. def ticket_all(ticket)
  552. # get attributes to update
  553. attributes_to_change = Ticket::ScreenOptions.attributes_to_change(user: current_user, ticket: ticket)
  554. # get related users
  555. assets = attributes_to_change[:assets]
  556. assets = ticket.assets(assets)
  557. # get related users
  558. article_ids = []
  559. ticket.articles.order('created_at ASC, id ASC').each { |article|
  560. # ignore internal article if customer is requesting
  561. next if article.internal == true && current_user.permissions?('ticket.customer')
  562. article_ids.push article.id
  563. assets = article.assets(assets)
  564. }
  565. # get links
  566. links = Link.list(
  567. link_object: 'Ticket',
  568. link_object_value: ticket.id,
  569. )
  570. link_list = []
  571. links.each { |item|
  572. link_list.push item
  573. if item['link_object'] == 'Ticket'
  574. linked_ticket = Ticket.lookup(id: item['link_object_value'])
  575. assets = linked_ticket.assets(assets)
  576. end
  577. }
  578. # get tags
  579. tags = Tag.tag_list(
  580. object: 'Ticket',
  581. o_id: ticket.id,
  582. )
  583. # return result
  584. {
  585. ticket_id: ticket.id,
  586. ticket_article_ids: article_ids,
  587. assets: assets,
  588. links: link_list,
  589. tags: tags,
  590. form_meta: attributes_to_change[:form_meta],
  591. }
  592. end
  593. end