Auth: - profile_repository.dart: /api/auth/change-password → /api/membres/auth/change-password Multi-org (Phase 3): - OrgSelectorPage, OrgSwitcherBloc, OrgSwitcherEntry - org_context_service.dart: headers X-Active-Organisation-Id + X-Active-Role Navigation: - MorePage: navigation conditionnelle par typeOrganisation - Suppression adaptive_navigation (remplacé par main_navigation_layout) Auth AppAuth: - keycloak_webview_auth_service: fixes AppAuth Android - AuthBloc: gestion REAUTH_REQUIS + premierLoginComplet Onboarding: - Nouveaux états: payment_method_page, onboarding_shared_widgets - SouscriptionStatusModel mis à jour StatutValidationSouscription Android: - build.gradle: ProGuard/R8, network_security_config - Gradle wrapper mis à jour
126 lines
4.2 KiB
Dart
126 lines
4.2 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import '../../../../core/config/environment.dart';
|
|
import '../../../../core/utils/logger.dart';
|
|
import '../../../../features/authentication/data/datasources/keycloak_auth_service.dart';
|
|
import '../models/formule_model.dart';
|
|
import '../models/souscription_status_model.dart';
|
|
|
|
@lazySingleton
|
|
class SouscriptionDatasource {
|
|
final KeycloakAuthService _authService;
|
|
final Dio _dio = Dio();
|
|
|
|
SouscriptionDatasource(this._authService);
|
|
|
|
Future<Options> _authOptions() async {
|
|
final token = await _authService.getValidToken();
|
|
return Options(
|
|
headers: {'Authorization': 'Bearer $token'},
|
|
validateStatus: (s) => s != null && s < 500,
|
|
);
|
|
}
|
|
|
|
String get _base => AppConfig.apiBaseUrl;
|
|
|
|
/// Liste toutes les formules disponibles (public)
|
|
Future<List<FormuleModel>> getFormules() async {
|
|
try {
|
|
final opts = await _authOptions();
|
|
final response = await _dio.get('$_base/api/souscriptions/formules', options: opts);
|
|
if (response.statusCode == 200 && response.data is List) {
|
|
return (response.data as List)
|
|
.map((e) => FormuleModel.fromJson(e as Map))
|
|
.toList();
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('SouscriptionDatasource.getFormules: $e');
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/// Récupère la souscription courante de l'OrgAdmin connecté
|
|
Future<SouscriptionStatusModel?> getMaSouscription() async {
|
|
try {
|
|
final opts = await _authOptions();
|
|
final response = await _dio.get('$_base/api/souscriptions/ma-souscription', options: opts);
|
|
if (response.statusCode == 200 && response.data is Map) {
|
|
return SouscriptionStatusModel.fromJson(response.data as Map);
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('SouscriptionDatasource.getMaSouscription: $e');
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Crée une demande de souscription
|
|
Future<SouscriptionStatusModel?> creerDemande({
|
|
required String typeFormule,
|
|
required String plageMembres,
|
|
required String typePeriode,
|
|
String? typeOrganisation,
|
|
required String organisationId,
|
|
}) async {
|
|
try {
|
|
final opts = await _authOptions();
|
|
final body = <String, dynamic>{
|
|
'typeFormule': typeFormule,
|
|
'plageMembres': plageMembres,
|
|
'typePeriode': typePeriode,
|
|
'organisationId': organisationId,
|
|
};
|
|
if (typeOrganisation != null && typeOrganisation.isNotEmpty) {
|
|
body['typeOrganisation'] = typeOrganisation;
|
|
}
|
|
final response = await _dio.post(
|
|
'$_base/api/souscriptions/demande',
|
|
data: body,
|
|
options: opts,
|
|
);
|
|
if ((response.statusCode == 200 || response.statusCode == 201) && response.data is Map) {
|
|
return SouscriptionStatusModel.fromJson(response.data as Map);
|
|
}
|
|
final errMsg = response.data is Map
|
|
? (response.data as Map)['message'] ?? response.data.toString()
|
|
: response.data?.toString() ?? '(vide)';
|
|
AppLogger.warning('SouscriptionDatasource.creerDemande: HTTP ${response.statusCode} — erreur="$errMsg" — sentBody=$body');
|
|
} catch (e) {
|
|
AppLogger.error('SouscriptionDatasource.creerDemande: $e');
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Initie le paiement Wave pour une souscription existante
|
|
Future<SouscriptionStatusModel?> initierPaiement(String souscriptionId) async {
|
|
try {
|
|
final opts = await _authOptions();
|
|
final response = await _dio.post(
|
|
'$_base/api/souscriptions/$souscriptionId/initier-paiement',
|
|
options: opts,
|
|
);
|
|
if (response.statusCode == 200 && response.data is Map) {
|
|
return SouscriptionStatusModel.fromJson(response.data as Map);
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('SouscriptionDatasource.initierPaiement: $e');
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Confirme le paiement Wave après retour du deep link
|
|
Future<bool> confirmerPaiement(String souscriptionId) async {
|
|
try {
|
|
final opts = await _authOptions();
|
|
final response = await _dio.post(
|
|
'$_base/api/souscriptions/confirmer-paiement',
|
|
queryParameters: {'id': souscriptionId},
|
|
options: opts,
|
|
);
|
|
return response.statusCode == 200;
|
|
} catch (e) {
|
|
AppLogger.error('SouscriptionDatasource.confirmerPaiement: $e');
|
|
}
|
|
return false;
|
|
}
|
|
}
|