Modify endpoint to create new owner
This commit is contained in:
parent
cbc4e02766
commit
a201810134
3 changed files with 71 additions and 26 deletions
|
@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Modified API tests to use `session` parameter.
|
- 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
|
## [1.4.1] - 2023-05-02
|
||||||
|
|
||||||
|
|
33
src/Owner.hs
33
src/Owner.hs
|
@ -275,6 +275,39 @@ instance Val Owner where
|
||||||
, "crmToken" =: cT
|
, "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
|
-- Database actions
|
||||||
-- | Function to upsert an Owner
|
-- | Function to upsert an Owner
|
||||||
upsertOwner :: Owner -> Action IO ()
|
upsertOwner :: Owner -> Action IO ()
|
||||||
|
|
|
@ -938,33 +938,44 @@ routes pipe config = do
|
||||||
])
|
])
|
||||||
--Upsert owner to DB
|
--Upsert owner to DB
|
||||||
post "/api/owner" $ do
|
post "/api/owner" $ do
|
||||||
|
s <- param "session"
|
||||||
|
u <- liftAndCatchIO $ run (findUser s)
|
||||||
o <- jsonData
|
o <- jsonData
|
||||||
let q = payload (o :: Payload Owner)
|
now <- liftIO getCurrentTime
|
||||||
if not (opayconf q)
|
let q = payload (o :: Payload OwnerData)
|
||||||
then do
|
case parseUserBson =<< u of
|
||||||
_ <- liftAndCatchIO $ run (upsertOwner q)
|
Nothing -> status internalServerError500
|
||||||
status created201
|
Just u' -> do
|
||||||
else do
|
liftAndCatchIO $
|
||||||
known <- liftAndCatchIO $ listAddresses nodeUser nodePwd
|
run $
|
||||||
if oaddress q `elem` map addy known
|
upsertOwner $
|
||||||
then do
|
Owner
|
||||||
_ <- liftAndCatchIO $ run (upsertOwner q)
|
Nothing
|
||||||
status created201
|
(uaddress u')
|
||||||
else do
|
(od_name q)
|
||||||
vkInfo <-
|
"usd"
|
||||||
liftAndCatchIO $
|
False
|
||||||
makeZcashCall
|
0
|
||||||
nodeUser
|
False
|
||||||
nodePwd
|
0
|
||||||
"z_importviewingkey"
|
(od_first q)
|
||||||
[Data.Aeson.String (T.strip (oviewkey q)), "no"]
|
(od_last q)
|
||||||
let content = getResponseBody vkInfo :: RpcResponse Object
|
(od_email q)
|
||||||
if isNothing (err content)
|
(od_street q)
|
||||||
then do
|
(od_city q)
|
||||||
_ <- liftAndCatchIO $ run (upsertOwner q)
|
(od_state q)
|
||||||
status created201
|
(od_postal q)
|
||||||
else do
|
(od_phone q)
|
||||||
status internalServerError500
|
(od_website q)
|
||||||
|
(od_country q)
|
||||||
|
False
|
||||||
|
False
|
||||||
|
False
|
||||||
|
now
|
||||||
|
False
|
||||||
|
""
|
||||||
|
""
|
||||||
|
status accepted202
|
||||||
--Get items associated with the given address
|
--Get items associated with the given address
|
||||||
get "/api/items" $ do
|
get "/api/items" $ do
|
||||||
addr <- param "address"
|
addr <- param "address"
|
||||||
|
|
Loading…
Reference in a new issue