Class: Hammerhead::Commands::Status

Inherits:
Hammerhead::Command show all
Includes:
Utils
Defined in:
lib/hammerhead/commands/status.rb

Overview

Implements the status command.

Using a Harvest.connection and a client Id, like one obtained via the clients command, this command prepares a ‘status report’ for display in the console.

This command warns about: an inactive client, a client with no active projects, or no timesheet entries for the period specified.

The output consists of the client’s name, the text: ‘Status Report (week ending <date>)’, along with the timesheet entries returned. They are listed in project order, in entry order:

----------------------------------------

ACME Co, Inc

Status Report (week ending 9/19/20)

I worked 0 hours.
----------------------------------------

Instance Method Summary collapse

Methods included from Utils

#digits?

Constructor Details

#initialize(client, options) ⇒ Status

:nodoc:



34
35
36
37
38
# File 'lib/hammerhead/commands/status.rb', line 34

def initialize client, options # :nodoc:
  self.specified_client = client
  self.options = options
  configure_query_dates
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object

:stopdoc:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hammerhead/commands/status.rb', line 43

def execute input: $stdin, output: $stdout
  process_short_cut
  connection = Harvest.connection
  output.puts "Fetching details for specified client: #{specified_client}"
  client = connection.client specified_client
  unless client.active?
    output.puts "#{client.name} is not active."
    return
  end
  projects = connection.projects_for_client client
  if projects.empty?
    output.puts "#{client.name} has no active projects."
    return
  end
  entries = connection.my_time_sheet_entries start_date, end_date
  if entries.empty?
    output.puts "There are no timesheet entries for the period specified: #{start_date} -> #{end_date}"
    return
  end

  status_output = {
    hours: 0.0,
    projects: {},
    entries: {}
  }

  projects.each do |project|
    status_output[:projects][project.id] = project.code
  end

  entries.each do |entry|
    next unless status_output[:projects].key? entry.project_id

    status_output[:hours] += entry.hours
    project_code = status_output[:projects][entry.project_id]
    status_output[:entries][project_code] = [] unless status_output[:entries].key? project_code
    status_output[:entries][project_code] << entry.notes
  end

  output.puts '----------------------------------------'
  output.puts
  output.puts client.name.to_s
  output.puts
  output.puts "Status Report (week ending #{end_date.strftime('%-m/%-d/%y')})"
  output.puts

  if status_output[:hours] > 0.0
    output.puts "I worked #{status_output[:hours]} hours on the following:"
    status_output[:entries].each do |code, _entries|
      output.puts "\n[#{code}]"
      _entries.each do |entry|
        output.puts "- #{entry}"
      end
    end
  else
    output.puts 'I worked 0 hours.'
  end

  output.puts
  output.puts '----------------------------------------'
rescue StandardError => e
  Hammerhead.logger.error 'STATUS COMMAND ERROR:', e
end