tickets/app/controllers/admin/tickets_api_controller.rb

153 lines
4.5 KiB
Ruby
Raw Normal View History

2015-08-03 10:54:33 +00:00
class Admin::TicketsApiController < ApplicationController
protect_from_forgery :except => [:create, :destroy, :close, :reopen]
def create
site = RegisteredSite.where(:uid => ticket_params[:site_token]).first rescue nil
response = {}
if !site.nil?
ticket = Ticket.new
ticket.site_type = ticket_params[:site_type]
ticket.ticket_creater_id = ticket_params[:ticket_creater_id]
ticket.ticket_creater_name = ticket_params[:ticket_creater_name]
ticket.subject = ticket_params[:subject]
ticket.status = "open"
ticket.registered_site = site
type = Category.find(ticket_params[:ticket_type]) rescue nil
ticket.category = type if !type.nil?
ticket.save
tq = TicketQuery.new
tq.query = ticket_params[:query]
tq.ticket = ticket
tq.save
response["success"] = true
response["ticket"] = get_ticket_hash(ticket)
else
response["success"] = false
end
render :json => response.to_json
end
def destroy
tick = Ticket.where(:uid => params[:ticket_id]).first rescue nil
tick.destroy if !tick.nil?
render :json => {"success" => true}.to_json
end
def close
ticket = Ticket.where(:uid => params[:ticket_id]).first rescue nil
ticket.status = "closed"
ticket.save
response = {}
response["success"] = true
response["ticket"] = get_ticket_hash(ticket)
render :json => response.to_json
end
def reopen
ticket = Ticket.where(:uid => params[:ticket_id]).first rescue nil
ticket.status = "open"
ticket.reopened_count = ticket.reopened_count + 1
tq = TicketQuery.new
tq.query = params[:query]
tq.ticket = ticket
tq.save
ticket.save
response = {}
response["success"] = true
response["ticket"] = get_ticket_hash(ticket)
render :json => response.to_json
end
def tickettypes
site = RegisteredSite.where(:uid => params[:store_token]).first rescue nil
ma = ModuleApp.find_by_key("ticket")
if !site.nil?
types = ma.categories.enabled.collect do |tt|
{
"title_translations" => tt.title_translations,
"id" => tt.id.to_s
}
end
render :json => {"success" => true, "ticket_types" => types}.to_json
else
render :json => {"success" => false}.to_json
end
end
def gettickets
site = RegisteredSite.where(:uid => params[:site_token]).first rescue nil
tickets = {}
if !site.nil?
if params[:type].nil? && params[:query].nil?
site_tickets = site.tickets.order_by([:status, :desc],[:created_at, :desc]).page(params[:page]).per(5)
2015-08-03 10:54:33 +00:00
else
q = params[:query]
t = params[:type]
case t
when "text"
regex = Regexp.new(".*"+q+".*", "i")
site_tickets = site.tickets.where(:subject => regex).desc(:created_at).page(params[:page]).per(25)
when "number"
site_tickets = site.tickets.where(:uid => q).desc(:created_at).page(params[:page]).per(25)
end
end
tickets = site_tickets.collect do |ticket|
get_ticket_hash(ticket)
end
end
render :json => {"tickets" => tickets,"total_pages" => site_tickets.total_pages}.to_json
end
def getticket
ticket = Ticket.where(:uid => params[:ticket_id]).first rescue nil
response = {}
if !ticket.nil?
response["success"] = true
response["ticket"] = get_ticket_hash(ticket)
response["ticket"]["creator_name"] = ticket.ticket_creater_name
response["ticket"]["creator_id"] = ticket.ticket_creater_id
response["ticket"]["queries"] = []
2015-08-18 18:28:47 +00:00
ticket.ticket_queries.asc(:created_at).each do |qu|
2015-08-03 10:54:33 +00:00
r = {"query" => qu.query, "responses" => []}
2015-08-25 10:49:47 +00:00
r["responses"] = qu.ticket_query_responses.for_clients.asc(:created_at).collect do |qr|
2015-08-03 10:54:33 +00:00
user = User.find(qr.response_by) rescue nil
2015-08-21 07:25:40 +00:00
if user.member_profile.avatar.nil? || user.member_profile.avatar.thumb.url == "thumb_person.png"
avatar = "http://" + request.host_with_port + "/assets/thumb_person.png"
else
avatar = "http://" + request.host_with_port + user.member_profile.avatar.thumb.url
end
2015-08-03 10:54:33 +00:00
{
"response" => qr.response,
"response_time" => qr.created_at,
2015-08-18 18:28:47 +00:00
"response_by" => (user.name if !user.nil?),
2015-08-21 07:25:40 +00:00
"avatar" => avatar
2015-08-03 10:54:33 +00:00
}
end
response["ticket"]["queries"] << r
end
else
response["success"] = false
end
render :json => response.to_json
end
private
def ticket_params
params.require(:ticket).permit!
end
def get_ticket_hash(ticket)
t = {}
t["ticket_number"] = ticket.uid
t["status"] = ticket.status
t["can_be_reopened"] = ticket.can_be_reopened?
t["ticket_type"] = ticket.category.title
t["created_at"] = ticket.created_at
t["subject"] = ticket.subject
2015-08-18 18:28:47 +00:00
t["reopened"] = ticket.reopened?
2015-08-03 10:54:33 +00:00
t
end
end