Created At: 2026-11-18T17:15:14Z Completed At: 2026-11-18T17:15:14Z Command exited with code 0. Command output: import secrets from datetime import datetime, timedelta, timezone from typing import Sequence from uuid import UUID from sqlalchemy.ext.asyncio import AsyncSession from core.constants import SystemRole, ROLE_PERMISSIONS_MAP from core.exceptions import ConflictError, NotFoundError, ForbiddenError, TenantAccessError from core.event_bus import get_event_bus, TenantCreatedEvent from modules.tenants.models import Tenant, TenantMember, Invitation from modules.tenants.repository import TenantRepository, TenantMemberRepository from modules.tenants.schemas import ( TenantCreateRequest, TenantUpdateRequest, MemberInviteRequest, ) class TenantService: def __init__(self, db: AsyncSession): self.db = db self.tenant_repo = TenantRepository(db) self.member_repo = TenantMemberRepository(db) async def create_tenant(self, owner_user_id: UUID, req: TenantCreateRequest) -> Tenant: slug = req.slug.lower().strip() existing = await self.tenant_repo.get_by_slug(slug) if existing: raise ConflictError(f"Tenant slug '{slug}' is already taken") tenant = Tenant( name=req.name.strip(), slug=slug, status="active", plan_id="free", ) await self.tenant_repo.create(tenant) # Owner membership member = TenantMember( tenant_id=tenant.id, user_id=owner_user_id, role=SystemRole.OWNER, is_active=True, ) await self.member_repo.create(member) # Dispatch event await get_event_bus().publish( TenantCreatedEvent( tenant_id=tenant.id, tenant_name=tenant.name, slug=tenant.slug, owner_user_id=owner_user_id, ) ) return tenant async def get_tenant_by_id(self, tenant_id: UUID) -> Tenant: tenant = await self.tenant_repo.get_by_id(tenant_id) if not tenant: raise NotFoundError("Tenant workspace not found") return tenant async def get_user_tenants(self, user_id: UUID) -> list[Tenant]: memberships = await self.member_repo.get_user_memberships(user_id) tenants = [] for m in memberships: t = await self.tenant_repo.get_by_id(m.tenant_id) if t and t.status != "deleted": tenants.append(t) return tenants async def update_tenant(self, tenant_id: UUID, req: TenantUpdateRequest) -> Tenant: tenant = await self.get_tenant_by_id(tenant_id) if req.name is not None: tenant.name = req.name.strip() if req.status is not None: tenant.status = req.status await self.db.flush() return tenant async def verify_member_access(self, tenant_id: UUID, user_id: UUID) -> TenantMember: membership = await self.member_repo.get_membership(tenant_id, user_id) if not membership: raise TenantAccessError("You are not a member of this tenant workspace") return membership async def get_tenant_members(self, tenant_id: UUID) -> Sequence[TenantMember]: return await self.member_repo.get_tenant_members(tenant_id) async def remove_member(self, tenant_id: UUID, user_id_to_remove: UUID, requester_role: str) -> None: if requester_role not in (SystemRole.OWNER, SystemRole.ADMIN): raise ForbiddenError("Only workspace owners or admins can remove members") deleted = await self.member_repo.delete_membership(tenant_id, user_id_to_remove) if not deleted: raise NotFoundError("Member not found in workspace")