mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: implement logged in organization joining + improvements
This commit is contained in:
parent
25ac82f4ad
commit
693a28721d
19 changed files with 200 additions and 29 deletions
|
|
@ -4,11 +4,11 @@ from fastapi import Depends, APIRouter, HTTPException, Response, status, Request
|
|||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from sqlmodel import Session
|
||||
from src.db.users import AnonymousUser, PublicUser, UserRead
|
||||
from src.db.users import AnonymousUser, UserRead
|
||||
from src.core.events.database import get_db_session
|
||||
from config.config import get_learnhouse_config
|
||||
from src.security.auth import AuthJWT, authenticate_user, get_current_user
|
||||
from src.services.auth.utils import get_google_user_info, signWithGoogle
|
||||
from src.services.auth.utils import signWithGoogle
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from src.services.orgs.invites import (
|
|||
get_invite_code,
|
||||
get_invite_codes,
|
||||
)
|
||||
from src.services.orgs.join import JoinOrg, join_org
|
||||
from src.services.orgs.users import (
|
||||
get_list_of_invited_users,
|
||||
get_organization_users,
|
||||
|
|
@ -99,6 +100,19 @@ async def api_get_org_users(
|
|||
return await get_organization_users(request, org_id, db_session, current_user)
|
||||
|
||||
|
||||
@router.post("/join")
|
||||
async def api_join_an_org(
|
||||
request: Request,
|
||||
args: JoinOrg,
|
||||
current_user: PublicUser = Depends(get_current_user),
|
||||
db_session: Session = Depends(get_db_session),
|
||||
):
|
||||
"""
|
||||
Get single Org by ID
|
||||
"""
|
||||
return await join_org(request, args, current_user, db_session)
|
||||
|
||||
|
||||
@router.put("/{org_id}/users/{user_id}/role/{role_uuid}")
|
||||
async def api_update_user_role(
|
||||
request: Request,
|
||||
|
|
|
|||
|
|
@ -85,7 +85,6 @@ async def api_create_user_with_orgid(
|
|||
"""
|
||||
Create User with Org ID
|
||||
"""
|
||||
print(await get_org_join_mechanism(request, org_id, current_user, db_session))
|
||||
|
||||
# TODO(fix) : This is temporary, logic should be moved to service
|
||||
if (
|
||||
|
|
|
|||
119
apps/api/src/services/orgs/join.py
Normal file
119
apps/api/src/services/orgs/join.py
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from fastapi import HTTPException, Request
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import Session, select
|
||||
from src.db.organizations import Organization
|
||||
from src.db.user_organizations import UserOrganization
|
||||
from src.db.users import AnonymousUser, PublicUser, User
|
||||
from src.services.orgs.invites import get_invite_code
|
||||
from src.services.orgs.orgs import get_org_join_mechanism
|
||||
|
||||
|
||||
class JoinOrg(BaseModel):
|
||||
org_id: int
|
||||
user_id: str
|
||||
invite_code: Optional[str]
|
||||
|
||||
|
||||
async def join_org(
|
||||
request: Request,
|
||||
args: JoinOrg,
|
||||
current_user: PublicUser | AnonymousUser,
|
||||
db_session: Session,
|
||||
):
|
||||
statement = select(Organization).where(Organization.id == args.org_id)
|
||||
result = db_session.exec(statement)
|
||||
|
||||
org = result.first()
|
||||
|
||||
if not org:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Organization not found",
|
||||
)
|
||||
|
||||
join_method = await get_org_join_mechanism(
|
||||
request, args.org_id, current_user, db_session
|
||||
)
|
||||
|
||||
# Get User
|
||||
statement = select(User).where(User.id == args.user_id)
|
||||
result = db_session.exec(statement)
|
||||
|
||||
user = result.first()
|
||||
|
||||
# Check if User isn't already part of the org
|
||||
statement = select(UserOrganization).where(
|
||||
UserOrganization.user_id == args.user_id, UserOrganization.org_id == args.org_id
|
||||
)
|
||||
result = db_session.exec(statement)
|
||||
|
||||
userorg = result.first()
|
||||
|
||||
if userorg:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="User is already part of that organization"
|
||||
)
|
||||
|
||||
if join_method == "inviteOnly" and user and org and args.invite_code:
|
||||
if user.id is not None and org.id is not None:
|
||||
|
||||
# Check if invite code exists
|
||||
inviteCode = await get_invite_code(
|
||||
request, org.id, args.invite_code, current_user, db_session
|
||||
)
|
||||
|
||||
if not inviteCode:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Invite code is incorrect",
|
||||
)
|
||||
|
||||
# Link user and organization
|
||||
user_organization = UserOrganization(
|
||||
user_id=user.id,
|
||||
org_id=org.id,
|
||||
role_id=3,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
)
|
||||
|
||||
db_session.add(user_organization)
|
||||
db_session.commit()
|
||||
|
||||
return "Great, You're part of the Organization"
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Something wrong, try later.",
|
||||
)
|
||||
|
||||
if join_method == "open" and user and org:
|
||||
if user.id is not None and org.id is not None:
|
||||
# Link user and organization
|
||||
user_organization = UserOrganization(
|
||||
user_id=user.id,
|
||||
org_id=org.id,
|
||||
role_id=3,
|
||||
creation_date=str(datetime.now()),
|
||||
update_date=str(datetime.now()),
|
||||
)
|
||||
|
||||
db_session.add(user_organization)
|
||||
db_session.commit()
|
||||
|
||||
return "Great, You're part of the Organization"
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Something wrong, try later.",
|
||||
)
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Something wrong, try later.",
|
||||
)
|
||||
|
|
@ -436,8 +436,7 @@ async def get_orgs_by_user(
|
|||
|
||||
orgs = result.all()
|
||||
|
||||
return orgs
|
||||
|
||||
return orgs #type:ignore
|
||||
|
||||
# Config related
|
||||
async def update_org_signup_mechanism(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue