module ProjectM36.Notifications where
import ProjectM36.Base
import ProjectM36.RelationalExpression
import Control.Monad.Trans.Reader
import qualified Data.Map as M
import Data.Either (isRight)
notificationChanges :: Notifications -> DatabaseContext -> DatabaseContext -> Notifications
notificationChanges nots context1 context2 = M.filter notificationFilter nots
where
notificationFilter (Notification chExpr _ _) = let oldChangeEval = evalChangeExpr chExpr (RelationalExprStateElems context1)
newChangeEval = evalChangeExpr chExpr (RelationalExprStateElems context2) in
oldChangeEval /= newChangeEval && isRight oldChangeEval
evalChangeExpr chExpr = runReader (evalRelationalExpr chExpr)