major restructure + Permission system

This commit is contained in:
2026-02-20 13:33:51 +01:00
parent c1960932a2
commit 10b07d89ac
15 changed files with 3148 additions and 131 deletions

View File

@@ -1640,6 +1640,45 @@ class DatabaseClient:
)
return [LinkedInAccount(**row) for row in result.data]
# ==================== EMPLOYEE COMPANY PERMISSIONS ====================
async def get_employee_permissions(self, user_id: UUID, company_id: UUID):
"""Get permissions for an employee in a company. Returns None if no row exists (treat as all-true defaults)."""
from src.database.models import EmployeeCompanyPermissions
result = await asyncio.to_thread(
lambda: self.client.table("employee_company_permissions").select("*").eq(
"user_id", str(user_id)
).eq("company_id", str(company_id)).execute()
)
if result.data:
return EmployeeCompanyPermissions(**result.data[0])
return None
async def upsert_employee_permissions(self, user_id: UUID, company_id: UUID, updates: Dict[str, Any]) -> None:
"""Insert or update employee-company permissions."""
data = {
"user_id": str(user_id),
"company_id": str(company_id),
**updates
}
await asyncio.to_thread(
lambda: self.client.table("employee_company_permissions").upsert(
data, on_conflict="user_id,company_id"
).execute()
)
logger.info(f"Upserted permissions for user {user_id} in company {company_id}")
async def get_scheduled_posts_for_user(self, user_id: UUID) -> List[GeneratedPost]:
"""Get scheduled/approved/published posts for an employee (for their calendar)."""
result = await asyncio.to_thread(
lambda: self.client.table("generated_posts").select("*").eq(
"user_id", str(user_id)
).in_(
"status", ["approved", "ready", "scheduled", "published"]
).order("scheduled_at", desc=False, nullsfirst=False).execute()
)
return [GeneratedPost(**item) for item in result.data]
# Global database client instance
db = DatabaseClient()