Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Admin Panel / Deploy Admin Panel (push) Waiting to run
Deploy Admin Panel / Admin Panel Verification (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
88 lines
2.5 KiB
Dart
88 lines
2.5 KiB
Dart
import 'package:drift_postgres/drift_postgres.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:mnemo_cards_backend/database/database.dart';
|
|
import 'package:mnemo_cards_backend/repository/export.dart';
|
|
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
|
|
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
|
import 'package:drift/drift.dart';
|
|
|
|
@lazySingleton
|
|
class SubscriptionManager {
|
|
final SubscriptionRepository _subscriptionRepository;
|
|
|
|
SubscriptionManager(this._subscriptionRepository);
|
|
|
|
Future<SubscriptionDto> getSubscriptionDto(UserModel user) async {
|
|
if (user.id == null) {
|
|
return SubscriptionDto(
|
|
page: null,
|
|
isActive: false,
|
|
start: null,
|
|
finish: null,
|
|
);
|
|
}
|
|
|
|
final subscription = await _subscriptionRepository.getActiveSubscription(
|
|
user.id!,
|
|
);
|
|
|
|
return SubscriptionDto(
|
|
page: null,
|
|
isActive: subscription != null,
|
|
start: subscription?.start,
|
|
finish: subscription?.finish,
|
|
);
|
|
}
|
|
|
|
Future<void> createSubscription(
|
|
UserModel user,
|
|
SubscriptionPlanModel plan,
|
|
) async {
|
|
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));
|
|
|
|
await _subscriptionRepository.createSubscription(
|
|
UserSubscriptionsCompanion.insert(
|
|
userId: user.id!,
|
|
start: PgDateTime(now),
|
|
finish: PgDateTime(endDate),
|
|
features: const Value([]),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<List<SubscriptionPlanModel>> getAllPlans() async {
|
|
return await _subscriptionRepository.getAllPlans();
|
|
}
|
|
|
|
Future<List<SubscriptionPlanModel>> getAllSubscriptionPlans() async {
|
|
return await getAllPlans();
|
|
}
|
|
|
|
Future<void> purchaseSubscription(String userId, String planId) async {
|
|
final plan = await _subscriptionRepository.getPlanById(planId);
|
|
if (plan == null) {
|
|
throw StateError('Subscription plan not found');
|
|
}
|
|
|
|
final now = DateTime.now();
|
|
final endDate = now.add(Duration(days: plan.durationDays));
|
|
|
|
await _subscriptionRepository.createSubscription(
|
|
UserSubscriptionsCompanion.insert(
|
|
userId: userId,
|
|
start: PgDateTime(now),
|
|
finish: PgDateTime(endDate),
|
|
features: Value(plan.features.map((f) => f.name).toList()),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> cancelSubscription(String userId) async {
|
|
await _subscriptionRepository.cancelSubscription(userId);
|
|
}
|
|
}
|