From 769b4f8d667d20443d7385be7648f1c405dd0bb0 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 1 Sep 2023 16:12:20 +0200 Subject: [PATCH] fix(task): remove task relation in the other direction as well Resolves https://community.vikunja.io/t/removing-parent-relationship-doesnt-remove-the-subtask-relationship/1492/3 --- pkg/models/task_relation.go | 66 +++++++++++++++++--------------- pkg/models/task_relation_test.go | 5 +++ 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/pkg/models/task_relation.go b/pkg/models/task_relation.go index f67f3b8d699..55b12fcf74a 100644 --- a/pkg/models/task_relation.go +++ b/pkg/models/task_relation.go @@ -108,6 +108,36 @@ func (*TaskRelation) TableName() string { // This avoids the need for an extra type TaskWithRelation (or similar). type RelatedTaskMap map[RelationKind][]*Task +func getInverseRelation(kind RelationKind) RelationKind { + switch kind { + case RelationKindSubtask: + return RelationKindParenttask + case RelationKindParenttask: + return RelationKindSubtask + case RelationKindRelated: + return RelationKindRelated + case RelationKindDuplicateOf: + return RelationKindDuplicates + case RelationKindDuplicates: + return RelationKindDuplicateOf + case RelationKindBlocking: + return RelationKindBlocked + case RelationKindBlocked: + return RelationKindBlocking + case RelationKindPreceeds: + return RelationKindFollows + case RelationKindFollows: + return RelationKindPreceeds + case RelationKindCopiedFrom: + return RelationKindCopiedTo + case RelationKindCopiedTo: + return RelationKindCopiedFrom + case RelationKindUnknown: + // Nothing to do + } + return RelationKindUnknown +} + // Create creates a new task relation // @Summary Create a new relation between two tasks // @Description Creates a new relation between two tasks. The user needs to have update rights on the base task and at least read rights on the other task. Both tasks do not need to be on the same project. Take a look at the docs for available task relation kinds. @@ -155,36 +185,10 @@ func (rel *TaskRelation) Create(s *xorm.Session, a web.Auth) error { // Build up the other relation (see the comment above for explanation) otherRelation := &TaskRelation{ - TaskID: rel.OtherTaskID, - OtherTaskID: rel.TaskID, - CreatedByID: rel.CreatedByID, - } - - switch rel.RelationKind { - case RelationKindSubtask: - otherRelation.RelationKind = RelationKindParenttask - case RelationKindParenttask: - otherRelation.RelationKind = RelationKindSubtask - case RelationKindRelated: - otherRelation.RelationKind = RelationKindRelated - case RelationKindDuplicateOf: - otherRelation.RelationKind = RelationKindDuplicates - case RelationKindDuplicates: - otherRelation.RelationKind = RelationKindDuplicateOf - case RelationKindBlocking: - otherRelation.RelationKind = RelationKindBlocked - case RelationKindBlocked: - otherRelation.RelationKind = RelationKindBlocking - case RelationKindPreceeds: - otherRelation.RelationKind = RelationKindFollows - case RelationKindFollows: - otherRelation.RelationKind = RelationKindPreceeds - case RelationKindCopiedFrom: - otherRelation.RelationKind = RelationKindCopiedTo - case RelationKindCopiedTo: - otherRelation.RelationKind = RelationKindCopiedFrom - case RelationKindUnknown: - // Nothing to do + TaskID: rel.OtherTaskID, + OtherTaskID: rel.TaskID, + CreatedByID: rel.CreatedByID, + RelationKind: getInverseRelation(rel.RelationKind), } // Finally insert everything @@ -230,7 +234,7 @@ func (rel *TaskRelation) Delete(s *xorm.Session, a web.Auth) error { builder.And( builder.Eq{"task_id": rel.OtherTaskID}, builder.Eq{"other_task_id": rel.TaskID}, - builder.Eq{"relation_kind": rel.RelationKind}, + builder.Eq{"relation_kind": getInverseRelation(rel.RelationKind)}, ), ) diff --git a/pkg/models/task_relation_test.go b/pkg/models/task_relation_test.go index 3fd47b79ab4..da79ae368e4 100644 --- a/pkg/models/task_relation_test.go +++ b/pkg/models/task_relation_test.go @@ -118,6 +118,11 @@ func TestTaskRelation_Delete(t *testing.T) { "other_task_id": 29, "relation_kind": RelationKindSubtask, }) + db.AssertMissing(t, "task_relations", map[string]interface{}{ + "task_id": 29, + "other_task_id": 1, + "relation_kind": RelationKindParenttask, + }) }) t.Run("Not existing", func(t *testing.T) { db.LoadAndAssertFixtures(t)