Charge existingPhotoUrl dans les cartes enfant (étapes 3 et 5) et pré-coche le consentement photo lorsqu'une photo est déjà en base. Co-authored-by: Cursor <cursoragent@cursor.com>
124 lines
3.7 KiB
Dart
124 lines
3.7 KiB
Dart
import 'package:p_tits_pas/models/card_assets.dart';
|
|
import 'package:p_tits_pas/models/dossier_unifie.dart';
|
|
import 'package:p_tits_pas/models/reprise_dossier.dart';
|
|
import 'package:p_tits_pas/models/user_registration_data.dart';
|
|
import 'package:p_tits_pas/services/api/api_config.dart';
|
|
|
|
/// Mapping GET reprise-dossier → modèles wizard. Ticket #112.
|
|
class RepriseMapper {
|
|
RepriseMapper._();
|
|
|
|
static const List<CardColorVertical> _childCardColors = [
|
|
CardColorVertical.lavender,
|
|
CardColorVertical.pink,
|
|
CardColorVertical.peach,
|
|
CardColorVertical.lime,
|
|
CardColorVertical.red,
|
|
CardColorVertical.green,
|
|
CardColorVertical.blue,
|
|
];
|
|
|
|
static ParentData parentFromDossier(ParentDossier p) {
|
|
return ParentData(
|
|
firstName: p.prenom ?? '',
|
|
lastName: p.nom ?? '',
|
|
phone: p.telephone ?? '',
|
|
email: p.email,
|
|
address: p.adresse ?? '',
|
|
postalCode: p.codePostal ?? '',
|
|
city: p.ville ?? '',
|
|
password: '',
|
|
);
|
|
}
|
|
|
|
static ChildData childFromEnfant(EnfantDossier e, int index) {
|
|
final isUnborn = e.status == 'a_naitre';
|
|
final dob = isUnborn
|
|
? isoToDdMmYyyy(e.dueDate)
|
|
: isoToDdMmYyyy(e.birthDate);
|
|
final photo = e.photoUrl?.trim();
|
|
final hasPhoto = photo != null && photo.isNotEmpty;
|
|
return ChildData(
|
|
firstName: e.firstName ?? '',
|
|
lastName: e.lastName ?? '',
|
|
dob: dob,
|
|
genre: e.gender ?? '',
|
|
// Inscription initiale exigeait la coche pour envoyer la photo ; le back
|
|
// ne persistait pas toujours consent_photo — on pré-coche si photo en base.
|
|
photoConsent: e.consentPhoto || hasPhoto,
|
|
multipleBirth: e.estMultiple,
|
|
isUnbornChild: isUnborn,
|
|
cardColor: _childCardColors[index % _childCardColors.length],
|
|
repriseChildId: e.id,
|
|
existingPhotoUrl: photo != null && photo.isNotEmpty
|
|
? ApiConfig.absoluteMediaUrl(photo)
|
|
: null,
|
|
);
|
|
}
|
|
|
|
static void applyParentDossier(UserRegistrationData data, RepriseDossier dossier) {
|
|
ParentDossier? titulaire;
|
|
ParentDossier? coParent;
|
|
|
|
if (dossier.parents.isNotEmpty) {
|
|
for (final p in dossier.parents) {
|
|
if (p.id == dossier.id) {
|
|
titulaire = p;
|
|
} else {
|
|
coParent = p;
|
|
}
|
|
}
|
|
titulaire ??= dossier.parents.first;
|
|
if (coParent == null && dossier.parents.length > 1) {
|
|
coParent = dossier.parents.firstWhere(
|
|
(p) => p.id != titulaire!.id,
|
|
orElse: () => dossier.parents.last,
|
|
);
|
|
}
|
|
}
|
|
|
|
final p1 = titulaire != null
|
|
? parentFromDossier(titulaire)
|
|
: ParentData(
|
|
firstName: dossier.prenom ?? '',
|
|
lastName: dossier.nom ?? '',
|
|
phone: dossier.telephone ?? '',
|
|
email: dossier.email,
|
|
address: dossier.adresse ?? '',
|
|
postalCode: dossier.codePostal ?? '',
|
|
city: dossier.ville ?? '',
|
|
password: '',
|
|
);
|
|
|
|
final children = dossier.enfants
|
|
.asMap()
|
|
.entries
|
|
.map((e) => childFromEnfant(e.value, e.key))
|
|
.toList();
|
|
|
|
data.resetForReprise(
|
|
parent1Data: p1,
|
|
parent2Data: coParent != null ? parentFromDossier(coParent) : null,
|
|
childrenData: children,
|
|
motivation: dossier.texteMotivation ?? '',
|
|
);
|
|
}
|
|
|
|
static DateTime? parseIsoDate(String? raw) {
|
|
if (raw == null || raw.trim().isEmpty) return null;
|
|
try {
|
|
return DateTime.parse(raw.trim());
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static String isoToDdMmYyyy(String? iso) {
|
|
final dt = parseIsoDate(iso);
|
|
if (dt == null) return '';
|
|
return '${dt.day.toString().padLeft(2, '0')}/'
|
|
'${dt.month.toString().padLeft(2, '0')}/'
|
|
'${dt.year}';
|
|
}
|
|
}
|