@@ -20,14 +20,15 @@ package openproject
2020import (
2121 "encoding/json"
2222 "fmt"
23+ "net/url"
24+ "strconv"
25+
2326 "github.com/JankariTech/OpenProjectTmetricIntegration/config"
2427 "github.com/go-resty/resty/v2"
2528 "github.com/tidwall/gjson"
26- "net/url"
27- "strconv"
2829)
2930
30- func GetAllTimeEntries (config * config.Config , user User , startDate string , endDate string ) ([]TimeEntry , error ) {
31+ func GetAllTimeEntries (config * config.Config , user User , startDate string , endDate string , workpackages [] any ) ([]TimeEntry , error ) {
3132 httpClient := resty .New ()
3233 openProjectUrl , _ := url .JoinPath (config .OpenProjectUrl , "/api/v3/time_entries" )
3334 var userString string
@@ -38,18 +39,20 @@ func GetAllTimeEntries(config *config.Config, user User, startDate string, endDa
3839 } else {
3940 userString = strconv .Itoa (user .Id )
4041 }
42+
43+ filters := "["
44+ if workpackages != nil && len (workpackages ) > 0 {
45+ entityIds , _ := json .Marshal (workpackages )
46+ filters += fmt .Sprintf (`{"entity_id": {"operator":"=","values": %v}},` , string (entityIds ))
47+ }
48+ // the operator is '<>d' ("\u003c\u003ed") and means between the dates
49+ filters += fmt .Sprintf (`{"user":{"operator":"=","values":["%v"]}},{"spent_on":{"operator":"\u003c\u003ed","values":["%v","%v"]}}]` , userString , startDate , endDate )
4150 resp , err := httpClient .R ().
4251 SetBasicAuth ("apikey" , config .OpenProjectToken ).
4352 SetHeader ("Content-Type" , "application/json" ).
4453 SetQueryParam ("pageSize" , "3000" ).
4554 SetQueryParam ("sortBy" , "[[\" updated_at\" ,\" asc\" ]]" ).
46- // the operator is '<>d' and means between the dates
47- SetQueryParam ("filters" , fmt .Sprintf (
48- `[{"user":{"operator":"=","values":["%v"]}},{"spent_on":{"operator":"\u003c\u003ed","values":["%v","%v"]}}]` ,
49- userString ,
50- startDate ,
51- endDate ),
52- ).
55+ SetQueryParam ("filters" , filters ).
5356 Get (openProjectUrl )
5457 if err != nil || resp .StatusCode () != 200 {
5558 return nil , fmt .Errorf (
0 commit comments