from datetime import datetime, timezone from typing import List, Optional from sqlalchemy.orm import Session from sqlalchemy import or_ from app.models.CrmModel import CrmTask, CrmCalendarEvent from app.schemas.Crm import ( CrmTaskCreate, CrmTaskUpdate, CrmCalendarEventCreate, CrmCalendarEventUpdate ) class CrmTaskCalendarService: @staticmethod def list_tasks( db: Session, tenant_id: str = "tenant_default", location_id: Optional[str] = None, status: Optional[str] = None, assignee_id: Optional[str] = None, entity_type: Optional[str] = None, entity_id: Optional[str] = None ) -> List[CrmTask]: query = db.query(CrmTask).filter(CrmTask.tenant_id == tenant_id) if location_id: query = query.filter(or_(CrmTask.location_id == location_id, CrmTask.location_id.is_(None))) if status: query = query.filter(CrmTask.status == status) if assignee_id: query = query.filter(CrmTask.assignee_id == assignee_id) if entity_type: query = query.filter(CrmTask.entity_type == entity_type) if entity_id: query = query.filter(CrmTask.entity_id == entity_id) return query.order_by(CrmTask.due_at.asc().nulls_last(), CrmTask.created_at.desc()).all() @staticmethod def create_task(db: Session, data: CrmTaskCreate, tenant_id: str = "tenant_default") -> CrmTask: task = CrmTask( tenant_id=tenant_id, location_id=data.location_id, title=data.title, description=data.description, assignee_id=data.assignee_id, entity_type=data.entity_type, entity_id=data.entity_id, priority=data.priority or "MEDIUM", status=data.status or "PENDING", due_at=data.due_at ) db.add(task) db.commit() db.refresh(task) return task @staticmethod def update_task(db: Session, task_id: str, data: CrmTaskUpdate, tenant_id: str = "tenant_default") -> Optional[CrmTask]: task = db.query(CrmTask).filter(CrmTask.task_id == task_id, CrmTask.tenant_id == tenant_id).first() if not task: return None update_data = data.model_dump(exclude_unset=True) if update_data.get("status") == "COMPLETED" and not task.completed_at: update_data["completed_at"] = datetime.now(timezone.utc) elif update_data.get("status") == "PENDING": update_data["completed_at"] = None for key, value in update_data.items(): setattr(task, key, value) db.commit() db.refresh(task) return task @staticmethod def delete_task(db: Session, task_id: str, tenant_id: str = "tenant_default") -> bool: task = db.query(CrmTask).filter(CrmTask.task_id == task_id, CrmTask.tenant_id == tenant_id).first() if not task: return False db.delete(task) db.commit() return True # --- CALENDAR EVENTS --- @staticmethod def list_calendar_events( db: Session, tenant_id: str = "tenant_default", location_id: Optional[str] = None, start_from: Optional[datetime] = None, end_until: Optional[datetime] = None ) -> List[CrmCalendarEvent]: query = db.query(CrmCalendarEvent).filter(CrmCalendarEvent.tenant_id == tenant_id) if location_id: query = query.filter(or_(CrmCalendarEvent.location_id == location_id, CrmCalendarEvent.location_id.is_(None))) if start_from: query = query.filter(CrmCalendarEvent.end_time >= start_from) if end_until: query = query.filter(CrmCalendarEvent.start_time <= end_until) return query.order_by(CrmCalendarEvent.start_time.asc()).all() @staticmethod def create_calendar_event(db: Session, data: CrmCalendarEventCreate, tenant_id: str = "tenant_default") -> CrmCalendarEvent: event = CrmCalendarEvent( tenant_id=tenant_id, location_id=data.location_id, task_id=data.task_id, title=data.title, description=data.description, source_type=data.source_type or "MANUAL", source_id=data.source_id, start_time=data.start_time, end_time=data.end_time, is_all_day=data.is_all_day or False, status=data.status or "SCHEDULED" ) db.add(event) db.commit() db.refresh(event) return event @staticmethod def update_calendar_event(db: Session, event_id: str, data: CrmCalendarEventUpdate, tenant_id: str = "tenant_default") -> Optional[CrmCalendarEvent]: event = db.query(CrmCalendarEvent).filter(CrmCalendarEvent.event_id == event_id, CrmCalendarEvent.tenant_id == tenant_id).first() if not event: return None update_data = data.model_dump(exclude_unset=True) for key, value in update_data.items(): setattr(event, key, value) db.commit() db.refresh(event) return event @staticmethod def delete_calendar_event(db: Session, event_id: str, tenant_id: str = "tenant_default") -> bool: event = db.query(CrmCalendarEvent).filter(CrmCalendarEvent.event_id == event_id, CrmCalendarEvent.tenant_id == tenant_id).first() if not event: return False db.delete(event) db.commit() return True