feat(#131): fiche AM éditable en 3 onglets et widgets admin partagés.
Modale admin AM (identité, dossier pro, enfants), API front avec repli, note back affiliation, et extraction gélule statut / liste enfants pour la fiche parent. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -513,7 +513,150 @@ class UserService {
|
||||
}
|
||||
|
||||
final List<dynamic> data = jsonDecode(response.body);
|
||||
return data.map((e) => AssistanteMaternelleModel.fromJson(e)).toList();
|
||||
return data
|
||||
.map((e) => AssistanteMaternelleModel.fromJson(
|
||||
Map<String, dynamic>.from(e as Map),
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
static Future<AssistanteMaternelleModel> getAssistanteMaternelle(
|
||||
String userId,
|
||||
) async {
|
||||
final response = await http.get(
|
||||
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.assistantesMaternelles}/$userId'),
|
||||
headers: await _headers(),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final decoded = jsonDecode(response.body);
|
||||
return AssistanteMaternelleModel.fromJson(
|
||||
Map<String, dynamic>.from(decoded is Map ? decoded : {}),
|
||||
);
|
||||
}
|
||||
if (response.statusCode == 403 || response.statusCode == 404) {
|
||||
final all = await getAssistantesMaternelles();
|
||||
return all.firstWhere(
|
||||
(a) => a.user.id == userId,
|
||||
orElse: () => throw Exception('Assistante maternelle introuvable'),
|
||||
);
|
||||
}
|
||||
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
||||
throw Exception(_toStr(err?['message']) ?? 'Erreur chargement AM');
|
||||
}
|
||||
|
||||
/// Mise à jour fiche AM (identité + champs pro). Ticket #131.
|
||||
static Future<AssistanteMaternelleModel> updateAmFiche({
|
||||
required String amUserId,
|
||||
required Map<String, dynamic> body,
|
||||
}) async {
|
||||
final ficheResponse = await http.patch(
|
||||
Uri.parse(
|
||||
'${ApiConfig.baseUrl}${ApiConfig.assistantesMaternelles}/$amUserId/fiche',
|
||||
),
|
||||
headers: await _headers(),
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
if (ficheResponse.statusCode == 200) {
|
||||
return _amModelFromBody(ficheResponse.body);
|
||||
}
|
||||
if (ficheResponse.statusCode != 404) {
|
||||
throw Exception(
|
||||
_extractErrorMessage(ficheResponse.body, 'Erreur mise à jour AM'),
|
||||
);
|
||||
}
|
||||
|
||||
final userFields = <String, dynamic>{};
|
||||
for (final k in [
|
||||
'nom',
|
||||
'prenom',
|
||||
'email',
|
||||
'telephone',
|
||||
'adresse',
|
||||
'ville',
|
||||
'code_postal',
|
||||
'statut',
|
||||
]) {
|
||||
if (body.containsKey(k)) userFields[k] = body[k];
|
||||
}
|
||||
|
||||
final proFields = <String, dynamic>{};
|
||||
for (final entry in body.entries) {
|
||||
if (!userFields.containsKey(entry.key)) {
|
||||
proFields[entry.key] = entry.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (userFields.isNotEmpty) {
|
||||
final userResponse = await http.patch(
|
||||
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.users}/$amUserId'),
|
||||
headers: await _headers(),
|
||||
body: jsonEncode(userFields),
|
||||
);
|
||||
if (userResponse.statusCode != 200) {
|
||||
throw Exception(
|
||||
_extractErrorMessage(userResponse.body, 'Erreur mise à jour identité'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (proFields.isNotEmpty) {
|
||||
final proResponse = await http.patch(
|
||||
Uri.parse(
|
||||
'${ApiConfig.baseUrl}${ApiConfig.assistantesMaternelles}/$amUserId',
|
||||
),
|
||||
headers: await _headers(),
|
||||
body: jsonEncode(proFields),
|
||||
);
|
||||
if (proResponse.statusCode != 200) {
|
||||
throw Exception(
|
||||
_extractErrorMessage(proResponse.body, 'Erreur mise à jour fiche pro'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return getAssistanteMaternelle(amUserId);
|
||||
}
|
||||
|
||||
static Future<AssistanteMaternelleModel> attachEnfantToAm({
|
||||
required String amUserId,
|
||||
required String enfantId,
|
||||
}) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(
|
||||
'${ApiConfig.baseUrl}${ApiConfig.assistantesMaternelles}/$amUserId/enfants/$enfantId',
|
||||
),
|
||||
headers: await _headers(),
|
||||
);
|
||||
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||
throw Exception(_extractErrorMessage(response.body, 'Erreur rattachement enfant'));
|
||||
}
|
||||
return _amModelFromBody(response.body);
|
||||
}
|
||||
|
||||
static Future<AssistanteMaternelleModel> detachEnfantFromAm({
|
||||
required String amUserId,
|
||||
required String enfantId,
|
||||
}) async {
|
||||
final response = await http.delete(
|
||||
Uri.parse(
|
||||
'${ApiConfig.baseUrl}${ApiConfig.assistantesMaternelles}/$amUserId/enfants/$enfantId',
|
||||
),
|
||||
headers: await _headers(),
|
||||
);
|
||||
if (response.statusCode != 200 && response.statusCode != 204) {
|
||||
throw Exception(_extractErrorMessage(response.body, 'Erreur détachement enfant'));
|
||||
}
|
||||
if (response.body.isEmpty) {
|
||||
return getAssistanteMaternelle(amUserId);
|
||||
}
|
||||
return _amModelFromBody(response.body);
|
||||
}
|
||||
|
||||
static AssistanteMaternelleModel _amModelFromBody(String body) {
|
||||
final decoded = jsonDecode(body);
|
||||
return AssistanteMaternelleModel.fromJson(
|
||||
Map<String, dynamic>.from(decoded is Map ? decoded : {}),
|
||||
);
|
||||
}
|
||||
|
||||
// Récupérer la liste des administrateurs (via /users filtré ou autre)
|
||||
|
||||
Reference in New Issue
Block a user