294 lines
8.5 KiB
Dart
294 lines
8.5 KiB
Dart
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
|
import 'package:superport_v2/core/common/utils/json_utils.dart';
|
|
|
|
import '../../domain/entities/approval.dart';
|
|
|
|
class ApprovalDto {
|
|
ApprovalDto({
|
|
this.id,
|
|
required this.approvalNo,
|
|
this.transactionNo,
|
|
required this.status,
|
|
this.currentStep,
|
|
required this.requester,
|
|
required this.requestedAt,
|
|
this.decidedAt,
|
|
this.note,
|
|
this.isActive = true,
|
|
this.isDeleted = false,
|
|
this.steps = const [],
|
|
this.histories = const [],
|
|
this.createdAt,
|
|
this.updatedAt,
|
|
});
|
|
|
|
final int? id;
|
|
final String approvalNo;
|
|
final String? transactionNo;
|
|
final ApprovalStatusDto status;
|
|
final ApprovalStepDto? currentStep;
|
|
final ApprovalRequesterDto requester;
|
|
final DateTime requestedAt;
|
|
final DateTime? decidedAt;
|
|
final String? note;
|
|
final bool isActive;
|
|
final bool isDeleted;
|
|
final List<ApprovalStepDto> steps;
|
|
final List<ApprovalHistoryDto> histories;
|
|
final DateTime? createdAt;
|
|
final DateTime? updatedAt;
|
|
|
|
factory ApprovalDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalDto(
|
|
id: json['id'] as int?,
|
|
approvalNo: json['approval_no'] as String,
|
|
transactionNo: json['transaction'] is Map<String, dynamic>
|
|
? (json['transaction']['transaction_no'] as String?)
|
|
: json['transaction_no'] as String?,
|
|
status: ApprovalStatusDto.fromJson(
|
|
(json['status'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
currentStep: json['current_step'] is Map<String, dynamic>
|
|
? ApprovalStepDto.fromJson(
|
|
json['current_step'] as Map<String, dynamic>,
|
|
)
|
|
: null,
|
|
requester: ApprovalRequesterDto.fromJson(
|
|
(json['requester'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
requestedAt: _parseDate(json['requested_at']) ?? DateTime.now(),
|
|
decidedAt: _parseDate(json['decided_at']),
|
|
note: json['note'] as String?,
|
|
isActive: (json['is_active'] as bool?) ?? true,
|
|
isDeleted: (json['is_deleted'] as bool?) ?? false,
|
|
steps: (json['steps'] as List<dynamic>? ?? [])
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(ApprovalStepDto.fromJson)
|
|
.toList(),
|
|
histories: (json['histories'] as List<dynamic>? ?? [])
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(ApprovalHistoryDto.fromJson)
|
|
.toList(),
|
|
createdAt: _parseDate(json['created_at']),
|
|
updatedAt: _parseDate(json['updated_at']),
|
|
);
|
|
}
|
|
|
|
Approval toEntity() => Approval(
|
|
id: id,
|
|
approvalNo: approvalNo,
|
|
transactionNo: transactionNo ?? '-',
|
|
status: status.toEntity(),
|
|
currentStep: currentStep?.toEntity(),
|
|
requester: requester.toEntity(),
|
|
requestedAt: requestedAt,
|
|
decidedAt: decidedAt,
|
|
note: note,
|
|
isActive: isActive,
|
|
isDeleted: isDeleted,
|
|
steps: steps.map((e) => e.toEntity()).toList(),
|
|
histories: histories.map((e) => e.toEntity()).toList(),
|
|
createdAt: createdAt,
|
|
updatedAt: updatedAt,
|
|
);
|
|
|
|
static PaginatedResult<Approval> parsePaginated(Map<String, dynamic>? json) {
|
|
final rawItems = JsonUtils.extractList(json, keys: const ['items']);
|
|
final items = rawItems
|
|
.map(ApprovalDto.fromJson)
|
|
.map((dto) => dto.toEntity())
|
|
.toList(growable: false);
|
|
return PaginatedResult<Approval>(
|
|
items: items,
|
|
page: JsonUtils.readInt(json, 'page', fallback: 1),
|
|
pageSize: JsonUtils.readInt(json, 'page_size', fallback: items.length),
|
|
total: JsonUtils.readInt(json, 'total', fallback: items.length),
|
|
);
|
|
}
|
|
}
|
|
|
|
class ApprovalStatusDto {
|
|
ApprovalStatusDto({required this.id, required this.name, this.color});
|
|
|
|
final int id;
|
|
final String name;
|
|
final String? color;
|
|
|
|
factory ApprovalStatusDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalStatusDto(
|
|
id: json['id'] as int? ?? json['status_id'] as int? ?? 0,
|
|
name: json['name'] as String? ?? json['status_name'] as String? ?? '-',
|
|
color: json['color'] as String?,
|
|
);
|
|
}
|
|
|
|
ApprovalStatus toEntity() => ApprovalStatus(id: id, name: name, color: color);
|
|
}
|
|
|
|
class ApprovalRequesterDto {
|
|
ApprovalRequesterDto({
|
|
required this.id,
|
|
required this.employeeNo,
|
|
required this.name,
|
|
});
|
|
|
|
final int id;
|
|
final String employeeNo;
|
|
final String name;
|
|
|
|
factory ApprovalRequesterDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalRequesterDto(
|
|
id: json['id'] as int? ?? json['employee_id'] as int? ?? 0,
|
|
employeeNo: json['employee_no'] as String? ?? '-',
|
|
name: json['name'] as String? ?? json['employee_name'] as String? ?? '-',
|
|
);
|
|
}
|
|
|
|
ApprovalRequester toEntity() =>
|
|
ApprovalRequester(id: id, employeeNo: employeeNo, name: name);
|
|
}
|
|
|
|
class ApprovalApproverDto {
|
|
ApprovalApproverDto({
|
|
required this.id,
|
|
required this.employeeNo,
|
|
required this.name,
|
|
});
|
|
|
|
final int id;
|
|
final String employeeNo;
|
|
final String name;
|
|
|
|
factory ApprovalApproverDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalApproverDto(
|
|
id: json['id'] as int? ?? json['approver_id'] as int? ?? 0,
|
|
employeeNo: json['employee_no'] as String? ?? '-',
|
|
name: json['name'] as String? ?? json['employee_name'] as String? ?? '-',
|
|
);
|
|
}
|
|
|
|
ApprovalApprover toEntity() =>
|
|
ApprovalApprover(id: id, employeeNo: employeeNo, name: name);
|
|
}
|
|
|
|
class ApprovalStepDto {
|
|
ApprovalStepDto({
|
|
this.id,
|
|
required this.stepOrder,
|
|
required this.approver,
|
|
required this.status,
|
|
required this.assignedAt,
|
|
this.decidedAt,
|
|
this.note,
|
|
});
|
|
|
|
final int? id;
|
|
final int stepOrder;
|
|
final ApprovalApproverDto approver;
|
|
final ApprovalStatusDto status;
|
|
final DateTime assignedAt;
|
|
final DateTime? decidedAt;
|
|
final String? note;
|
|
|
|
factory ApprovalStepDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalStepDto(
|
|
id: json['id'] as int?,
|
|
stepOrder: json['step_order'] as int? ?? 0,
|
|
approver: ApprovalApproverDto.fromJson(
|
|
(json['approver'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
status: ApprovalStatusDto.fromJson(
|
|
(json['status'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
assignedAt: _parseDate(json['assigned_at']) ?? DateTime.now(),
|
|
decidedAt: _parseDate(json['decided_at']),
|
|
note: json['note'] as String?,
|
|
);
|
|
}
|
|
|
|
ApprovalStep toEntity() => ApprovalStep(
|
|
id: id,
|
|
stepOrder: stepOrder,
|
|
approver: approver.toEntity(),
|
|
status: status.toEntity(),
|
|
assignedAt: assignedAt,
|
|
decidedAt: decidedAt,
|
|
note: note,
|
|
);
|
|
}
|
|
|
|
class ApprovalHistoryDto {
|
|
ApprovalHistoryDto({
|
|
this.id,
|
|
required this.action,
|
|
this.fromStatus,
|
|
required this.toStatus,
|
|
required this.approver,
|
|
required this.actionAt,
|
|
this.note,
|
|
});
|
|
|
|
final int? id;
|
|
final ApprovalActionDto action;
|
|
final ApprovalStatusDto? fromStatus;
|
|
final ApprovalStatusDto toStatus;
|
|
final ApprovalApproverDto approver;
|
|
final DateTime actionAt;
|
|
final String? note;
|
|
|
|
factory ApprovalHistoryDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalHistoryDto(
|
|
id: json['id'] as int?,
|
|
action: ApprovalActionDto.fromJson(
|
|
(json['action'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
fromStatus: json['from_status'] is Map<String, dynamic>
|
|
? ApprovalStatusDto.fromJson(
|
|
json['from_status'] as Map<String, dynamic>,
|
|
)
|
|
: null,
|
|
toStatus: ApprovalStatusDto.fromJson(
|
|
(json['to_status'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
approver: ApprovalApproverDto.fromJson(
|
|
(json['approver'] as Map<String, dynamic>? ?? const {}),
|
|
),
|
|
actionAt: _parseDate(json['action_at']) ?? DateTime.now(),
|
|
note: json['note'] as String?,
|
|
);
|
|
}
|
|
|
|
ApprovalHistory toEntity() => ApprovalHistory(
|
|
id: id,
|
|
action: action.toEntity(),
|
|
fromStatus: fromStatus?.toEntity(),
|
|
toStatus: toStatus.toEntity(),
|
|
approver: approver.toEntity(),
|
|
actionAt: actionAt,
|
|
note: note,
|
|
);
|
|
}
|
|
|
|
class ApprovalActionDto {
|
|
ApprovalActionDto({required this.id, required this.name});
|
|
|
|
final int id;
|
|
final String name;
|
|
|
|
factory ApprovalActionDto.fromJson(Map<String, dynamic> json) {
|
|
return ApprovalActionDto(
|
|
id: json['id'] as int? ?? json['action_id'] as int? ?? 0,
|
|
name: json['name'] as String? ?? json['action_name'] as String? ?? '-',
|
|
);
|
|
}
|
|
|
|
ApprovalAction toEntity() => ApprovalAction(id: id, name: name);
|
|
}
|
|
|
|
DateTime? _parseDate(Object? value) {
|
|
if (value == null) return null;
|
|
if (value is DateTime) return value;
|
|
if (value is String) return DateTime.tryParse(value);
|
|
return null;
|
|
}
|