Skip to content

Commit

Permalink
feat: Updated backend/app/models/case.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Jan 23, 2024
1 parent 2aff274 commit 513b931
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend/app/models/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Case < ApplicationRecord
validates :status, presence: true
validates :priority, presence: true
validates :created_by, presence: true
validates :code, presence: true

belongs_to :created_by, :class_name => 'User'
belongs_to :assigned_to, :class_name => 'User', optional: true
Expand All @@ -21,6 +22,7 @@ class Case < ApplicationRecord
has_many :tasks, through: :task_groups
has_many :attachments, as: :attachable, dependent: :destroy

before_validation :set_unique_code
after_create :add_creator_to_members

acts_as_taggable_on :tags
Expand Down Expand Up @@ -93,6 +95,12 @@ def attachment_count
end

private

def set_unique_code
highest_code = Case.maximum(:code).to_s.sub("CODE-", '').to_i
self.code = "CODE-#{highest_code + 1}"
end

def add_creator_to_members
# Add the user who created this case to its list of members, so he/she can access it.
self.case_members.create(user: self.created_by, role: "CAN_EDIT")
Expand Down

0 comments on commit 513b931

Please sign in to comment.