2025-12-13 23:35:14 +00:00
|
|
|
import 'package:drift_postgres/drift_postgres.dart';
|
2025-11-16 11:25:27 +00:00
|
|
|
import 'package:injectable/injectable.dart';
|
2025-12-13 13:27:05 +00:00
|
|
|
import 'package:mnemo_cards_backend/database/database.dart';
|
2026-01-09 17:21:18 +00:00
|
|
|
import 'package:mnemo_cards_backend/repository/export.dart';
|
2025-11-16 11:25:27 +00:00
|
|
|
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
|
|
|
|
|
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
2025-12-13 14:48:00 +00:00
|
|
|
import 'package:drift/drift.dart';
|
2025-11-16 11:25:27 +00:00
|
|
|
|
|
|
|
|
@lazySingleton
|
|
|
|
|
class SubscriptionManager {
|
2026-01-09 17:21:18 +00:00
|
|
|
final SubscriptionRepository _subscriptionRepository;
|
2025-11-16 11:25:27 +00:00
|
|
|
|
2026-01-09 17:21:18 +00:00
|
|
|
SubscriptionManager(this._subscriptionRepository);
|
2025-11-16 11:25:27 +00:00
|
|
|
|
2025-12-13 13:27:05 +00:00
|
|
|
Future<SubscriptionDto> getSubscriptionDto(UserModel user) async {
|
2025-12-13 14:48:00 +00:00
|
|
|
if (user.id == null) {
|
|
|
|
|
return SubscriptionDto(
|
|
|
|
|
page: null,
|
|
|
|
|
isActive: false,
|
|
|
|
|
start: null,
|
|
|
|
|
finish: null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 17:21:18 +00:00
|
|
|
final subscription = await _subscriptionRepository.getActiveSubscription(
|
2025-12-20 18:26:15 +00:00
|
|
|
user.id!,
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-16 11:25:27 +00:00
|
|
|
return SubscriptionDto(
|
2025-12-13 13:27:05 +00:00
|
|
|
page: null,
|
2025-12-13 14:48:00 +00:00
|
|
|
isActive: subscription != null,
|
2026-01-09 17:21:18 +00:00
|
|
|
start: subscription?.start,
|
|
|
|
|
finish: subscription?.finish,
|
2025-11-16 11:25:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 18:26:15 +00:00
|
|
|
Future<void> createSubscription(
|
|
|
|
|
UserModel user,
|
|
|
|
|
SubscriptionPlanModel plan,
|
|
|
|
|
) async {
|
2025-12-13 14:48:00 +00:00
|
|
|
if (user.id == null || plan.id == null) {
|
|
|
|
|
throw ArgumentError('User ID and Plan ID are required');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final now = DateTime.now();
|
|
|
|
|
final endDate = now.add(Duration(days: plan.durationDays));
|
|
|
|
|
|
2026-01-09 17:21:18 +00:00
|
|
|
await _subscriptionRepository.createSubscription(
|
2025-12-13 14:48:00 +00:00
|
|
|
UserSubscriptionsCompanion.insert(
|
|
|
|
|
userId: user.id!,
|
2025-12-13 23:35:14 +00:00
|
|
|
start: PgDateTime(now),
|
|
|
|
|
finish: PgDateTime(endDate),
|
2025-12-13 14:48:00 +00:00
|
|
|
features: const Value([]),
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-12-13 13:27:05 +00:00
|
|
|
}
|
2025-11-16 11:25:27 +00:00
|
|
|
|
2025-12-13 13:27:05 +00:00
|
|
|
Future<List<SubscriptionPlanModel>> getAllPlans() async {
|
2026-01-09 17:21:18 +00:00
|
|
|
return await _subscriptionRepository.getAllPlans();
|
2025-12-13 14:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<List<SubscriptionPlanModel>> getAllSubscriptionPlans() async {
|
|
|
|
|
return await getAllPlans();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 20:55:50 +00:00
|
|
|
Future<void> purchaseSubscription(String userId, String planId) async {
|
2026-01-09 17:21:18 +00:00
|
|
|
final plan = await _subscriptionRepository.getPlanById(planId);
|
2025-12-13 14:48:00 +00:00
|
|
|
if (plan == null) {
|
|
|
|
|
throw StateError('Subscription plan not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final now = DateTime.now();
|
|
|
|
|
final endDate = now.add(Duration(days: plan.durationDays));
|
|
|
|
|
|
2026-01-09 17:21:18 +00:00
|
|
|
await _subscriptionRepository.createSubscription(
|
2025-12-13 14:48:00 +00:00
|
|
|
UserSubscriptionsCompanion.insert(
|
|
|
|
|
userId: userId,
|
2025-12-13 23:35:14 +00:00
|
|
|
start: PgDateTime(now),
|
|
|
|
|
finish: PgDateTime(endDate),
|
2026-01-09 17:21:18 +00:00
|
|
|
features: Value(plan.features.map((f) => f.name).toList()),
|
2025-12-13 14:48:00 +00:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 20:55:50 +00:00
|
|
|
Future<void> cancelSubscription(String userId) async {
|
2026-01-09 17:21:18 +00:00
|
|
|
await _subscriptionRepository.cancelSubscription(userId);
|
2025-11-16 11:25:27 +00:00
|
|
|
}
|
2025-12-20 18:26:15 +00:00
|
|
|
}
|