From 1555e9071f1c040cd3e902093f3994abaaec2f24 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Mon, 29 May 2023 16:46:20 -0400 Subject: [PATCH] FIX actually throw error when folding periods/days --- lib/Internal/Budget.hs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/Internal/Budget.hs b/lib/Internal/Budget.hs index 088d533..d343d6a 100644 --- a/lib/Internal/Budget.hs +++ b/lib/Internal/Budget.hs @@ -1,6 +1,7 @@ module Internal.Budget (insertBudget) where import Control.Monad.Except +import Data.Foldable import Database.Persist.Monad import Internal.Database.Ops import Internal.Types.Main @@ -235,19 +236,23 @@ workingDays wds start end = fromIntegral $ daysFull + daysTail wds' = L.sort $ (\x -> diff (fromWeekday x) startDay) <$> L.nub wds diff a b = fromIntegral $ mod (fromEnum a - fromEnum b) 7 +-- ASSUME days is a sorted list foldDays :: MonadInsertError m => (Day -> Day -> m a) -> Day -> [Day] -> m [a] -foldDays f start days - -- TODO throw real error here - | any (start >) days = throwError undefined - | otherwise = - combineErrors $ - snd $ - L.mapAccumL (\prevDay day -> (day, f prevDay day)) start days +foldDays f start days = case NE.nonEmpty days of + Nothing -> return [] + Just ds + | any (start >) ds -> + throwError $ + InsertException [PeriodError start $ minimum ds] + | otherwise -> + combineErrors $ + snd $ + L.mapAccumL (\prevDay day -> (day, f prevDay day)) start days checkAcntType :: (MonadInsertError m, MonadFinance m)