2019-09-08 10:48:57 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-09-18 13:24:23 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2019-10-14 15:40:20 +00:00
|
|
|
{-# LANGUAGE PackageImports #-}
|
2019-09-08 15:27:38 +00:00
|
|
|
module Util where
|
2019-09-08 10:48:57 +00:00
|
|
|
|
|
|
|
import Servant hiding (addHeader)
|
2019-10-14 15:40:20 +00:00
|
|
|
import Servant.Client
|
2019-09-11 04:27:54 +00:00
|
|
|
import Servant.Client.Core.Request
|
2019-09-08 10:48:57 +00:00
|
|
|
import Servant.Client.Core.Request (addHeader)
|
|
|
|
|
2019-09-11 04:27:54 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Text.Lazy.Encoding
|
|
|
|
|
|
|
|
import Data.ByteString.Builder
|
|
|
|
|
|
|
|
import Data.List (intercalate)
|
|
|
|
import Data.List.Split (chunksOf)
|
|
|
|
|
|
|
|
import Data.String (fromString)
|
|
|
|
|
|
|
|
import Web.Cookie
|
|
|
|
|
|
|
|
import Text.Printf (printf)
|
|
|
|
|
2019-10-14 15:40:20 +00:00
|
|
|
import Network.HTTP.Types.Status
|
|
|
|
|
|
|
|
-- imports from "mateamt"
|
|
|
|
|
|
|
|
import qualified "mateamt" Types as MT
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import API
|
|
|
|
|
2019-09-11 04:27:54 +00:00
|
|
|
setCookie
|
|
|
|
:: SetCookie
|
|
|
|
-> Request
|
|
|
|
-> Request
|
|
|
|
setCookie c = addHeader "Set-Cookie" (decodeUtf8 . toLazyByteString $ renderSetCookie c)
|
|
|
|
|
|
|
|
formatMoney :: Int -> T.Text
|
|
|
|
formatMoney amount = pre <> t <> "," <> c
|
|
|
|
where
|
|
|
|
pre = if amount < 0
|
|
|
|
then "-"
|
|
|
|
else ""
|
|
|
|
t = fromString $ reverse (intercalate "." $ chunksOf 3 $ reverse $ fst sp)
|
|
|
|
c = fromString $ snd sp
|
|
|
|
sp = tail <$>
|
2019-10-14 04:38:16 +00:00
|
|
|
break (== '.') (printf "%.2f" (fromIntegral $ abs amount :: Float))
|
2019-09-18 13:24:23 +00:00
|
|
|
|
|
|
|
redirect303
|
2019-10-04 07:04:23 +00:00
|
|
|
:: Link
|
|
|
|
-> ServerError
|
2019-09-18 13:24:23 +00:00
|
|
|
redirect303 link =
|
2019-10-04 07:04:23 +00:00
|
|
|
err303
|
2019-09-18 13:24:23 +00:00
|
|
|
{ errHeaders =
|
2019-10-14 04:38:16 +00:00
|
|
|
[ ("Location", "/" <> (fromString $ show $ linkURI link))
|
2019-09-18 13:24:23 +00:00
|
|
|
]
|
|
|
|
}
|
2019-10-04 07:04:23 +00:00
|
|
|
|
|
|
|
parseCookieText
|
|
|
|
:: T.Text
|
|
|
|
-> [(T.Text, T.Text)]
|
|
|
|
parseCookieText = map
|
|
|
|
(\x -> let (one, two) = T.breakOn "=" x in (one, T.tail two))
|
|
|
|
. T.splitOn "; "
|
2019-10-14 15:40:20 +00:00
|
|
|
|
|
|
|
handleErrors
|
|
|
|
:: (Traversable t, Show (t (Maybe ClientError)))
|
|
|
|
=> Maybe Int
|
|
|
|
-> t (Maybe ClientError)
|
|
|
|
-> UserHandler UserOverviewPage
|
|
|
|
handleErrors mAuthUser merrs = do
|
|
|
|
codes <- mapM
|
|
|
|
(\eerr -> case eerr of
|
|
|
|
Just (FailureResponse _ resp) ->
|
|
|
|
return $ statusCode (responseStatusCode resp)
|
|
|
|
Just err ->
|
|
|
|
throwError $ err500
|
|
|
|
{ errBody = fromString (show err)
|
|
|
|
}
|
|
|
|
Nothing ->
|
|
|
|
return 200
|
|
|
|
)
|
|
|
|
merrs
|
|
|
|
if (any (== 401) codes)
|
|
|
|
then
|
|
|
|
redirectOverAuth mAuthUser Nothing
|
|
|
|
else
|
|
|
|
throwError $ err500
|
|
|
|
{ errBody = fromString (show merrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectOverAuth
|
|
|
|
:: Maybe Int
|
|
|
|
-> Maybe (MT.ProductRefine)
|
|
|
|
-> UserHandler UserOverviewPage
|
|
|
|
redirectOverAuth muid mRefine = do
|
|
|
|
let redirectHeaders = errHeaders $
|
|
|
|
redirect303 (authLink (Just $ "/" <>
|
|
|
|
fromString (show $ linkURI (case muid of
|
|
|
|
Just uid -> userOverviewLink uid mRefine
|
|
|
|
Nothing -> userSelectLink
|
|
|
|
))))
|
|
|
|
throwError
|
|
|
|
(err303
|
|
|
|
{ errHeaders = redirectHeaders ++ (case muid of
|
|
|
|
Just uid ->
|
|
|
|
[ ( "Set-Cookie"
|
|
|
|
, "x-auth-user=" <> (fromString $ show uid) <> "; Path=/"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
Nothing ->
|
|
|
|
[
|
|
|
|
]
|
|
|
|
)
|
|
|
|
++
|
|
|
|
[ ( "Set-Cookie"
|
|
|
|
, "x-token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
|
|
|
|
)
|
|
|
|
, ( "Set-Cookie"
|
|
|
|
, "x-method=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|