Skip to content

Commit

Permalink
feat: make user org manager after org approval
Browse files Browse the repository at this point in the history
  • Loading branch information
sujanadh committed Feb 20, 2024
1 parent 4407f74 commit 945c8bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 28 additions & 5 deletions src/backend/app/organisations/organisation_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from app.organisations.organisation_schemas import OrganisationEdit, OrganisationIn
from app.s3 import add_obj_to_bucket
from app.users.user_crud import get_user
from app.users.user_deps import user_exists_in_db


async def init_admin_org(db: Session):
Expand Down Expand Up @@ -179,7 +180,10 @@ async def upload_logo_to_s3(


async def create_organisation(
db: Session, org_model: OrganisationIn, logo: Optional[UploadFile] = File(None)
db: Session,
org_model: OrganisationIn,
current_user: AuthUser,
logo: Optional[UploadFile] = File(None),
) -> db_models.DbOrganisation:
"""Creates a new organisation with the given name, description, url, type, and logo.
Expand All @@ -190,6 +194,7 @@ async def create_organisation(
org_model (OrganisationIn): Pydantic model for organisation input.
logo (UploadFile, optional): logo file of the organisation.
Defaults to File(...).
current_user: logged in user.
Returns:
DbOrganisation: SQLAlchemy Organisation model.
Expand All @@ -206,6 +211,7 @@ async def create_organisation(
try:
# Create new organisation without logo set
db_organisation = db_models.DbOrganisation(**org_model.model_dump())
db_organisation.user_id = current_user.id

db.add(db_organisation)
db.commit()
Expand Down Expand Up @@ -324,10 +330,27 @@ async def approve_organisation(db, organisation):
Returns:
Response: An HTTP response with the status code 200.
"""
log.info(f"Approving organisation ID {organisation.id}")
organisation.approved = True
db.commit()
return Response(status_code=HTTPStatus.OK)
try:
log.info(f"Approving organisation ID {organisation.id}")
user = await user_exists_in_db(organisation.user_id, db)

if organisation and user:
organisation.managers.append(user)
organisation.approved = True
db.commit()
return Response(
status_code=HTTPStatus.OK,
detail="Organisation is approved successfully",
)
else:
raise HTTPException(
status_code=404, detail="Organisation or user not found."
)
except Exception as e:
log.error(f"Error approving organisation: {str(e)}")
# Rollback the transaction if an error occurs
db.rollback()
raise HTTPException(status_code=500, detail=str(e)) from e


async def get_unapproved_org_detail(db, org_id):
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/organisations/organisation_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def create_organisation(
TODO refactor to use base64 encoded logo / no upload file.
TODO then we can use the pydantic model as intended.
"""
return await organisation_crud.create_organisation(db, org, logo)
return await organisation_crud.create_organisation(db, org, current_user, logo)


@router.patch("/{org_id}/", response_model=organisation_schemas.OrganisationOut)
Expand Down

0 comments on commit 945c8bf

Please sign in to comment.