{-# LANGUAGE DataKinds #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-} module Server.Frontend.Tickets ( getTicketsR , postGtfsTicketImportR , getTicketViewR , getTicketMapViewR , getDelAnnounceR , postAnnounceR , getTokenBlock ) where import Server.Frontend.Routes import Config (ServerConfig (..), UffdConfig (..)) import Control.Monad (forM, forM_, join) import Control.Monad.Extra (maybeM) import Control.Monad.IO.Class (MonadIO (liftIO)) import Data.Coerce (coerce) import Data.Function (on, (&)) import Data.Functor ((<&>)) import Data.List (lookup, nubBy) import Data.List.NonEmpty (nonEmpty) import Data.Map (Map) import qualified Data.Map as M import Data.Maybe (catMaybes, fromJust, isJust) import Data.Text (Text) import qualified Data.Text as T import Data.Time (UTCTime (..), addDays, getCurrentTime, utctDay) import Data.Time.Calendar (Day) import Data.Time.Format.ISO8601 (iso8601Show) import Data.UUID (UUID) import qualified Data.UUID as UUID import qualified Data.Vector as V import Extrapolation (Extrapolator (..), LinearExtrapolator (..)) import Fmt ((+|), (|+)) import GHC.Float (int2Double) import qualified GTFS import Numeric (showFFloat) import Persist import Server.Frontend.SpaceTime (mkSpaceTimeDiagram, mkSpaceTimeDiagramHandler) import Server.Frontend.Ticker (tickerWidget) import Server.Util (Service, secondsNow) import Text.Read (readMaybe) import Yesod import Yesod.Auth import Yesod.Auth.Uffd (UffdUser (..), uffdClient) getTicketsR :: Handler Html getTicketsR = do req <- getRequest let maybeDay = lookup "day" (reqGetParams req) >>= (readMaybe . T.unpack) mdisplayname <- maybeAuthId <&> fmap uffdDisplayName (day, isToday) <- liftIO $ getCurrentTime <&> utctDay <&> \today -> case maybeDay of Just day -> (day, day == today) Nothing -> (today, True) maybeSpaceTime <- mkSpaceTimeDiagramHandler 1 day [ TicketDay ==. day ] let prevday = (T.pack . iso8601Show . addDays (-1)) day let nextday = (T.pack . iso8601Show . addDays 1) day gtfs <- getYesod <&> getGtfs -- TODO: tickets should have all trip information saved tickets <- runDB $ selectList [ TicketDay ==. day ] [ Asc TicketTripName ] >>= mapM (\ticket -> do stops <- selectList [ StopTicket ==. entityKey ticket ] [] startStation <- getJust (stopStation $ entityVal $ head stops) pure (ticket, startStation, fmap entityVal stops)) let trips = GTFS.tripsOnDay gtfs day tickerAnnounceWidget <- tickerWidget (widget, enctype) <- generateFormPost (tripImportForm (fmap (,day) (M.elems trips))) defaultLayout $ do [whamlet|
_{MsgLoggedInAs name} - _{MsgLogout}