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) 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"] = [] ticket.ticket_queries.asc(:created_at).each do |qu| r = {"query" => qu.query, "responses" => []} r["responses"] = qu.ticket_query_responses.for_clients.asc(:created_at).collect do |qr| user = User.find(qr.response_by) rescue nil 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 { "response" => qr.response, "response_time" => qr.created_at, "response_by" => (user.name if !user.nil?), "avatar" => avatar } 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 t["reopened"] = ticket.reopened? t end end