module Jobs.Handler.PersonalisedSheetFiles
  ( dispatchJobPruneFallbackPersonalisedSheetFilesKeys
  ) where

import Import

import Database.Persist.Sql (deleteWhereCount)


dispatchJobPruneFallbackPersonalisedSheetFilesKeys :: JobHandler UniWorX
dispatchJobPruneFallbackPersonalisedSheetFilesKeys = JobHandlerAtomic . hoist lift $ do
  now <- liftIO getCurrentTime
  expires <- getsYesod $ view _appFallbackPersonalisedSheetFilesKeysExpire
  n <- deleteWhereCount [ FallbackPersonalisedSheetFilesKeyGenerated <. addUTCTime (- expires) now ]
  $logInfoS "PruneFallbackPersonalisedSheetFilesKeys" [st|Deleted #{n} expired fallback personalised sheet files keys|]
