tickets/app/models/ticket.rb

86 lines
2.2 KiB
Ruby
Raw Normal View History

2015-08-03 10:54:33 +00:00
class Ticket
include Mongoid::Document
include Mongoid::Timestamps
include OrbitTag::Taggable
2021-08-03 02:07:01 +00:00
include OrbitCategory::Categorizable
2015-08-03 10:54:33 +00:00
include Slug
field :site_type
field :ticket_creater_id
field :ticket_creater_name
2017-04-12 11:43:24 +00:00
field :ticket_creater_cpanel_id
2015-08-03 10:54:33 +00:00
field :subject
2015-08-18 18:28:47 +00:00
field :urgent, type: Boolean, default: false
2015-08-03 10:54:33 +00:00
field :reopened_count, type: Integer, :default => 0
field :status, :default => "open"
field :taken_by, type: BSON::ObjectId
2017-04-12 11:43:24 +00:00
scope :open, ->{ where(status: "open") }
scope :closed, ->{ where(status: "closed") }
scope :commenced, ->{ where(status: "commenced") }
scope :not_urgent, ->{ where(:urgent.in => [false,nil]) }
scope :all_urgent, ->{ where(urgent: true) }
2015-08-03 10:54:33 +00:00
belongs_to :registered_site
2017-04-12 11:43:24 +00:00
has_many :ticket_queries, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :ticket_queries, :allow_destroy => true
2015-08-03 10:54:33 +00:00
def can_be_reopened?
self.reopened_count < 2
end
2017-04-12 11:43:24 +00:00
def author
!self.ticket_creater_name.nil? ? self.ticket_creater_name : self.ticket_creater_cpanel_id
end
2015-08-18 18:28:47 +00:00
def is_urgent?
self.urgent
end
def reopened?
self.reopened_count > 0
end
2015-08-03 10:54:33 +00:00
2015-08-18 18:28:47 +00:00
def registered_site_title
self.registered_site.title
end
def registered_site_domain
self.registered_site.site_domain
end
def self.get_ticket_unread_count_for_user(user)
2015-10-16 19:07:52 +00:00
if user.instance_of?(User)
tqrs = TicketQueryResponse.where(:user_tags.in => [user.id.to_s], :read_by.nin => [user.id.to_s])
elsif user.instance_of?(String)
tqrs = TicketQueryResponse.where(:user_tags.in => [user], :read_by.nin => [user])
end
unread_tickets = []
tqrs.each do |tqr|
2015-10-02 08:35:47 +00:00
unread_tickets << tqr.ticket_query.ticket if !tqr.ticket_query.nil? && !tqr.ticket_query.ticket.nil? && tqr.ticket_query.ticket.status != "closed"
end
unread_tickets.uniq!
unread_tickets.count
end
def tagged_status(user)
queries = self.ticket_queries
status = "not_tagged"
queries.each do |query|
tqrs = query.ticket_query_responses.where(:user_tags.in => [user.id.to_s])
if tqrs.count > 0
tqrs = query.ticket_query_responses.where(:user_tags.in => [user.id.to_s], :read_by.in => [user.id.to_s])
if tqrs.count > 0
status = "read"
else
status = "tagged"
end
else
status = "not_tagged"
end
end
status
end
2015-08-03 10:54:33 +00:00
end