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
This commit is contained in:
kolaente 2023-09-01 16:12:20 +02:00
parent e518fb1191
commit 769b4f8d66
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 40 additions and 31 deletions

View File

@ -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)},
),
)

View File

@ -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)