Skip to content

Commit

Permalink
feat: avoid clock in when you have worked the scheduled hours
Browse files Browse the repository at this point in the history
  • Loading branch information
Madh93 committed Jul 26, 2023
1 parent 406b19f commit c69f788
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
25 changes: 5 additions & 20 deletions internal/toffu/toffu.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (t *Toffu) ClockIn() (err error) {
return errors.New("error clocking in, no scheduled working hours today")
}

if t.signSlots.TimeWorked() >= time.Duration(t.workday.ScheduleHours*float64(time.Hour)) {
return errors.New("error clocking in, you have worked too much today")
}

err = t.api.Sign(t.userId)
if err != nil {
return fmt.Errorf("error clocking in: %v", err)
Expand Down Expand Up @@ -167,26 +171,7 @@ func (t *Toffu) GetStatus() (err error) {
fmt.Printf("Status: %s\n", status)

// Hours worked
totalDuration := 0 * time.Second
location, err := time.LoadLocation("Europe/Madrid") // TODO: Woffu TZ or Office TZ?
if err != nil {
return err
}

for _, slot := range t.signSlots {
inTime, _ := time.Parse("15:04:05", slot.In.ShortTrueTime)
outTime, _ := time.Parse("15:04:05", time.Now().In(location).Format("15:04:05"))
// In Office
if slot.Out.ShortTrueTime != "" {
outTime, _ = time.Parse("15:04:05", slot.Out.ShortTrueTime)
}
// Day/Night shift transition
if inTime.After(outTime) {
outTime = outTime.Add(24 * time.Hour)
}
delta := outTime.Sub(inTime)
totalDuration += delta
}
totalDuration := t.signSlots.TimeWorked()

// Remaining hours
remainingDuration := time.Duration(t.workday.ScheduleHours*float64(time.Hour)) - totalDuration
Expand Down
23 changes: 23 additions & 0 deletions internal/woffuapi/signs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"time"
)

type Signs []struct {
Expand Down Expand Up @@ -69,6 +70,28 @@ type SignSlotEvent struct {
SignEventId string `json:"SignEventId"`
}

func (s SignSlots) TimeWorked() time.Duration {
totalDuration := 0 * time.Second
location, _ := time.LoadLocation("Europe/Madrid") // TODO: Woffu TZ or Office TZ?

for _, slot := range s {
inTime, _ := time.Parse("15:04:05", slot.In.ShortTrueTime)
outTime, _ := time.Parse("15:04:05", time.Now().In(location).Format("15:04:05"))
// In Office
if slot.Out.ShortTrueTime != "" {
outTime, _ = time.Parse("15:04:05", slot.Out.ShortTrueTime)
}
// Day/Night shift transition
if inTime.After(outTime) {
outTime = outTime.Add(24 * time.Hour)
}
delta := outTime.Sub(inTime)
totalDuration += delta
}

return totalDuration
}

func (w WoffuAPI) GetSignSlots() (SignSlots, error) {
if w.auth.Type() != "TokenAuth" {
return nil, errors.New("token authentication is required")
Expand Down

0 comments on commit c69f788

Please sign in to comment.