|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'thor' |
| 4 | +require_relative '../lib/freeagent' |
| 5 | + |
| 6 | +def match type, collection, needle, *values |
| 7 | + found = collection.select do |item| |
| 8 | + values.map(&:to_proc).any? {|v| v.call(item) == needle } |
| 9 | + end |
| 10 | + if found.none? |
| 11 | + possibles = collection.product(values).map {|item, value| value.to_proc.call(item).to_s } |
| 12 | + raise ArgumentError, "No #{type} matching '#{needle}' found, expecting one from #{possibles}" |
| 13 | + end |
| 14 | + unless found.one? |
| 15 | + raise ArgumentError, "Ambiguous #{type}: could be #{found.map(&:url).join(', ')}" |
| 16 | + end |
| 17 | + found.first |
| 18 | +end |
| 19 | + |
| 20 | +module Freeagent |
| 21 | + class Timeslips < Thor |
| 22 | + no_commands do |
| 23 | + def api |
| 24 | + @api ||= API.new |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + desc 'list USER PROJECT TASK FROM TO', "List timeslips" |
| 29 | + def list user, project, task, from, to |
| 30 | + from = Date::parse from |
| 31 | + to = Date::parse to |
| 32 | + user = match :user, api.users, user, :first_name, :last_name, :email |
| 33 | + project = match :project, api.projects, project, :name |
| 34 | + task = match :task, api.tasks(project), task, :name |
| 35 | + |
| 36 | + api.timeslips(user, project, task, from, to).each do |timeslip| |
| 37 | + puts [Date::parse(timeslip.dated_on).strftime('%a %d %b %Y'), user.email, project.name, task.name, timeslip.hours].join("\t") |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + desc 'create USER PROJECT TASK FROM TO', "Create timeslips" |
| 42 | + method_option :hours, aliases: '-h', type: :numeric, default: 8, desc: "Number of hours per day" |
| 43 | + method_option :weekends, aliases: '-w', type: :boolean, default: false, desc: "Create timeslips on Saturdays and Sundays" |
| 44 | + def create user, project, task, from, to |
| 45 | + from = Date::parse from |
| 46 | + to = Date::parse to |
| 47 | + user = match :user, api.users, user, :first_name, :last_name, :email |
| 48 | + project = match :project, api.projects, project, :name |
| 49 | + task = match :task, api.tasks(project), task, :name |
| 50 | + |
| 51 | + puts "Creating timeslips for #{user.first_name} #{user.last_name} for task '#{task.name}' in project '#{project.name}' between #{from} and #{to} inclusive" |
| 52 | + timeslips = from.step(to).map do |date| |
| 53 | + next if (date.saturday? || date.sunday?) && !options[:weekends] |
| 54 | + {'task' => task.url, 'project' => project.url, 'user' => user.url, 'dated_on' => date.to_s, 'hours' => options[:hours]} |
| 55 | + end.reject(&:nil?) |
| 56 | + api.batch_create_timeslips timeslips |
| 57 | + end |
| 58 | + |
| 59 | + desc 'delete USER PROJECT TASK FROM TO', 'Delete timeslips' |
| 60 | + def delete user, project, task, from, to |
| 61 | + from = Date::parse from |
| 62 | + to = Date::parse to |
| 63 | + user = match :user, api.users, user, :first_name, :last_name, :email |
| 64 | + project = match :project, api.projects, project, :name |
| 65 | + task = match :task, api.tasks(project), task, :name |
| 66 | + |
| 67 | + puts "Deleting timeslips for #{user.first_name} #{user.last_name} for task '#{task.name}' in project '#{project.name}' between #{from} and #{to} inclusive" |
| 68 | + api.timeslips(user, project, task, from, to).each do |timeslip| |
| 69 | + api.delete_timeslip(timeslip) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + class Command < Thor |
| 75 | + desc "timeslips", "Create, read, update and delete timeslips" |
| 76 | + subcommand "timeslips", Timeslips |
| 77 | + end |
| 78 | +end |
| 79 | + |
| 80 | +Freeagent::Command.start |
0 commit comments