Fix: Hive sync et update entité via API REST

- Correction mapping JSON membres (fk_role, chk_active)
- Ajout traitement amicale au login
- Fix callback onSubmit pour sync Hive après update API
This commit is contained in:
d6soft
2025-06-09 18:46:49 +02:00
parent 15a0f2d2be
commit f3f1a9c5e8
56 changed files with 141860 additions and 142386 deletions

View File

@@ -61,6 +61,15 @@ class ClientModel extends HiveObject {
@HiveField(18)
final bool? chkActive;
@HiveField(19)
final bool? chkStripe;
@HiveField(20)
final DateTime? createdAt;
@HiveField(21)
final DateTime? updatedAt;
ClientModel({
required this.id,
required this.name,
@@ -81,6 +90,9 @@ class ClientModel extends HiveObject {
this.chkCopieMailRecu,
this.chkAcceptSms,
this.chkActive,
this.chkStripe,
this.createdAt,
this.updatedAt,
});
// Factory pour convertir depuis JSON (API)
@@ -93,8 +105,7 @@ class ClientModel extends HiveObject {
int? fkRegion;
if (json['fk_region'] != null) {
final dynamic rawFkRegion = json['fk_region'];
fkRegion =
rawFkRegion is String ? int.parse(rawFkRegion) : rawFkRegion as int;
fkRegion = rawFkRegion is String ? int.parse(rawFkRegion) : rawFkRegion as int;
}
// Convertir fk_type en int si présent
@@ -121,11 +132,12 @@ class ClientModel extends HiveObject {
gpsLng: json['gps_lng'],
stripeId: json['stripe_id'],
chkDemo: json['chk_demo'] == 1 || json['chk_demo'] == true,
chkCopieMailRecu: json['chk_copie_mail_recu'] == 1 ||
json['chk_copie_mail_recu'] == true,
chkAcceptSms:
json['chk_accept_sms'] == 1 || json['chk_accept_sms'] == true,
chkCopieMailRecu: json['chk_copie_mail_recu'] == 1 || json['chk_copie_mail_recu'] == true,
chkAcceptSms: json['chk_accept_sms'] == 1 || json['chk_accept_sms'] == true,
chkActive: json['chk_active'] == 1 || json['chk_active'] == true,
chkStripe: json['chk_stripe'] == 1 || json['chk_stripe'] == true,
createdAt: json['created_at'] != null ? DateTime.parse(json['created_at']) : null,
updatedAt: json['updated_at'] != null ? DateTime.parse(json['updated_at']) : null,
);
}
@@ -151,6 +163,9 @@ class ClientModel extends HiveObject {
'chk_copie_mail_recu': chkCopieMailRecu,
'chk_accept_sms': chkAcceptSms,
'chk_active': chkActive,
'chk_stripe': chkStripe,
'created_at': createdAt?.toIso8601String(),
'updated_at': updatedAt?.toIso8601String(),
};
}
@@ -174,9 +189,12 @@ class ClientModel extends HiveObject {
bool? chkCopieMailRecu,
bool? chkAcceptSms,
bool? chkActive,
bool? chkStripe,
DateTime? createdAt,
DateTime? updatedAt,
}) {
return ClientModel(
id: this.id,
id: id,
name: name ?? this.name,
adresse1: adresse1 ?? this.adresse1,
adresse2: adresse2 ?? this.adresse2,
@@ -195,6 +213,9 @@ class ClientModel extends HiveObject {
chkCopieMailRecu: chkCopieMailRecu ?? this.chkCopieMailRecu,
chkAcceptSms: chkAcceptSms ?? this.chkAcceptSms,
chkActive: chkActive ?? this.chkActive,
chkStripe: chkStripe ?? this.chkStripe,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
}
}

View File

@@ -36,13 +36,16 @@ class ClientModelAdapter extends TypeAdapter<ClientModel> {
chkCopieMailRecu: fields[16] as bool?,
chkAcceptSms: fields[17] as bool?,
chkActive: fields[18] as bool?,
chkStripe: fields[19] as bool?,
createdAt: fields[20] as DateTime?,
updatedAt: fields[21] as DateTime?,
);
}
@override
void write(BinaryWriter writer, ClientModel obj) {
writer
..writeByte(19)
..writeByte(22)
..writeByte(0)
..write(obj.id)
..writeByte(1)
@@ -80,7 +83,13 @@ class ClientModelAdapter extends TypeAdapter<ClientModel> {
..writeByte(17)
..write(obj.chkAcceptSms)
..writeByte(18)
..write(obj.chkActive);
..write(obj.chkActive)
..writeByte(19)
..write(obj.chkStripe)
..writeByte(20)
..write(obj.createdAt)
..writeByte(21)
..write(obj.updatedAt);
}
@override

View File

@@ -8,51 +8,53 @@ class MembreModel extends HiveObject {
final int id;
@HiveField(1)
final int fkRole;
int? fkEntite;
@HiveField(2)
final int fkTitre;
final int role;
@HiveField(3)
final String firstName;
int? fkTitre;
@HiveField(4)
final String? sectName;
String? name;
@HiveField(5)
final DateTime? dateNaissance;
String? firstName;
@HiveField(6)
final DateTime? dateEmbauche;
String? username;
@HiveField(7)
final int chkActive;
String? sectName;
@HiveField(8)
final String name;
@HiveField(9)
final String username;
@HiveField(10)
final String email;
@HiveField(9)
String? phone;
@HiveField(10)
String? mobile;
@HiveField(11)
final int fkEntite;
DateTime? dateNaissance;
@HiveField(12)
DateTime? dateEmbauche;
@HiveField(13)
final DateTime createdAt;
@HiveField(14)
bool isActive;
MembreModel({
required this.id,
required this.fkRole,
required this.fkTitre,
required this.firstName,
this.fkEntite,
required this.role,
this.fkTitre,
this.name,
this.firstName,
this.username,
this.sectName,
required this.email,
this.phone,
this.mobile,
this.dateNaissance,
this.dateEmbauche,
required this.chkActive,
required this.name,
required this.username,
required this.email,
required this.fkEntite,
required this.createdAt,
required this.isActive,
});
// Factory pour convertir depuis JSON (API)
@@ -61,35 +63,53 @@ class MembreModel extends HiveObject {
final dynamic rawId = json['id'];
final int id = rawId is String ? int.parse(rawId) : rawId as int;
// Convertir le rôle en int, qu'il soit déjà int ou string
final dynamic rawRole = json['fk_role'];
final int fkRole = rawRole is String ? int.parse(rawRole) : rawRole as int;
// Convertir le rôle en int (ATTENTION: le champ JSON est 'fk_role' pas 'role')
final dynamic rawRole = json['fk_role']; // Correction ici !
final int role = rawRole is String ? int.parse(rawRole) : rawRole as int;
// Convertir le titre en int, qu'il soit déjà int ou string
final dynamic rawTitre = json['fk_titre'];
final int fkTitre = rawTitre is String ? int.parse(rawTitre) : rawTitre as int;
// Convertir fkEntite en int si présent
int? fkEntite;
if (json['fk_entite'] != null) {
final dynamic rawEntite = json['fk_entite'];
fkEntite = rawEntite is String ? int.parse(rawEntite) : rawEntite as int;
}
// Convertir le chkActive en int, qu'il soit déjà int ou string
final dynamic rawActive = json['chk_active'];
final int chkActive = rawActive is String ? int.parse(rawActive) : rawActive as int;
// Convertir fkTitre en int si présent
int? fkTitre;
if (json['fk_titre'] != null) {
final dynamic rawTitre = json['fk_titre'];
fkTitre = rawTitre is String ? int.parse(rawTitre) : rawTitre as int;
}
// Convertir le fkEntite en int, qu'il soit déjà int ou string
final dynamic rawEntite = json['fk_entite'];
final int fkEntite = rawEntite is String ? int.parse(rawEntite) : rawEntite as int;
// Gérer les dates nulles ou avec des valeurs invalides comme "0000-00-00"
DateTime? parseDate(String? dateStr) {
if (dateStr == null || dateStr.isEmpty || dateStr == "0000-00-00") {
return null;
}
try {
return DateTime.parse(dateStr);
} catch (e) {
return null;
}
}
return MembreModel(
id: id,
fkRole: fkRole,
fkTitre: fkTitre,
firstName: json['first_name'] ?? '',
sectName: json['sect_name'],
dateNaissance: json['date_naissance'] != null ? DateTime.parse(json['date_naissance']) : null,
dateEmbauche: json['date_embauche'] != null ? DateTime.parse(json['date_embauche']) : null,
chkActive: chkActive,
name: json['name'] ?? '',
username: json['username'] ?? '',
email: json['email'] ?? '',
fkEntite: fkEntite,
role: role,
fkTitre: fkTitre,
name: json['name'],
firstName: json['first_name'],
username: json['username'],
sectName: json['sect_name'],
email: json['email'] ?? '',
phone: json['phone'],
mobile: json['mobile'],
dateNaissance: parseDate(json['date_naissance']),
dateEmbauche: parseDate(json['date_embauche']),
createdAt: json['created_at'] != null ? DateTime.parse(json['created_at']) : DateTime.now(),
// Le champ JSON est 'chk_active' pas 'is_active'
isActive: json['chk_active'] == 1 || json['chk_active'] == true,
);
}
@@ -97,47 +117,56 @@ class MembreModel extends HiveObject {
Map<String, dynamic> toJson() {
return {
'id': id,
'fk_role': fkRole,
'fk_entite': fkEntite,
'fk_role': role, // Changé pour correspondre à l'API
'fk_titre': fkTitre,
'name': name,
'first_name': firstName,
'username': username,
'sect_name': sectName,
'email': email,
'phone': phone,
'mobile': mobile,
'date_naissance': dateNaissance?.toIso8601String(),
'date_embauche': dateEmbauche?.toIso8601String(),
'chk_active': chkActive,
'name': name,
'username': username,
'email': email,
'fk_entite': fkEntite,
'created_at': createdAt.toIso8601String(),
'chk_active': isActive ? 1 : 0, // Changé pour correspondre à l'API
};
}
// Copier avec de nouvelles valeurs
MembreModel copyWith({
int? fkRole,
int? fkEntite,
int? role,
int? fkTitre,
String? name,
String? firstName,
String? username,
String? sectName,
String? email,
String? phone,
String? mobile,
DateTime? dateNaissance,
DateTime? dateEmbauche,
int? chkActive,
String? name,
String? username,
String? email,
int? fkEntite,
DateTime? createdAt,
bool? isActive,
}) {
return MembreModel(
id: id,
fkRole: fkRole ?? this.fkRole,
fkEntite: fkEntite ?? this.fkEntite,
role: role ?? this.role,
fkTitre: fkTitre ?? this.fkTitre,
name: name ?? this.name,
firstName: firstName ?? this.firstName,
username: username ?? this.username,
sectName: sectName ?? this.sectName,
email: email ?? this.email,
phone: phone ?? this.phone,
mobile: mobile ?? this.mobile,
dateNaissance: dateNaissance ?? this.dateNaissance,
dateEmbauche: dateEmbauche ?? this.dateEmbauche,
chkActive: chkActive ?? this.chkActive,
name: name ?? this.name,
username: username ?? this.username,
email: email ?? this.email,
fkEntite: fkEntite ?? this.fkEntite,
createdAt: createdAt ?? this.createdAt,
isActive: isActive ?? this.isActive,
);
}
}

View File

@@ -18,48 +18,57 @@ class MembreModelAdapter extends TypeAdapter<MembreModel> {
};
return MembreModel(
id: fields[0] as int,
fkRole: fields[1] as int,
fkTitre: fields[2] as int,
firstName: fields[3] as String,
sectName: fields[4] as String?,
dateNaissance: fields[5] as DateTime?,
dateEmbauche: fields[6] as DateTime?,
chkActive: fields[7] as int,
name: fields[8] as String,
username: fields[9] as String,
email: fields[10] as String,
fkEntite: fields[11] as int,
fkEntite: fields[1] as int?,
role: fields[2] as int,
fkTitre: fields[3] as int?,
name: fields[4] as String?,
firstName: fields[5] as String?,
username: fields[6] as String?,
sectName: fields[7] as String?,
email: fields[8] as String,
phone: fields[9] as String?,
mobile: fields[10] as String?,
dateNaissance: fields[11] as DateTime?,
dateEmbauche: fields[12] as DateTime?,
createdAt: fields[13] as DateTime,
isActive: fields[14] as bool,
);
}
@override
void write(BinaryWriter writer, MembreModel obj) {
writer
..writeByte(12)
..writeByte(15)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.fkRole)
..write(obj.fkEntite)
..writeByte(2)
..write(obj.fkTitre)
..write(obj.role)
..writeByte(3)
..write(obj.firstName)
..write(obj.fkTitre)
..writeByte(4)
..write(obj.sectName)
..writeByte(5)
..write(obj.dateNaissance)
..writeByte(6)
..write(obj.dateEmbauche)
..writeByte(7)
..write(obj.chkActive)
..writeByte(8)
..write(obj.name)
..writeByte(9)
..writeByte(5)
..write(obj.firstName)
..writeByte(6)
..write(obj.username)
..writeByte(10)
..writeByte(7)
..write(obj.sectName)
..writeByte(8)
..write(obj.email)
..writeByte(9)
..write(obj.phone)
..writeByte(10)
..write(obj.mobile)
..writeByte(11)
..write(obj.fkEntite);
..write(obj.dateNaissance)
..writeByte(12)
..write(obj.dateEmbauche)
..writeByte(13)
..write(obj.createdAt)
..writeByte(14)
..write(obj.isActive);
}
@override

