From 1cb67eee691f6020e181ad0f52c945319176828b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Wed, 11 Jul 2018 16:30:03 +0200 Subject: [PATCH] Implement leave organization (accessed from the bottom of the user's settings page) --- src/api/core/mod.rs | 1 + src/api/core/organizations.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs index 24a7de1d..8609ed3d 100644 --- a/src/api/core/mod.rs +++ b/src/api/core/mod.rs @@ -65,6 +65,7 @@ pub fn routes() -> Vec { get_organization, create_organization, delete_organization, + leave_organization, get_user_collections, get_org_collections, get_org_collection_detail, diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 1d53222d..8c87a1dd 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -73,6 +73,29 @@ fn delete_organization(org_id: String, data: JsonUpcase, headers: } } +#[post("/organizations//leave")] +fn leave_organization(org_id: String, headers: Headers, conn: DbConn) -> EmptyResult { + match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) { + None => err!("User not part of organization"), + Some(user_org) => { + if user_org.type_ == UserOrgType::Owner as i32 { + let num_owners = UserOrganization::find_by_org_and_type( + &org_id, UserOrgType::Owner as i32, &conn) + .len(); + + if num_owners <= 1 { + err!("The last owner can't leave") + } + } + + match user_org.delete(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed leaving the organization") + } + } + } +} + #[get("/organizations/")] fn get_organization(org_id: String, _headers: OwnerHeaders, conn: DbConn) -> JsonResult { match Organization::find_by_uuid(&org_id, &conn) {