From c1104449dd45a3f5b5ff4befa4ee75f488bf791f Mon Sep 17 00:00:00 2001 From: Icereed Date: Mon, 13 Jan 2025 14:09:03 +0100 Subject: [PATCH] feat: enhance tag management by preserving previous tags and removing duplicates (#119) Closes #90 --- paperless.go | 6 ++++++ paperless_test.go | 16 +++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/paperless.go b/paperless.go index 063097b..8c33f21 100644 --- a/paperless.go +++ b/paperless.go @@ -283,6 +283,12 @@ func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []Docum originalFields["tags"] = originalTags // remove autoTag to prevent infinite loop - this is required in case of undo tags = removeTagFromList(tags, autoTag) + + // keep previous tags + tags = append(tags, originalTags...) + // remove duplicates + slices.Sort(tags) + tags = slices.Compact(tags) } updatedTagsJSON, err := json.Marshal(tags) diff --git a/paperless_test.go b/paperless_test.go index 85e056a..9693b24 100644 --- a/paperless_test.go +++ b/paperless_test.go @@ -300,18 +300,24 @@ func TestUpdateDocuments(t *testing.T) { OriginalDocument: Document{ ID: 1, Title: "Old Title", - Tags: []string{"tag1"}, + Tags: []string{"tag1", "tag3", "manual", "removeMe"}, }, SuggestedTitle: "New Title", - SuggestedTags: []string{"tag2"}, + SuggestedTags: []string{"tag2", "tag3"}, + RemoveTags: []string{"removeMe"}, }, } + idTag1 := 1 + idTag2 := 2 + idTag3 := 4 // Mock data for tags tagsResponse := map[string]interface{}{ "results": []map[string]interface{}{ - {"id": 1, "name": "tag1"}, - {"id": 2, "name": "tag2"}, + {"id": idTag1, "name": "tag1"}, + {"id": idTag2, "name": "tag2"}, {"id": 3, "name": "manual"}, + {"id": idTag3, "name": "tag3"}, + {"id": 5, "name": "removeMe"}, }, "next": nil, } @@ -342,7 +348,7 @@ func TestUpdateDocuments(t *testing.T) { // Expected updated fields expectedFields := map[string]interface{}{ "title": "New Title", - "tags": []interface{}{float64(2)}, // tag2 ID + "tags": []interface{}{float64(idTag1), float64(idTag2), float64(idTag3)}, // keep also previous tags } assert.Equal(t, expectedFields, updatedFields)