diff --git a/backend/api/api.go b/backend/api/api.go index 3744074f..f43cdff6 100644 --- a/backend/api/api.go +++ b/backend/api/api.go @@ -68,7 +68,7 @@ func Setup() *fiber.App { app.Get("/api/issues", getIssuesHandler) - app.Get("/api/activities", getActivitiesHandler) + app.Get("/api/activities", getProjectActivitiesHandler) app.Get("/api/priority_entries", getPriorityEntriesHandler) diff --git a/backend/api/getActivitiesHandler.go b/backend/api/getActivitiesHandler.go index 043be488..94f5ad53 100644 --- a/backend/api/getActivitiesHandler.go +++ b/backend/api/getActivitiesHandler.go @@ -3,22 +3,15 @@ package api import ( "encoding/json" "fmt" - "log" "sort" "strconv" "urdr-api/internal/config" + "urdr-api/internal/redmine" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/proxy" ) -type ProjectsResponse struct { - TimeEntryActivities []struct { - Id int `json:"id"` - Name string `json:"name"` - } `json:"time_entry_activities"` -} - // getActivitiesHandler godoc // @Summary Get a list of activities from the Redmine projects endpoint // @Accept json @@ -28,7 +21,7 @@ type ProjectsResponse struct { // @Router /api/activities [get] // @Param project_id query string false "Project ID" default(0) // @Param issue_id query string false "Issue ID" default(0) -func getActivitiesHandler(c *fiber.Ctx) error { +func getProjectActivitiesHandler(c *fiber.Ctx) error { redmineProjectId, err := strconv.Atoi(c.Query("project_id", "0")) if err != nil { return c.SendStatus(fiber.StatusInternalServerError) @@ -42,13 +35,12 @@ func getActivitiesHandler(c *fiber.Ctx) error { return err } - log.Println("redmineProjectId:", redmineProjectId) - var redmineURL string + // If we don't have a real project ID, return an empty list of activities. if redmineProjectId == 0 { - redmineURL = fmt.Sprintf("%s/enumerations/time_entry_activities.json", - config.Config.Redmine.URL) + emptyListResponse := redmine.ProjectEntry{} + return c.JSON(emptyListResponse) } else { redmineURL = fmt.Sprintf("%s/projects/%d.json?include=time_entry_activities", config.Config.Redmine.URL, redmineProjectId) @@ -61,7 +53,7 @@ func getActivitiesHandler(c *fiber.Ctx) error { return nil } - activitiesResponse := ProjectsResponse{} + activitiesResponse := redmine.ProjectEntry{} if err := json.Unmarshal(c.Response().Body(), &activitiesResponse); err != nil { c.Response().Reset() @@ -69,25 +61,19 @@ func getActivitiesHandler(c *fiber.Ctx) error { } // Sort the activities list alphabetically on the name. - sort.Slice(activitiesResponse.TimeEntryActivities, func(i, j int) bool { - return activitiesResponse.TimeEntryActivities[i].Name < - activitiesResponse.TimeEntryActivities[j].Name + sort.Slice(activitiesResponse.Project.TimeEntryActivities, func(i, j int) bool { + return activitiesResponse.Project.TimeEntryActivities[i].Name < + activitiesResponse.Project.TimeEntryActivities[j].Name }) - // Bypass filtering if we don't have a real issue ID. - if redmineIssueId == 0 { - // Return all activities. - return c.JSON(activitiesResponse) - } - - filteredActivities := ProjectsResponse{} + filteredActivities := redmine.ProjectEntry{} - for _, activity := range activitiesResponse.TimeEntryActivities { + for _, activity := range activitiesResponse.Project.TimeEntryActivities { if db.IsValidEntry(redmineIssueId, activity.Id) { - filteredActivities.TimeEntryActivities = - append(filteredActivities.TimeEntryActivities, activity) + filteredActivities.Project.TimeEntryActivities = + append(filteredActivities.Project.TimeEntryActivities, activity) } } - return c.JSON(filteredActivities) + return c.JSON(filteredActivities.Project) } diff --git a/backend/api/getPriorityEntriesHandler.go b/backend/api/getPriorityEntriesHandler.go index 06ed8ec6..244938a2 100644 --- a/backend/api/getPriorityEntriesHandler.go +++ b/backend/api/getPriorityEntriesHandler.go @@ -62,7 +62,7 @@ func getPriorityEntriesHandler(c *fiber.Ctx) error { // Now fetch the activities from Redmine and fill out the // activity names. c.Response().Reset() - if err := getActivitiesHandler(c); err != nil { + if err := getProjectActivitiesHandler(c); err != nil { // There was some error in the handler. return err } else if c.Response().StatusCode() != fiber.StatusOK { diff --git a/backend/api/handlers_test.go b/backend/api/handlers_test.go index ba98cc0e..a3730e21 100644 --- a/backend/api/handlers_test.go +++ b/backend/api/handlers_test.go @@ -102,18 +102,19 @@ func Test_Handlers(t *testing.T) { createdEntry, _ := json.Marshal(entryResult) fetchedEntries, _ := json.Marshal(entriesResult) - - entryActs := redmine.TimeEntryActivitiesResult{ - TimeEntryActivities: []redmine.TimeEntryActivity{ - { - Id: 1, - Name: "Test activity", - IsDefault: true, + projectEntry := redmine.ProjectEntry{ + Project: redmine.Project{ + TimeEntryActivities: []redmine.TimeEntryActivity{ + { + Id: 1, + Name: "test activity", + IsDefault: true, + }, }, }, } - entryActsResponse, _ := json.Marshal(entryActs) + projectActivities, _ := json.Marshal(projectEntry) issueAct := []api.PriorityEntry{ { @@ -180,7 +181,7 @@ func Test_Handlers(t *testing.T) { case "/issues.json": _, err = w.Write(issuesResponse) case "/projects/1.json": - _, err = w.Write(entryActsResponse) + _, err = w.Write(projectActivities) default: log.Debugf("%s.\n", endpoint) _, err = w.Write(nil) @@ -315,7 +316,7 @@ func Test_Handlers(t *testing.T) { { name: "Entry activities", method: "GET", - endpoint: "/api/activities", + endpoint: "/api/activities?project_id=1&issue_id=1", testRedmine: fakeRedmine, useSessionHeader: true, statusCode: fiber.StatusOK, @@ -323,7 +324,7 @@ func Test_Handlers(t *testing.T) { { name: "Entry activities 401", method: "GET", - endpoint: "/api/activities", + endpoint: "/api/activities?project_id=1&issue_id=1", testRedmine: fakeRedmine, useSessionHeader: false, statusCode: fiber.StatusUnauthorized, @@ -331,11 +332,19 @@ func Test_Handlers(t *testing.T) { { name: "Entry activities 422", method: "GET", - endpoint: "/api/activities", + endpoint: "/api/activities?project_id=1&issue_id=1", testRedmine: badRedmine, useSessionHeader: true, statusCode: fiber.StatusUnprocessableEntity, }, + { + name: "Entry activities no params", + method: "GET", + endpoint: "/api/activities", + testRedmine: badRedmine, + useSessionHeader: true, + statusCode: fiber.StatusOK, + }, { name: "priority_entries POST", method: "POST", diff --git a/backend/internal/redmine/models.go b/backend/internal/redmine/models.go index e72ceb28..c5bf7c70 100644 --- a/backend/internal/redmine/models.go +++ b/backend/internal/redmine/models.go @@ -75,3 +75,10 @@ type Group struct { Id int `json:"id"` Name string `json:"name"` } +type Project struct { + TimeEntryActivities []TimeEntryActivity `json:"time_entry_activities"` +} + +type ProjectEntry struct { + Project Project `json:"project"` +}