fix: remove is_active check + auto-generate slug for organizations
This commit is contained in:
parent
4590cfd6d4
commit
b9aa833bd5
|
|
@ -22,7 +22,6 @@ async def list_organizations(
|
||||||
select(Organization)
|
select(Organization)
|
||||||
.join(OrganizationMember)
|
.join(OrganizationMember)
|
||||||
.where(OrganizationMember.user_id == user.id)
|
.where(OrganizationMember.user_id == user.id)
|
||||||
.where(Organization.is_active == True)
|
|
||||||
)
|
)
|
||||||
return result.scalars().all()
|
return result.scalars().all()
|
||||||
|
|
||||||
|
|
@ -33,16 +32,33 @@ async def create_organization(
|
||||||
db: AsyncSession = Depends(get_db)
|
db: AsyncSession = Depends(get_db)
|
||||||
):
|
):
|
||||||
"""Create a new organization."""
|
"""Create a new organization."""
|
||||||
|
import re
|
||||||
|
|
||||||
|
# Auto-generate slug if not provided
|
||||||
|
if not org_in.slug:
|
||||||
|
base_slug = re.sub(r'[^\w\s-]', '', org_in.name.lower())
|
||||||
|
base_slug = re.sub(r'[-\s]+', '-', base_slug).strip('-')
|
||||||
|
|
||||||
|
# Check uniqueness and add number if needed
|
||||||
|
slug = base_slug
|
||||||
|
counter = 1
|
||||||
|
while True:
|
||||||
|
result = await db.execute(select(Organization).where(Organization.slug == slug))
|
||||||
|
if not result.scalar_one_or_none():
|
||||||
|
break
|
||||||
|
counter += 1
|
||||||
|
slug = f"{base_slug}-{counter}"
|
||||||
|
else:
|
||||||
|
slug = org_in.slug
|
||||||
# Check slug uniqueness
|
# Check slug uniqueness
|
||||||
result = await db.execute(select(Organization).where(Organization.slug == org_in.slug))
|
result = await db.execute(select(Organization).where(Organization.slug == slug))
|
||||||
if result.scalar_one_or_none():
|
if result.scalar_one_or_none():
|
||||||
raise HTTPException(status_code=400, detail="Slug already exists")
|
raise HTTPException(status_code=400, detail="Slug already exists")
|
||||||
|
|
||||||
# Create org
|
# Create org
|
||||||
org = Organization(
|
org = Organization(
|
||||||
name=org_in.name,
|
name=org_in.name,
|
||||||
slug=org_in.slug,
|
slug=slug
|
||||||
description=org_in.description
|
|
||||||
)
|
)
|
||||||
db.add(org)
|
db.add(org)
|
||||||
await db.flush()
|
await db.flush()
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class OrganizationBase(BaseModel):
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
|
|
||||||
class OrganizationCreate(OrganizationBase):
|
class OrganizationCreate(OrganizationBase):
|
||||||
slug: str
|
slug: Optional[str] = None # Auto-generated if not provided
|
||||||
|
|
||||||
class OrganizationUpdate(BaseModel):
|
class OrganizationUpdate(BaseModel):
|
||||||
name: Optional[str] = None
|
name: Optional[str] = None
|
||||||
|
|
@ -22,8 +22,8 @@ class OrganizationRead(OrganizationBase):
|
||||||
id: int
|
id: int
|
||||||
slug: str
|
slug: str
|
||||||
logo_url: Optional[str] = None
|
logo_url: Optional[str] = None
|
||||||
plan: str
|
plan: str = "free"
|
||||||
is_active: bool
|
is_active: bool = True
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
member_count: Optional[int] = None
|
member_count: Optional[int] = None
|
||||||
|
|
||||||
|
|
@ -44,4 +44,3 @@ class MemberRead(BaseModel):
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
from_attributes = True
|
from_attributes = True
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue