nexus-3/backend/core/services/scheduling.py
2026-01-26 10:30:49 -05:00

123 lines
3.9 KiB
Python

"""
Scheduling services for services and projects.
"""
from typing import List, Dict, Any
from datetime import date
from django.utils import timezone
from django.db import transaction
from backend.core.models import Service
from backend.core.repositories.accounts.accounts import AccountRepository
from backend.core.repositories.schedules.schedules import ScheduleRepository
from backend.core.repositories.services.services import ServiceRepository
class SchedulingService:
"""
Service for scheduling operations.
"""
@staticmethod
@transaction.atomic
def generate_services_from_schedule(
schedule_id: str,
start_date: date,
end_date: date
) -> List[Service]:
"""
Generate services based on a schedule for a date range.
Args:
schedule_id: Schedule ID
start_date: Start date (inclusive)
end_date: End date (inclusive)
Returns:
List of generated services
"""
return ScheduleRepository.generate_services(schedule_id, start_date, end_date)
@staticmethod
@transaction.atomic
def generate_services_for_all_accounts(
start_date: date,
end_date: date
) -> Dict[str, List[Service]]:
"""
Generate services for all accounts with active schedules.py.
Args:
start_date: Start date (inclusive)
end_date: End date (inclusive)
Returns:
Dictionary mapping account IDs to lists of generated services
"""
# Get all active schedules.py
active_schedules = ScheduleRepository.get_active()
# Generate services for each schedule
result = {}
for schedule in active_schedules:
services = ScheduleRepository.generate_services(
str(schedule.id), # Convert UUID to string if needed
start_date,
end_date
)
# Access account.id instead of account_id
account_id = str(schedule.account.id)
result[account_id] = services
return result
@staticmethod
def get_daily_service_schedule(date_str: str = None) -> List[Dict[str, Any]]:
"""
Get schedule of services for a specific day.
Args:
date_str: Date string (YYYY-MM-DD) or today if None
Returns:
List of services with account and team member info
"""
# Parse date or use today
if date_str:
try:
target_date = date.fromisoformat(date_str)
except ValueError:
# Invalid date format, use today
target_date = timezone.now().date()
else:
target_date = timezone.now().date()
# Get services for the day
services = ServiceRepository.get_all(date=target_date)
# Format the result
result = []
for service in services:
# Access account.id instead of account_id
account = AccountRepository.get_by_id(str(service.account.id))
service_data = {
'id': str(service.id),
'account_name': account.name if account else 'Unknown',
'account_address': f"{account.street_address}, {account.city}, {account.state}" if account else 'Unknown',
'status': service.status,
'deadline_start': service.deadline_start.strftime('%H:%M') if service.deadline_start else None,
'deadline_end': service.deadline_end.strftime('%H:%M') if service.deadline_end else None,
'team_members': [
{
'id': str(tm.id),
'name': f"{tm.first_name} {tm.last_name}",
'role': tm.role
}
for tm in service.team_members.all()
]
}
result.append(service_data)
return result