mateamt/src/Control/Product.hs

128 lines
3.3 KiB
Haskell
Raw Normal View History

2019-08-14 16:04:16 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Control.Product where
import Servant
import Control.Monad (void)
2019-10-14 20:50:42 +00:00
import Control.Monad.Reader (asks)
2019-08-14 16:04:16 +00:00
import Control.Monad.Extra (anyM)
2019-09-09 11:17:43 +00:00
import Data.Maybe (fromMaybe)
2019-08-14 16:04:16 +00:00
-- internal imports
import Types
import Model
import Control.Role (checkCapability)
2019-08-14 16:04:16 +00:00
2019-09-15 12:59:22 +00:00
productNew
:: Maybe (Int, AuthMethod)
-> ProductSubmit
-> MateHandler Int
productNew (Just (uid, auth)) bevsub = do
mayAddProduct <- checkCapability uid roleCanManageProducts
2020-08-24 07:43:22 +00:00
if auth `elem` [PrimaryPass, ChallengeResponse] && mayAddProduct
then do
conn <- asks rsConnection
bevid <- insertProduct bevsub conn
void $ insertNewEmptyAmount bevid bevsub conn
return bevid
else
throwError $ err401
{ errBody = "You are not authorized for this action."
}
2019-08-14 16:04:16 +00:00
productNew Nothing _ =
throwError $ err401
{ errBody = "No Authentication present."
}
2019-08-14 16:04:16 +00:00
2019-09-15 12:59:22 +00:00
productOverview
:: Int
-> MateHandler ProductOverview
2019-08-14 16:04:16 +00:00
productOverview pid = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
2019-08-14 16:04:16 +00:00
productOverviewSelectSingle pid conn
2019-09-15 12:59:22 +00:00
productStockRefill
:: Maybe (Int, AuthMethod)
-> [AmountRefill]
-> MateHandler ()
productStockRefill (Just (uid, auth)) amorefs = do
mayRefill <- anyM
(checkCapability uid)
[ roleCanRefillStock, roleCanManageProducts ]
2020-08-24 07:43:22 +00:00
if auth `elem` [PrimaryPass, ChallengeResponse] && mayRefill
then do
conn <- asks rsConnection
prods <- mapM
(\refill -> productSelectSingle (amountRefillProductId refill) conn)
amorefs
2020-08-24 07:43:22 +00:00
if not (any null prods)
then
if
all
(\refill ->
(>= 0) (amountRefillAmountSingles refill) &&
(>= 0) (amountRefillAmountCrates refill)
)
amorefs
then do
void $ manualProductAmountRefill amorefs conn
else
throwError $ err400
{ errBody = "Amounts less than 0 are not acceptable."
}
2019-12-14 23:03:49 +00:00
else
throwError $ err400
{ errBody = "Non-existent Products are non-refillable."
2019-12-14 23:03:49 +00:00
}
2019-08-14 16:04:16 +00:00
else
throwError $ err401
{ errBody = "You are not authorized for this action."
2019-08-14 16:04:16 +00:00
}
productStockRefill Nothing _ =
2019-09-11 11:54:18 +00:00
throwError $ err401
2019-09-06 19:44:31 +00:00
{ errBody = "No Authentication present."
2019-08-14 16:04:16 +00:00
}
2019-09-15 12:59:22 +00:00
productStockUpdate
:: Maybe (Int, AuthMethod)
-> [AmountUpdate]
-> MateHandler ()
productStockUpdate (Just (uid, method)) amoups = do
mayUpdateStock <- checkCapability uid roleCanManageProducts
2020-08-24 07:43:22 +00:00
if method `elem` [PrimaryPass, ChallengeResponse] && mayUpdateStock
2019-12-13 22:48:09 +00:00
then
if all ((>= 0) . amountUpdateRealAmount) amoups
then do
conn <- asks rsConnection
void $ manualProductAmountUpdate amoups conn
else
throwError $ err400
{ errBody = "Amounts less than 0 are not acceptable."
}
2019-08-14 16:04:16 +00:00
else
2019-12-13 22:48:09 +00:00
throwError $ err401
{ errBody = "Wrong Authentication present."
2019-08-14 16:04:16 +00:00
}
productStockUpdate Nothing _ =
2019-09-11 11:54:18 +00:00
throwError $ err401
2019-09-06 19:44:31 +00:00
{ errBody = "No Authentication present."
2019-08-14 16:04:16 +00:00
}
2019-09-15 12:59:22 +00:00
productList
:: Maybe ProductRefine
-> MateHandler [ProductOverview]
2019-09-09 10:57:08 +00:00
productList mrefine = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
2019-09-09 10:57:08 +00:00
productOverviewSelect (fromMaybe AvailableProducts mrefine) conn
productShortList
:: Maybe ProductRefine
-> MateHandler [ProductShortOverview]
productShortList mrefine = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
productShortOverviewSelect (fromMaybe AvailableProducts mrefine) conn