Automatically delete members when families are deleted

This commit is contained in:
Pierre HUBERT 2023-08-04 19:17:51 +02:00
parent f344765dd8
commit 8f5131429f
6 changed files with 34 additions and 9 deletions

View File

@ -13,6 +13,7 @@ export function AsyncWidget(p: {
errMsg: string;
build: () => React.ReactElement;
ready?: boolean;
errAdditionalElement?: () => React.ReactElement;
}): React.ReactElement {
const [state, setState] = useState(State.Loading);
@ -62,6 +63,8 @@ export function AsyncWidget(p: {
</Alert>
<Button onClick={load}>Réessayer</Button>
{p.errAdditionalElement && p.errAdditionalElement()}
</Box>
);

View File

@ -55,6 +55,11 @@ export function BaseAuthenticatedPage(): React.ReactElement {
loadKey="1"
load={load}
errMsg="Echec du chargement des informations utilisateur !"
errAdditionalElement={() => (
<>
<Button onClick={signOut}>Déconnexion</Button>
</>
)}
build={() => (
<UserContextK.Provider
value={{

View File

@ -46,8 +46,8 @@ CREATE TABLE members (
sex VARCHAR(1) NULL,
time_create BIGINT NOT NULL,
time_update BIGINT NOT NULL,
mother integer NULL REFERENCES members,
father integer NULL REFERENCES members,
mother integer NULL REFERENCES members ON DELETE SET NULL,
father integer NULL REFERENCES members ON DELETE SET NULL,
birth_year smallint NULL,
birth_month smallint NULL,
birth_day smallint NULL,

View File

@ -130,7 +130,7 @@ pub enum Sex {
}
impl Sex {
pub fn from_str(s: &str) -> Option<Self> {
pub fn parse_str(s: &str) -> Option<Self> {
match s {
"M" => Some(Sex::Male),
"F" => Some(Sex::Female),
@ -184,10 +184,7 @@ impl Member {
}
pub fn sex(&self) -> Option<Sex> {
self.sex
.as_deref()
.map(|s| Sex::from_str(s))
.unwrap_or_default()
self.sex.as_deref().map(Sex::parse_str).unwrap_or_default()
}
pub fn set_sex(&mut self, s: Option<Sex>) {

View File

@ -5,7 +5,7 @@ use crate::models::{
Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
};
use crate::schema::{families, memberships};
use crate::services::users_service;
use crate::services::{members_service, users_service};
use crate::utils::string_utils::rand_str;
use crate::utils::time_utils::time;
use diesel::prelude::*;
@ -183,7 +183,10 @@ pub async fn update_family(family: &Family) -> anyhow::Result<()> {
/// Delete a family
pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> {
// TODO : delete members and couples
// TODO : delete couples
// Remove all family members
members_service::delete_all_family(family_id).await?;
// Remove all memberships
db_connection::execute(|conn| {

View File

@ -25,6 +25,15 @@ pub async fn get_by_id(id: MemberID) -> anyhow::Result<Member> {
db_connection::execute(|conn| members::table.filter(members::dsl::id.eq(id.0)).first(conn))
}
/// Get all the members of a family
pub async fn get_all_of_family(id: FamilyID) -> anyhow::Result<Vec<Member>> {
db_connection::execute(|conn| {
members::table
.filter(members::dsl::family_id.eq(id.0))
.get_results(conn)
})
}
/// Check whether a member with a given id exists or not
pub async fn exists(family_id: FamilyID, member_id: MemberID) -> anyhow::Result<bool> {
db_connection::execute(|conn| {
@ -89,3 +98,11 @@ pub async fn delete(member: &Member) -> anyhow::Result<()> {
Ok(())
}
/// Delete all the members of a family
pub async fn delete_all_family(family_id: FamilyID) -> anyhow::Result<()> {
for m in get_all_of_family(family_id).await? {
delete(&m).await?;
}
Ok(())
}