11package apis
22
33import (
4+ "context"
5+ "errors"
46 "fmt"
57 "net/http"
68 "strconv"
@@ -16,6 +18,7 @@ import (
1618 auth "dkhalife.com/tasks/core/internal/utils/auth"
1719 jwt "github.com/appleboy/gin-jwt/v2"
1820 "github.com/gin-gonic/gin"
21+ "gorm.io/gorm"
1922)
2023
2124type LabelReq struct {
@@ -533,6 +536,65 @@ func (h *TasksAPIHandler) completeTask(c *gin.Context) {
533536 })
534537}
535538
539+ func (h * TasksAPIHandler ) uncompleteTask (c * gin.Context ) {
540+ currentIdentity := auth .CurrentIdentity (c )
541+ log := logging .FromContext (c )
542+
543+ rawID := c .Param ("id" )
544+ id , err := strconv .Atoi (rawID )
545+ if err != nil {
546+ c .JSON (http .StatusBadRequest , gin.H {
547+ "error" : "Invalid task ID" ,
548+ })
549+ return
550+ }
551+
552+ task , err := h .tRepo .GetTask (c , id )
553+ if err != nil {
554+ log .Errorf ("error getting task: %s" , err .Error ())
555+ c .JSON (http .StatusInternalServerError , gin.H {
556+ "error" : "Error getting task" ,
557+ })
558+ return
559+ }
560+
561+ if currentIdentity .UserID != task .CreatedBy {
562+ c .JSON (http .StatusForbidden , gin.H {
563+ "error" : "You are not allowed to update this task" ,
564+ })
565+ return
566+ }
567+
568+ if err := h .tRepo .UncompleteTask (c , id ); err != nil {
569+ if errors .Is (err , gorm .ErrRecordNotFound ) {
570+ c .JSON (http .StatusBadRequest , gin.H {"error" : "Task was not completed already" })
571+ return
572+ }
573+ log .Errorf ("error uncompleting task: %s" , err .Error ())
574+ c .JSON (http .StatusInternalServerError , gin.H {
575+ "error" : "Error uncompleting task" ,
576+ })
577+ return
578+ }
579+
580+ updatedTask , err := h .tRepo .GetTask (c , id )
581+ if err != nil {
582+ log .Errorf ("error getting updated task: %s" , err .Error ())
583+ c .JSON (http .StatusInternalServerError , gin.H {
584+ "error" : "Error getting updated task" ,
585+ })
586+ return
587+ }
588+
589+ go func (task * models.Task ) {
590+ h .nRepo .GenerateNotifications (context .Background (), task )
591+ }(updatedTask )
592+
593+ c .JSON (http .StatusOK , gin.H {
594+ "task" : updatedTask ,
595+ })
596+ }
597+
536598func (h * TasksAPIHandler ) GetTaskHistory (c * gin.Context ) {
537599 currentIdentity := auth .CurrentIdentity (c )
538600 log := logging .FromContext (c )
@@ -578,6 +640,7 @@ func TaskRoutes(router *gin.Engine, h *TasksAPIHandler, auth *jwt.GinJWTMiddlewa
578640 tasksRoutes .GET ("/:id" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskRead ), h .getTask )
579641 tasksRoutes .GET ("/:id/history" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskRead ), h .GetTaskHistory )
580642 tasksRoutes .POST ("/:id/do" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskWrite ), h .completeTask )
643+ tasksRoutes .POST ("/:id/undo" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskWrite ), h .uncompleteTask )
581644 tasksRoutes .POST ("/:id/skip" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskWrite ), h .skipTask )
582645 tasksRoutes .PUT ("/:id/dueDate" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskWrite ), h .updateDueDate )
583646 tasksRoutes .DELETE ("/:id" , authMW .ScopeMiddleware (models .ApiTokenScopeTaskWrite ), h .deleteTask )
0 commit comments