Add CLI command to GC expired emails
This commit is contained in:
parent
4f8705d681
commit
b5ef36a1bb
|
@ -132,6 +132,12 @@ addEmailVerification secret = do
|
||||||
, dBEmailVerificationExpires = expires
|
, dBEmailVerificationExpires = expires
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deleteExpiredEmails :: MonadDB m => Time -> m ()
|
||||||
|
deleteExpiredEmails time = runQuery $ delete $ do
|
||||||
|
verification <- from $ table @DBEmailVerification
|
||||||
|
where_ $ verification ^. DBEmailVerificationExpires <=. val time
|
||||||
|
|
||||||
|
|
||||||
updateEmail :: (MonadDB m, MonadTime m) => UserID -> Email -> Text -> m (Key DBEmail)
|
updateEmail :: (MonadDB m, MonadTime m) => UserID -> Email -> Text -> m (Key DBEmail)
|
||||||
updateEmail user email secret = getExpireTime >>= \expires -> runQuery $ do
|
updateEmail user email secret = getExpireTime >>= \expires -> runQuery $ do
|
||||||
delete $ do
|
delete $ do
|
||||||
|
|
|
@ -24,7 +24,11 @@ main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
CLIOptions {..} <- execParser $ info (cliOptions <**> helper) mempty
|
CLIOptions {..} <- execParser $ info (cliOptions <**> helper) mempty
|
||||||
runCLIM optionsDBUrl $ case optionsSubCommand of
|
runCLIM optionsDBUrl $ case optionsSubCommand of
|
||||||
AddUser {..} -> do
|
(AddUser addUserOpts) -> addUserMain addUserOpts
|
||||||
|
GCEmails -> gcEmailsMain
|
||||||
|
|
||||||
|
addUserMain :: AddUserOpts -> CLIM ()
|
||||||
|
addUserMain AddUserOpts {..} = do
|
||||||
time <- currentTime
|
time <- currentTime
|
||||||
passwordHash <- putStr "Password: " >> liftIO (withoutInputEcho getLine) >>= hashPassword
|
passwordHash <- putStr "Password: " >> liftIO (withoutInputEcho getLine) >>= hashPassword
|
||||||
userID <- addUser $ DBUser
|
userID <- addUser $ DBUser
|
||||||
|
@ -47,10 +51,15 @@ main = do
|
||||||
_ <- addEmail $ DBEmail
|
_ <- addEmail $ DBEmail
|
||||||
{ dBEmailUid = toDBKey userID
|
{ dBEmailUid = toDBKey userID
|
||||||
, dBEmailEmail = addUserEmail
|
, dBEmailEmail = addUserEmail
|
||||||
, dBEmailVerificationSecret = Nothing
|
, dBEmailVid = Nothing
|
||||||
}
|
}
|
||||||
pure ()
|
pure ()
|
||||||
|
|
||||||
|
gcEmailsMain :: CLIM ()
|
||||||
|
gcEmailsMain = do
|
||||||
|
time <- currentTime
|
||||||
|
deleteExpiredEmails time
|
||||||
|
|
||||||
cliOptions :: Parser CLIOptions
|
cliOptions :: Parser CLIOptions
|
||||||
cliOptions = CLIOptions
|
cliOptions = CLIOptions
|
||||||
<$> strOption (short 'u' <> long "db-url" <> metavar "URL" <> value "postgres:///datarekisteri-backend")
|
<$> strOption (short 'u' <> long "db-url" <> metavar "URL" <> value "postgres:///datarekisteri-backend")
|
||||||
|
@ -59,9 +68,10 @@ cliOptions = CLIOptions
|
||||||
cliCommandParser :: Parser CLISubCommand
|
cliCommandParser :: Parser CLISubCommand
|
||||||
cliCommandParser = hsubparser
|
cliCommandParser = hsubparser
|
||||||
$ command "add-user" (info addUserCommand (progDesc "Add a user to datarekisteri"))
|
$ command "add-user" (info addUserCommand (progDesc "Add a user to datarekisteri"))
|
||||||
|
<> command "gc-expired-emails" (info (pure GCEmails) (progDesc "Delete emails that have not been verified in time."))
|
||||||
|
|
||||||
addUserCommand :: Parser CLISubCommand
|
addUserCommand :: Parser CLISubCommand
|
||||||
addUserCommand = AddUser
|
addUserCommand = fmap AddUser $ AddUserOpts
|
||||||
<$> optional (strOption (long "nickname" <> metavar "NAME"))
|
<$> optional (strOption (long "nickname" <> metavar "NAME"))
|
||||||
<*> strOption (long "name" <> metavar "NAME")
|
<*> strOption (long "name" <> metavar "NAME")
|
||||||
<*> option (maybeReader $ toDate . toText) (long "birthdate" <> metavar "DATE" <> help "The user's birthdate, YYYY-MM-DD")
|
<*> option (maybeReader $ toDate . toText) (long "birthdate" <> metavar "DATE" <> help "The user's birthdate, YYYY-MM-DD")
|
||||||
|
@ -81,7 +91,10 @@ data CLIOptions = CLIOptions
|
||||||
, optionsSubCommand :: CLISubCommand
|
, optionsSubCommand :: CLISubCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
data CLISubCommand = AddUser
|
data CLISubCommand = AddUser AddUserOpts
|
||||||
|
| GCEmails
|
||||||
|
|
||||||
|
data AddUserOpts = AddUserOpts
|
||||||
{ addUserNickname :: Maybe Text
|
{ addUserNickname :: Maybe Text
|
||||||
, addUserName :: Text
|
, addUserName :: Text
|
||||||
, addUserBirthdate :: Date
|
, addUserBirthdate :: Date
|
||||||
|
|
Loading…
Reference in New Issue