From d4b56ca641935da504520621bf385b52c193f7df Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Fri, 12 May 2023 13:57:56 -0500 Subject: [PATCH] Update owner data endpoints --- CHANGELOG.md | 2 ++ src/Owner.hs | 80 +++++++++++++++++++++++++++++++++++++++++++++-- src/ZGoBackend.hs | 18 +---------- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98fb08a..03b9130 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `isUserValid` function - New middleware to validated requests come from an existing user - New endpoint for the language data of the login page +- New `OwnerData` to represent informational values +- New `OwnerSettings` to abstract configuration settings for owners ### Changed diff --git a/src/Owner.hs b/src/Owner.hs index 9df5128..45fdf68 100644 --- a/src/Owner.hs +++ b/src/Owner.hs @@ -289,9 +289,8 @@ data OwnerData = , od_email :: T.Text , od_website :: T.Text , od_phone :: T.Text - , od_payconf :: Bool } - deriving (Eq, Show) + deriving (Eq, Show, Generic) instance FromJSON OwnerData where parseJSON = @@ -307,8 +306,83 @@ instance FromJSON OwnerData where e <- obj .: "email" w <- obj .: "website" ph <- obj .: "phone" + pure $ OwnerData f l n s c st p co e w ph + +data OwnerSettings = + OwnerSettings + { os_id :: Maybe ObjectId + , os_address :: T.Text + , os_name :: T.Text + , os_currency :: T.Text + , os_tax :: Bool + , os_taxValue :: Double + , os_vat :: Bool + , os_vatValue :: Double + , os_paid :: Bool + , os_zats :: Bool + , os_invoices :: Bool + , os_expiration :: UTCTime + , os_payconf :: Bool + , os_crmToken :: T.Text + } + deriving (Eq, Show, Generic) + +instance FromJSON OwnerSettings where + parseJSON = + withObject "OwnerSettings" $ \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" + p <- obj .: "paid" + z <- obj .: "zats" + inv <- obj .: "invoices" + e <- obj .: "expiration" pc <- obj .: "payconf" - pure $ OwnerData f l n s c st p co e w ph pc + cT <- obj .: "crmToken" + pure $ OwnerSettings ((Just . read) =<< i) a n c t tV v vV p z inv e pc cT + +instance ToJSON OwnerSettings where + toJSON (OwnerSettings i a n c t tV v vV p z inv e pc cT) = + object + [ "_id" .= maybe "" show i + , "address" .= a + , "name" .= n + , "currency" .= c + , "tax" .= t + , "taxValue" .= tV + , "vat" .= v + , "vatValue" .= vV + , "paid" .= p + , "zats" .= z + , "invoices" .= inv + , "expiration" .= e + , "payconf" .= pc + , "crmToken" .= cT + ] + +-- Helper Functions +getOwnerSettings :: Owner -> OwnerSettings +getOwnerSettings o = + OwnerSettings + (o_id o) + (oaddress o) + (oname o) + (ocurrency o) + (otax o) + (otaxValue o) + (ovat o) + (ovatValue o) + (opaid o) + (ozats o) + (oinvoices o) + (oexpiration o) + (opayconf o) + (ocrmToken o) -- Database actions -- | Function to upsert an Owner diff --git a/src/ZGoBackend.hs b/src/ZGoBackend.hs index d3dfc70..7c82b52 100644 --- a/src/ZGoBackend.hs +++ b/src/ZGoBackend.hs @@ -927,23 +927,7 @@ routes pipe config = do Web.Scotty.json (object [ "message" .= ("Owner found!" :: String) - , "owner" .= - object - [ "_id" .= (maybe "" show $ o_id o :: String) - , "address" .= oaddress o - , "name" .= oname o - , "currency" .= ocurrency o - , "tax" .= otax o - , "taxValue" .= otaxValue o - , "vat" .= ovat o - , "vatValue" .= ovatValue o - , "paid" .= opaid o - , "zats" .= ozats o - , "invoices" .= oinvoices o - , "expiration" .= oexpiration o - , "payconf" .= opayconf o - , "crmToken" .= ocrmToken o - ] + , "owner" .= getOwnerSettings o ]) get "/api/ownerid" $ do id <- param "id"