module Jobs.Handler.ExternalApis
  ( dispatchJobExternalApiExpire
  ) where

import Import


dispatchJobExternalApiExpire :: ExternalApiId -> JobHandler UniWorX
dispatchJobExternalApiExpire apiId = JobHandlerAtomic $ do
  now <- liftIO getCurrentTime
  expiry <- getsYesod $ view _appExternalApisExpiry
  void . runMaybeT $ do
    ExternalApi{..} <- MaybeT $ get apiId
    guard $ externalApiLastAlive <= addUTCTime (- expiry) now
    lift $ delete apiId
