diff --git a/CHANGELOG.md b/CHANGELOG.md index 164d3b7..54f0bf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Modified API tests to use `session` parameter. +- Modified `api/owner` endpoint to use a specific data structure to create new owners ## [1.4.1] - 2023-05-02 diff --git a/src/Owner.hs b/src/Owner.hs index f426636..a7ebe1d 100644 --- a/src/Owner.hs +++ b/src/Owner.hs @@ -275,6 +275,39 @@ instance Val Owner where , "crmToken" =: cT ] +-- | Type to represent informational data for Owners from UI +data OwnerData = + OwnerData + { od_first :: T.Text + , od_last :: T.Text + , od_name :: T.Text + , od_street :: T.Text + , od_city :: T.Text + , od_state :: T.Text + , od_postal :: T.Text + , od_country :: T.Text + , od_email :: T.Text + , od_website :: T.Text + , od_phone :: T.Text + } + deriving (Eq, Show) + +instance FromJSON OwnerData where + parseJSON = + withObject "OwnerData" $ \obj -> do + f <- obj .: "first" + l <- obj .: "last" + n <- obj .: "name" + s <- obj .: "street" + c <- obj .: "city" + st <- obj .: "state" + p <- obj .: "postal" + co <- obj .: "country" + e <- obj .: "email" + w <- obj .: "website" + ph <- obj .: "phone" + pure $ OwnerData f l n s c st p co e w ph + -- Database actions -- | Function to upsert an Owner upsertOwner :: Owner -> Action IO () diff --git a/src/ZGoBackend.hs b/src/ZGoBackend.hs index 44f7bbb..6cb8276 100644 --- a/src/ZGoBackend.hs +++ b/src/ZGoBackend.hs @@ -938,33 +938,44 @@ routes pipe config = do ]) --Upsert owner to DB post "/api/owner" $ do + s <- param "session" + u <- liftAndCatchIO $ run (findUser s) o <- jsonData - let q = payload (o :: Payload Owner) - if not (opayconf q) - then do - _ <- liftAndCatchIO $ run (upsertOwner q) - status created201 - else do - known <- liftAndCatchIO $ listAddresses nodeUser nodePwd - if oaddress q `elem` map addy known - then do - _ <- liftAndCatchIO $ run (upsertOwner q) - status created201 - else do - vkInfo <- - liftAndCatchIO $ - makeZcashCall - nodeUser - nodePwd - "z_importviewingkey" - [Data.Aeson.String (T.strip (oviewkey q)), "no"] - let content = getResponseBody vkInfo :: RpcResponse Object - if isNothing (err content) - then do - _ <- liftAndCatchIO $ run (upsertOwner q) - status created201 - else do - status internalServerError500 + now <- liftIO getCurrentTime + let q = payload (o :: Payload OwnerData) + case parseUserBson =<< u of + Nothing -> status internalServerError500 + Just u' -> do + liftAndCatchIO $ + run $ + upsertOwner $ + Owner + Nothing + (uaddress u') + (od_name q) + "usd" + False + 0 + False + 0 + (od_first q) + (od_last q) + (od_email q) + (od_street q) + (od_city q) + (od_state q) + (od_postal q) + (od_phone q) + (od_website q) + (od_country q) + False + False + False + now + False + "" + "" + status accepted202 --Get items associated with the given address get "/api/items" $ do addr <- param "address"