Skip to content

Commit

Permalink
Add a CLI tool to add an email address to a user
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Sep 22, 2024
1 parent 87f3452 commit d5ce0d5
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions crates/cli/src/commands/manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub(super) struct Options {

#[derive(Parser, Debug)]
enum Subcommand {
/// Add an email address to the specified user
AddEmail { username: String, email: String },

/// Mark email address as verified
VerifyEmail { username: String, email: String },

Expand Down Expand Up @@ -219,6 +222,49 @@ impl Options {
Ok(ExitCode::SUCCESS)
}

SC::AddEmail { username, email } => {
let _span = info_span!(
"cli.manage.add_email",
user.username = username,
user_email.email = email
)
.entered();

let database_config = DatabaseConfig::extract_or_default(figment)?;
let mut conn = database_connection_from_config(&database_config).await?;
let txn = conn.begin().await?;
let mut repo = PgRepository::from_conn(txn);

let user = repo
.user()
.find_by_username(&username)
.await?
.context("User not found")?;

// Find any existing email address
let existing_email = repo.user_email().find(&user, &email).await?;
let email = if let Some(email) = existing_email {
info!(%email.id, "Email already exists, makring as verified");
email
} else {
repo.user_email()
.add(&mut rng, &clock, &user, email)
.await?
};

let email = repo.user_email().mark_as_verified(&clock, email).await?;

// If the user has no primary email, set this one as primary.
if user.primary_user_email_id.is_none() {
repo.user_email().set_as_primary(&email).await?;
}

repo.into_inner().commit().await?;
info!(?email, "Email added and marked as verified");

Ok(ExitCode::SUCCESS)
}

SC::VerifyEmail { username, email } => {
let _span = info_span!(
"cli.manage.verify_email",
Expand Down

0 comments on commit d5ce0d5

Please sign in to comment.