Class: Staff

Inherits:
ApplicationRecord show all
Includes:
TypesOfActor
Defined in:
app/models/staff.rb

Overview

There are 3 types of staff

  1. Owner's staff, which handle single restaurant

  2. Restaurant group's staff, which handle many restaurants

  3. Hotel's staff, which only has access to specific restaurant's packages 3.basecamp.com/5190892/projects/28806625

to create staff hotel, use following code

packages = [
  HhPackage::RestaurantPackage.find_by(slug: '3453-XP-xxx'),
  HhPackage::RestaurantPackage.find_by(slug: '2082-XP-xxx'),
  HhPackage::RestaurantPackage.find_by(slug: '2082-XP-xxx'),
]

staff= Staff.new email: 'xxxx', name: 'xxxx'
staff.password = 'monowheel123'
staff.save
packages.each do |package|
  sp = staff.staff_packages.build
  sp.restaurant_package = package
  sp.save
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypesOfActor

#kinda_like_owner?, #owner?, #restaurant_group?, #staff?, #user?

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.default_password(account) ⇒ Object

generate default password from phone number if phone number is present



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/staff.rb', line 122

def self.default_password()
  phone = if .is_a?(RestaurantGroup)
            .restaurants.first.owner&.phone
          else
            &.phone
          end

  return 'default_password' if phone.nil?

  phone = phone.dup
  phone.delete!('+')

  if phone[0] == '0'
    phone[0] = '66'
  end

  phone
end

.from_token_payload(payload) ⇒ Object



141
142
143
# File 'app/models/staff.rb', line 141

def self.from_token_payload(payload)
  find payload['sub']
end

Instance Method Details

#critical_role?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/models/staff.rb', line 228

def critical_role?
  roles.select { |st| st.role.admin? || st.role.owner? }.present?
end

#default_restaurantObject



205
206
207
208
209
# File 'app/models/staff.rb', line 205

def default_restaurant
  restaurant_id = restaurants.find_by(allow_booking: true)&.id || restaurants.first.id

  @default_restaurant = Restaurant.find_by(id: restaurant_id)
end

#enable_mongodb_syncObject



198
199
200
201
202
203
# File 'app/models/staff.rb', line 198

def enable_mongodb_sync
  restaurants.each do |restaurant|
    actor_restaurant_id = Flipper::Actor.new(restaurant.id)
    Flipper[:mongo_db_sync_enabled].enable_actor(actor_restaurant_id)
  end
end

#expired?Boolean

Returns:

  • (Boolean)


211
212
213
214
215
# File 'app/models/staff.rb', line 211

def expired?
  return false if expired_at.blank?

  expired_at < Time.current
end

#first_nameObject



149
150
151
152
153
154
155
156
157
158
# File 'app/models/staff.rb', line 149

def first_name
  return self[:first_name] if first_name?

  return '' if name.blank?

  names = name.strip.split
  return name if names.size == 1

  names.slice(0..-2).join(' ')
end

#last_nameObject



160
161
162
163
164
165
166
167
168
169
# File 'app/models/staff.rb', line 160

def last_name
  return self[:last_name] if last_name?

  return '' if name.blank?

  names = name.strip.split
  return '' if names.size == 1

  names.last
end

#send_welcome_emailObject



217
218
219
220
221
222
# File 'app/models/staff.rb', line 217

def send_welcome_email
  return if expired_at.present? # skip if temporary staff
  return if roles.blank? # skip if staff has no role or restaurant

  StaffMailer.with(staff: self, password: password).welcome.deliver_later!
end

Synchronize password changes to related Owner or RestaurantGroup accounts This ensures password changes made directly to Staff are propagated



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/models/staff.rb', line 234

def sync_password_to_related_accounts
  # Skip if this is triggered by an owner/restaurant_group update to prevent recursion
  return if @skip_password_sync
  return unless saved_change_to_encrypted_password? && password.present?

  if owner.present?
    # Set a flag on the owner to prevent it from updating this staff again
    owner.instance_variable_set(:@skip_staff_update, true)
    owner.password = password
    owner.audit_comment = "Password synchronized from Staff ID: #{id}"
    owner.save
  elsif restaurant_group.present?
    # Set a flag on the restaurant_group to prevent it from updating this staff again
    restaurant_group.instance_variable_set(:@skip_staff_update, true)
    restaurant_group.password = password
    restaurant_group.audit_comment = "Password synchronized from Staff ID: #{id}"
    restaurant_group.save
  end
end

#temporary_staff?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'app/models/staff.rb', line 224

def temporary_staff?
  roles.first.role&.temporary?
end

#update_first_name_last_nameObject



171
172
173
174
175
176
177
# File 'app/models/staff.rb', line 171

def update_first_name_last_name
  return if name.blank? || last_name.present? && first_name.present?

  names = name.strip.split
  self[:first_name] = names.size == 1 ? name : names.slice(0..-2).join(' ')
  self[:last_name] = names.size == 1 ? '' : names.last
end

#update_tracked_fields(request) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/staff.rb', line 179

def update_tracked_fields(request)
  old_current = 
  new_current = Time.now.utc
  self.     = old_current || new_current
  self.  = new_current

  old_current = 
  new_current = request.ip
  self.     = old_current || new_current
  self.  = new_current

  old_current = current_platform_stay_in
  new_current = UserAgentParser.new(request.user_agent).platform
  self.last_platform_stay_in     = old_current || new_current
  self.current_platform_stay_in  = new_current

  save(validate: false)
end