Files
worker/lib/features/home/domain/usecases/get_promotions.dart
Phuoc Nguyen 57bf73e4d1 update
2025-10-17 17:49:01 +07:00

85 lines
2.5 KiB
Dart

/// Use Case: Get Promotions
///
/// Retrieves the list of active promotional offers.
/// This use case encapsulates the business logic for fetching promotions.
///
/// Follows the Single Responsibility Principle - one use case, one operation.
library;
import 'package:worker/features/home/domain/entities/promotion.dart';
import 'package:worker/features/home/domain/repositories/home_repository.dart';
/// Get Promotions Use Case
///
/// Fetches active promotional offers to display on home screen.
///
/// Usage:
/// ```dart
/// final promotions = await getPromotions();
/// ```
///
/// This use case:
/// - Retrieves promotions from repository (which handles caching)
/// - Filters only active promotions
/// - Sorts by priority/date
/// - Returns list of Promotion entities
class GetPromotions {
/// Home repository instance
final HomeRepository repository;
/// Constructor
const GetPromotions(this.repository);
/// Execute the use case
///
/// Returns list of active [Promotion] objects.
/// Returns empty list if no promotions available.
///
/// Throws:
/// - [NetworkException] if network error occurs
/// - [CacheException] if local data corrupted
Future<List<Promotion>> call() async {
// Fetch promotions from repository
final promotions = await repository.getPromotions();
// TODO: Add business logic here if needed
// For example:
// - Filter only active promotions
// - Sort by priority or start date
// - Filter by user tier (Diamond exclusive promotions)
// - Add personalization logic
// - Log analytics events
// Filter only active promotions
final activePromotions = promotions
.where((promotion) => promotion.isActive)
.toList();
// Sort by start date (newest first)
activePromotions.sort((a, b) => b.startDate.compareTo(a.startDate));
return activePromotions;
}
/// Execute with force refresh
///
/// Forces a refresh from the server instead of using cached data.
///
/// Use this when:
/// - User explicitly pulls to refresh
/// - Cached data is known to be stale
/// - Need to show latest promotions immediately
Future<List<Promotion>> refresh() async {
final promotions = await repository.refreshPromotions();
// Apply same filtering logic
final activePromotions = promotions
.where((promotion) => promotion.isActive)
.toList();
activePromotions.sort((a, b) => b.startDate.compareTo(a.startDate));
return activePromotions;
}
}