From aa3794b504055838a701d8c443a60b4375953e38 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 20 Jun 2023 13:27:53 -0500 Subject: [PATCH] Modify xero endpoints --- src/ZGoBackend.hs | 80 +++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 51 deletions(-) diff --git a/src/ZGoBackend.hs b/src/ZGoBackend.hs index e4182d3..aaed2d4 100644 --- a/src/ZGoBackend.hs +++ b/src/ZGoBackend.hs @@ -628,52 +628,46 @@ routes pipe config = do ]) get "/api/xerotoken" $ do code <- param "code" - address <- param "address" + session <- param "session" + user <- liftAndCatchIO $ run (findUser session) xeroConfig <- liftAndCatchIO $ run findXero - case xeroConfig of + case cast' . Doc =<< xeroConfig of Nothing -> status noContent204 - Just x -> do - let xConfig = cast' (Doc x) - case xConfig of - Nothing -> status noContent204 - Just c -> do + Just c -> do + case cast' . Doc =<< user of + Nothing -> status unauthorized401 + Just u -> do res <- liftAndCatchIO $ - requestXeroToken pipe (c_dbName config) c code address + requestXeroToken pipe (c_dbName config) c code $ uaddress u if res then status ok200 else status noContent204 - get "/api/invdata" $ do + get "/invdata" $ do inv <- param "inv" oAddress <- param "address" xeroConfig <- liftAndCatchIO $ run findXero - case xeroConfig of + case cast' . Doc =<< xeroConfig of Nothing -> do status noContent204 - text "Xero App credentials not found" - Just x -> do - let xConfig = cast' (Doc x) - case xConfig of - Nothing -> do - status noContent204 - text "Xero App credentials corrupted" - Just c -> do - res <- + text "Xero App credentials not available" + Just c -> do + res <- + liftAndCatchIO $ + requestXeroToken pipe (c_dbName config) c "none" oAddress + if res + then do + resInv <- liftAndCatchIO $ - requestXeroToken pipe (c_dbName config) c "none" oAddress - if res - then do - resInv <- - liftAndCatchIO $ - getXeroInvoice pipe (c_dbName config) inv oAddress - case resInv of - Nothing -> do - status noContent204 - text "Xero invoice not found" - Just xI -> do - status ok200 - Web.Scotty.json (object ["invdata" .= toJSON xI]) - else status noContent204 + getXeroInvoice pipe (c_dbName config) inv oAddress + case resInv of + Nothing -> do + 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 "/api/xeroaccount" $ do session <- param "session" @@ -972,7 +966,7 @@ routes pipe config = do [ "message" .= ("Owner found!" :: String) , "owner" .= getOwnerSettings o ]) - get "/api/ownerid" $ do + get "/ownerid" $ do id <- param "id" owner <- liftAndCatchIO $ run (findOwnerById id) case owner of @@ -986,23 +980,7 @@ routes pipe config = do Web.Scotty.json (object [ "message" .= ("Owner found!" :: String) - , "owner" .= - 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 - ] + , "owner" .= getOwnerSettings q ]) --Upsert owner to DB post "/api/owner" $ do