new user transfer function added

This commit is contained in:
nek0 2019-09-06 21:43:46 +02:00
parent 130e64cbd7
commit 537a88638f
3 changed files with 50 additions and 17 deletions

View file

@ -36,6 +36,8 @@ type UserAPI =
:> QueryParam "refine" Refine :> Get '[JSON] [User] :> QueryParam "refine" Refine :> Get '[JSON] [User]
:<|> "user" :> "recharge" :> AuthProtect "header-auth" :<|> "user" :> "recharge" :> AuthProtect "header-auth"
:> ReqBody '[JSON] UserRecharge :> Post '[JSON] () :> ReqBody '[JSON] UserRecharge :> Post '[JSON] ()
:<|> "user" :> "transfer" :> AuthProtect "header-auth"
:> ReqBody '[JSON] UserTransfer :> Post '[JSON] ()
:<|> "product" :> AuthProtect "header-auth" :> ReqBody '[JSON] ProductSubmit :<|> "product" :> AuthProtect "header-auth" :> ReqBody '[JSON] ProductSubmit
:> Post '[JSON] Int :> Post '[JSON] Int

View file

@ -66,30 +66,60 @@ userList muid ref = do
userSelect conn ref userSelect conn ref
userRecharge :: Maybe Int -> UserRecharge -> MateHandler () userRecharge :: Maybe Int -> UserRecharge -> MateHandler ()
userRecharge (Just auid) (UserRecharge uid amount) = userRecharge (Just auid) (UserRecharge amount) =
if auid == uid 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
then then
if amount >= 0 if auid /= target
then do then do
conn <- rsConnection <$> ask conn <- rsConnection <$> ask
ud <- userDetailsSelect conn uid user <- userDetailsSelect auid conn
void $ insertNewJournalEntry if amount < userDetailsBalance user
(JournalSubmit then do
("User \"" <> userDetailsIdent ud <> "\" recharged " <> mtarget <- filter (\u -> userId u == target) <$> userSelect (Just All) conn
T.pack (show (fromIntegral amount / 100 :: Double))) if not (null mtarget)
amount then do
) void $ addToUserBalance auid (-amount) conn
conn void $ addToUserBalance target amount conn
void $ addToUserBalance uid amount conn else
else throwError $ err400
throwError $ err406 { errBody = "Target user not found."
{ errBody = "Amounts less or equal zero are not acceptable" }
else
throwError $ err400
{ errBody = "Not enough credit balance"
} }
else
throwError $ err400
{ errBody = "You can not transfer yourself money."
}
else else
throwError $ err403 throwError $ err403
{ errBody = "Wrong Authentication present" { errBody = "Wrong Authentication present"
} }
userRecharge Nothing _ = userTransfer Nothing _ =
throwError $ err403 throwError $ err403
{ errBody = "No Authentication present" { errBody = "No Authentication present."
} }

View file

@ -76,6 +76,7 @@ app initState =
userUpdate :<|> userUpdate :<|>
userList :<|> userList :<|>
userRecharge :<|> userRecharge :<|>
userTransfer :<|>
productNew :<|> productNew :<|>
productOverview :<|> productOverview :<|>