From ee92429c30bccc4195d10addd4300ace4de99326 Mon Sep 17 00:00:00 2001 From: Steffen Jost Date: Thu, 22 Aug 2019 07:42:34 +0200 Subject: [PATCH] refactor(exam csv import): remove unnecessary esqueleto exists --- src/Handler/Exam/Users.hs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Handler/Exam/Users.hs b/src/Handler/Exam/Users.hs index 8347e26ef..7a5107c2b 100644 --- a/src/Handler/Exam/Users.hs +++ b/src/Handler/Exam/Users.hs @@ -642,13 +642,15 @@ postEUsersR tid ssh csh examn = do , (studyFeatures E.^. StudyFeaturesSemester E.==.) . E.val <$> csvEUserSemester ] E.where_ $ studyFeatures E.^. StudyFeaturesUser E.==. E.val uid - let isCourseParticipantFeature = E.exists $ E.from $ \courseParticipant -> do - E.where_ $ courseParticipant E.^. CourseParticipantCourse E.==. E.val examCourse - E.where_ $ courseParticipant E.^. CourseParticipantUser E.==. E.val uid - E.where_ $ courseParticipant E.^. CourseParticipantField E.==. E.just (studyFeatures E.^. StudyFeaturesId) - E.where_ $ (studyFeatures E.^. StudyFeaturesValid E.==. E.val True) - E.||. isCourseParticipantFeature -- either active studyFeature or the one previously associated with the course - E.orderBy [E.desc isCourseParticipantFeature, E.asc (E.orderByOrd $ studyFeatures E.^. StudyFeaturesType)] + let isActive = studyFeatures E.^. StudyFeaturesValid E.==. E.val True + -- isActiveOrPrevious = maybe isActive (\Entity sfid _ -> isActive E.||. (studyFeatures E.^. StudyFeaturesId E.==. E.val sfid)) oldFeatures -- one line, but obfuscates the `or else` structure + -- isActiveOrPrevious = isActive E.||. $ maybe (E.val False) (\Entity sfid _ -> (studyFeatures E.^. StudyFeaturesId E.==. E.val sfid)) oldFeatures -- meh + isActiveOrPrevious = case oldFeatures of + Just (Entity _ CourseParticipant{courseParticipantField = Just sfid}) + -> isActive E.||. (E.val sfid E.==. studyFeatures E.^. StudyFeaturesId) + _ -> isActive + E.where_ $ isActiveOrPrevious -- either active studyFeature or the one previously associated with this course + E.orderBy [E.desc isActiveOrPrevious, E.asc (E.orderByOrd $ studyFeatures E.^. StudyFeaturesType)] E.limit 2 -- we just need to know whether there is a unique one, none, or more than one return $ studyFeatures E.^. StudyFeaturesId case studyFeatures of