zgo-backend/src/Owner.hs

290 lines
6.7 KiB
Haskell
Raw Normal View History

2022-05-11 20:04:46 +00:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Owner where
import Data.Aeson
import qualified Data.Bson as B
import Data.Maybe
2022-05-11 20:04:46 +00:00
import qualified Data.Text as T
2022-05-17 17:47:27 +00:00
import Data.Time.Clock
2022-05-11 20:04:46 +00:00
import Data.Typeable
import Database.MongoDB
import GHC.Generics
-- | Type to represent a ZGo shop owner/business
data Owner =
Owner
{ o_id :: Maybe ObjectId
, oaddress :: T.Text
, oname :: T.Text
, ocurrency :: T.Text
, otax :: Bool
, otaxValue :: Double
, ovat :: Bool
, ovatValue :: Double
, ofirst :: T.Text
, olast :: T.Text
, oemail :: T.Text
, ostreet :: T.Text
, ocity :: T.Text
, ostate :: T.Text
, opostal :: T.Text
, ophone :: T.Text
, owebsite :: T.Text
, ocountry :: T.Text
, opaid :: Bool
, ozats :: Bool
, oinvoices :: Bool
2022-05-17 17:47:27 +00:00
, oexpiration :: UTCTime
, opayconf :: Bool
2022-07-13 14:21:23 +00:00
, oviewkey :: T.Text
2022-08-03 18:48:51 +00:00
, ocrmToken :: T.Text
2022-05-11 20:04:46 +00:00
}
deriving (Eq, Show, Generic, Typeable)
instance ToJSON Owner where
2022-08-03 18:48:51 +00:00
toJSON (Owner i a n c t tV v vV f l e s ct st p ph w co paid zats inv eTs pc vk cT) =
2022-05-11 20:04:46 +00:00
case i of
Just oid ->
object
[ "_id" .= show oid
, "address" .= a
, "name" .= n
, "currency" .= c
, "tax" .= t
, "taxValue" .= tV
, "vat" .= v
, "vatValue" .= vV
, "first" .= f
, "last" .= l
, "email" .= e
, "street" .= s
, "city" .= ct
, "state" .= st
, "postal" .= p
, "phone" .= ph
, "website" .= w
, "country" .= co
, "paid" .= paid
, "zats" .= zats
, "invoices" .= inv
2022-05-17 17:47:27 +00:00
, "expiration" .= eTs
, "payconf" .= pc
2022-07-20 15:24:39 +00:00
, "viewkey" .= vk
2022-08-03 18:48:51 +00:00
, "crmToken" .= cT
2022-05-11 20:04:46 +00:00
]
Nothing ->
object
[ "_id" .= ("" :: String)
, "address" .= a
, "name" .= n
, "currency" .= c
, "tax" .= t
, "taxValue" .= tV
, "vat" .= v
, "vatValue" .= vV
, "first" .= f
, "last" .= l
, "email" .= e
, "street" .= s
, "city" .= ct
, "state" .= st
, "postal" .= p
, "phone" .= ph
, "website" .= w
, "country" .= co
, "paid" .= paid
, "zats" .= zats
, "invoices" .= inv
2022-05-17 17:47:27 +00:00
, "expiration" .= eTs
, "payconf" .= pc
2022-07-20 15:24:39 +00:00
, "viewkey" .= vk
2022-08-03 18:48:51 +00:00
, "crmToken" .= cT
2022-05-11 20:04:46 +00:00
]
instance FromJSON Owner where
parseJSON =
withObject "Owner" $ \obj -> do
i <- obj .: "_id"
a <- obj .: "address"
n <- obj .: "name"
c <- obj .: "currency"
t <- obj .: "tax"
tV <- obj .: "taxValue"
v <- obj .: "vat"
vV <- obj .: "vatValue"
f <- obj .: "first"
l <- obj .: "last"
e <- obj .: "email"
s <- obj .: "street"
ct <- obj .: "city"
st <- obj .: "state"
p <- obj .: "postal"
ph <- obj .: "phone"
w <- obj .: "website"
co <- obj .: "country"
paid <- obj .: "paid"
zats <- obj .: "zats"
inv <- obj .: "invoices"
2022-05-17 17:47:27 +00:00
ets <- obj .: "expiration"
pc <- obj .:? "payconf"
2022-07-20 15:24:39 +00:00
vk <- obj .:? "viewkey"
2022-08-03 18:48:51 +00:00
cT <- obj .:? "crmToken"
2022-05-11 20:04:46 +00:00
pure $
Owner
(if not (null i)
then Just (read i)
else Nothing)
a
n
c
t
tV
v
vV
f
l
e
s
ct
st
p
ph
w
co
paid
zats
inv
2022-05-17 17:47:27 +00:00
ets
(fromMaybe False pc)
(fromMaybe "" vk)
2022-08-03 18:48:51 +00:00
(fromMaybe "" cT)
2022-05-11 20:04:46 +00:00
instance Val Owner where
cast' (Doc d) = do
i <- B.lookup "_id" d
a <- B.lookup "address" d
n <- B.lookup "name" d
c <- B.lookup "currency" d
t <- B.lookup "tax" d
tV <- B.lookup "taxValue" d
v <- B.lookup "vat" d
vV <- B.lookup "vatValue" d
f <- B.lookup "first" d
l <- B.lookup "last" d
e <- B.lookup "email" d
s <- B.lookup "street" d
ct <- B.lookup "city" d
st <- B.lookup "state" d
p <- B.lookup "postal" d
ph <- B.lookup "phone" d
w <- B.lookup "website" d
co <- B.lookup "country" d
paid <- B.lookup "paid" d
zats <- B.lookup "zats" d
inv <- B.lookup "invoices" d
2022-05-17 17:47:27 +00:00
ets <- B.lookup "expiration" d
pc <- B.lookup "payconf" d
2022-07-13 14:21:23 +00:00
vk <- B.lookup "viewKey" d
2022-08-03 18:48:51 +00:00
cT <- B.lookup "crmToken" d
Just
2022-08-03 18:48:51 +00:00
(Owner
i
a
n
c
t
tV
v
vV
f
l
e
s
ct
st
p
ph
w
co
paid
zats
inv
ets
pc
vk
cT)
2022-05-11 20:04:46 +00:00
cast' _ = Nothing
2022-08-03 18:48:51 +00:00
val (Owner i a n c t tV v vV f l e s ct st p ph w co paid zats inv ets pc vk cT) =
2022-05-11 20:04:46 +00:00
case i of
Just oid ->
Doc
[ "_id" =: oid
, "address" =: a
, "name" =: n
, "currency" =: c
, "tax" =: t
, "taxValue" =: tV
, "vat" =: v
, "vatValue" =: vV
, "first" =: f
, "last" =: l
, "email" =: e
, "street" =: s
, "city" =: ct
, "state" =: st
, "postal" =: p
, "phone" =: ph
, "website" =: w
, "country" =: co
, "paid" =: paid
, "zats" =: zats
, "invoices" =: inv
2022-05-17 17:47:27 +00:00
, "expiration" =: ets
, "payconf" =: pc
2022-07-13 14:21:23 +00:00
, "viewKey" =: vk
2022-08-03 18:48:51 +00:00
, "crmToken" =: cT
2022-05-11 20:04:46 +00:00
]
Nothing ->
Doc
[ "address" =: a
, "name" =: n
, "currency" =: c
, "tax" =: t
, "taxValue" =: tV
, "vat" =: v
, "vatValue" =: vV
, "first" =: f
, "last" =: l
, "email" =: e
, "street" =: s
, "city" =: ct
, "state" =: st
, "postal" =: p
, "phone" =: ph
, "website" =: w
, "country" =: co
, "paid" =: paid
, "zats" =: zats
, "invoices" =: inv
2022-05-17 17:47:27 +00:00
, "expiration" =: ets
, "payconf" =: pc
2022-07-13 14:21:23 +00:00
, "viewKey" =: vk
2022-08-03 18:48:51 +00:00
, "crmToken" =: cT
2022-05-11 20:04:46 +00:00
]
-- Database actions
-- | Function to upsert an Owner
upsertOwner :: Owner -> Action IO ()
upsertOwner o = do
let owner = val o
case owner of
Doc d -> upsert (select ["address" =: oaddress o] "owners") d
_ -> return ()
-- | Function to get an Owner
findOwner :: T.Text -> Action IO (Maybe Document)
findOwner zaddy = findOne (select ["address" =: zaddy] "owners")