create empty balance entry if none exists

This commit is contained in:
Bryan Stitt 2023-06-18 16:25:25 -07:00
parent 891fb7b0b3
commit 2f78e64c5d
2 changed files with 37 additions and 15 deletions

@ -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 {
match balance::Entity::find()
.filter(balance::Column::UserId.eq(user_id)) .filter(balance::Column::UserId.eq(user_id))
.one(db_replica.as_ref()) .one(db_replica.as_ref())
.await? .await?
{ {
Some(x) => Balance { Some(x) => {
let x = Balance {
total_deposit: x.total_deposits, total_deposit: x.total_deposits,
total_spend: x.total_spent_outside_free_tier, total_spend: x.total_spent_outside_free_tier,
},
None => Default::default(),
}; };
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),