getOrgSubscription method

Future<SubscriptionModel?> getOrgSubscription(
  1. String orgId
)

Fetch the active subscription for an organization (Stripe-sourced).

Returns null if the org is on trial or has no Stripe subscription yet.

Implementation

Future<SubscriptionModel?> getOrgSubscription(String orgId) async {
  try {
    final response = await _client
        .from('subscriptions')
        .select()
        .eq('target_org_id', orgId)
        .inFilter('status', ['active', 'past_due'])
        .order('current_period_end', ascending: false)
        .limit(1)
        .maybeSingle();
    if (response == null) return null;
    return SubscriptionModel.fromJson(response);
  } catch (e) {
    dev.log('getOrgSubscription failed: $e', name: 'BillingDatasource');
    return null;
  }
}