This repository has been archived on 2025-08-18. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
redtime/api_activities.go
Dani Rodríguez 4800e9c752 track: enforce an activity value
If the activity flag is not given when calling track, it will now try to
use the activity mark as default. If no activity type is marked as
default, it will fail.
2023-08-30 11:52:26 +02:00

58 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"net/http"
)
type remoteActivityType struct {
Id uint `json:"id"`
Name string `json:"name"`
IsDefault bool `json:"is_default"`
Active bool `json:"active"`
}
type remoteTimeEntryActivities struct {
Activities []remoteActivityType `json:"time_entry_activities"`
}
func (act *remoteTimeEntryActivities) Map() map[uint]remoteActivityType {
activities := map[uint]remoteActivityType{}
for _, item := range act.Activities {
activities[item.Id] = item
}
return activities
}
func (ctx *redmineClient) fetchActivities() (map[uint]remoteActivityType, error) {
var (
req *http.Request
data []byte
activities remoteTimeEntryActivities
err error
)
if req, err = ctx.buildGetRequest("enumerations/time_entry_activities", nil); err != nil {
return nil, err
}
if data, err = ctx.secureRequest(req); err != nil {
return nil, err
}
if err = json.Unmarshal(data, &activities); err != nil {
return nil, err
}
return activities.Map(), nil
}
func (ctx *redmineClient) fetchDefaultActivity() (*remoteActivityType, error) {
activities, err := ctx.fetchActivities()
if err != nil {
return nil, err
}
for _, act := range activities {
if act.IsDefault {
return &act, nil
}
}
return nil, nil
}