From 9e5baa6333e57a0c30a58add889994db318c890f Mon Sep 17 00:00:00 2001 From: Sujan Adhikari Date: Mon, 11 Nov 2024 23:23:40 +0545 Subject: [PATCH] fix(backend): get org_id from db_org (org) from dependency org_admin and delete org managers on deletion of org --- src/backend/app/db/models.py | 6 +++++- src/backend/app/organisations/organisation_routes.py | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/backend/app/db/models.py b/src/backend/app/db/models.py index a2292335d8..bd62346174 100644 --- a/src/backend/app/db/models.py +++ b/src/backend/app/db/models.py @@ -516,7 +516,11 @@ async def delete(cls, db: Connection, org_id: int) -> bool: DELETE FROM projects WHERE organisation_id = %(org_id)s RETURNING organisation_id - ), deleted_org AS ( + ), deleted_org_managers AS ( + DELETE FROM organisation_managers + WHERE organisation_id = %(org_id)s + RETURNING organisation_id + ),deleted_org AS ( DELETE FROM organisations WHERE id = %(org_id)s RETURNING id diff --git a/src/backend/app/organisations/organisation_routes.py b/src/backend/app/organisations/organisation_routes.py index eba6d7edb4..62c49eb4fe 100644 --- a/src/backend/app/organisations/organisation_routes.py +++ b/src/backend/app/organisations/organisation_routes.py @@ -121,12 +121,13 @@ async def delete_org( org_user_dict: Annotated[AuthUser, Depends(org_admin)], ): """Delete an organisation.""" - org_deleted = await DbOrganisation.delete(db, org_user_dict.id) + org = org_user_dict.get("org") + org_deleted = await DbOrganisation.delete(db, org.id) if not org_deleted: - log.error(f"Failed deleting org ({org_user_dict.name}).") + log.error(f"Failed deleting org ({org.name}).") raise HTTPException( status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - detail=f"Failed deleting org ({org_user_dict.name}).", + detail=f"Failed deleting org ({org.name}).", ) return Response(status_code=HTTPStatus.NO_CONTENT)