110 lines
3.6 KiB
Dart
110 lines
3.6 KiB
Dart
import 'package:dartz/dartz.dart';
|
|
import '../../../core/errors/failures.dart';
|
|
import '../../../core/utils/hierarchy_validator.dart';
|
|
import '../../../services/company_service.dart';
|
|
import '../base_usecase.dart';
|
|
import '../../../data/models/company/company_dto.dart';
|
|
|
|
/// 회사 삭제 가능 여부 검증 파라미터
|
|
class ValidateCompanyDeletionParams {
|
|
final int companyId;
|
|
|
|
const ValidateCompanyDeletionParams({
|
|
required this.companyId,
|
|
});
|
|
}
|
|
|
|
/// 회사 삭제 가능 여부 검증 결과
|
|
class CompanyDeletionValidationResult {
|
|
final bool canDelete;
|
|
final String message;
|
|
final List<String> blockers;
|
|
|
|
const CompanyDeletionValidationResult({
|
|
required this.canDelete,
|
|
required this.message,
|
|
this.blockers = const [],
|
|
});
|
|
}
|
|
|
|
/// 회사 삭제 가능 여부 검증 UseCase
|
|
class ValidateCompanyDeletionUseCase extends UseCase<CompanyDeletionValidationResult, ValidateCompanyDeletionParams> {
|
|
final CompanyService _companyService;
|
|
|
|
ValidateCompanyDeletionUseCase(this._companyService);
|
|
|
|
@override
|
|
Future<Either<Failure, CompanyDeletionValidationResult>> call(ValidateCompanyDeletionParams params) async {
|
|
try {
|
|
final blockers = <String>[];
|
|
|
|
// 1. 자식 회사 존재 여부 확인
|
|
final response = await _companyService.getCompanies(
|
|
page: 1,
|
|
perPage: 1000,
|
|
);
|
|
|
|
// CompanyDto 리스트로 변환 (검증용)
|
|
final companyResponses = response.items.map((company) => CompanyDto(
|
|
id: company.id ?? 0,
|
|
name: company.name,
|
|
address: company.address.toString(),
|
|
contactName: company.contactName ?? '',
|
|
contactPhone: company.contactPhone ?? '',
|
|
contactEmail: company.contactEmail ?? '',
|
|
isActive: true,
|
|
parentCompanyId: company.parentCompanyId,
|
|
registeredAt: DateTime.now(),
|
|
)).toList();
|
|
|
|
// HierarchyValidator를 사용한 삭제 가능 여부 검증
|
|
final deletionValidation = HierarchyValidator.validateDeletion(
|
|
companyId: params.companyId,
|
|
allCompanies: companyResponses,
|
|
);
|
|
|
|
if (!deletionValidation.isValid) {
|
|
blockers.add(deletionValidation.message);
|
|
blockers.addAll(deletionValidation.errors);
|
|
}
|
|
|
|
// 2. 연결된 사용자 존재 여부 확인
|
|
// TODO: CompanyService에 hasLinkedUsers 메서드 추가 후 활성화
|
|
// final hasUsers = await _companyService.hasLinkedUsers(params.companyId);
|
|
// if (hasUsers) {
|
|
// blockers.add('이 회사에 연결된 사용자가 존재합니다.');
|
|
// }
|
|
|
|
// 3. 연결된 장비 존재 여부 확인
|
|
// TODO: CompanyService에 hasLinkedEquipment 메서드 추가 후 활성화
|
|
// final hasEquipment = await _companyService.hasLinkedEquipment(params.companyId);
|
|
// if (hasEquipment) {
|
|
// blockers.add('이 회사에 연결된 장비가 존재합니다.');
|
|
// }
|
|
|
|
// 결과 생성
|
|
if (blockers.isEmpty) {
|
|
return const Right(CompanyDeletionValidationResult(
|
|
canDelete: true,
|
|
message: '이 회사를 삭제할 수 있습니다.',
|
|
));
|
|
} else {
|
|
return Right(CompanyDeletionValidationResult(
|
|
canDelete: false,
|
|
message: '이 회사를 삭제할 수 없습니다.',
|
|
blockers: blockers,
|
|
));
|
|
}
|
|
} on ServerFailure catch (e) {
|
|
return Left(ServerFailure(
|
|
message: e.message,
|
|
originalError: e,
|
|
));
|
|
} catch (e) {
|
|
return Left(UnknownFailure(
|
|
message: '회사 삭제 가능 여부 검증 중 오류가 발생했습니다.',
|
|
originalError: e,
|
|
));
|
|
}
|
|
}
|
|
} |