Module: EmployeeLastSeen

Defined in:
app/lib/employee_last_seen.rb

Overview

Real-time "where is this employee working from", based on the IP of their
most recent CRM request. CrmController touches this on every authenticated
request; the who's-working grid reads it and classifies the IP against
IpDetector's fixed office blocks. Cache-only — nothing is persisted, and an
entry silently expires after TTL without activity.

Constant Summary collapse

TTL =
12.hours

Class Method Summary collapse

Class Method Details

.cache_key(employee_id) ⇒ Object



29
30
31
# File 'app/lib/employee_last_seen.rb', line 29

def self.cache_key(employee_id)
  "employee_last_seen:v1:#{employee_id}"
end

.for_employees(employee_ids) ⇒ Hash{Integer => Hash}

Batch read for a page of employees.

Parameters:

  • employee_ids (Array<Integer>)

Returns:

  • (Hash{Integer => Hash})

    employee id => { 'ip' =>, 'at' => epoch seconds }



24
25
26
27
# File 'app/lib/employee_last_seen.rb', line 24

def self.for_employees(employee_ids)
  id_by_key = employee_ids.index_by { |id| cache_key(id) }
  Rails.cache.read_multi(*id_by_key.keys).transform_keys { |key| id_by_key[key] }
end

.touch(employee_id, ip) ⇒ void

This method returns an undefined value.

Parameters:

  • employee_id (Integer)
  • ip (String)

    request.remote_ip



14
15
16
17
18
# File 'app/lib/employee_last_seen.rb', line 14

def self.touch(employee_id, ip)
  return if employee_id.blank? || ip.blank?

  Rails.cache.write(cache_key(employee_id), { 'ip' => ip.to_s, 'at' => Time.current.to_i }, expires_in: TTL)
end