From 0c2f923ce75ffc8bc7bbe9b7e03bbc0793315573 Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 14 Mar 2024 22:23:06 +0000 Subject: [PATCH] Deduplicate tags before inserting them --- tasklite-core/source/Lib.hs | 4 +++- tasklite-core/test/Spec.hs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tasklite-core/source/Lib.hs b/tasklite-core/source/Lib.hs index 2909adc..15e9dce 100644 --- a/tasklite-core/source/Lib.hs +++ b/tasklite-core/source/Lib.hs @@ -93,6 +93,7 @@ import Protolude qualified as P import Control.Arrow ((>>>)) import Data.Aeson as Aeson (KeyValue ((.=)), encode, object) import Data.Coerce (coerce) +import Data.List (nub) import Data.Generics (Data, constrFields, toConstr) import Data.Hourglass ( DateTime (dtTime), @@ -369,7 +370,8 @@ updateTask connection task = do insertTags :: Connection -> Maybe DateTime -> Task -> [Text] -> IO () insertTags connection mbCreatedUtc task tags = do - taskToTags <- forM tags $ \tag -> do + let uniqueTags = nub tags + taskToTags <- forM uniqueTags $ \tag -> do tagUlid <- getULID pure $ TaskToTag diff --git a/tasklite-core/test/Spec.hs b/tasklite-core/test/Spec.hs index db251ff..5263198 100644 --- a/tasklite-core/test/Spec.hs +++ b/tasklite-core/test/Spec.hs @@ -309,6 +309,27 @@ testSuite conf now = do } _ -> P.die "More than one task found" + it "deduplicates tags when adding a task" $ do + withMemoryDb conf $ \memConn -> do + _ <- addTask conf memConn ["Buy milk +drink +drink"] + (tasks :: [FullTask]) <- query_ memConn "SELECT * FROM tasks_view" + case tasks of + [updatedTask] -> do + updatedTask `shouldSatisfy` (\task -> task.ulid /= "") + updatedTask `shouldSatisfy` (\task -> task.modified_utc /= "") + updatedTask `shouldSatisfy` (\task -> task.user /= "") + updatedTask + { FullTask.ulid = "" + , FullTask.modified_utc = "" + , FullTask.user = "" + } + `shouldBe` emptyFullTask + { FullTask.body = "Buy milk" + , FullTask.priority = Just 2.0 + , FullTask.tags = Just ["drink"] + } + _ -> P.die "More than one task found" + it "logs a task" $ do withMemoryDb conf $ \memConn -> do result <- logTask conf memConn ["Just a test"]