zgo-backend/src/Xero.hs

36 lines
837 B
Haskell
Raw Normal View History

2022-08-10 15:17:47 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Xero where
import Data.Aeson
import qualified Data.Bson as B
import qualified Data.Text as T
import Database.MongoDB
import GHC.Generics
-- | Type to represent a Xero app configuration
data Xero =
Xero
{ x_id :: ObjectId
, x_clientId :: T.Text
, x_clientSecret :: T.Text
}
deriving (Eq, Show)
instance ToJSON Xero where
toJSON (Xero i cI s) =
object ["_id" .= show i, "clientId" .= cI, "clientSecret" .= s]
instance Val Xero where
val (Xero i cI s) = Doc ["_id" =: i, "clientId" =: cI, "clientSecret" =: s]
cast' (Doc d) = do
i <- B.lookup "_id" d
cI <- B.lookup "clientId" d
s <- B.lookup "clientSecret" d
Just (Xero i cI s)
cast' _ = Nothing
-- Database actions
findXero :: Action IO (Maybe Document)
findXero = findOne (select [] "xero")