Skip to content

Commit

Permalink
Merge tag 'v2.1'
Browse files Browse the repository at this point in the history
v2.1
  • Loading branch information
alejoar committed Apr 29, 2022
2 parents 414363d + 03ee80f commit d0d077a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GLOBAL OPTIONS:
--today, -t clock in for today only (default: false)
--until-today, --ut clock in only until today (default: false)
--dry-run, --dr do a dry run without actually clocking in (default: false)
--reset-month, --rm delete all shifts for the given month (default: false)
--help, -h show help (default: false)
```

Expand Down
37 changes: 32 additions & 5 deletions factorial/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ func (c *factorialClient) ClockIn(dry_run bool) {
t = time.Date(c.year, time.Month(c.month), d.Day, 0, 0, 0, 0, time.UTC)
message = fmt.Sprintf("%s... ", t.Format("02 Jan"))
spinner.Prefix = message + " "
if c.clockedIn(d.Day) {
message = fmt.Sprintf("%s ❌ Already clocked in\n", message)
clocked_in, clocked_times := c.clockedIn(d.Day, shift)
if clocked_in {
message = fmt.Sprintf("%s ❌ Period overlap: %s\n", message, clocked_times)
} else if d.Is_leave {
message = fmt.Sprintf("%s ❌ %s\n", message, d.Leave_name)
} else if !d.Is_laborable {
Expand Down Expand Up @@ -245,11 +246,37 @@ func (c *factorialClient) setShifts() error {
return nil
}

func (c *factorialClient) clockedIn(day int) bool {
func (c *factorialClient) clockedIn(day int, input_shift shift) (bool, string) {
clock_in, _ := strconv.Atoi(strings.Join(strings.Split(input_shift.Clock_in, ":"), ""))
clock_out, _ := strconv.Atoi(strings.Join(strings.Split(input_shift.Clock_out, ":"), ""))
for _, shift := range c.shifts {
if shift.Day == day {
return true
shift_clock_in, _ := strconv.Atoi(strings.Join(strings.Split(shift.Clock_in, ":"), ""))
shift_clock_out, _ := strconv.Atoi(strings.Join(strings.Split(shift.Clock_out, ":"), ""))
if (clock_in < shift_clock_in && shift_clock_in < clock_out) ||
(clock_in < shift_clock_out && shift_clock_out < clock_out) ||
(shift_clock_in <= clock_in && shift_clock_out >= clock_out) {
return true, strings.Join([]string{shift.Clock_in, shift.Clock_out}, " - ")
}
}
}
return false
return false, ""
}

func (c *factorialClient) ResetMonth() {
var t time.Time
var message string
for _, shift := range c.shifts {
req, _ := http.NewRequest("DELETE", BASE_URL+"/attendance/shifts/"+strconv.Itoa(int(shift.Id)), nil)
resp, _ := c.Do(req)
t = time.Date(c.year, time.Month(c.month), shift.Day, 0, 0, 0, 0, time.UTC)
message = fmt.Sprintf("%s... ", t.Format("02 Jan"))
if resp.StatusCode != 204 {
fmt.Print(fmt.Sprintf("%s ❌ Error when attempting to delete shift: %d, %s - %s\n", message, shift.Day, shift.Clock_in, shift.Clock_out))
} else {
fmt.Print(fmt.Sprintf("%s ✅ Shift deleted: %d, %s - %s\n", message, shift.Day, shift.Clock_in, shift.Clock_out))
}
defer resp.Body.Close()
}
fmt.Println("done!")
}
16 changes: 13 additions & 3 deletions factorialsucks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
app := &cli.App{
Name: "factorialsucks",
Usage: "FactorialHR auto clock in for the whole month from the command line",
Version: "2.0.2",
Version: "2.1",
Compiled: time.Now(),
UsageText: "factorialsucks [options]",
HideHelpCommand: true,
Expand Down Expand Up @@ -70,6 +70,12 @@ func main() {
Aliases: []string{"dr"},
Usage: "do a dry run without actually clocking in",
},
&cli.BoolFlag{
Name: "reset-month",
Aliases: []string{"rm"},
Usage: "delete all shifts for the given month",
Value: false,
},
},
Action: factorialsucks,
}
Expand All @@ -95,9 +101,13 @@ func factorialsucks(c *cli.Context) error {
clock_out := c.String("clock-out")
dry_run := c.Bool("dry-run")
until_today := c.Bool("until-today")
reset_month := c.Bool("reset-month")

client := factorial.NewFactorialClient(email, password, year, month, clock_in, clock_out, today_only, until_today)
client.ClockIn(dry_run)

if reset_month {
client.ResetMonth()
} else {
client.ClockIn(dry_run)
}
return nil
}

0 comments on commit d0d077a

Please sign in to comment.