결재 템플릿 CRUD 화면과 컨트롤러 정식화
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'package:superport_v2/core/common/models/paginated_result.dart';
|
||||
|
||||
import '../../domain/entities/approval_template.dart';
|
||||
|
||||
class ApprovalTemplateDto {
|
||||
@@ -7,6 +9,7 @@ class ApprovalTemplateDto {
|
||||
required this.name,
|
||||
this.description,
|
||||
required this.isActive,
|
||||
this.note,
|
||||
this.createdBy,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
@@ -18,6 +21,7 @@ class ApprovalTemplateDto {
|
||||
final String name;
|
||||
final String? description;
|
||||
final bool isActive;
|
||||
final String? note;
|
||||
final ApprovalTemplateAuthorDto? createdBy;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
@@ -29,6 +33,7 @@ class ApprovalTemplateDto {
|
||||
code: json['template_code'] as String? ?? json['code'] as String? ?? '-',
|
||||
name: json['template_name'] as String? ?? json['name'] as String? ?? '-',
|
||||
description: json['description'] as String?,
|
||||
note: json['note'] as String?,
|
||||
isActive: (json['is_active'] as bool?) ?? true,
|
||||
createdBy: json['created_by'] is Map<String, dynamic>
|
||||
? ApprovalTemplateAuthorDto.fromJson(
|
||||
@@ -50,6 +55,7 @@ class ApprovalTemplateDto {
|
||||
code: code,
|
||||
name: name,
|
||||
description: description,
|
||||
note: note,
|
||||
isActive: isActive,
|
||||
createdBy: createdBy?.toEntity(),
|
||||
createdAt: createdAt,
|
||||
@@ -57,6 +63,23 @@ class ApprovalTemplateDto {
|
||||
steps: includeSteps ? steps.map((e) => e.toEntity()).toList() : const [],
|
||||
);
|
||||
}
|
||||
|
||||
static PaginatedResult<ApprovalTemplate> parsePaginated(
|
||||
Map<String, dynamic>? json, {
|
||||
bool includeSteps = false,
|
||||
}) {
|
||||
final items = (json?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(ApprovalTemplateDto.fromJson)
|
||||
.map((dto) => dto.toEntity(includeSteps: includeSteps))
|
||||
.toList();
|
||||
return PaginatedResult<ApprovalTemplate>(
|
||||
items: items,
|
||||
page: json?['page'] as int? ?? 1,
|
||||
pageSize: json?['page_size'] as int? ?? items.length,
|
||||
total: json?['total'] as int? ?? items.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ApprovalTemplateAuthorDto {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/common/models/paginated_result.dart';
|
||||
import '../../../../core/network/api_client.dart';
|
||||
import '../../domain/entities/approval_template.dart';
|
||||
import '../../domain/repositories/approval_template_repository.dart';
|
||||
@@ -14,18 +15,23 @@ class ApprovalTemplateRepositoryRemote implements ApprovalTemplateRepository {
|
||||
static const _basePath = '/approval-templates';
|
||||
|
||||
@override
|
||||
Future<List<ApprovalTemplate>> list({bool activeOnly = true}) async {
|
||||
Future<PaginatedResult<ApprovalTemplate>> list({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
String? query,
|
||||
bool? isActive,
|
||||
}) async {
|
||||
final response = await _api.get<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
query: {'page': 1, 'page_size': 100, if (activeOnly) 'active': true},
|
||||
query: {
|
||||
'page': page,
|
||||
'page_size': pageSize,
|
||||
if (query != null && query.isNotEmpty) 'q': query,
|
||||
if (isActive != null) 'active': isActive,
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final items = (response.data?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(ApprovalTemplateDto.fromJson)
|
||||
.map((dto) => dto.toEntity(includeSteps: false))
|
||||
.toList();
|
||||
return items;
|
||||
return ApprovalTemplateDto.parsePaginated(response.data);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -43,4 +49,85 @@ class ApprovalTemplateRepositoryRemote implements ApprovalTemplateRepository {
|
||||
data,
|
||||
).toEntity(includeSteps: includeSteps);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ApprovalTemplate> create(
|
||||
ApprovalTemplateInput input, {
|
||||
List<ApprovalTemplateStepInput> steps = const [],
|
||||
}) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
data: input.toCreatePayload(),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
final created = ApprovalTemplateDto.fromJson(
|
||||
data,
|
||||
).toEntity(includeSteps: false);
|
||||
if (steps.isNotEmpty) {
|
||||
await _postSteps(created.id, steps);
|
||||
}
|
||||
return fetchDetail(created.id, includeSteps: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ApprovalTemplate> update(
|
||||
int id,
|
||||
ApprovalTemplateInput input, {
|
||||
List<ApprovalTemplateStepInput>? steps,
|
||||
}) async {
|
||||
await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$id',
|
||||
data: input.toUpdatePayload(id),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
if (steps != null) {
|
||||
await _patchSteps(id, steps);
|
||||
}
|
||||
return fetchDetail(id, includeSteps: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete<void>('$_basePath/$id');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ApprovalTemplate> restore(int id) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
'$_basePath/$id/restore',
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return ApprovalTemplateDto.fromJson(data).toEntity(includeSteps: false);
|
||||
}
|
||||
|
||||
Future<void> _postSteps(
|
||||
int templateId,
|
||||
List<ApprovalTemplateStepInput> steps,
|
||||
) async {
|
||||
if (steps.isEmpty) return;
|
||||
await _api.post<Map<String, dynamic>>(
|
||||
'$_basePath/$templateId/steps',
|
||||
data: {
|
||||
'id': templateId,
|
||||
'steps': steps.map((step) => step.toJson(includeId: false)).toList(),
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _patchSteps(
|
||||
int templateId,
|
||||
List<ApprovalTemplateStepInput> steps,
|
||||
) async {
|
||||
await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$templateId/steps',
|
||||
data: {
|
||||
'id': templateId,
|
||||
'steps': steps.map((step) => step.toJson()).toList(),
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user