Can leave a family

This commit is contained in:
2023-06-21 17:35:07 +02:00
parent ad54f83c2c
commit a94b26b23c
5 changed files with 60 additions and 8 deletions

View File

@ -132,9 +132,49 @@ pub async fn get_family_membership(
})
}
/// Delete a family
pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> {
// TODO : delete members and couples
// Remove all memberships
db_connection::execute(|conn| {
diesel::delete(
memberships::dsl::memberships.filter(memberships::dsl::family_id.eq(family_id.0)),
)
.execute(conn)
})?;
// Remove the family itself
db_connection::execute(|conn| {
diesel::delete(families::dsl::families.filter(families::dsl::id.eq(family_id.0)))
.execute(conn)
})?;
Ok(())
}
/// Remove a membership to a family
pub async fn remove_membership(_family_id: FamilyID, _user_id: UserID) {
todo!()
pub async fn remove_membership(family_id: FamilyID, user_id: UserID) -> anyhow::Result<()> {
let family = get_family_membership(family_id, user_id).await?;
if family.is_admin && family.count_admins == 1 {
// We need to delete the whole family
delete_family(family_id).await
} else {
// Remove the single membership
db_connection::execute(|conn| {
diesel::delete(
memberships::dsl::memberships.filter(
memberships::dsl::user_id
.eq(user_id.0)
.and(memberships::dsl::family_id.eq(family_id.0)),
),
)
.execute(conn)
})?;
Ok(())
}
}
/// Remove all memberships of user

View File

@ -177,9 +177,10 @@ pub async fn delete_account(user: &User) -> anyhow::Result<()> {
login_token_service::disconnect_user_from_all_devices(user.id()).await?;
db_connection::execute(|conn| {
diesel::delete(users::dsl::users.filter(users::dsl::id.eq(user.id().0))).execute(conn)?;
Ok(())
})
diesel::delete(users::dsl::users.filter(users::dsl::id.eq(user.id().0))).execute(conn)
})?;
Ok(())
}
/// Mark account as validated