@@ -13,8 +13,8 @@ use crate::{
1313 create_stripe_plan_query, create_stripe_setup_checkout_session,
1414 create_stripe_subscription_query, create_usage_based_stripe_payment_link,
1515 create_usage_stripe_subscription_query, delete_subscription_by_id_query,
16- get_all_plans_query , get_all_usage_plans_query , get_invoices_for_org_query ,
17- get_option_subscription_by_organization_id_query,
16+ delete_usage_subscription_by_id_query , get_all_plans_query , get_all_usage_plans_query ,
17+ get_invoices_for_org_query , get_option_subscription_by_organization_id_query,
1818 get_option_usage_based_subscription_by_organization_id_query,
1919 get_option_usage_based_subscription_by_subscription_id_query, get_plan_by_id_query,
2020 get_stripe_client, get_trieve_plan_by_id_query, get_trieve_subscription_by_id_query,
@@ -153,6 +153,21 @@ pub async fn webhook(
153153
154154 if plan_type == "usage-based" {
155155 log:: info!( "Creating usage based stripe subscription" ) ;
156+ let optional_existing_subscription =
157+ get_option_usage_based_subscription_by_organization_id_query (
158+ organization_id,
159+ optional_subscription_pool,
160+ )
161+ . await ?;
162+
163+ if let Some ( existing_subscription) = optional_existing_subscription
164+ {
165+ delete_usage_subscription_by_id_query (
166+ existing_subscription. id ,
167+ pool. clone ( ) ,
168+ )
169+ . await ?;
170+ }
156171 // record current usage
157172 let usage =
158173 get_org_usage_by_id_query ( organization_id, pool. clone ( ) )
@@ -331,21 +346,20 @@ pub async fn direct_to_payment_link(
331346 query_params : web:: Query < CreateDirectPaymentLinkQueryParams > ,
332347 pool : web:: Data < Pool > ,
333348) -> Result < HttpResponse , actix_web:: Error > {
334- let organization_pool = pool. clone ( ) ;
335- let subscription_pool = pool. clone ( ) ;
336349 let subscription_org_id = path_data. organization_id ;
337350
338351 let current_subscription =
339- get_option_subscription_by_organization_id_query ( subscription_org_id, subscription_pool)
340- . await ?;
352+ get_trieve_subscription_by_id_query ( subscription_org_id, pool. clone ( ) )
353+ . await
354+ . ok ( ) ;
341355
342- if current_subscription. is_some_and ( |s| s. current_period_end . is_none ( ) ) {
356+ if current_subscription. is_some_and ( |s| s. current_period_end ( ) . is_none ( ) ) {
343357 return Ok ( HttpResponse :: Conflict ( ) . finish ( ) ) ;
344358 }
345359
346360 let organization_id = path_data. organization_id ;
347361 let organization_id_clone = path_data. organization_id ;
348- let _org_plan_sub = get_org_from_id_query ( organization_id_clone, organization_pool ) . await ?;
362+ let _org_plan_sub = get_org_from_id_query ( organization_id_clone, pool . clone ( ) ) . await ?;
349363 let plan_id = path_data. plan_id ;
350364
351365 let payment_link = match query_params. usage_based {
@@ -440,13 +454,13 @@ pub async fn update_subscription_plan(
440454 let trieve_subscription =
441455 get_trieve_subscription_by_id_query ( current_subscription_id, pool. clone ( ) ) . await ?;
442456
443- let trieve_plan = get_trieve_plan_by_id_query ( new_plan_id, pool. clone ( ) ) . await ?;
457+ let new_trieve_plan = get_trieve_plan_by_id_query ( new_plan_id, pool. clone ( ) ) . await ?;
444458
445459 if !verify_owner ( & user, & trieve_subscription. organization_id ( ) ) {
446460 return Err ( ServiceError :: Forbidden . into ( ) ) ;
447461 } ;
448462
449- match trieve_plan {
463+ match new_trieve_plan {
450464 TrievePlan :: Flat ( flat_plan) => {
451465 update_to_flat_stripe_subscription (
452466 trieve_subscription. stripe_subscription_id ( ) ,
0 commit comments