Files
unionflow-server-api/unionflow-mobile-apps/lib/features/notifications/domain/usecases/obtenir_notifications_usecase.dart
2025-09-17 17:54:06 +00:00

275 lines
8.2 KiB
Dart

import 'package:dartz/dartz.dart';
import '../../../../core/error/failures.dart';
import '../../../../core/usecases/usecase.dart';
import '../entities/notification.dart';
import '../repositories/notifications_repository.dart';
/// Use case pour obtenir les notifications d'un utilisateur
class ObtenirNotificationsUseCase implements UseCase<List<NotificationEntity>, ObtenirNotificationsParams> {
final NotificationsRepository repository;
ObtenirNotificationsUseCase(this.repository);
@override
Future<Either<Failure, List<NotificationEntity>>> call(ObtenirNotificationsParams params) async {
// Vérification du cache en premier
final cacheValide = await repository.isCacheValide(
params.utilisateurId,
maxAgeMinutes: params.maxAgeCacheMinutes,
);
if (!cacheValide || params.forceRefresh) {
// Synchronisation avec le serveur si nécessaire
final syncResult = await repository.synchroniser(
params.utilisateurId,
forceSync: params.forceRefresh,
);
// On continue même si la sync échoue (mode offline)
if (syncResult.isLeft()) {
// Log de l'erreur mais on continue avec les données en cache
print('Erreur de synchronisation: ${syncResult.fold((l) => l.toString(), (r) => '')}');
}
}
// Récupération des notifications
return await repository.obtenirNotifications(
utilisateurId: params.utilisateurId,
includeArchivees: params.includeArchivees,
limite: params.limite,
offset: params.offset,
);
}
}
/// Paramètres pour obtenir les notifications
class ObtenirNotificationsParams {
final String utilisateurId;
final bool includeArchivees;
final int limite;
final int offset;
final bool forceRefresh;
final int maxAgeCacheMinutes;
const ObtenirNotificationsParams({
required this.utilisateurId,
this.includeArchivees = false,
this.limite = 50,
this.offset = 0,
this.forceRefresh = false,
this.maxAgeCacheMinutes = 5,
});
ObtenirNotificationsParams copyWith({
String? utilisateurId,
bool? includeArchivees,
int? limite,
int? offset,
bool? forceRefresh,
int? maxAgeCacheMinutes,
}) {
return ObtenirNotificationsParams(
utilisateurId: utilisateurId ?? this.utilisateurId,
includeArchivees: includeArchivees ?? this.includeArchivees,
limite: limite ?? this.limite,
offset: offset ?? this.offset,
forceRefresh: forceRefresh ?? this.forceRefresh,
maxAgeCacheMinutes: maxAgeCacheMinutes ?? this.maxAgeCacheMinutes,
);
}
@override
String toString() {
return 'ObtenirNotificationsParams{utilisateurId: $utilisateurId, includeArchivees: $includeArchivees, limite: $limite, offset: $offset, forceRefresh: $forceRefresh}';
}
}
/// Use case pour obtenir les notifications non lues
class ObtenirNotificationsNonLuesUseCase implements UseCase<List<NotificationEntity>, String> {
final NotificationsRepository repository;
ObtenirNotificationsNonLuesUseCase(this.repository);
@override
Future<Either<Failure, List<NotificationEntity>>> call(String utilisateurId) async {
return await repository.obtenirNotificationsNonLues(utilisateurId);
}
}
/// Use case pour obtenir le nombre de notifications non lues
class ObtenirNombreNonLuesUseCase implements UseCase<int, String> {
final NotificationsRepository repository;
ObtenirNombreNonLuesUseCase(this.repository);
@override
Future<Either<Failure, int>> call(String utilisateurId) async {
return await repository.obtenirNombreNonLues(utilisateurId);
}
}
/// Use case pour rechercher des notifications
class RechercherNotificationsUseCase implements UseCase<List<NotificationEntity>, RechercherNotificationsParams> {
final NotificationsRepository repository;
RechercherNotificationsUseCase(this.repository);
@override
Future<Either<Failure, List<NotificationEntity>>> call(RechercherNotificationsParams params) async {
return await repository.rechercherNotifications(
utilisateurId: params.utilisateurId,
query: params.query,
types: params.types,
statuts: params.statuts,
dateDebut: params.dateDebut,
dateFin: params.dateFin,
limite: params.limite,
);
}
}
/// Paramètres pour la recherche de notifications
class RechercherNotificationsParams {
final String utilisateurId;
final String? query;
final List<TypeNotification>? types;
final List<StatutNotification>? statuts;
final DateTime? dateDebut;
final DateTime? dateFin;
final int limite;
const RechercherNotificationsParams({
required this.utilisateurId,
this.query,
this.types,
this.statuts,
this.dateDebut,
this.dateFin,
this.limite = 50,
});
RechercherNotificationsParams copyWith({
String? utilisateurId,
String? query,
List<TypeNotification>? types,
List<StatutNotification>? statuts,
DateTime? dateDebut,
DateTime? dateFin,
int? limite,
}) {
return RechercherNotificationsParams(
utilisateurId: utilisateurId ?? this.utilisateurId,
query: query ?? this.query,
types: types ?? this.types,
statuts: statuts ?? this.statuts,
dateDebut: dateDebut ?? this.dateDebut,
dateFin: dateFin ?? this.dateFin,
limite: limite ?? this.limite,
);
}
@override
String toString() {
return 'RechercherNotificationsParams{utilisateurId: $utilisateurId, query: $query, types: $types, statuts: $statuts, dateDebut: $dateDebut, dateFin: $dateFin, limite: $limite}';
}
}
/// Use case pour obtenir les notifications par type
class ObtenirNotificationsParTypeUseCase implements UseCase<List<NotificationEntity>, ObtenirNotificationsParTypeParams> {
final NotificationsRepository repository;
ObtenirNotificationsParTypeUseCase(this.repository);
@override
Future<Either<Failure, List<NotificationEntity>>> call(ObtenirNotificationsParTypeParams params) async {
return await repository.obtenirNotificationsParType(
params.utilisateurId,
params.type,
limite: params.limite,
);
}
}
/// Paramètres pour obtenir les notifications par type
class ObtenirNotificationsParTypeParams {
final String utilisateurId;
final TypeNotification type;
final int limite;
const ObtenirNotificationsParTypeParams({
required this.utilisateurId,
required this.type,
this.limite = 50,
});
ObtenirNotificationsParTypeParams copyWith({
String? utilisateurId,
TypeNotification? type,
int? limite,
}) {
return ObtenirNotificationsParTypeParams(
utilisateurId: utilisateurId ?? this.utilisateurId,
type: type ?? this.type,
limite: limite ?? this.limite,
);
}
@override
String toString() {
return 'ObtenirNotificationsParTypeParams{utilisateurId: $utilisateurId, type: $type, limite: $limite}';
}
}
/// Use case pour obtenir les notifications importantes
class ObtenirNotificationsImportantesUseCase implements UseCase<List<NotificationEntity>, String> {
final NotificationsRepository repository;
ObtenirNotificationsImportantesUseCase(this.repository);
@override
Future<Either<Failure, List<NotificationEntity>>> call(String utilisateurId) async {
return await repository.obtenirNotificationsImportantes(utilisateurId);
}
}
/// Use case pour obtenir les statistiques des notifications
class ObtenirStatistiquesNotificationsUseCase implements UseCase<Map<String, dynamic>, ObtenirStatistiquesParams> {
final NotificationsRepository repository;
ObtenirStatistiquesNotificationsUseCase(this.repository);
@override
Future<Either<Failure, Map<String, dynamic>>> call(ObtenirStatistiquesParams params) async {
return await repository.obtenirStatistiques(
params.utilisateurId,
periode: params.periode,
);
}
}
/// Paramètres pour obtenir les statistiques
class ObtenirStatistiquesParams {
final String utilisateurId;
final int periode;
const ObtenirStatistiquesParams({
required this.utilisateurId,
this.periode = 30,
});
ObtenirStatistiquesParams copyWith({
String? utilisateurId,
int? periode,
}) {
return ObtenirStatistiquesParams(
utilisateurId: utilisateurId ?? this.utilisateurId,
periode: periode ?? this.periode,
);
}
@override
String toString() {
return 'ObtenirStatistiquesParams{utilisateurId: $utilisateurId, periode: $periode}';
}
}