module Bank.TrueLayer.DataAPI.Cards
  ( cards
  , card
  , cardBalance
  , transactions
  , pendingTransactions
  ) where

import           Bank.TrueLayer.Internal
    (AccessToken, Endpoint, defaults, fromString, getWithAuthAndOptions, header, (&), (.~), (</>))

import           Bank.TrueLayer.DataAPI.Schema
    ( AccountId (..)
    , CardBalances
    , Cards
    , Ip (..)
    , TransactionParams
    , Transactions
    , addTransactionParams
    )

cards :: Ip -> Endpoint -> AccessToken -> IO (Maybe Cards)
cards :: Ip -> Endpoint -> AccessToken -> IO (Maybe Cards)
cards (Ip String
ip) = do
  let opts :: Options
opts = Options
defaults Options -> (Options -> Options) -> Options
forall a b. a -> (a -> b) -> b
& HeaderName -> Lens' Options [ByteString]
header HeaderName
"X-PSU-IP" (([ByteString] -> Identity [ByteString])
 -> Options -> Identity Options)
-> [ByteString] -> Options -> Options
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [String -> ByteString
forall a. IsString a => String -> a
fromString String
ip]
  Options -> String -> Endpoint -> AccessToken -> IO (Maybe Cards)
forall a.
FromJSON a =>
Options -> String -> Endpoint -> AccessToken -> IO (Maybe a)
getWithAuthAndOptions Options
opts String
"/data/v1/cards"

card :: Ip -> AccountId -> Endpoint -> AccessToken -> IO (Maybe Cards)
card :: Ip -> AccountId -> Endpoint -> AccessToken -> IO (Maybe Cards)
card (Ip String
ip) (AccountId String
accountId) = do
  let opts :: Options
opts = Options
defaults Options -> (Options -> Options) -> Options
forall a b. a -> (a -> b) -> b
& HeaderName -> Lens' Options [ByteString]
header HeaderName
"X-PSU-IP" (([ByteString] -> Identity [ByteString])
 -> Options -> Identity Options)
-> [ByteString] -> Options -> Options
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [String -> ByteString
forall a. IsString a => String -> a
fromString String
ip]
  Options -> String -> Endpoint -> AccessToken -> IO (Maybe Cards)
forall a.
FromJSON a =>
Options -> String -> Endpoint -> AccessToken -> IO (Maybe a)
getWithAuthAndOptions Options
opts (String
"/data/v1/cards" String -> String -> String
</> String
accountId)

cardBalance :: Ip -> AccountId -> Endpoint -> AccessToken -> IO (Maybe CardBalances)
cardBalance :: Ip
-> AccountId -> Endpoint -> AccessToken -> IO (Maybe CardBalances)
cardBalance (Ip String
ip) (AccountId String
accountId) = do
  let opts :: Options
opts = Options
defaults Options -> (Options -> Options) -> Options
forall a b. a -> (a -> b) -> b
& HeaderName -> Lens' Options [ByteString]
header HeaderName
"X-PSU-IP" (([ByteString] -> Identity [ByteString])
 -> Options -> Identity Options)
-> [ByteString] -> Options -> Options
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [String -> ByteString
forall a. IsString a => String -> a
fromString String
ip]
  Options
-> String -> Endpoint -> AccessToken -> IO (Maybe CardBalances)
forall a.
FromJSON a =>
Options -> String -> Endpoint -> AccessToken -> IO (Maybe a)
getWithAuthAndOptions Options
opts (String
"/data/v1/cards" String -> String -> String
</> String
accountId String -> String -> String
</> String
"balance")

transactions :: Ip -> AccountId -> Maybe TransactionParams -> Endpoint -> AccessToken -> IO (Maybe Transactions)
transactions :: Ip
-> AccountId
-> Maybe TransactionParams
-> Endpoint
-> AccessToken
-> IO (Maybe Transactions)
transactions (Ip String
ip) (AccountId String
accountId) Maybe TransactionParams
params Endpoint
endpoint AccessToken
accessToken = do
  Options
opts <- Maybe TransactionParams -> Options -> IO Options
addTransactionParams Maybe TransactionParams
params (Options
defaults Options -> (Options -> Options) -> Options
forall a b. a -> (a -> b) -> b
& HeaderName -> Lens' Options [ByteString]
header HeaderName
"X-PSU-IP" (([ByteString] -> Identity [ByteString])
 -> Options -> Identity Options)
-> [ByteString] -> Options -> Options
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [String -> ByteString
forall a. IsString a => String -> a
fromString String
ip])
  Options
-> String -> Endpoint -> AccessToken -> IO (Maybe Transactions)
forall a.
FromJSON a =>
Options -> String -> Endpoint -> AccessToken -> IO (Maybe a)
getWithAuthAndOptions Options
opts (String
"/data/v1/cards" String -> String -> String
</> String
accountId String -> String -> String
</> String
"transactions") Endpoint
endpoint AccessToken
accessToken

pendingTransactions :: Ip -> AccountId -> Maybe TransactionParams -> Endpoint -> AccessToken -> IO (Maybe Transactions)
pendingTransactions :: Ip
-> AccountId
-> Maybe TransactionParams
-> Endpoint
-> AccessToken
-> IO (Maybe Transactions)
pendingTransactions (Ip String
ip) (AccountId String
accountId) Maybe TransactionParams
params Endpoint
endpoint AccessToken
accessToken = do
  Options
opts <- Maybe TransactionParams -> Options -> IO Options
addTransactionParams Maybe TransactionParams
params (Options
defaults Options -> (Options -> Options) -> Options
forall a b. a -> (a -> b) -> b
& HeaderName -> Lens' Options [ByteString]
header HeaderName
"X-PSU-IP" (([ByteString] -> Identity [ByteString])
 -> Options -> Identity Options)
-> [ByteString] -> Options -> Options
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [String -> ByteString
forall a. IsString a => String -> a
fromString String
ip])
  Options
-> String -> Endpoint -> AccessToken -> IO (Maybe Transactions)
forall a.
FromJSON a =>
Options -> String -> Endpoint -> AccessToken -> IO (Maybe a)
getWithAuthAndOptions Options
opts (String
"/data/v1/cards" String -> String -> String
</> String
accountId String -> String -> String
</> String
"transactions/pending") Endpoint
endpoint AccessToken
accessToken