Allow user to change subscription settings

This commit is contained in:
silverpill 2022-09-15 20:18:14 +00:00
parent d50262d1e5
commit 02408ae57a
4 changed files with 34 additions and 33 deletions

View file

@ -687,7 +687,7 @@ paths:
items: items:
$ref: '#/components/schemas/SubscriptionOption' $ref: '#/components/schemas/SubscriptionOption'
post: post:
summary: Enable subscriptions summary: Enable subscriptions or update subscription settings
security: security:
- tokenAuth: [] - tokenAuth: []
requestBody: requestBody:

View file

@ -267,18 +267,7 @@ async fn create_identity_proof(
value: proof_data.signature.clone(), value: proof_data.signature.clone(),
}; };
let mut profile_data = ProfileUpdateData::from(&current_user.profile); let mut profile_data = ProfileUpdateData::from(&current_user.profile);
match profile_data.identity_proofs.iter_mut() profile_data.add_identity_proof(proof);
.find(|item| item.issuer == proof.issuer) {
Some(mut item) => {
// Replace
item.proof_type = proof.proof_type;
item.value = proof.value;
},
None => {
// Add new proof
profile_data.identity_proofs.push(proof);
},
};
current_user.profile = update_profile( current_user.profile = update_profile(
db_client, db_client,
&current_user.id, &current_user.id,

View file

@ -79,7 +79,7 @@ async fn get_subscription_options(
} }
#[post("/options")] #[post("/options")]
pub async fn subscriptions_enabled( pub async fn register_subscription_option(
auth: BearerAuth, auth: BearerAuth,
config: web::Data<Config>, config: web::Data<Config>,
db_pool: web::Data<Pool>, db_pool: web::Data<Pool>,
@ -89,8 +89,7 @@ pub async fn subscriptions_enabled(
let db_client = &**get_database_client(&db_pool).await?; let db_client = &**get_database_client(&db_pool).await?;
let mut current_user = get_current_user(db_client, auth.token()).await?; let mut current_user = get_current_user(db_client, auth.token()).await?;
let mut maybe_payment_option = None; let maybe_payment_option = match subscription_option.into_inner() {
match subscription_option.into_inner() {
SubscriptionOption::Ethereum => { SubscriptionOption::Ethereum => {
let ethereum_config = config.blockchain() let ethereum_config = config.blockchain()
.and_then(|conf| conf.ethereum_config()) .and_then(|conf| conf.ethereum_config())
@ -100,9 +99,12 @@ pub async fn subscriptions_enabled(
let wallet_address = current_user let wallet_address = current_user
.public_wallet_address(&Currency::Ethereum) .public_wallet_address(&Currency::Ethereum)
.ok_or(HttpError::PermissionError)?; .ok_or(HttpError::PermissionError)?;
if !current_user.profile.payment_options if current_user.profile.payment_options
.any(PaymentType::EthereumSubscription) .any(PaymentType::EthereumSubscription)
{ {
// Ignore attempts to update payment option
None
} else {
let is_registered = is_registered_recipient( let is_registered = is_registered_recipient(
contract_set, contract_set,
&wallet_address, &wallet_address,
@ -110,32 +112,26 @@ pub async fn subscriptions_enabled(
if !is_registered { if !is_registered {
return Err(ValidationError("recipient is not registered").into()); return Err(ValidationError("recipient is not registered").into());
}; };
maybe_payment_option = Some(PaymentOption::ethereum_subscription( Some(PaymentOption::ethereum_subscription(
ethereum_config.chain_id.clone(), ethereum_config.chain_id.clone(),
)); ))
}; }
}, },
SubscriptionOption::Monero { price, payout_address } => { SubscriptionOption::Monero { price, payout_address } => {
let monero_config = config.blockchain() let monero_config = config.blockchain()
.and_then(|conf| conf.monero_config()) .and_then(|conf| conf.monero_config())
.ok_or(HttpError::NotSupported)?; .ok_or(HttpError::NotSupported)?;
if !current_user.profile.payment_options let payment_info = MoneroSubscription {
.any(PaymentType::MoneroSubscription) chain_id: monero_config.chain_id.clone(),
{ price,
let payment_info = MoneroSubscription { payout_address,
chain_id: monero_config.chain_id.clone(),
price,
payout_address,
};
maybe_payment_option =
Some(PaymentOption::MoneroSubscription(payment_info));
}; };
Some(PaymentOption::MoneroSubscription(payment_info))
}, },
}; };
if let Some(payment_option) = maybe_payment_option { if let Some(payment_option) = maybe_payment_option {
// Add payment option to profile
let mut profile_data = ProfileUpdateData::from(&current_user.profile); let mut profile_data = ProfileUpdateData::from(&current_user.profile);
profile_data.payment_options.push(payment_option); profile_data.add_payment_option(payment_option);
current_user.profile = update_profile( current_user.profile = update_profile(
db_client, db_client,
&current_user.id, &current_user.id,
@ -214,7 +210,7 @@ pub fn subscription_api_scope() -> Scope {
web::scope("/api/v1/subscriptions") web::scope("/api/v1/subscriptions")
.service(authorize_subscription) .service(authorize_subscription)
.service(get_subscription_options) .service(get_subscription_options)
.service(subscriptions_enabled) .service(register_subscription_option)
.service(find_subscription) .service(find_subscription)
.service(create_invoice_view) .service(create_invoice_view)
.service(get_invoice) .service(get_invoice)

View file

@ -359,6 +359,22 @@ pub struct ProfileUpdateData {
} }
impl ProfileUpdateData { impl ProfileUpdateData {
/// Adds new identity proof
/// or replaces the existing one if it has the same issuer.
pub fn add_identity_proof(&mut self, proof: IdentityProof) -> () {
self.identity_proofs.retain(|item| item.issuer != proof.issuer);
self.identity_proofs.push(proof);
}
/// Adds new payment option
/// or replaces the existing one if it has the same type.
pub fn add_payment_option(&mut self, option: PaymentOption) -> () {
self.payment_options.retain(|item| {
item.payment_type() != option.payment_type()
});
self.payment_options.push(option);
}
pub fn clean(&mut self) -> Result<(), ValidationError> { pub fn clean(&mut self) -> Result<(), ValidationError> {
if let Some(display_name) = &self.display_name { if let Some(display_name) = &self.display_name {
validate_display_name(display_name)?; validate_display_name(display_name)?;