major restructure + Permission system
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -415,3 +415,19 @@ class CompanyDailyQuota(DBModel):
|
||||
tokens_used: int = 0
|
||||
created_at: Optional[datetime] = None
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class EmployeeCompanyPermissions(DBModel):
|
||||
"""Permissions granted by an employee to their company."""
|
||||
id: Optional[UUID] = None
|
||||
user_id: UUID
|
||||
company_id: UUID
|
||||
can_create_posts: bool = True
|
||||
can_view_posts: bool = True
|
||||
can_edit_posts: bool = True
|
||||
can_schedule_posts: bool = True
|
||||
can_manage_post_types: bool = True
|
||||
can_do_research: bool = True
|
||||
can_see_in_calendar: bool = True
|
||||
created_at: Optional[datetime] = None
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
Reference in New Issue
Block a user