Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion backend/controllers/add_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"ccsync_backend/models"
"ccsync_backend/utils"
"ccsync_backend/utils/tw"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -53,11 +54,18 @@ func AddTaskHandler(w http.ResponseWriter, r *http.Request) {
recur := requestBody.Recur
tags := requestBody.Tags
annotations := requestBody.Annotations
depends := requestBody.Depends

if description == "" {
http.Error(w, "Description is required, and cannot be empty!", http.StatusBadRequest)
return
}

// Validate dependencies
if err := utils.ValidateDependencies(depends, ""); err != nil {
http.Error(w, fmt.Sprintf("Invalid dependencies: %v", err), http.StatusBadRequest)
return
}
var dueDateStr string
if dueDate != nil && *dueDate != "" {
dueDateStr = *dueDate
Expand All @@ -68,7 +76,7 @@ func AddTaskHandler(w http.ResponseWriter, r *http.Request) {
Name: "Add Task",
Execute: func() error {
logStore.AddLog("INFO", fmt.Sprintf("Adding task: %s", description), uuid, "Add Task")
err := tw.AddTaskToTaskwarrior(email, encryptionSecret, uuid, description, project, priority, dueDateStr, start, entryDate, waitDate, end, recur, tags, annotations)
err := tw.AddTaskToTaskwarrior(email, encryptionSecret, uuid, description, project, priority, dueDateStr, start, entryDate, waitDate, end, recur, tags, annotations, depends)
if err != nil {
logStore.AddLog("ERROR", fmt.Sprintf("Failed to add task: %v", err), uuid, "Add Task")
return err
Expand Down
76 changes: 76 additions & 0 deletions backend/controllers/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,79 @@ func Test_AddTaskHandler_MissingDescription(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, rr.Code)
assert.Contains(t, rr.Body.String(), "Description is required")
}

// Task Dependencies Tests
func Test_AddTaskHandler_WithDependencies(t *testing.T) {
GlobalJobQueue = NewJobQueue()

requestBody := map[string]interface{}{
"email": "test@example.com",
"encryptionSecret": "secret",
"UUID": "test-uuid",
"description": "Task with dependencies",
"project": "TestProject",
"priority": "H",
"depends": []string{"task-uuid-1", "task-uuid-2"},
"tags": []string{"dependent"},
}

body, _ := json.Marshal(requestBody)
req, err := http.NewRequest("POST", "/add-task", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")

rr := httptest.NewRecorder()
AddTaskHandler(rr, req)

assert.Equal(t, http.StatusAccepted, rr.Code)
}

func Test_AddTaskHandler_WithEmptyDependencies(t *testing.T) {
GlobalJobQueue = NewJobQueue()

requestBody := map[string]interface{}{
"email": "test@example.com",
"encryptionSecret": "secret",
"UUID": "test-uuid",
"description": "Task with empty dependencies",
"project": "TestProject",
"priority": "M",
"depends": []string{},
"tags": []string{"test"},
}

body, _ := json.Marshal(requestBody)
req, err := http.NewRequest("POST", "/add-task", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")

rr := httptest.NewRecorder()
AddTaskHandler(rr, req)

assert.Equal(t, http.StatusAccepted, rr.Code)
}

func Test_EditTaskHandler_WithDependencies(t *testing.T) {
GlobalJobQueue = NewJobQueue()

requestBody := map[string]interface{}{
"email": "test@example.com",
"encryptionSecret": "secret",
"UUID": "test-uuid",
"taskID": "1",
"description": "Edited task with dependencies",
"project": "EditedProject",
"depends": []string{"task-uuid-3"},
"tags": []string{"edited", "dependent"},
}

body, _ := json.Marshal(requestBody)
req, err := http.NewRequest("POST", "/edit-task", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")

rr := httptest.NewRecorder()
EditTaskHandler(rr, req)

assert.Equal(t, http.StatusAccepted, rr.Code)
}
7 changes: 7 additions & 0 deletions backend/controllers/edit_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"ccsync_backend/models"
"ccsync_backend/utils"
"ccsync_backend/utils/tw"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -60,6 +61,12 @@ func EditTaskHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Validate dependencies
if err := utils.ValidateDependencies(depends, uuid); err != nil {
http.Error(w, fmt.Sprintf("Invalid dependencies: %v", err), http.StatusBadRequest)
return
}

logStore := models.GetLogStore()
job := Job{
Name: "Edit Task",
Expand Down
10 changes: 9 additions & 1 deletion backend/controllers/modify_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"ccsync_backend/models"
"ccsync_backend/utils"
"ccsync_backend/utils/tw"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -48,6 +49,7 @@ func ModifyTaskHandler(w http.ResponseWriter, r *http.Request) {
status := requestBody.Status
due := requestBody.Due
tags := requestBody.Tags
depends := requestBody.Depends

if description == "" {
http.Error(w, "Description is required, and cannot be empty!", http.StatusBadRequest)
Expand All @@ -58,6 +60,12 @@ func ModifyTaskHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Validate dependencies
if err := utils.ValidateDependencies(depends, uuid); err != nil {
http.Error(w, fmt.Sprintf("Invalid dependencies: %v", err), http.StatusBadRequest)
return
}

// if err := tw.ModifyTaskInTaskwarrior(uuid, description, project, priority, status, due, email, encryptionSecret, taskID); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
Expand All @@ -68,7 +76,7 @@ func ModifyTaskHandler(w http.ResponseWriter, r *http.Request) {
Name: "Modify Task",
Execute: func() error {
logStore.AddLog("INFO", fmt.Sprintf("Modifying task ID: %s", taskID), uuid, "Modify Task")
err := tw.ModifyTaskInTaskwarrior(uuid, description, project, priority, status, due, email, encryptionSecret, taskID, tags)
err := tw.ModifyTaskInTaskwarrior(uuid, description, project, priority, status, due, email, encryptionSecret, taskID, tags, depends)
if err != nil {
logStore.AddLog("ERROR", fmt.Sprintf("Failed to modify task ID %s: %v", taskID, err), uuid, "Modify Task")
return err
Expand Down
2 changes: 2 additions & 0 deletions backend/models/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AddTaskRequestBody struct {
Recur string `json:"recur"`
Tags []string `json:"tags"`
Annotations []Annotation `json:"annotations"`
Depends []string `json:"depends"`
}
type ModifyTaskRequestBody struct {
Email string `json:"email"`
Expand All @@ -28,6 +29,7 @@ type ModifyTaskRequestBody struct {
Status string `json:"status"`
Due string `json:"due"`
Tags []string `json:"tags"`
Depends []string `json:"depends"`
}
type EditTaskRequestBody struct {
Email string `json:"email"`
Expand Down
7 changes: 6 additions & 1 deletion backend/utils/tw/add_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// add task to the user's tw client
func AddTaskToTaskwarrior(email, encryptionSecret, uuid, description, project, priority, dueDate, start, entryDate string, waitDate string, end, recur string, tags []string, annotations []models.Annotation) error {
func AddTaskToTaskwarrior(email, encryptionSecret, uuid, description, project, priority, dueDate, start, entryDate string, waitDate string, end, recur string, tags []string, annotations []models.Annotation, depends []string) error {
if err := utils.ExecCommand("rm", "-rf", "/root/.task"); err != nil {
return fmt.Errorf("error deleting Taskwarrior data: %v", err)
}
Expand Down Expand Up @@ -43,6 +43,11 @@ func AddTaskToTaskwarrior(email, encryptionSecret, uuid, description, project, p
if start != "" {
cmdArgs = append(cmdArgs, "start:"+start)
}
// Add dependencies to the task
if len(depends) > 0 {
dependsStr := strings.Join(depends, ",")
cmdArgs = append(cmdArgs, "depends:"+dependsStr)
}
if entryDate != "" {
cmdArgs = append(cmdArgs, "entry:"+entryDate)
}
Expand Down
10 changes: 4 additions & 6 deletions backend/utils/tw/edit_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ func EditTaskInTaskwarrior(uuid, description, email, encryptionSecret, taskID st
}
}

// Handle depends
if len(depends) > 0 {
dependsStr := strings.Join(depends, ",")
if err := utils.ExecCommand("task", taskID, "modify", "depends:"+dependsStr); err != nil {
return fmt.Errorf("failed to set depends %s: %v", dependsStr, err)
}
// Handle depends - always set to ensure clearing works
dependsStr := strings.Join(depends, ",")
if err := utils.ExecCommand("task", taskID, "modify", "depends:"+dependsStr); err != nil {
return fmt.Errorf("failed to set depends %s: %v", dependsStr, err)
}

// Handle due date
Expand Down
8 changes: 7 additions & 1 deletion backend/utils/tw/modify_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func ModifyTaskInTaskwarrior(uuid, description, project, priority, status, due, email, encryptionSecret, taskID string, tags []string) error {
func ModifyTaskInTaskwarrior(uuid, description, project, priority, status, due, email, encryptionSecret, taskID string, tags []string, depends []string) error {
if err := utils.ExecCommand("rm", "-rf", "/root/.task"); err != nil {
fmt.Println("1")
return fmt.Errorf("error deleting Taskwarrior data: %v", err)
Expand Down Expand Up @@ -55,6 +55,12 @@ func ModifyTaskInTaskwarrior(uuid, description, project, priority, status, due,
return fmt.Errorf("failed to edit task due: %v", err)
}

// Handle dependencies - always set to ensure clearing works
dependsStr := strings.Join(depends, ",")
if err := utils.ExecCommand("task", taskID, "modify", "depends:"+dependsStr); err != nil {
return fmt.Errorf("failed to set dependencies %s: %v", dependsStr, err)
}

// escapedStatus := fmt.Sprintf(`status:%s`, strings.ReplaceAll(status, `"`, `\"`))
if status == "completed" {
utils.ExecCommand("task", taskID, "done", "rc.confirmation=off")
Expand Down
6 changes: 3 additions & 3 deletions backend/utils/tw/taskwarrior_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestExportTasks(t *testing.T) {
}

func TestAddTaskToTaskwarrior(t *testing.T) {
err := AddTaskToTaskwarrior("email", "encryption_secret", "clientId", "description", "", "H", "2025-03-03", "2025-03-01", "2025-03-01", "2025-03-01", "2025-03-03", "daily", nil, []models.Annotation{{Description: "note"}})
err := AddTaskToTaskwarrior("email", "encryption_secret", "clientId", "description", "", "H", "2025-03-03", "2025-03-01", "2025-03-01", "2025-03-01", "2025-03-03", "daily", nil, []models.Annotation{{Description: "note"}}, []string{})
if err != nil {
t.Errorf("AddTaskToTaskwarrior failed: %v", err)
} else {
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestCompleteTaskInTaskwarrior(t *testing.T) {
}

func TestAddTaskWithTags(t *testing.T) {
err := AddTaskToTaskwarrior("email", "encryption_secret", "clientId", "description", "", "H", "2025-03-03", "2025-03-01", "2025-03-01", "2025-03-01", "2025-03-03", "daily", []string{"work", "important"}, []models.Annotation{{Description: "note"}})
err := AddTaskToTaskwarrior("email", "encryption_secret", "clientId", "description", "", "H", "2025-03-03", "2025-03-01", "2025-03-01", "2025-03-01", "2025-03-03", "daily", []string{"work", "important"}, []models.Annotation{{Description: "note"}}, []string{})
if err != nil {
t.Errorf("AddTaskToTaskwarrior with tags failed: %v", err)
} else {
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestEditTaskWithMixedTagOperations(t *testing.T) {
}

func TestModifyTaskWithTags(t *testing.T) {
err := ModifyTaskInTaskwarrior("uuid", "description", "project", "H", "pending", "2025-03-03", "email", "encryptionSecret", "taskuuid", []string{"+urgent", "-work", "normal"})
err := ModifyTaskInTaskwarrior("uuid", "description", "project", "H", "pending", "2025-03-03", "email", "encryptionSecret", "taskuuid", []string{"+urgent", "-work", "normal"}, []string{})
if err != nil {
t.Errorf("ModifyTaskInTaskwarrior with tags failed: %v", err)
} else {
Expand Down
14 changes: 14 additions & 0 deletions backend/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ func Test_ExecCommandForOutputInDir(t *testing.T) {
t.Errorf("Expected output but got empty result")
}
}

func Test_ValidateDependencies_ValidDependencies(t *testing.T) {
depends := []string{"task-uuid-1", "task-uuid-2"}
currentTaskUUID := "current-task-uuid"
err := ValidateDependencies(depends, currentTaskUUID)
assert.NoError(t, err)
}

func Test_ValidateDependencies_EmptyList(t *testing.T) {
depends := []string{}
currentTaskUUID := "current-task-uuid"
err := ValidateDependencies(depends, currentTaskUUID)
assert.NoError(t, err)
}
21 changes: 21 additions & 0 deletions backend/utils/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import (
"fmt"
)

// ValidateDependencies validates dependencies
func ValidateDependencies(depends []string, currentTaskUUID string) error {
if len(depends) == 0 {
return nil
}

// check for self-dependency
for _, dep := range depends {
if dep == currentTaskUUID {
return fmt.Errorf("task cannot depend on itself: %s", dep)
}
}

return nil
}
Loading
Loading