Skip to content

Commit

Permalink
Fix setting the closed_utc value when editing a task
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed May 13, 2024
1 parent e03c978 commit 369098b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tasklite-core/source/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import Text.Parsec.Rfc2822 (GenericMessage (..), message)
import Text.Parsec.Rfc2822 qualified as Email
import Text.ParserCombinators.Parsec as Parsec (parse)
import Text.PortableLines.ByteString.Lazy (lines8)
import Time.System (timeCurrent)
import Time.System (dateCurrent, timeCurrent)
import Utils (
IdText,
emptyUlid,
Expand Down Expand Up @@ -766,6 +766,8 @@ editTaskByTask preEdit conn taskToEdit = do
if hasMetadata == Just True
then importTaskRecord.task.metadata
else Nothing
, -- Set to previous value to force SQL trigger to update it
Task.modified_utc = taskToEdit.modified_utc
}
notesCorrectUtc =
importTaskRecord.notes
Expand All @@ -780,6 +782,14 @@ editTaskByTask preEdit conn taskToEdit = do
)

updateTask conn taskFixed

-- TODO: Remove after it was added to `createSetClosedUtcTrigger`
-- Update again with the same `state` field to avoid firing
-- SQL trigger which would overwrite the `closed_utc` field.
P.when (isJust taskFixed.closed_utc) $ do
now_ <- dateCurrent
updateTask conn taskFixed{Task.modified_utc = show @DateTime now_}

tagWarnings <- insertTags conn Nothing taskFixed importTaskRecord.tags
insertNotes conn Nothing taskFixed notesCorrectUtc
pure $
Expand Down
37 changes: 37 additions & 0 deletions tasklite-core/test/LibSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Test.Hspec (
)

import Config (defaultConfig)
import Control.Arrow ((>>>))
import FullTask (FullTask, emptyFullTask)
import FullTask qualified
import ImportExport (PreEdit (ApplyPreEdit), editTaskByTask)
Expand Down Expand Up @@ -67,6 +68,7 @@ import Lib (
updateTask,
)
import Note (Note)
import System.Hourglass (dateCurrent)
import Task (
Task (
body,
Expand Down Expand Up @@ -110,6 +112,7 @@ task1 =
emptyTask
{ Task.ulid = "01hs68z7mdg4ktpxbv0yfafznq"
, Task.body = "New task 1"
, Task.modified_utc = "2024-03-17 13:17:44.461"
}


Expand Down Expand Up @@ -457,6 +460,40 @@ spec = do
let errMsg = "Tag \"" <> T.unpack existTag <> "\" is already assigned"
show cliOutput `shouldContain` errMsg

it "lets you edit a task and specify closed_utc" $ do
withMemoryDb defaultConfig $ \memConn -> do
insertRecord "tasks" memConn task1

let
replaceBs needle replacement haystack = do
haystack
& P.decodeUtf8
& T.replace (P.decodeUtf8 needle) (P.decodeUtf8 replacement)
& P.encodeUtf8

cliOutput <-
editTaskByTask
( ApplyPreEdit $
replaceBs
"state: null"
"state: Done"
>>> replaceBs
"closed_utc: null"
"closed_utc: 2024-05-08 10:04"
)
memConn
task1
show cliOutput `shouldContain` "Edited task"
tasks :: [Task] <- query_ memConn "SELECT * FROM tasks"
case tasks of
[task] -> do
task.closed_utc `shouldBe` Just "2024-05-08 10:04:00"
task.state `shouldBe` Just Done
now_ <- dateCurrent
let today = now_ & show & T.take 10
task.modified_utc `shouldSatisfy` (today `T.isPrefixOf`)
_ -> P.die "More than one task found"

it "lets you add notes while editing a task" $ do
withMemoryDb defaultConfig $ \memConn -> do
insertRecord "tasks" memConn task1
Expand Down

0 comments on commit 369098b

Please sign in to comment.