user.rb 7.84 KB
Newer Older
Tugce Nur Tas's avatar
Tugce Nur Tas committed
1
class User < ApplicationRecord
2
  devise :invitable, :database_authenticatable, :recoverable, :rememberable,
chrysalag's avatar
chrysalag committed
3
    :trackable, :validatable
Tugce Nur Tas's avatar
Tugce Nur Tas committed
4

5
6
  before_validation :assign_primary_email, if: :profile

7
  has_one :volunteer, dependent: :nullify, inverse_of: 'user'
8

9
  has_one :profile, -> { with_deleted }, dependent: :destroy
10
11
  accepts_nested_attributes_for :profile

12
13
  ransack_alias :full_name, :profile_contact_full_name_or_volunteer_contact_full_name_or_email

Tugce Nur Tas's avatar
Tugce Nur Tas committed
14
  has_many :events, inverse_of: 'creator', foreign_key: 'creator_id'
Tugce Nur Tas's avatar
Tugce Nur Tas committed
15
  has_many :event_volunteers, inverse_of: 'creator', foreign_key: 'creator_id'
16
  has_many :certificates
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
17
  has_many :clients, inverse_of: 'user', foreign_key: 'user_id'
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
18
  has_many :volunteers, inverse_of: 'registrar', foreign_key: 'registrar_id'
19
20
  has_many :involved_authorities, class_name: 'Client', foreign_key: 'involved_authority_id',
    inverse_of: 'involved_authority'
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
21
22
  has_many :journals, inverse_of: 'user'

Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
23
  has_many :assignments, inverse_of: 'creator', foreign_key: 'creator_id'
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
24
25
26
  has_many :assignment_clients, through: :assignments, source: :client
  has_many :assignment_volunteers, through: :assignments, source: :volunteer

Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
27
  has_many :feedbacks, inverse_of: 'author', foreign_key: 'author_id'
28
  has_many :trial_feedbacks, inverse_of: 'author', foreign_key: 'author_id'
29
  has_many :billing_expenses
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
30

31
  has_many :group_offers, inverse_of: 'creator', foreign_key: 'creator_id'
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
32
33
34
35
36
37
  has_many :group_offer_group_assignments, through: :group_offers, inverse_of: 'group_assignments'
  has_many :group_offer_group_assignment_logs, through: :group_offers,
    inverse_of: 'group_assignment_logs'
  has_many :group_offer_volunteers, through: :group_offer_group_assignments, source: :volunteer,
    inverse_of: 'volunteer'

Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
38
  has_many :reminder_mailings, inverse_of: 'creator', foreign_key: 'creator_id'
39
40
  has_many :reviewed_feedbacks, class_name: 'Feedback', foreign_key: 'reviewer_id',
    inverse_of: 'reviewer'
41
42
  has_many :responsible_feedbacks, inverse_of: 'responsible', foreign_key: 'responsible_id',
    class_name: 'Feedback'
43
44
45
  has_many :reviewed_trial_feedbacks, class_name: 'TrialFeedback', foreign_key: 'reviewer_id',
    inverse_of: 'reviewer'
  has_many :reviewed_hours, class_name: 'Hour', foreign_key: 'reviewer_id', inverse_of: 'reviewer'
46

47
  # Assignment termination relations
48
49
  has_many :assignment_period_ends_set, class_name: 'Assignment',
    foreign_key: 'period_end_set_by_id', inverse_of: 'period_end_set_by'
50
  has_many :assignment_terminations_submitted, class_name: 'Assignment',
51
    foreign_key: 'termination_submitted_by_id', inverse_of: 'termination_submitted_by'
52
  has_many :assignment_terminations_verified, class_name: 'Assignment',
53
    foreign_key: 'termination_verified_by_id', inverse_of: 'termination_verified_by'
54
55
  has_many :assignments_submitted, class_name: 'Assignment',
    foreign_key: 'submitted_by_id', inverse_of: 'submitted_by'
56

57
58
  # GroupAssignment termination relations
  has_many :group_assignment_period_ends_set, class_name: 'GroupAssignment',
59
    foreign_key: 'period_end_set_by_id', inverse_of: 'period_end_set_by'
60
  has_many :group_assignment_terminations_submitted, class_name: 'GroupAssignment',
61
    foreign_key: 'termination_submitted_by_id', inverse_of: 'termination_submitted_by'
62
  has_many :group_assignment_terminations_verified, class_name: 'GroupAssignment',
63
    foreign_key: 'termination_verified_by', inverse_of: 'termination_verified_by'
64
65
  has_many :group_assignments_submitted, class_name: 'GroupAssignment',
    foreign_key: 'submitted_by_id', inverse_of: 'submitted_by'
66
67
68
69
70

  has_many :group_offer_period_ends_set, class_name: 'GroupOffer',
    foreign_key: 'period_end_set_by_id', inverse_of: 'period_end_set_by'
  has_many :group_offer_terminations_verified, class_name: 'GroupOffer',
    foreign_key: 'termination_verified_by_id', inverse_of: 'termination_verified_by'
71

Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
72
73
  has_many :resigned_clients, class_name: 'Client', foreign_key: 'resigned_by_id',
    inverse_of: 'resigned_by'
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
74

75
  # Mailing process done relation
