Modify xero endpoints

This commit is contained in:
Rene Vergara 2023-06-20 13:27:53 -05:00
parent f469ed6763
commit aa3794b504
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2

View file

@ -628,52 +628,46 @@ routes pipe config = do
]) ])
get "/api/xerotoken" $ do get "/api/xerotoken" $ do
code <- param "code" code <- param "code"
address <- param "address" session <- param "session"
user <- liftAndCatchIO $ run (findUser session)
xeroConfig <- liftAndCatchIO $ run findXero xeroConfig <- liftAndCatchIO $ run findXero
case xeroConfig of case cast' . Doc =<< xeroConfig of
Nothing -> status noContent204 Nothing -> status noContent204
Just x -> do Just c -> do
let xConfig = cast' (Doc x) case cast' . Doc =<< user of
case xConfig of Nothing -> status unauthorized401
Nothing -> status noContent204 Just u -> do
Just c -> do
res <- res <-
liftAndCatchIO $ liftAndCatchIO $
requestXeroToken pipe (c_dbName config) c code address requestXeroToken pipe (c_dbName config) c code $ uaddress u
if res if res
then status ok200 then status ok200
else status noContent204 else status noContent204
get "/api/invdata" $ do get "/invdata" $ do
inv <- param "inv" inv <- param "inv"
oAddress <- param "address" oAddress <- param "address"
xeroConfig <- liftAndCatchIO $ run findXero xeroConfig <- liftAndCatchIO $ run findXero
case xeroConfig of case cast' . Doc =<< xeroConfig of
Nothing -> do Nothing -> do
status noContent204 status noContent204
text "Xero App credentials not found" text "Xero App credentials not available"
Just x -> do Just c -> do
let xConfig = cast' (Doc x) res <-
case xConfig of liftAndCatchIO $
Nothing -> do requestXeroToken pipe (c_dbName config) c "none" oAddress
status noContent204 if res
text "Xero App credentials corrupted" then do
Just c -> do resInv <-
res <-
liftAndCatchIO $ liftAndCatchIO $
requestXeroToken pipe (c_dbName config) c "none" oAddress getXeroInvoice pipe (c_dbName config) inv oAddress
if res case resInv of
then do Nothing -> do
resInv <- status noContent204
liftAndCatchIO $ text "Xero invoice not found"
getXeroInvoice pipe (c_dbName config) inv oAddress Just xI -> do
case resInv of status ok200
Nothing -> do Web.Scotty.json (object ["invdata" .= toJSON xI])
status noContent204 else status noContent204
text "Xero invoice not found"
Just xI -> do
status ok200
Web.Scotty.json (object ["invdata" .= toJSON xI])
else status noContent204
-- Get the xeroaccount code -- Get the xeroaccount code
get "/api/xeroaccount" $ do get "/api/xeroaccount" $ do
session <- param "session" session <- param "session"
@ -972,7 +966,7 @@ routes pipe config = do
[ "message" .= ("Owner found!" :: String) [ "message" .= ("Owner found!" :: String)
, "owner" .= getOwnerSettings o , "owner" .= getOwnerSettings o
]) ])
get "/api/ownerid" $ do get "/ownerid" $ do
id <- param "id" id <- param "id"
owner <- liftAndCatchIO $ run (findOwnerById id) owner <- liftAndCatchIO $ run (findOwnerById id)
case owner of case owner of
@ -986,23 +980,7 @@ routes pipe config = do
Web.Scotty.json Web.Scotty.json
(object (object
[ "message" .= ("Owner found!" :: String) [ "message" .= ("Owner found!" :: String)
, "owner" .= , "owner" .= getOwnerSettings q
object
[ "_id" .= (maybe "" show $ o_id q :: String)
, "address" .= oaddress q
, "name" .= oname q
, "currency" .= ocurrency q
, "tax" .= otax q
, "taxValue" .= otaxValue q
, "vat" .= ovat q
, "vatValue" .= ovatValue q
, "paid" .= opaid q
, "zats" .= ozats q
, "invoices" .= oinvoices q
, "expiration" .= oexpiration q
, "payconf" .= opayconf q
, "crmToken" .= ocrmToken q
]
]) ])
--Upsert owner to DB --Upsert owner to DB
post "/api/owner" $ do post "/api/owner" $ do