Skip to content

Commit

Permalink
Add support for billable property
Browse files Browse the repository at this point in the history
- Update billable property for projects
- New time entries take billable value from project
- Only show billable option for premium workspaces
  • Loading branch information
blackjid authored and jason0x43 committed May 15, 2017
1 parent adcebe8 commit a0359a8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
19 changes: 19 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ func projectItems(project toggl.Project, arg string) (items []alfred.Item, err e
}
}

if isWorkspacePremium(project.Wid) && alfred.FuzzyMatches("billable:", arg) {
var item alfred.Item

updateEntry := project
updateEntry.Billable = !project.Billable

item.Title = "Billable"
item.Subtitle = "Update default project's billable flag"
item.Autocomplete = "Billable: " + alfred.Stringify(project.Billable)
item.Arg = &alfred.ItemArg{
Keyword: "projects",
Mode: alfred.ModeDo,
Data: alfred.Stringify(projectCfg{ToUpdate: &updateEntry}),
}
item.AddCheckBox(project.Billable)

items = append(items, item)
}

if alfred.FuzzyMatches("timers", arg) {
items = append(items, alfred.Item{
Title: "Time entries...",
Expand Down
14 changes: 14 additions & 0 deletions support.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ func getClientByID(id int) (client toggl.Client, index int, found bool) {
return
}

func getWorkspaceByID(id int) (workspace toggl.Workspace, index int, found bool) {
for i, workspace := range cache.Account.Data.Workspaces {
if workspace.ID == id {
return workspace, i, true
}
}
return
}

func findTimersByProjectID(pid int) (entries []toggl.TimeEntry) {
for _, entry := range cache.Account.Data.TimeEntries[:] {
if entry.Pid == pid {
Expand Down Expand Up @@ -189,6 +198,11 @@ func getLatestTimeEntriesForProject(pid int) (matchedArr []toggl.TimeEntry) {
return
}

func isWorkspacePremium(id int) bool {
workspace, _, _ := getWorkspaceByID(id)
return workspace.Premium
}

func projectHasTimeEntries(pid int) bool {
entries := cache.Account.Data.TimeEntries
for i := range entries {
Expand Down
23 changes: 22 additions & 1 deletion time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ type timerCfg struct {
Timer *int `json:"timer,omitempty"`
Property *string `json:"property,omitempty"`
Project *int `json:"project,omitempty"`
Billable *int `json:"billable,omitempty"`
Tag *int `json:"tag,omitempty"`
ToStart *startDesc `json:"tostart,omitempty"`
ToUpdate *toggl.TimeEntry `json:"toupdate,omitempty"`
Expand Down Expand Up @@ -404,7 +405,8 @@ func startTimeEntry(desc startDesc) (entry toggl.TimeEntry, err error) {
session := toggl.OpenSession(config.APIKey)

if desc.Pid != 0 {
entry, err = session.StartTimeEntryForProject(desc.Description, desc.Pid)
project, _, _ := getProjectByID(desc.Pid)
entry, err = session.StartTimeEntryForProject(desc.Description, desc.Pid, project.Billable)
} else {
entry, err = session.StartTimeEntry(desc.Description)
}
Expand Down Expand Up @@ -661,6 +663,25 @@ func timeEntryItems(entry *toggl.TimeEntry, query string) (items []alfred.Item,
}
}

if isWorkspacePremium(entry.Wid) && alfred.FuzzyMatches("billable:", parts[0]) {
var item alfred.Item

updateEntry := entry.Copy()
updateEntry.Billable = !entry.Billable

item.Title = "Billable"
item.Subtitle = "Update this entry's billable condition"
item.Autocomplete = "Billable: " + alfred.Stringify(entry.Billable)
item.Arg = &alfred.ItemArg{
Keyword: "timers",
Mode: alfred.ModeDo,
Data: alfred.Stringify(timerCfg{ToUpdate: &updateEntry}),
}
item.AddCheckBox(entry.Billable)

items = append(items, item)
}

if alfred.FuzzyMatches("start:", parts[0]) {
command := "Start"

Expand Down

0 comments on commit a0359a8

Please sign in to comment.