create empty balance entry if none exists
This commit is contained in:
parent
891fb7b0b3
commit
2f78e64c5d
@ -22,7 +22,6 @@ use reqwest::header::ToStrError;
|
|||||||
use rust_decimal::Error as DecimalError;
|
use rust_decimal::Error as DecimalError;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::value::RawValue;
|
use serde_json::value::RawValue;
|
||||||
use std::error::Error;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{borrow::Cow, net::IpAddr};
|
use std::{borrow::Cow, net::IpAddr};
|
||||||
use tokio::{sync::AcquireError, task::JoinError, time::Instant};
|
use tokio::{sync::AcquireError, task::JoinError, time::Instant};
|
||||||
|
@ -24,7 +24,8 @@ use http::HeaderValue;
|
|||||||
use ipnet::IpNet;
|
use ipnet::IpNet;
|
||||||
use log::{error, trace, warn};
|
use log::{error, trace, warn};
|
||||||
use migration::sea_orm::prelude::Decimal;
|
use migration::sea_orm::prelude::Decimal;
|
||||||
use migration::sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
|
use migration::sea_orm::{self, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
|
||||||
|
use migration::OnConflict;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use rdkafka::message::{Header as KafkaHeader, OwnedHeaders as KafkaOwnedHeaders, OwnedMessage};
|
use rdkafka::message::{Header as KafkaHeader, OwnedHeaders as KafkaOwnedHeaders, OwnedMessage};
|
||||||
use rdkafka::producer::{FutureProducer, FutureRecord};
|
use rdkafka::producer::{FutureProducer, FutureRecord};
|
||||||
@ -72,7 +73,9 @@ pub enum AuthorizationType {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct Balance {
|
pub struct Balance {
|
||||||
|
/// The total USD value deposited.
|
||||||
pub total_deposit: Decimal,
|
pub total_deposit: Decimal,
|
||||||
|
/// The amount spent outside the free tier.
|
||||||
pub total_spend: Decimal,
|
pub total_spend: Decimal,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1187,21 +1190,41 @@ impl Web3ProxyApp {
|
|||||||
.try_get_with(x, async move {
|
.try_get_with(x, async move {
|
||||||
let db_replica = self
|
let db_replica = self
|
||||||
.db_replica()
|
.db_replica()
|
||||||
.web3_context("Getting database connection")?;
|
.web3_context("Getting database replica connection")?;
|
||||||
|
|
||||||
let balance = match balance::Entity::find()
|
loop {
|
||||||
.filter(balance::Column::UserId.eq(user_id))
|
match balance::Entity::find()
|
||||||
.one(db_replica.as_ref())
|
.filter(balance::Column::UserId.eq(user_id))
|
||||||
.await?
|
.one(db_replica.as_ref())
|
||||||
{
|
.await?
|
||||||
Some(x) => Balance {
|
{
|
||||||
total_deposit: x.total_deposits,
|
Some(x) => {
|
||||||
total_spend: x.total_spent_outside_free_tier,
|
let x = Balance {
|
||||||
},
|
total_deposit: x.total_deposits,
|
||||||
None => Default::default(),
|
total_spend: x.total_spent_outside_free_tier,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Arc::new(RwLock::new(balance)))
|
return Ok(Arc::new(RwLock::new(x)));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// no balance row. make one now
|
||||||
|
let db_conn =
|
||||||
|
self.db_conn().web3_context("Getting database connection")?;
|
||||||
|
|
||||||
|
let balance_entry = balance::ActiveModel {
|
||||||
|
user_id: sea_orm::Set(user_id),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
balance::Entity::insert(balance_entry)
|
||||||
|
.on_conflict(OnConflict::new().to_owned())
|
||||||
|
.exec(&db_conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into),
|
.map_err(Into::into),
|
||||||
|
Loading…
Reference in New Issue
Block a user