Skip to content

Commit

Permalink
Introduce a new /billing/monthly_spend API (#19354)
Browse files Browse the repository at this point in the history
Fixes #19353

Release Notes:

- N/A
  • Loading branch information
as-cii authored and osiewicz committed Oct 18, 2024
1 parent 41e8aa4 commit 9a8ccd5
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions crates/collab/src/api/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn router() -> Router {
"/billing/subscriptions/manage",
post(manage_billing_subscription),
)
.route("/billing/monthly_spend", get(get_monthly_spend))
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -672,6 +673,42 @@ async fn handle_customer_subscription_event(
Ok(())
}

#[derive(Debug, Deserialize)]
struct GetMonthlySpendParams {
github_user_id: i32,
}

#[derive(Debug, Serialize)]
struct GetMonthlySpendResponse {
monthly_spend_in_cents: i32,
}

async fn get_monthly_spend(
Extension(app): Extension<Arc<AppState>>,
Query(params): Query<GetMonthlySpendParams>,
) -> Result<Json<GetMonthlySpendResponse>> {
let user = app
.db
.get_user_by_github_user_id(params.github_user_id)
.await?
.ok_or_else(|| anyhow!("user not found"))?;

let Some(llm_db) = app.llm_db.clone() else {
return Err(Error::http(
StatusCode::NOT_IMPLEMENTED,
"LLM database not available".into(),
));
};

let monthly_spend = llm_db
.get_user_spending_for_month(user.id, Utc::now())
.await?;

Ok(Json(GetMonthlySpendResponse {
monthly_spend_in_cents: monthly_spend.0 as i32,
}))
}

impl From<SubscriptionStatus> for StripeSubscriptionStatus {
fn from(value: SubscriptionStatus) -> Self {
match value {
Expand Down

0 comments on commit 9a8ccd5

Please sign in to comment.