View File

@@ -2,7 +2,7 @@ import 'package:hive/hive.dart';
part 'region_model.g.dart';
@HiveType(typeId: 7) // Assurez-vous que cet ID est unique
@HiveType(typeId: 7)
class RegionModel extends HiveObject {
@HiveField(0)
final int id;

View File

@@ -6,8 +6,7 @@ part 'user_sector_model.g.dart';
///
/// Cette classe représente l'association entre un utilisateur et un secteur,
/// telle que reçue de l'API dans la réponse users_sectors.
@HiveType(
typeId: 7) // Assurez-vous que cet ID est unique parmi vos modèles Hive
@HiveType(typeId: 6)
class UserSectorModel extends HiveObject {
@HiveField(0)
final int id; // ID de l'utilisateur
@@ -38,9 +37,7 @@ class UserSectorModel extends HiveObject {
id: json['id'] is String ? int.parse(json['id']) : json['id'],
firstName: json['first_name'],
sectName: json['sect_name'],
fkSector: json['fk_sector'] is String
? int.parse(json['fk_sector'])
: json['fk_sector'],
fkSector: json['fk_sector'] is String ? int.parse(json['fk_sector']) : json['fk_sector'],
name: json['name'],
);
}

View File

@@ -8,7 +8,7 @@ part of 'user_sector_model.dart';
class UserSectorModelAdapter extends TypeAdapter<UserSectorModel> {
@override
final int typeId = 7;
final int typeId = 6;
@override
UserSectorModel read(BinaryReader reader) {