import 'package:dartz/dartz.dart'; import 'package:injectable/injectable.dart'; import '../../../data/models/license/license_dto.dart'; import '../../../models/license_model.dart'; import '../../repositories/license_repository.dart'; import '../../../core/errors/failures.dart'; import '../base_usecase.dart'; /// 라이선스 생성 UseCase @injectable class CreateLicenseUseCase implements UseCase { final LicenseRepository repository; CreateLicenseUseCase(this.repository); @override Future> call(CreateLicenseParams params) async { try { // 비즈니스 로직: 만료일 검증 if (params.expiryDate.isBefore(params.purchaseDate)) { return Left(ValidationFailure(message: '만료일은 구매일 이후여야 합니다')); } // 비즈니스 로직: 최소 라이선스 기간 검증 (30일) final duration = params.expiryDate.difference(params.purchaseDate).inDays; if (duration < 30) { return Left(ValidationFailure(message: '라이선스 기간은 최소 30일 이상이어야 합니다')); } final license = License( licenseKey: params.licenseKey, productName: params.productName, vendor: params.vendor, licenseType: params.licenseType, userCount: params.userCount, purchaseDate: params.purchaseDate, expiryDate: params.expiryDate, purchasePrice: params.purchasePrice, companyId: params.companyId, branchId: params.branchId, remark: params.remark, ); final result = await repository.createLicense(license); return result.map((createdLicense) => LicenseDto( id: createdLicense.id!, licenseKey: createdLicense.licenseKey, productName: createdLicense.productName, vendor: createdLicense.vendor, licenseType: createdLicense.licenseType, userCount: createdLicense.userCount, purchaseDate: createdLicense.purchaseDate, expiryDate: createdLicense.expiryDate, purchasePrice: createdLicense.purchasePrice, companyId: createdLicense.companyId, branchId: createdLicense.branchId, assignedUserId: createdLicense.assignedUserId, remark: createdLicense.remark, isActive: createdLicense.isActive, createdAt: createdLicense.createdAt ?? DateTime.now(), updatedAt: createdLicense.updatedAt ?? DateTime.now(), companyName: createdLicense.companyName, branchName: createdLicense.branchName, assignedUserName: createdLicense.assignedUserName, )); } catch (e) { return Left(ServerFailure(message: e.toString())); } } } /// 라이선스 생성 파라미터 class CreateLicenseParams { final String licenseKey; final String productName; final String? vendor; final String? licenseType; final int? userCount; final DateTime purchaseDate; final DateTime expiryDate; final double? purchasePrice; final int companyId; final int? branchId; final String? remark; CreateLicenseParams({ required this.licenseKey, required this.productName, this.vendor, this.licenseType, this.userCount, required this.purchaseDate, required this.expiryDate, this.purchasePrice, required this.companyId, this.branchId, this.remark, }); }