Class: CloverSandboxSimulator::Services::Clover::ShiftService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/clover_sandbox_simulator/services/clover/shift_service.rb

Overview

Manages Clover employee shifts (clock in/out, shift tracking)

Instance Attribute Summary

Attributes inherited from BaseService

#config, #logger

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from CloverSandboxSimulator::Services::BaseService

Instance Method Details

#calculate_duration(shift) ⇒ Float

Calculate shift duration in hours

Parameters:

  • shift (Hash)

    A shift object with inTime and outTime

Returns:

  • (Float)

    Duration in hours



78
79
80
81
82
83
84
85
86
87
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 78

def calculate_duration(shift)
  in_time = shift["inTime"]
  out_time = shift["outTime"]

  return 0 if in_time.nil? || out_time.nil?

  # Times are in milliseconds
  duration_ms = out_time - in_time
  duration_ms / (1000.0 * 60 * 60) # Convert to hours
end

#clock_in(employee_id:, in_time: nil) ⇒ Object

Clock in an employee (start a shift)

Parameters:

  • employee_id (String)

    The employee ID

  • in_time (Integer, nil) (defaults to: nil)

    The clock-in time in milliseconds (defaults to now)



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 20

def clock_in(employee_id:, in_time: nil)
  in_time ||= (Time.now.to_f * 1000).to_i

  logger.info "Clocking in employee #{employee_id}"

  payload = {
    "employee" => { "id" => employee_id },
    "inTime" => in_time
  }

  request(:post, endpoint("shifts"), payload: payload)
end

#clock_out(shift_id:, out_time: nil) ⇒ Object

Clock out an employee (end a shift)

Parameters:

  • shift_id (String)

    The shift ID

  • out_time (Integer, nil) (defaults to: nil)

    The clock-out time in milliseconds (defaults to now)



36
37
38
39
40
41
42
43
44
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 36

def clock_out(shift_id:, out_time: nil)
  out_time ||= (Time.now.to_f * 1000).to_i

  logger.info "Clocking out shift #{shift_id}"

  payload = { "outTime" => out_time }

  request(:post, endpoint("shifts/#{shift_id}"), payload: payload)
end

#clock_out_all_active(out_time: nil) ⇒ Object

Clock out all active shifts (end of day cleanup)



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 61

def clock_out_all_active(out_time: nil)
  active = get_active_shifts
  return [] if active.empty?

  logger.info "Clocking out #{active.size} active shifts"

  active.map do |shift|
    clock_out(shift_id: shift["id"], out_time: out_time)
  rescue StandardError => e
    logger.warn "Failed to clock out shift #{shift['id']}: #{e.message}"
    nil
  end.compact
end

#get_active_shiftsObject

Get all active shifts (shifts without an outTime)



47
48
49
50
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 47

def get_active_shifts
  shifts = get_shifts
  shifts.select { |s| s["outTime"].nil? }
end

#get_employee_shift(employee_id:) ⇒ Hash?

Get the active shift for a specific employee

Parameters:

  • employee_id (String)

    The employee ID

Returns:

  • (Hash, nil)

    The active shift or nil



55
56
57
58
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 55

def get_employee_shift(employee_id:)
  active = get_active_shifts
  active.find { |s| s.dig("employee", "id") == employee_id }
end

#get_shifts(limit: 100) ⇒ Object

Fetch all shifts



9
10
11
12
13
14
15
# File 'lib/clover_sandbox_simulator/services/clover/shift_service.rb', line 9

def get_shifts(limit: 100)
  logger.info "Fetching shifts..."
  response = request(:get, endpoint("shifts"), params: { limit: limit })
  shifts = response&.dig("elements") || []
  logger.info "Found #{shifts.size} shifts"
  shifts
end