Skip to content

Commit

Permalink
wip: fix teas
Browse files Browse the repository at this point in the history
  • Loading branch information
jonandernovella committed Sep 8, 2023
1 parent 295c514 commit aea5749
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
35 changes: 24 additions & 11 deletions backend/api/getActivitiesHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"log"
"sort"
"strconv"
"urdr-api/internal/config"
Expand All @@ -11,24 +12,27 @@ import (
"github.com/gofiber/fiber/v2/middleware/proxy"
)

type TimeEntryActivityResponse struct {
type ProjectsResponse struct {
TimeEntryActivities []struct {
Id int `json:"id"`
Name string `json:"name"`
IsDefault bool `json:"is_default"`
Active bool `json:"active"`
Id int `json:"id"`
Name string `json:"name"`
} `json:"time_entry_activities"`
}

// getActivitiesHandler godoc
// @Summary (Mostly) a proxy for the "/enumerations/time_entry_activities.json" Redmine endpoint
// @Summary Get a list of activities from the Redmine projects endpoint
// @Accept json
// @Produce json
// @Failure 401 {string} error "Unauthorized"
// @Failure 500 {string} error "Internal Server Error"
// @Router /api/activities [get]
// @Param session_id query string false "Issue ID" default(0)
// @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 {
redmineProjectId, err := strconv.Atoi(c.Query("project_id", "0"))
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}
redmineIssueId, err := strconv.Atoi(c.Query("issue_id", "0"))
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
Expand All @@ -38,8 +42,17 @@ func getActivitiesHandler(c *fiber.Ctx) error {
return err
}

redmineURL := fmt.Sprintf("%s/enumerations/time_entry_activities.json",
config.Config.Redmine.URL)
log.Println("redmineProjectId:", redmineProjectId)

var redmineURL string

if redmineProjectId == 0 {
redmineURL = fmt.Sprintf("%s/enumerations/time_entry_activities.json",
config.Config.Redmine.URL)
} else {
redmineURL = fmt.Sprintf("%s/projects/%d.json?include=time_entry_activities",
config.Config.Redmine.URL, redmineProjectId)
}

// Proxy the request to Redmine
if err := proxy.Do(c, redmineURL); err != nil {
Expand All @@ -48,7 +61,7 @@ func getActivitiesHandler(c *fiber.Ctx) error {
return nil
}

activitiesResponse := TimeEntryActivityResponse{}
activitiesResponse := ProjectsResponse{}

if err := json.Unmarshal(c.Response().Body(), &activitiesResponse); err != nil {
c.Response().Reset()
Expand All @@ -67,7 +80,7 @@ func getActivitiesHandler(c *fiber.Ctx) error {
return c.JSON(activitiesResponse)
}

filteredActivities := TimeEntryActivityResponse{}
filteredActivities := ProjectsResponse{}

for _, activity := range activitiesResponse.TimeEntryActivities {
if db.IsValidEntry(redmineIssueId, activity.Id) {
Expand Down
2 changes: 1 addition & 1 deletion backend/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func Test_Handlers(t *testing.T) {
}
case "/issues.json":
_, err = w.Write(issuesResponse)
case "/enumerations/time_entry_activities.json":
case "/projects/1.json":
_, err = w.Write(entryActsResponse)
default:
log.Debugf("%s.\n", endpoint)
Expand Down
2 changes: 2 additions & 0 deletions backend/api/postTimeEntriesHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func postTimeEntriesHandler(c *fiber.Ctx) error {
log.Errorf("proxy.Do() failed: %v\n", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
log.Debugf("respose from redmine: %s", c.Response().Body())

if session, err := store.Get(c); err != nil {

return c.SendStatus(fiber.StatusInternalServerError)
} else {
// Extend the session's expiry time to a week.
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/QuickAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const QuickAdd = ({

React.useEffect(() => {
let endpoint = "/api/activities";
if (issue) endpoint += "?issue_id=" + issue.id;
if (issue) endpoint += "?project_id=" + (issue.project.id ? issue.project.id : "0") + "&issue_id=" + (issue.id ? issue.id : "0");
let didCancel = false;
const loadActivities = async () => {
let result: { time_entry_activities: IdName[] } = await getApiEndpoint(
Expand Down Expand Up @@ -80,6 +80,7 @@ export const QuickAdd = ({
candidateIssues = [foundIssue];
//allow for valid issue id autoselection
if (isNumber(search.text)) {
console.log(foundIssue)
setIssue(foundIssue);
}
} else {
Expand All @@ -104,6 +105,7 @@ export const QuickAdd = ({
);

const suggestionSelected = (selection: Issue) => {
console.log(selection)
setIssue(selection);
// Update input box with selected issue
let element = issueInputRef.current;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IssueActivityPair {
export interface Issue {
id: number;
subject: string;
project: IdName;
}

export interface TimeEntry {
Expand Down

0 comments on commit aea5749

Please sign in to comment.