76
77
78
79
80
  has_many :mailing_volunteer_processes_submitted, class_name: 'ReminderMailingVolunteer',
    inverse_of: 'process_submitted_by', foreign_key: 'process_submitted_by_id'

  has_many :mailing_processes_submitted, through: :mailing_volunteer_processes_submitted,
    source: :process_submitted_by
81

82
83
  # Semester Process relations
  #
84
85
  has_many :semester_processes, inverse_of: 'creator', foreign_key: 'creator_id',
    class_name: 'SemesterProcess', dependent: :destroy
86
  has_many :semester_process_mails_posted, inverse_of: 'mail_posted_by',
87
    class_name: 'SemesterProcess', foreign_key: 'mail_posted_by_id', dependent: :nullify
88
  has_many :semester_process_reminder_mail_posted, inverse_of: 'reminder_mail_posted_by',
89
    class_name: 'SemesterProcess', foreign_key: 'reminder_mail_posted_by_id', dependent: :nullify
90

91
92
  has_many :semester_process_mails, class_name: 'SemesterProcessMail', foreign_key: 'sent_by_id',
    inverse_of: 'sent_by', dependent: :nullify
93
94
95
96
97
98
99
100
101
  has_many :semester_process_reminders, -> { reminder }, through: :semester_process_mails

  has_many :semester_process_responsibilities, inverse_of: 'responsible',
    class_name: 'SemesterProcessVolunteer', foreign_key: 'responsible_id', dependent: :nullify
  has_many :semester_process_reviews, inverse_of: 'reviewed_by',
  class_name: 'SemesterProcessVolunteer', foreign_key: 'reviewed_by_id', dependent: :nullify
  has_many :semester_process_commits, inverse_of: 'commited_by',
    class_name: 'SemesterProcessVolunteer', foreign_key: 'commited_by_id', dependent: :nullify

102
  has_many :semester_feedbacks, inverse_of: 'author', foreign_key: 'author_id', dependent: :destroy
103

104
  has_and_belongs_to_many :department
105

106

107
  # Roles definition
108
  SUPERADMIN = 'superadmin'.freeze
109
  SOCIAL_WORKER = 'social_worker'.freeze
110
  DEPARTMENT_MANAGER = 'department_manager'.freeze
111
  VOLUNTEER = 'volunteer'.freeze
112
113

  CAN_MANAGE_DEPARTMENT = [SUPERADMIN, DEPARTMENT_MANAGER].freeze
114
  ROLES_FOR_USER_CREATE = [SUPERADMIN, SOCIAL_WORKER, DEPARTMENT_MANAGER].freeze
115
116

  ROLES = ROLES_FOR_USER_CREATE.dup.push(VOLUNTEER).freeze
chrysalag's avatar
chrysalag committed
117

Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
118
  validates :role, inclusion: { in: ROLES }
119
  validates :email, uniqueness: true
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
120

121
  scope :department_assocable, (-> { where(role: CAN_MANAGE_DEPARTMENT) })
122
  scope :superadmins, (-> { where(role: SUPERADMIN) })
123
  scope :department_managers, (-> { where(role: DEPARTMENT_MANAGER) })
124
  scope :social_workers, (-> { where(role: SOCIAL_WORKER) })
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
125

126
  scope :signed_in_at_least_once, (-> { where.not(last_sign_in_at: nil) })
127
128
129
  scope :with_pending_invitation, lambda {
    where(invitation_accepted_at: nil).where.not(invitation_sent_at: nil)
  }
130

131
  def superadmin?
132
    role == SUPERADMIN
133
134
  end

Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
135
  def department_manager?
136
137
138
    role == DEPARTMENT_MANAGER
  end

139
140
141
142
  def social_worker?
    role == SOCIAL_WORKER
  end

143
144
  def volunteer?
    role == VOLUNTEER
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
145
146
  end

147
148
  def manages_department?
    department.any?
149
150
  end

151
  def profile_entity
152
153
154
155
156
157
158
    @profile_entity ||= if volunteer?
                          volunteer
                        elsif profile
                          profile
                        else
                          self
                        end
159
160
  end

Markus Koller's avatar
Markus Koller committed
161
162
163
164
  def missing_profile?
    !volunteer? && !profile
  end

165
  def self.create_user_and_send_password_reset(email:, role:)
166
    new_user = User.new(
167
      email: email, password: Devise.friendly_token, role: role
168
    )
169
    new_user.save! && new_user.send_reset_password_instructions
170
    new_user
171
  end
172

173
174
175
176
177
  def self.new_volunteer(volunteer)
    User.new(email: volunteer.primary_email, password: Devise.friendly_token, role: 'volunteer',
      volunteer: volunteer).save
  end

Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
178
  def to_s
179
    full_name
180
181
  end

182
  def full_name
183
    if profile&.contact
184
      "#{profile.contact.last_name}, #{profile.contact.first_name}"
185
186
    elsif volunteer?
      volunteer.contact.full_name
187
188
189
    else
      email
    end
190
191
  end

192
  def self.role_collection
193
    ROLES_FOR_USER_CREATE.map(&:to_sym)
194
  end
195

196
197
198
199
  protected

  def assign_primary_email
    profile.contact.primary_email = email
200
  end
Tugce Nur Tas's avatar
Tugce Nur Tas committed
201
end