-
Notifications
You must be signed in to change notification settings - Fork 61
feat(tasks): implement bulk task actions (complete/delete) #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "ccsync_backend/models" | ||
| "ccsync_backend/utils/tw" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // BulkCompleteTaskHandler godoc | ||
| // @Summary Bulk complete tasks | ||
| // @Description Mark multiple tasks as completed in Taskwarrior | ||
| // @Tags Tasks | ||
| // @Accept json | ||
| // @Produce json | ||
| // @Param task body models.BulkCompleteTaskRequestBody true "Bulk task completion details" | ||
| // @Success 202 {string} string "Bulk task completion accepted for processing" | ||
| // @Failure 400 {string} string "Invalid request - missing or empty taskuuids" | ||
| // @Failure 405 {string} string "Method not allowed" | ||
| // @Router /complete-tasks [post] | ||
| func BulkCompleteTaskHandler(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| http.Error(w, fmt.Sprintf("error reading request body: %v", err), http.StatusBadRequest) | ||
| return | ||
| } | ||
| defer r.Body.Close() | ||
|
|
||
| var requestBody models.BulkCompleteTaskRequestBody | ||
|
|
||
| if err := json.Unmarshal(body, &requestBody); err != nil { | ||
| http.Error(w, fmt.Sprintf("error decoding request body: %v", err), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| email := requestBody.Email | ||
| encryptionSecret := requestBody.EncryptionSecret | ||
| uuid := requestBody.UUID | ||
| taskUUIDs := requestBody.TaskUUIDs | ||
|
|
||
| if len(taskUUIDs) == 0 { | ||
| http.Error(w, "taskuuids is required and cannot be empty", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| logStore := models.GetLogStore() | ||
|
|
||
| // Create a *single* job for all UUIDs | ||
| job := Job{ | ||
| Name: "Bulk Complete Tasks", | ||
| Execute: func() error { | ||
| logStore.AddLog("INFO", fmt.Sprintf("[Bulk Complete] Starting %d tasks", len(taskUUIDs)), uuid, "Bulk Complete Task") | ||
|
|
||
| failedTasks, err := tw.CompleteTasksInTaskwarrior(email, encryptionSecret, uuid, taskUUIDs) | ||
|
|
||
| for taskUUID, errMsg := range failedTasks { | ||
| logStore.AddLog("ERROR", fmt.Sprintf("[Bulk Complete] Failed: %s (%s)", taskUUID, errMsg), uuid, "Bulk Complete Task") | ||
| } | ||
|
|
||
| if err != nil { | ||
| logStore.AddLog("ERROR", fmt.Sprintf("[Bulk Complete] Sync error: %v", err), uuid, "Bulk Complete Task") | ||
| return err | ||
| } | ||
|
|
||
| successCount := len(taskUUIDs) - len(failedTasks) | ||
| logStore.AddLog("INFO", fmt.Sprintf("[Bulk Complete] Finished: %d succeeded, %d failed", successCount, len(failedTasks)), uuid, "Bulk Complete Task") | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| GlobalJobQueue.AddJob(job) | ||
| w.WriteHeader(http.StatusAccepted) | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use DeleteTasksInTaskwarrior signature that returns a failed tasks map. Logs individual failures and final success/failure counts to DevLogs for debugging visibility. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "ccsync_backend/models" | ||
| "ccsync_backend/utils/tw" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // BulkDeleteTaskHandler godoc | ||
| // @Summary Bulk delete tasks | ||
| // @Description Delete multiple tasks in Taskwarrior | ||
| // @Tags Tasks | ||
| // @Accept json | ||
| // @Produce json | ||
| // @Param task body models.BulkDeleteTaskRequestBody true "Bulk task deletion details" | ||
| // @Success 202 {string} string "Bulk task deletion accepted for processing" | ||
| // @Failure 400 {string} string "Invalid request - missing or empty taskuuids" | ||
| // @Failure 405 {string} string "Method not allowed" | ||
| // @Router /delete-tasks [post] | ||
| func BulkDeleteTaskHandler(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| http.Error(w, fmt.Sprintf("error reading request body: %v", err), http.StatusBadRequest) | ||
| return | ||
| } | ||
| defer r.Body.Close() | ||
|
|
||
| var requestBody models.BulkDeleteTaskRequestBody | ||
|
|
||
| if err := json.Unmarshal(body, &requestBody); err != nil { | ||
| http.Error(w, fmt.Sprintf("error decoding request body: %v", err), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| email := requestBody.Email | ||
| encryptionSecret := requestBody.EncryptionSecret | ||
| uuid := requestBody.UUID | ||
| taskUUIDs := requestBody.TaskUUIDs | ||
|
|
||
| if len(taskUUIDs) == 0 { | ||
| http.Error(w, "taskuuids is required and cannot be empty", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| logStore := models.GetLogStore() | ||
|
|
||
| job := Job{ | ||
| Name: "Bulk Delete Tasks", | ||
| Execute: func() error { | ||
| logStore.AddLog("INFO", fmt.Sprintf("[Bulk Delete] Starting %d tasks", len(taskUUIDs)), uuid, "Bulk Delete Task") | ||
|
|
||
| failedTasks, err := tw.DeleteTasksInTaskwarrior(email, encryptionSecret, uuid, taskUUIDs) | ||
|
|
||
| for taskUUID, errMsg := range failedTasks { | ||
| logStore.AddLog("ERROR", fmt.Sprintf("[Bulk Delete] Failed: %s (%s)", taskUUID, errMsg), uuid, "Bulk Delete Task") | ||
| } | ||
|
|
||
| if err != nil { | ||
| logStore.AddLog("ERROR", fmt.Sprintf("[Bulk Delete] Sync error: %v", err), uuid, "Bulk Delete Task") | ||
| return err | ||
| } | ||
|
|
||
| successCount := len(taskUUIDs) - len(failedTasks) | ||
| logStore.AddLog("INFO", fmt.Sprintf("[Bulk Delete] Finished: %d succeeded, %d failed", successCount, len(failedTasks)), uuid, "Bulk Delete Task") | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| GlobalJobQueue.AddJob(job) | ||
| w.WriteHeader(http.StatusAccepted) | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new routes added |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added bulk request body structs, it is using array for taskUUIDs to accept multiple tasks identifiers in one request. |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returns map[string]string (uuid → error) to collect individual task failures and optimized from N syncs to single sync cycle pattern (SetConfig -> InitialSync -> Loop -> FinalSync) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package tw | ||
|
|
||
| import ( | ||
| "ccsync_backend/utils" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func CompleteTasksInTaskwarrior(email, encryptionSecret, uuid string, taskUUIDs []string) (map[string]string, error) { | ||
| failedTasks := make(map[string]string) | ||
|
|
||
| if err := utils.ExecCommand("rm", "-rf", "/root/.task"); err != nil { | ||
| return nil, fmt.Errorf("error deleting Taskwarrior data: %v", err) | ||
| } | ||
|
|
||
| tempDir, err := os.MkdirTemp("", "taskwarrior-"+email) | ||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create temporary directory: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| origin := os.Getenv("CONTAINER_ORIGIN") | ||
| if err := SetTaskwarriorConfig(tempDir, encryptionSecret, origin, uuid); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err := SyncTaskwarrior(tempDir); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, taskuuid := range taskUUIDs { | ||
| if err := utils.ExecCommandInDir(tempDir, "task", taskuuid, "done", "rc.confirmation=off"); err != nil { | ||
| failedTasks[taskuuid] = err.Error() | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Sync Taskwarrior again | ||
| if err := SyncTaskwarrior(tempDir); err != nil { | ||
| return failedTasks, err | ||
| } | ||
|
|
||
| return failedTasks, nil | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returns map[string]string (uuid → error) to collect individual task failures and optimized from N syncs to single sync cycle pattern (SetConfig -> InitialSync -> Loop -> FinalSync) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package tw | ||
|
|
||
| import ( | ||
| "ccsync_backend/utils" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func DeleteTasksInTaskwarrior(email, encryptionSecret, uuid string, taskUUIDs []string) (map[string]string, error) { | ||
| failedTasks := make(map[string]string) | ||
|
|
||
| if err := utils.ExecCommand("rm", "-rf", "/root/.task"); err != nil { | ||
| return nil, fmt.Errorf("error deleting Taskwarrior data: %v", err) | ||
| } | ||
|
|
||
| tempDir, err := os.MkdirTemp("", "taskwarrior-"+email) | ||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create temporary directory: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| origin := os.Getenv("CONTAINER_ORIGIN") | ||
| if err := SetTaskwarriorConfig(tempDir, encryptionSecret, origin, uuid); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err := SyncTaskwarrior(tempDir); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, taskuuid := range taskUUIDs { | ||
| if err := utils.ExecCommandInDir(tempDir, "task", taskuuid, "delete", "rc.confirmation=off"); err != nil { | ||
| failedTasks[taskuuid] = err.Error() | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Sync Taskwarrior again | ||
| if err := SyncTaskwarrior(tempDir); err != nil { | ||
| return failedTasks, err | ||
| } | ||
|
|
||
| return failedTasks, nil | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added checkbox column to task rows and also deleted tasks have disabled checkboxes since they can't be bulk actioned |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use CompleteTasksInTaskwarrior signature that returns a failed tasks map. logs individual failures and final success/failure counts to DevLogs for debugging visibility.