2015-08-09 19:16:33 +00:00
|
|
|
-- yammat - Yet Another MateMAT
|
|
|
|
-- Copyright (C) 2015 Amedeo Molnár
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published
|
|
|
|
-- by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-28 19:47:18 +00:00
|
|
|
module Handler.Transfer where
|
|
|
|
|
|
|
|
import Import
|
|
|
|
import Handler.Common
|
|
|
|
import Data.Maybe
|
|
|
|
import Text.Shakespeare.Text (stext)
|
|
|
|
|
|
|
|
getTransferSelectR :: UserId -> Handler Html
|
2016-05-23 18:35:14 +00:00
|
|
|
getTransferSelectR from =
|
|
|
|
isUser from HomeR >>= (\_ -> do
|
|
|
|
users <- runDB $ selectList [UserId !=. from] [Asc UserIdent]
|
2018-09-04 16:06:12 +00:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitleI MsgTransfer
|
2016-05-23 18:35:14 +00:00
|
|
|
$(widgetFile "transferSelect")
|
|
|
|
)
|
2015-07-28 19:47:18 +00:00
|
|
|
|
|
|
|
getTransferR :: UserId -> UserId -> Handler Html
|
2016-05-23 18:35:14 +00:00
|
|
|
getTransferR from to =
|
|
|
|
isUser from HomeR >>= (\sender ->
|
|
|
|
isUser to (TransferSelectR from) >>= (\recpt -> do
|
|
|
|
(transferWidget, enctype) <- generateFormPost
|
|
|
|
$ renderBootstrap3 BootstrapBasicForm transferForm
|
|
|
|
currency <- appCurrency <$> appSettings <$> getYesod
|
|
|
|
defaultLayout $ do
|
2018-09-04 16:06:12 +00:00
|
|
|
setTitleI MsgTransfer
|
2016-05-23 18:35:14 +00:00
|
|
|
$(widgetFile "transfer")
|
|
|
|
)
|
|
|
|
)
|
2015-07-28 19:47:18 +00:00
|
|
|
|
|
|
|
postTransferR :: UserId -> UserId -> Handler Html
|
2016-05-23 18:35:14 +00:00
|
|
|
postTransferR from to =
|
|
|
|
isUser from HomeR >>= (\sender ->
|
|
|
|
isUser to (TransferSelectR from) >>= (\recpt -> do
|
|
|
|
((res, _), _) <- runFormPost
|
|
|
|
$ renderBootstrap3 BootstrapBasicForm transferForm
|
|
|
|
case res of
|
2017-08-04 05:04:29 +00:00
|
|
|
FormSuccess amount
|
|
|
|
| amount < 0 -> do
|
2016-05-23 18:35:14 +00:00
|
|
|
setMessageI MsgNegativeTransfer
|
|
|
|
redirect $ TransferR from to
|
2017-08-04 05:04:29 +00:00
|
|
|
| userBalance sender < amount -> do
|
|
|
|
setMessageI MsgNotEnoughFunds
|
|
|
|
redirect HomeR
|
|
|
|
| otherwise -> do
|
2016-05-23 18:35:14 +00:00
|
|
|
runDB $ update from [UserBalance -=. amount]
|
|
|
|
runDB $ update to [UserBalance +=. amount]
|
|
|
|
master <- getYesod
|
|
|
|
liftIO $ notify sender recpt amount master
|
|
|
|
setMessageI MsgTransferComplete
|
2015-07-28 19:47:18 +00:00
|
|
|
redirect HomeR
|
2016-05-23 18:35:14 +00:00
|
|
|
_ -> do
|
|
|
|
setMessageI MsgTransferError
|
|
|
|
redirect HomeR
|
|
|
|
)
|
|
|
|
)
|
2015-07-28 19:47:18 +00:00
|
|
|
|
2015-10-22 21:57:27 +00:00
|
|
|
transferForm :: AForm Handler Int
|
|
|
|
transferForm = areq currencyField (bfs MsgValue) (Just 0)
|
|
|
|
<* bootstrapSubmit (msgToBSSubmit MsgTransfer)
|
2015-07-28 19:47:18 +00:00
|
|
|
|
|
|
|
notify :: User -> User -> Int -> App -> IO ()
|
2015-09-16 12:27:37 +00:00
|
|
|
notify sender rcpt amount master = do
|
2017-08-04 05:05:04 +00:00
|
|
|
when (isJust $ userEmail sender) $
|
2021-03-06 21:09:08 +00:00
|
|
|
liftIO $ sendMail master (fromJust $ userEmail sender) "Guthabentransfer beim Matematen"
|
2017-08-04 05:05:04 +00:00
|
|
|
[stext|
|
2015-07-28 19:47:18 +00:00
|
|
|
Hallo #{userIdent sender}
|
|
|
|
|
2015-09-16 12:27:37 +00:00
|
|
|
Du hast gerade #{formatIntCurrency amount}#{appCurrency $ appSettings master} an #{userIdent rcpt} transferiert.
|
2015-07-28 19:47:18 +00:00
|
|
|
|
2015-09-16 12:27:37 +00:00
|
|
|
Viele Grüße,
|
|
|
|
|
2016-02-02 23:25:06 +00:00
|
|
|
Dein Matemat
|
2017-08-04 05:05:04 +00:00
|
|
|
|]
|
|
|
|
when (isJust $ userEmail rcpt) $
|
2021-03-06 21:09:08 +00:00
|
|
|
liftIO $ sendMail master (fromJust $ userEmail rcpt) "Guthabentransfer eingetroffen"
|
2017-08-04 05:05:04 +00:00
|
|
|
[stext|
|
2015-09-16 12:27:37 +00:00
|
|
|
Hallo #{userIdent rcpt}
|
|
|
|
|
|
|
|
Du hast gerade #{formatIntCurrency amount}#{appCurrency $ appSettings master} von #{userIdent sender} erhalten.
|
|
|
|
|
|
|
|
Viele Grüße,
|
2015-07-28 19:47:18 +00:00
|
|
|
|
2016-02-02 23:25:06 +00:00
|
|
|
Dein Matemat
|
2017-08-04 05:05:04 +00:00
|
|
|
|]
|