2022-10-26 20:34:29 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module TokenRefresh where
|
|
|
|
|
|
|
|
import Config
|
|
|
|
import Data.Time.Clock
|
|
|
|
import Database.MongoDB
|
|
|
|
import Xero
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
putStrLn "Reading config..."
|
|
|
|
now <- getCurrentTime
|
|
|
|
loadedConfig <- loadZGoConfig "zgo.cfg"
|
|
|
|
pipe <- connect $ host (c_dbHost loadedConfig)
|
|
|
|
let db = c_dbName loadedConfig
|
|
|
|
j <-
|
|
|
|
access
|
|
|
|
pipe
|
|
|
|
master
|
|
|
|
db
|
|
|
|
(auth (c_dbUser loadedConfig) (c_dbPassword loadedConfig))
|
|
|
|
if j
|
|
|
|
then putStrLn "Connected to MongoDB!"
|
|
|
|
else fail "MongoDB connection failed!"
|
|
|
|
credsData <- access pipe master db findXero
|
|
|
|
let creds = cast' . Doc =<< credsData
|
|
|
|
tokens <- access pipe master db (findExpiringTokens now)
|
|
|
|
if not (null tokens)
|
|
|
|
then do
|
|
|
|
let t = map (cast' . Doc) tokens
|
|
|
|
case creds of
|
2023-03-15 20:52:52 +00:00
|
|
|
Just c -> mapM_ (refreshToken pipe db c "" "") t
|
2022-10-26 20:34:29 +00:00
|
|
|
Nothing -> fail "No credentials"
|
|
|
|
else putStrLn "No tokens to refresh1"
|