From 31246e242214b79744d84af17f61a100f3dacc1d Mon Sep 17 00:00:00 2001 From: jonandernovella Date: Fri, 27 Oct 2023 13:57:15 +0200 Subject: [PATCH 1/2] fix: activity name labels --- backend/api/getActivitiesHandler.go | 3 + backend/api/getPriorityEntriesHandler.go | 91 ++++++++++++++++-------- backend/api/models.go | 1 + 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/backend/api/getActivitiesHandler.go b/backend/api/getActivitiesHandler.go index 94f5ad53..986f5657 100644 --- a/backend/api/getActivitiesHandler.go +++ b/backend/api/getActivitiesHandler.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "log" "sort" "strconv" "urdr-api/internal/config" @@ -31,6 +32,8 @@ func getProjectActivitiesHandler(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusInternalServerError) } + log.Printf("Project ID: %d, Issue ID: %d", redmineProjectId, redmineIssueId) + if ok, err := prepareRedmineRequest(c); !ok { return err } 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 +} diff --git a/backend/api/models.go b/backend/api/models.go index 102e4aa4..84fe9e56 100644 --- a/backend/api/models.go +++ b/backend/api/models.go @@ -31,6 +31,7 @@ type Activity struct { type Entry struct { Issue Issue `json:"issue"` Activity Activity `json:"activity"` + Project Project `json:"project"` } type PriorityEntry struct { From 128cfbe9f32faa323c6e178608cd20a9fd7d79c3 Mon Sep 17 00:00:00 2001 From: jonandernovella Date: Fri, 27 Oct 2023 14:46:02 +0200 Subject: [PATCH 2/2] fix: cleanup logs and unused attribute --- backend/api/getActivitiesHandler.go | 3 --- backend/api/models.go | 1 - 2 files changed, 4 deletions(-) diff --git a/backend/api/getActivitiesHandler.go b/backend/api/getActivitiesHandler.go index 986f5657..94f5ad53 100644 --- a/backend/api/getActivitiesHandler.go +++ b/backend/api/getActivitiesHandler.go @@ -3,7 +3,6 @@ package api import ( "encoding/json" "fmt" - "log" "sort" "strconv" "urdr-api/internal/config" @@ -32,8 +31,6 @@ func getProjectActivitiesHandler(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusInternalServerError) } - log.Printf("Project ID: %d, Issue ID: %d", redmineProjectId, redmineIssueId) - if ok, err := prepareRedmineRequest(c); !ok { return err } diff --git a/backend/api/models.go b/backend/api/models.go index 84fe9e56..102e4aa4 100644 --- a/backend/api/models.go +++ b/backend/api/models.go @@ -31,7 +31,6 @@ type Activity struct { type Entry struct { Issue Issue `json:"issue"` Activity Activity `json:"activity"` - Project Project `json:"project"` } type PriorityEntry struct {