tickets_controller.rb 20 KB

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