diff --git a/backend/api/getPriorityEntriesHandler.go b/backend/api/getPriorityEntriesHandler.go index 244938a2..4d2c4ee7 100644 --- a/backend/api/getPriorityEntriesHandler.go +++ b/backend/api/getPriorityEntriesHandler.go @@ -2,6 +2,8 @@ package api import ( "encoding/json" + "fmt" + "urdr-api/internal/redmine" "github.com/gofiber/fiber/v2" log "github.com/sirupsen/logrus" @@ -59,29 +61,6 @@ func getPriorityEntriesHandler(c *fiber.Ctx) error { return err } - // Now fetch the activities from Redmine and fill out the - // activity names. - c.Response().Reset() - if err := getProjectActivitiesHandler(c); err != nil { - // There was some error in the handler. - return err - } else if c.Response().StatusCode() != fiber.StatusOK { - // There was some error sent to us from Redmine. - return nil - } - - activitiesResponse := struct { - Activities []struct { - Id int `json:"id"` - Name string `json:"name"` - } `json:"time_entry_activities"` - }{} - - if err := json.Unmarshal(c.Response().Body(), &activitiesResponse); err != nil { - c.Response().Reset() - return c.SendStatus(fiber.StatusUnprocessableEntity) - } - var priorityEntries []PriorityEntry for i, dbPriorityEntry := range dbPriorityEntries { @@ -91,15 +70,71 @@ func getPriorityEntriesHandler(c *fiber.Ctx) error { CustomName: dbPriorityEntry.Name, IsHidden: dbPriorityEntry.IsHidden, } - for _, activity := range activitiesResponse.Activities { - if activity.Id == priorityEntry.Activity.Id { - priorityEntry.Activity.Name = activity.Name - break - } + projectId, err := getProjectIdForIssue(c, priorityEntry.Issue.Id) + if err != nil { + return err + } + + activityName, err := getActivityNameForIssue(c, priorityEntry.Issue.Id, projectId, priorityEntry.Activity.Id) + if err != nil { + return err } + priorityEntry.Activity.Name = activityName priorityEntries = append(priorityEntries, priorityEntry) } return c.JSON(priorityEntries) } + +func getProjectIdForIssue(c *fiber.Ctx, issueId int) (int, error) { + c.Response().Reset() + c.Request().URI().SetQueryString( + fmt.Sprintf("issue_id=%d", issueId)) + + if err := getIssuesHandler(c); err != nil { + } else if c.Response().StatusCode() != fiber.StatusOK { + return 0, nil + } + issuesResponse := struct { + Issues []Issue `json:"issues"` + }{} + if err := json.Unmarshal(c.Response().Body(), &issuesResponse); err != nil { + c.Response().Reset() + return 0, err + } + + projectId := issuesResponse.Issues[0].Project.Id + return projectId, nil + +} + +func getActivityNameForIssue(c *fiber.Ctx, issueId int, projectId int, activityId int) (string, error) { + c.Response().Reset() + c.Context().URI().SetQueryString(fmt.Sprintf("project_id=%d&issue_id=%d", projectId, issueId)) + if err := getProjectActivitiesHandler(c); err != nil { + log.Errorf("Error getProjectActivitiesHandler %v", err) + return "", err + } else if c.Response().StatusCode() != fiber.StatusOK { + log.Errorf("Error StatCode %v", err) + return "", err + } + + projectEntryResponse := redmine.Project{} + + if err := json.Unmarshal(c.Response().Body(), &projectEntryResponse); err != nil { + c.Response().Reset() + log.Errorf("Error Unmarshal projectEntryResponse %v", err) + return "", err + } + + activityName := "" + for _, activity := range projectEntryResponse.TimeEntryActivities { + if activity.Id == activityId { + activityName = activity.Name + break + } + } + + return activityName, nil +}