backend sliced by modules

This commit is contained in:
2026-05-02 18:24:57 +07:00
parent b6306d5aa9
commit 693e48af59
42 changed files with 1602 additions and 779 deletions
+71
View File
@@ -0,0 +1,71 @@
use crate::{
db::{models::Subscription, repository::AppRepository},
error::AppError,
};
use std::sync::Arc;
use tracing::info;
use uuid::Uuid;
#[derive(Clone)]
pub struct BillingService {
repo: Arc<dyn AppRepository>,
}
impl BillingService {
pub fn new(repo: Arc<dyn AppRepository>) -> Self {
Self { repo }
}
pub async fn get_subscription(&self, user_id: Uuid) -> Result<Option<Subscription>, AppError> {
self.repo
.get_subscription(user_id)
.await
.map_err(AppError::Database)
}
pub async fn buy_subscription(&self, tg_id: i64, plan_name: &str) -> Result<(), AppError> {
info!(
"Processing mock subscription for TG: {}, Plan: {}",
tg_id, plan_name
);
let price_usd = self.repo.get_plan_price_usd(plan_name).await?.unwrap_or(0);
let user = self
.repo
.get_user_by_tg_id(tg_id)
.await?
.ok_or(AppError::UserNotFound)?;
self.repo
.buy_subscription(user.id, plan_name)
.await
.map_err(AppError::Database)?;
let revshare_bonus = (price_usd as i64) * 10;
if revshare_bonus > 0 {
let _ = self.repo.reward_referrer(user.id, revshare_bonus).await;
}
Ok(())
}
pub async fn activate_trial(&self, tg_id: i64) -> Result<(), AppError> {
let user = self
.repo
.get_user_by_tg_id(tg_id)
.await?
.ok_or(AppError::UserNotFound)?;
if user.has_used_trial {
return Err(AppError::TrialAlreadyUsed);
}
self.repo
.activate_trial(user.id)
.await
.map_err(AppError::Database)
}
pub async fn reset_expired_subscriptions(&self) -> Result<u64, AppError> {
self.repo
.reset_expired_subscriptions()
.await
.map_err(AppError::Database)
}
}