Module: Sfctl::Harvest::Sync

Defined in:
lib/sfctl/harvest/sync.rb

Class Method Summary collapse

Class Method Details

.assignment_items(time_entries, connection) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sfctl/harvest/sync.rb', line 68

def self.assignment_items(time_entries, connection)
  hours_field = hours_field(connection['rounding'])
  time_entries.map do |te|
    hours = te[hours_field]
    time_seconds = hours * 60 * 60
    {
      time_seconds: time_seconds.round,
      date: te['spent_date'].to_s,
      comment: te['notes'],
      external_id: te['id'].to_s
    }
  end
end

.get_time_entries(connection, harvest_config, report_interval) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/sfctl/harvest/sync.rb', line 26

def self.get_time_entries(connection, harvest_config, report_interval)
  _success, data = Harvest::Client.time_entries(
    harvest_config['account_id'],
    harvest_config['access_token'],
    time_entries_params(connection, report_interval)
  )

  data
end

.hours_field(rounding) ⇒ Object



36
37
38
39
40
# File 'lib/sfctl/harvest/sync.rb', line 36

def self.hours_field(rounding)
  return 'rounded_hours' if rounding == 'on'

  'hours'
end

.load_data(output, connection, harvest_config, pastel, report_interval) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sfctl/harvest/sync.rb', line 9

def self.load_data(output, connection, harvest_config, pastel, report_interval)
  spinner = TTY::Spinner.new("Loaded data from #{Sfctl::Command::HARVEST_PROVIDER}: [:spinner]", format: :dots)
  spinner.auto_spin

  time_entries = get_time_entries(connection, harvest_config, report_interval)

  spinner.success(pastel.green('Done'))

  table = TTY::Table.new %w[Date Comment Time], time_entries_table_rows(time_entries, connection)
  output.puts
  output.print table.render(:unicode, padding: [0, 1], alignments: %i[left left right])
  output.puts
  output.puts

  time_entries
end

.time_entries_params(connection, report_interval) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sfctl/harvest/sync.rb', line 56

def self.time_entries_params(connection, report_interval)
  start_date, end_date = report_interval
  params = {
    project_id: connection['project_id'],
    task_id: connection['task_id'],
    from: start_date.to_s,
    to: end_date.to_s
  }
  params[:is_billed] = connection['billable'] == 'yes' unless connection['billable'] == 'both'
  params
end

.time_entries_table_rows(time_entries, connection) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sfctl/harvest/sync.rb', line 42

def self.time_entries_table_rows(time_entries, connection)
  hours_field = hours_field(connection['rounding'])
  rows = time_entries.sort_by { |te| te['spent_date'] }.map do |te|
    [
      te['spent_date'],
      te['notes'],
      "#{te[hours_field]}h"
    ]
  end
  total_grand = time_entries.map { |te| te[hours_field] }.sum
  rows.push(['Total:', '', "#{total_grand}h"])
  rows
end