add change_user_tier_by_address subcommand

This commit is contained in:
Bryan Stitt 2022-12-28 08:43:44 -08:00
parent 3b1d6574e3
commit 84517ed5a5
2 changed files with 70 additions and 1 deletions

@ -0,0 +1,62 @@
use anyhow::Context;
use argh::FromArgs;
use entities::{rpc_key, user, user_tier};
use log::{debug, info};
use migration::sea_orm::{
self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel,
QueryFilter,
};
use uuid::Uuid;
use ethers::types::Address;
/// change a user's tier.
#[derive(FromArgs, PartialEq, Eq, Debug)]
#[argh(subcommand, name = "change_user_tier_by_address")]
pub struct ChangeUserTierByAddressCommand {
#[argh(positional)]
/// the address of the user you want to change.
user_address: Address,
/// the title of the desired user tier.
#[argh(positional)]
user_tier_title: String,
}
impl ChangeUserTierByAddressCommand {
pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> {
let address: Vec<u8> = self.user_address.to_fixed_bytes().into();
// use the address to get the user
let user = user::Entity::find()
.filter(user::Column::Address.eq(address))
.one(db_conn)
.await?
.context("No user found with that key")?;
// TODO: don't serialize the rpc key
debug!("user: {:#?}", user);
// use the title to get the user tier
let user_tier = user_tier::Entity::find()
.filter(user_tier::Column::Title.eq(self.user_tier_title))
.one(db_conn)
.await?
.context("No user tier found with that name")?;
debug!("user_tier: {:#?}", user_tier);
if user.user_tier_id == user_tier.id {
info!("user already has that tier");
} else {
let mut user = user.into_active_model();
user.user_tier_id = sea_orm::Set(user_tier.id);
user.save(db_conn).await?;
info!("user's tier changed");
}
Ok(())
}
}

@ -1,6 +1,7 @@
mod change_user_address;
mod change_user_address_by_key;
mod change_user_tier;
mod change_user_tier_by_address;
mod change_user_tier_by_key;
mod check_config;
mod cost_calculator;
@ -43,9 +44,10 @@ enum SubCommand {
ChangeUserAddress(change_user_address::ChangeUserAddressCommand),
ChangeUserAddressByKey(change_user_address_by_key::ChangeUserAddressByKeyCommand),
ChangeUserTier(change_user_tier::ChangeUserTierCommand),
ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressCommand),
ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand),
CostCalculatorCommand(cost_calculator::CostCalculatorCommand),
CheckConfig(check_config::CheckConfigSubCommand),
CostCalculatorCommand(cost_calculator::CostCalculatorCommand),
CreateUser(create_user::CreateUserSubCommand),
DropMigrationLock(drop_migration_lock::DropMigrationLockSubCommand),
HealthCompass(health_compass::HealthCompassSubCommand),
@ -101,6 +103,11 @@ async fn main() -> anyhow::Result<()> {
x.main(&db_conn).await
}
SubCommand::ChangeUserTierByAddress(x) => {
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
x.main(&db_conn).await
}
SubCommand::ChangeUserTierByKey(x) => {
let db_conn = get_db(cli_config.db_url, 1, 1).await?;