Skip to content

Commit

Permalink
[#500] Trails: Add check for unfound parent entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-stacey committed Aug 19, 2019
1 parent 2898be9 commit 5a37a16
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions projects/trails/src/Mirza/Trails/Handlers/Trails.hs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ addEntryQuery entriesRaw = do
let parentSignatures = (trailEntryParentSignatures <$> uniqueNewEntries)
throwing_If _DuplicateParentsTSE ((nub <$> parentSignatures) /= parentSignatures)

-- This constraint could possibly be handled more efficently by letting the DB check the constraints directly and then
-- transform the error returned.
parentsExist <- validParents newEntries
throwing_If _ParentEntryNotFoundTSE $ (not parentsExist)

-- Add the entries to the DB.
let entries = trailEntryToEntriesT <$> uniqueNewEntries
let previous = concat $ trailEntryToParentsT <$> uniqueNewEntries
Expand All @@ -176,6 +181,22 @@ addEntryQuery entriesRaw = do
pure ()


validParents :: [TrailEntry] -> DB context err Bool
validParents entries = do
let searchParent sig = pg $ runSelectReturningOne $ select $ do
entry <- all_ (_entries trailsDB)
guard_ (entries_signature entry ==. val_ sig)
pure entry
parents <- traverse searchParent (unmatchedParents entries)
pure $ (all isJust parents)


unmatchedParents :: [TrailEntry] -> [SignaturePlaceholder]
unmatchedParents entries = catMaybes $ (\parent -> if elem parent signatures then Nothing else Just parent) <$> requiredParents where
signatures = trailEntrySignature <$> entries
requiredParents = concat $ trailEntryParentSignatures <$> entries


trailEntryToEntriesT :: TrailEntry -> EntriesT Identity
trailEntryToEntriesT trailEntry = EntriesT (trailEntrySignature trailEntry)
(toDbTimestamp $ getEntryTime $ trailEntryTimestamp trailEntry)
Expand Down

0 comments on commit 5a37a16

Please sign in to comment.