119 lines
3.9 KiB
Python
119 lines
3.9 KiB
Python
"""
|
|
Command for bulk scheduling services.
|
|
"""
|
|
from typing import Any, Dict, List
|
|
from datetime import datetime
|
|
|
|
from backend.core.commands.base import Command, CommandResult
|
|
from backend.core.repositories.accounts.accounts import AccountRepository
|
|
from backend.core.repositories.schedules.schedules import ScheduleRepository
|
|
from backend.core.factories.services.services import ServiceFactory
|
|
from backend.core.utils.validators import validate_required_fields
|
|
|
|
|
|
class BulkScheduleServicesCommand(Command):
|
|
"""
|
|
Command to bulk schedule services for multiple accounts.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
account_repo: AccountRepository,
|
|
schedule_repo: ScheduleRepository,
|
|
account_ids: List[str],
|
|
year: int,
|
|
month: int
|
|
):
|
|
"""
|
|
Initialize the bulk schedule services command.
|
|
|
|
Args:
|
|
account_repo: Repository for account operations.
|
|
schedule_repo: Repository for schedule operations.
|
|
account_ids: List of account IDs to schedule services for.
|
|
year: The year for scheduling.
|
|
month: The month for scheduling (1-12).
|
|
"""
|
|
self.account_repo = account_repo
|
|
self.schedule_repo = schedule_repo
|
|
self.account_ids = account_ids
|
|
self.year = year
|
|
self.month = month
|
|
|
|
def validate(self) -> Dict[str, Any]:
|
|
"""
|
|
Validate the bulk schedule services request.
|
|
|
|
Returns:
|
|
Dict[str, Any]: Validation result with 'is_valid' and optional 'errors'.
|
|
"""
|
|
errors = []
|
|
|
|
# Validate required fields
|
|
field_values = {
|
|
'account_ids': self.account_ids,
|
|
'year': self.year,
|
|
'month': self.month
|
|
}
|
|
missing_fields = validate_required_fields(field_values, ['account_ids', 'year', 'month'])
|
|
if missing_fields:
|
|
return {
|
|
'is_valid': False,
|
|
'errors': [f"Required fields missing: {', '.join(missing_fields)}"]
|
|
}
|
|
|
|
# Validate account IDs
|
|
if not self.account_ids:
|
|
errors.append("No account IDs provided")
|
|
|
|
# Validate year and month
|
|
current_year = datetime.now().year
|
|
if self.year < current_year or self.year > current_year + 5:
|
|
errors.append(f"Year must be between {current_year} and {current_year + 5}")
|
|
|
|
if self.month < 1 or self.month > 12:
|
|
errors.append("Month must be between 1 and 12")
|
|
|
|
return {
|
|
'is_valid': len(errors) == 0,
|
|
'errors': errors
|
|
}
|
|
|
|
def execute(self) -> CommandResult[Dict[str, Any]]:
|
|
"""
|
|
Execute the bulk schedule services command.
|
|
|
|
Returns:
|
|
CommandResult[Dict[str, Any]]: Result of the command execution with scheduled services.
|
|
"""
|
|
# Validate command data
|
|
validation = self.validate()
|
|
if not validation['is_valid']:
|
|
return CommandResult.failure_result(validation['errors'])
|
|
|
|
try:
|
|
# Get active schedules for the specified accounts
|
|
active_schedules = []
|
|
for account_id in self.account_ids:
|
|
active_schedule = self.schedule_repo.get_active_by_account(account_id)
|
|
if active_schedule:
|
|
active_schedules.append(active_schedule)
|
|
|
|
# Generate services for each account
|
|
result = ServiceFactory.generate_services_for_accounts(
|
|
active_schedules,
|
|
self.year,
|
|
self.month
|
|
)
|
|
|
|
return CommandResult.success_result(
|
|
result,
|
|
f"Bulk scheduled services for {len(active_schedules)} accounts for {self.month}/{self.year}"
|
|
)
|
|
|
|
except Exception as e:
|
|
return CommandResult.failure_result(
|
|
str(e),
|
|
"Failed to bulk schedule services"
|
|
)
|