schema fix

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Sven laptop
2026-05-05 19:55:51 +02:00
parent c847d32743
commit 8d4e8b3075
7 changed files with 99 additions and 44 deletions
+44 -6
View File
@@ -1,7 +1,7 @@
/// User model
class User {
final int id;
final String userCode;
final int userCode;
final String? job;
User({
@@ -11,9 +11,20 @@ class User {
});
factory User.fromJson(Map<String, dynamic> json) {
// userCode may be provided as 'userCode' or 'usercode' and may be int or string
final dynamic rawUserCode = json['userCode'] ?? json['usercode'] ?? json['user_code'];
int parsedUserCode;
if (rawUserCode is int) {
parsedUserCode = rawUserCode;
} else if (rawUserCode is String) {
parsedUserCode = int.tryParse(rawUserCode) ?? (throw FormatException('Invalid userCode'));
} else {
throw FormatException('Missing userCode');
}
return User(
id: json['id'] as int,
userCode: json['userCode'] as String,
userCode: parsedUserCode,
job: json['job'] as String?,
);
}
@@ -30,17 +41,44 @@ class User {
/// Login response model
class AuthResponse {
final String token;
final User user;
final User? user;
AuthResponse({
required this.token,
required this.user,
this.user,
});
factory AuthResponse.fromJson(Map<String, dynamic> json) {
final dynamic rawToken = json['token'] ?? json['Token'] ?? json['authToken'];
final token = rawToken?.toString();
if (token == null || token.isEmpty) throw FormatException('Missing token in auth response');
// Determine if a user object exists and appears valid
Map<String, dynamic>? userMap;
if (json.containsKey('user') && json['user'] is Map<String, dynamic>) {
userMap = json['user'] as Map<String, dynamic>;
} else {
// maybe user fields are top-level (id, userCode, job)
final hasUserFields = json.containsKey('id') || json.containsKey('userCode') || json.containsKey('usercode') || json.containsKey('user_code');
if (hasUserFields) {
// treat whole json as user map
userMap = json.cast<String, dynamic>();
}
}
User? parsedUser;
if (userMap != null) {
try {
parsedUser = User.fromJson(userMap);
} catch (e) {
// If parsing user fails, ignore user but keep token
parsedUser = null;
}
}
return AuthResponse(
token: json['token'] as String,
user: User.fromJson(json['user'] as Map<String, dynamic>),
token: token,
user: parsedUser,
);
}
}