28 lines
1.4 KiB
Haskell
28 lines
1.4 KiB
Haskell
module Handler.Utils.ExamOffice.Exam
|
|
( resultIsSynced
|
|
) where
|
|
|
|
import Import.NoFoundation
|
|
|
|
import qualified Database.Esqueleto as E
|
|
|
|
resultIsSynced :: E.SqlExpr (E.Value UserId) -- ^ office
|
|
-> E.SqlExpr (Entity ExamResult)
|
|
-> E.SqlExpr (E.Value Bool)
|
|
resultIsSynced authId examResult = (hasSchool E.&&. allSchools) E.||. anySync
|
|
where
|
|
anySync = E.exists . E.from $ \synced ->
|
|
E.where_ $ synced E.^. ExamOfficeResultSyncedResult E.==. examResult E.^. ExamResultId
|
|
E.&&. synced E.^. ExamOfficeResultSyncedTime E.>=. examResult E.^. ExamResultLastChanged
|
|
|
|
hasSchool = E.exists . E.from $ \userFunction ->
|
|
E.where_ $ userFunction E.^. UserFunctionUser E.==. authId
|
|
E.&&. userFunction E.^. UserFunctionFunction E.==. E.val SchoolExamOffice
|
|
allSchools = E.not_ . E.exists . E.from $ \userFunction -> do
|
|
E.where_ $ userFunction E.^. UserFunctionUser E.==. authId
|
|
E.&&. userFunction E.^. UserFunctionFunction E.==. E.val SchoolExamOffice
|
|
E.where_ . E.not_ . E.exists . E.from $ \synced ->
|
|
E.where_ $ synced E.^. ExamOfficeResultSyncedSchool E.==. E.just (userFunction E.^. UserFunctionSchool)
|
|
E.&&. synced E.^. ExamOfficeResultSyncedResult E.==. examResult E.^. ExamResultId
|
|
E.&&. synced E.^. ExamOfficeResultSyncedTime E.>=. examResult E.^. ExamResultLastChanged
|