bharat_erp/lib/shared/models/user_management_models.dart
2026-06-22 12:35:37 +05:30

386 lines
13 KiB
Dart

import 'package:freezed_annotation/freezed_annotation.dart';
part 'user_management_models.freezed.dart';
part 'user_management_models.g.dart';
Object? _readId(Map<dynamic, dynamic> json, String key) => json[key];
String _idFromJson(Object? value) => value?.toString() ?? '';
String? _idFromJsonNullable(Object? value) =>
value == null ? null : value.toString();
Object? _readEmployeeCode(Map<dynamic, dynamic> json, String key) =>
json['employee_code'] ?? json['employee_id'];
Object? _readFullName(Map<dynamic, dynamic> json, String key) {
final fullName = json['full_name'] ?? json['name'];
if (fullName != null) return fullName;
final first = json['first_name'] as String?;
final last = json['last_name'] as String?;
if (first != null || last != null) {
return [first, last].where((e) => e != null && e.isNotEmpty).join(' ');
}
return null;
}
Object? _readRoleName(Map<dynamic, dynamic> json, String key) {
final roleName = json['role_name'];
if (roleName is String) return roleName;
final role = json['role'];
if (role is Map) return role['name'];
if (role is String) return role;
return null;
}
Object? _readDepartmentName(Map<dynamic, dynamic> json, String key) {
final name = json['department_name'];
if (name is String) return name;
final department = json['department'];
if (department is Map) return department['name'];
return null;
}
Object? _readPlantName(Map<dynamic, dynamic> json, String key) {
final name = json['plant_name'];
if (name is String) return name;
final plant = json['plant'];
if (plant is Map) return plant['name'];
if (plant is String) return plant;
return null;
}
Object? _readRoleId(Map<dynamic, dynamic> json, String key) {
final roleId = json['role_id'];
if (roleId != null) return roleId;
final role = json['role'];
if (role is Map) return role['id'];
return null;
}
Object? _readRoleIds(Map<dynamic, dynamic> json, String key) {
final roleIds = json['role_ids'];
if (roleIds is List && roleIds.isNotEmpty) return roleIds;
final roles = json['roles'];
if (roles is List && roles.isNotEmpty) {
return roles
.map((role) => role is Map ? role['id'] : role)
.where((id) => id != null)
.toList();
}
return null;
}
List<String> _roleIdsFromJson(dynamic value) {
if (value is List) {
return value.map((item) => item.toString()).toList();
}
return const [];
}
@freezed
class UserSummaryModel with _$UserSummaryModel {
const factory UserSummaryModel({
@JsonKey(name: 'total_users') @Default(0) int totalUsers,
@JsonKey(name: 'active_users') @Default(0) int activeUsers,
@JsonKey(name: 'inactive_users') @Default(0) int inactiveUsers,
@JsonKey(name: 'locked_users') @Default(0) int lockedUsers,
@JsonKey(name: 'roles_count') @Default(0) int rolesCount,
}) = _UserSummaryModel;
factory UserSummaryModel.fromJson(Map<String, dynamic> json) =>
_$UserSummaryModelFromJson(json);
}
@freezed
class FilterOptionModel with _$FilterOptionModel {
const factory FilterOptionModel({
@JsonKey(fromJson: _idFromJson) required String id,
required String name,
String? slug,
}) = _FilterOptionModel;
factory FilterOptionModel.fromJson(Map<String, dynamic> json) =>
_$FilterOptionModelFromJson(json);
}
@freezed
class UserFiltersModel with _$UserFiltersModel {
const factory UserFiltersModel({
@Default([]) List<FilterOptionModel> roles,
@Default([]) List<FilterOptionModel> departments,
@Default([]) List<FilterOptionModel> statuses,
}) = _UserFiltersModel;
factory UserFiltersModel.fromJson(Map<String, dynamic> json) =>
_$UserFiltersModelFromJson(json);
}
@freezed
class ManagedUserModel with _$ManagedUserModel {
const factory ManagedUserModel({
@JsonKey(fromJson: _idFromJson, readValue: _readId) required String id,
@JsonKey(name: 'employee_code', readValue: _readEmployeeCode)
required String employeeCode,
@JsonKey(name: 'full_name', readValue: _readFullName) required String fullName,
@JsonKey(name: 'first_name') String? firstName,
@JsonKey(name: 'last_name') String? lastName,
required String email,
@Default('') String mobile,
@JsonKey(fromJson: _idFromJsonNullable, name: 'role_id', readValue: _readRoleId)
String? roleId,
@JsonKey(
name: 'role_ids',
readValue: _readRoleIds,
fromJson: _roleIdsFromJson,
)
@Default([])
List<String> roleIds,
@JsonKey(name: 'role_name', readValue: _readRoleName) String? roleName,
@JsonKey(fromJson: _idFromJsonNullable, name: 'department_id')
String? departmentId,
@JsonKey(name: 'department_name', readValue: _readDepartmentName)
String? departmentName,
@JsonKey(fromJson: _idFromJsonNullable, name: 'designation_id')
String? designationId,
@JsonKey(name: 'designation_name') String? designationName,
@JsonKey(fromJson: _idFromJsonNullable, name: 'plant_id') String? plantId,
@JsonKey(name: 'plant_name', readValue: _readPlantName) String? plantName,
@JsonKey(fromJson: _idFromJsonNullable, name: 'reporting_to')
String? reportingTo,
@JsonKey(name: 'reporting_to_name') String? reportingToName,
@JsonKey(name: 'last_login_at') DateTime? lastLoginAt,
String? initials,
@Default('active') String status,
@JsonKey(name: 'is_active') @Default(true) bool isActive,
@JsonKey(name: 'avatar_url') String? avatarUrl,
@JsonKey(name: 'created_at') DateTime? createdAt,
@JsonKey(name: 'updated_at') DateTime? updatedAt,
}) = _ManagedUserModel;
factory ManagedUserModel.fromJson(Map<String, dynamic> json) =>
_$ManagedUserModelFromJson(json);
}
extension ManagedUserModelX on ManagedUserModel {
String get displayName => fullName;
String get roleLabel => roleName ?? '';
String get departmentLabel => departmentName ?? '';
String get plantLabel => plantName ?? '';
List<String> get effectiveRoleIds {
if (roleIds.isNotEmpty) return roleIds;
if (roleId != null && roleId!.isNotEmpty) return [roleId!];
return const [];
}
String get initialsDisplay {
if (initials != null && initials!.trim().isNotEmpty) {
return initials!.trim().toUpperCase();
}
final parts = fullName.trim().split(RegExp(r'\s+'));
if (parts.length >= 2) {
return '${parts.first[0]}${parts[1][0]}'.toUpperCase();
}
return fullName.isNotEmpty ? fullName[0].toUpperCase() : 'U';
}
}
@freezed
class CreateUserRequest with _$CreateUserRequest {
const factory CreateUserRequest({
@JsonKey(name: 'employee_code') required String employeeCode,
@JsonKey(name: 'full_name') required String fullName,
required String email,
required String password,
String? mobile,
@JsonKey(name: 'role_id') required int roleId,
@JsonKey(name: 'role_ids') @Default([]) List<int> roleIds,
@JsonKey(name: 'department_id') int? departmentId,
@JsonKey(name: 'designation_id') int? designationId,
@JsonKey(name: 'plant_id') int? plantId,
@JsonKey(name: 'reporting_to') int? reportingTo,
@Default('active') String status,
@JsonKey(name: 'is_active') @Default(true) bool isActive,
}) = _CreateUserRequest;
factory CreateUserRequest.fromJson(Map<String, dynamic> json) =>
_$CreateUserRequestFromJson(json);
}
@freezed
class UpdateUserRequest with _$UpdateUserRequest {
const factory UpdateUserRequest({
@JsonKey(name: 'employee_code') String? employeeCode,
@JsonKey(name: 'full_name') String? fullName,
String? email,
String? password,
String? mobile,
@JsonKey(name: 'role_id') int? roleId,
@JsonKey(name: 'role_ids') List<int>? roleIds,
@JsonKey(name: 'department_id') int? departmentId,
@JsonKey(name: 'designation_id') int? designationId,
@JsonKey(name: 'plant_id') int? plantId,
@JsonKey(name: 'reporting_to') int? reportingTo,
String? status,
@JsonKey(name: 'is_active') bool? isActive,
}) = _UpdateUserRequest;
factory UpdateUserRequest.fromJson(Map<String, dynamic> json) =>
_$UpdateUserRequestFromJson(json);
}
@freezed
class UserListQuery with _$UserListQuery {
const factory UserListQuery({
@Default(1) int page,
@Default(20) int limit,
String? search,
String? status,
@JsonKey(name: 'role_id') int? roleId,
@JsonKey(name: 'department_id') int? departmentId,
@JsonKey(name: 'sort_by') String? sortBy,
@JsonKey(name: 'sort_order') @Default('asc') String sortOrder,
@JsonKey(name: 'is_active') bool? isActive,
}) = _UserListQuery;
}
@freezed
class RoleCardModel with _$RoleCardModel {
const factory RoleCardModel({
@JsonKey(fromJson: _idFromJson, readValue: _readId) required String id,
required String name,
String? description,
@JsonKey(name: 'user_count') @Default(0) int userCount,
@JsonKey(name: 'permission_count') @Default(0) int permissionCount,
@JsonKey(name: 'is_active') @Default(true) bool isActive,
}) = _RoleCardModel;
factory RoleCardModel.fromJson(Map<String, dynamic> json) =>
_$RoleCardModelFromJson(json);
}
@freezed
class CreateRoleRequest with _$CreateRoleRequest {
const factory CreateRoleRequest({
required String name,
String? description,
@JsonKey(name: 'is_active') @Default(true) bool isActive,
@JsonKey(name: 'permission_ids') @Default([]) List<int> permissionIds,
@JsonKey(name: 'view_modules') @Default([]) List<String> viewModules,
}) = _CreateRoleRequest;
factory CreateRoleRequest.fromJson(Map<String, dynamic> json) =>
_$CreateRoleRequestFromJson(json);
}
@freezed
class UpdateRoleRequest with _$UpdateRoleRequest {
const factory UpdateRoleRequest({
String? name,
String? description,
@JsonKey(name: 'is_active') bool? isActive,
}) = _UpdateRoleRequest;
factory UpdateRoleRequest.fromJson(Map<String, dynamic> json) =>
_$UpdateRoleRequestFromJson(json);
}
@freezed
class PermissionCatalogModel with _$PermissionCatalogModel {
const factory PermissionCatalogModel({
@JsonKey(fromJson: _idFromJson, readValue: _readId) required String id,
required String module,
required String action,
String? description,
@JsonKey(name: 'module_id', fromJson: _idFromJsonNullable)
String? moduleId,
}) = _PermissionCatalogModel;
factory PermissionCatalogModel.fromJson(Map<String, dynamic> json) =>
_$PermissionCatalogModelFromJson(json);
}
@freezed
class PermissionMatrixActions with _$PermissionMatrixActions {
const factory PermissionMatrixActions({
@Default(false) bool view,
@Default(false) bool edit,
@Default(false) bool approve,
@Default(false) bool export,
}) = _PermissionMatrixActions;
factory PermissionMatrixActions.fromJson(Map<String, dynamic> json) =>
_$PermissionMatrixActionsFromJson(json);
}
@freezed
class PermissionMatrixRow with _$PermissionMatrixRow {
const factory PermissionMatrixRow({
@JsonKey(name: 'module_id', fromJson: _idFromJson) required String moduleId,
required String module,
required PermissionMatrixActions actions,
}) = _PermissionMatrixRow;
factory PermissionMatrixRow.fromJson(Map<String, dynamic> json) =>
_$PermissionMatrixRowFromJson(json);
}
@freezed
class PermissionMatrixModel with _$PermissionMatrixModel {
const factory PermissionMatrixModel({
@JsonKey(fromJson: _idFromJson, readValue: _readId) required String roleId,
required String roleName,
@Default([]) List<PermissionMatrixRow> matrix,
}) = _PermissionMatrixModel;
factory PermissionMatrixModel.fromJson(Map<String, dynamic> json) =>
_$PermissionMatrixModelFromJson(json);
}
@freezed
class PermissionMatrixSaveRequest with _$PermissionMatrixSaveRequest {
const factory PermissionMatrixSaveRequest({
required List<PermissionMatrixSaveRow> matrix,
}) = _PermissionMatrixSaveRequest;
factory PermissionMatrixSaveRequest.fromJson(Map<String, dynamic> json) =>
_$PermissionMatrixSaveRequestFromJson(json);
}
extension PermissionMatrixSaveRequestX on PermissionMatrixSaveRequest {
Map<String, dynamic> toApiJson() => {
'matrix': matrix
.map(
(row) => {
'module_id': int.tryParse(row.moduleId) ?? row.moduleId,
'actions': row.actions.toJson(),
},
)
.toList(),
};
}
@freezed
class PermissionMatrixSaveRow with _$PermissionMatrixSaveRow {
const factory PermissionMatrixSaveRow({
@JsonKey(name: 'module_id', fromJson: _idFromJson) required String moduleId,
required PermissionMatrixActions actions,
}) = _PermissionMatrixSaveRow;
factory PermissionMatrixSaveRow.fromJson(Map<String, dynamic> json) =>
_$PermissionMatrixSaveRowFromJson(json);
}
@freezed
class UpdateProfileRequest with _$UpdateProfileRequest {
const factory UpdateProfileRequest({
@JsonKey(name: 'full_name') String? fullName,
String? mobile,
@JsonKey(name: 'avatar_url') String? avatarUrl,
}) = _UpdateProfileRequest;
factory UpdateProfileRequest.fromJson(Map<String, dynamic> json) =>
_$UpdateProfileRequestFromJson(json);
}