initializeSubscriptions method

  1. @override
Future<void> initializeSubscriptions()
override

Implementation

@override
Future<void> initializeSubscriptions() async {
  AppConfig.logger.t("Initializing Subscriptions");
  _subscriptionPlans = await SubscriptionPlanFirestore().getAll();
  if(_subscriptionPlans.isNotEmpty) {
    // Filter by environment: debug=test plans only, release=live plans only.
    // Role does NOT affect which environment is used — admins in debug
    // should still see test plans to avoid hitting the live Stripe API.
    final isLiveMode = !kDebugMode;
    _subscriptionPlans.removeWhere((_, plan) => plan.isLive != isLiveMode);

    final keysToRemove = <String>[];

    for(final entry in _subscriptionPlans.entries) {
      final plan = entry.value;
      StripePrice? stripePrice = await Sint.find<StripeApiService>().getPrice(plan.priceId);
      if(stripePrice != null) {
        plan.price = Price.fromStripe(stripePrice);
      } else {
        // Price could not be fetched — plan belongs to the other Stripe
        // environment (test vs live). Remove it so only valid plans show.
        AppConfig.logger.w('Plan "${entry.key}" skipped: priceId ${plan.priceId} not found in current Stripe environment');
        keysToRemove.add(entry.key);
        continue;
      }

      // Optional yearly price
      if (plan.priceIdYearly.isNotEmpty) {
        try {
          final yearlyStripe = await Sint.find<StripeApiService>().getPrice(plan.priceIdYearly);
          if (yearlyStripe != null) {
            plan.priceYearly = Price.fromStripe(yearlyStripe);
          }
        } catch (e) {
          AppConfig.logger.w('Yearly price not available for plan "${entry.key}": $e');
        }
      }
    }

    for (final key in keysToRemove) {
      _subscriptionPlans.remove(key);
    }

    // Enrich founder plans with remaining seats from Firestore counters
    await _loadFounderCounters();

    if(_subscriptionPlans.isNotEmpty) {
      setProfileTypePlans();
    }
  }

  await setActiveSubscriptions();
  _isLoading.value = false;
}