2019-08-14 16:04:16 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Control.User where
|
|
|
|
|
|
|
|
import Servant
|
|
|
|
|
|
|
|
import Control.Monad (void)
|
|
|
|
|
|
|
|
import Control.Monad.Reader (ask)
|
|
|
|
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
|
|
|
|
import Data.Time (getCurrentTime, utctDay)
|
|
|
|
|
|
|
|
import Data.ByteString.Random (random)
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import Model
|
|
|
|
|
|
|
|
userNew :: UserSubmit -> MateHandler Int
|
|
|
|
userNew us = do
|
|
|
|
now <- liftIO $ getCurrentTime
|
|
|
|
randSalt <- liftIO $ random 8
|
|
|
|
conn <- rsConnection <$> ask
|
|
|
|
insertUser us (utctDay now) randSalt conn
|
|
|
|
|
|
|
|
userGet :: Maybe Int -> Int -> MateHandler UserDetails
|
|
|
|
userGet Nothing _ =
|
|
|
|
throwError $ err403
|
2019-09-06 19:44:31 +00:00
|
|
|
{ errBody = "No Authentication present."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|
2019-09-07 00:48:16 +00:00
|
|
|
userGet (Just aid) uid =
|
|
|
|
if aid == uid
|
2019-08-14 16:04:16 +00:00
|
|
|
then do
|
|
|
|
conn <- rsConnection <$> ask
|
2019-09-07 00:48:16 +00:00
|
|
|
userDetailsSelect uid conn
|
2019-08-14 16:04:16 +00:00
|
|
|
else
|
|
|
|
throwError $ err403
|
2019-09-06 19:44:31 +00:00
|
|
|
{ errBody = "Wrong Authentication present."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userUpdate :: Maybe Int -> Int -> UserDetailsSubmit -> MateHandler ()
|
|
|
|
userUpdate Nothing _ _ =
|
|
|
|
throwError $ err403
|
2019-09-06 19:44:31 +00:00
|
|
|
{ errBody = "No Authentication present."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|
2019-09-07 00:48:16 +00:00
|
|
|
userUpdate (Just aid) uid uds =
|
|
|
|
if aid == uid
|
2019-08-14 16:04:16 +00:00
|
|
|
then do
|
|
|
|
now <- liftIO $ getCurrentTime
|
|
|
|
conn <- rsConnection <$> ask
|
2019-09-07 00:48:16 +00:00
|
|
|
void $ updateUserDetails uid uds (utctDay now) conn
|
2019-08-14 16:04:16 +00:00
|
|
|
else
|
|
|
|
throwError $ err403
|
2019-09-06 19:44:31 +00:00
|
|
|
{ errBody = "Wrong Authentication present."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userList :: Maybe Int -> Maybe Refine -> MateHandler [User]
|
2019-09-07 00:48:16 +00:00
|
|
|
userList _ ref = do
|
2019-08-14 16:04:16 +00:00
|
|
|
conn <- rsConnection <$> ask
|
2019-09-06 19:44:47 +00:00
|
|
|
userSelect ref conn
|
2019-08-14 16:04:16 +00:00
|
|
|
|
|
|
|
userRecharge :: Maybe Int -> UserRecharge -> MateHandler ()
|
2019-09-06 19:43:46 +00:00
|
|
|
userRecharge (Just auid) (UserRecharge amount) =
|
|
|
|
if amount >= 0
|
|
|
|
then do
|
|
|
|
conn <- rsConnection <$> ask
|
|
|
|
ud <- userDetailsSelect auid conn
|
|
|
|
void $ insertNewJournalEntry
|
|
|
|
(JournalSubmit
|
|
|
|
("User \"" <> userDetailsIdent ud <> "\" recharged " <>
|
|
|
|
T.pack (show (fromIntegral amount / 100 :: Double)))
|
|
|
|
amount
|
|
|
|
)
|
|
|
|
conn
|
|
|
|
void $ addToUserBalance auid amount conn
|
|
|
|
else
|
|
|
|
throwError $ err400
|
|
|
|
{ errBody = "Amounts less or equal zero are not acceptable."
|
|
|
|
}
|
|
|
|
userRecharge Nothing _ =
|
|
|
|
throwError $ err403
|
|
|
|
{ errBody = "No Authentication present."
|
|
|
|
}
|
|
|
|
|
|
|
|
userTransfer :: Maybe Int -> UserTransfer -> MateHandler ()
|
|
|
|
userTransfer (Just auid) (UserTransfer target amount) =
|
|
|
|
if amount >= 0
|
2019-08-14 16:04:16 +00:00
|
|
|
then
|
2019-09-06 19:43:46 +00:00
|
|
|
if auid /= target
|
2019-08-14 16:04:16 +00:00
|
|
|
then do
|
|
|
|
conn <- rsConnection <$> ask
|
2019-09-06 19:43:46 +00:00
|
|
|
user <- userDetailsSelect auid conn
|
|
|
|
if amount < userDetailsBalance user
|
|
|
|
then do
|
|
|
|
mtarget <- filter (\u -> userId u == target) <$> userSelect (Just All) conn
|
|
|
|
if not (null mtarget)
|
|
|
|
then do
|
|
|
|
void $ addToUserBalance auid (-amount) conn
|
|
|
|
void $ addToUserBalance target amount conn
|
|
|
|
else
|
|
|
|
throwError $ err400
|
|
|
|
{ errBody = "Target user not found."
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throwError $ err400
|
2019-09-06 19:48:27 +00:00
|
|
|
{ errBody = "Not enough credit balance."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|
2019-09-06 19:43:46 +00:00
|
|
|
else
|
|
|
|
throwError $ err400
|
|
|
|
{ errBody = "You can not transfer yourself money."
|
|
|
|
}
|
2019-08-14 16:04:16 +00:00
|
|
|
else
|
2019-09-06 19:44:31 +00:00
|
|
|
throwError $ err400
|
|
|
|
{ errBody = "Amounts less or equal zero are not acceptable."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|
2019-09-06 19:43:46 +00:00
|
|
|
userTransfer Nothing _ =
|
2019-08-14 16:04:16 +00:00
|
|
|
throwError $ err403
|
2019-09-06 19:43:46 +00:00
|
|
|
{ errBody = "No Authentication present."
|
2019-08-14 16:04:16 +00:00
|
|
|
}
|