Automatically delete members when families are deleted
This commit is contained in:
parent
f344765dd8
commit
8f5131429f
@ -13,6 +13,7 @@ export function AsyncWidget(p: {
|
|||||||
errMsg: string;
|
errMsg: string;
|
||||||
build: () => React.ReactElement;
|
build: () => React.ReactElement;
|
||||||
ready?: boolean;
|
ready?: boolean;
|
||||||
|
errAdditionalElement?: () => React.ReactElement;
|
||||||
}): React.ReactElement {
|
}): React.ReactElement {
|
||||||
const [state, setState] = useState(State.Loading);
|
const [state, setState] = useState(State.Loading);
|
||||||
|
|
||||||
@ -62,6 +63,8 @@ export function AsyncWidget(p: {
|
|||||||
</Alert>
|
</Alert>
|
||||||
|
|
||||||
<Button onClick={load}>Réessayer</Button>
|
<Button onClick={load}>Réessayer</Button>
|
||||||
|
|
||||||
|
{p.errAdditionalElement && p.errAdditionalElement()}
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -55,6 +55,11 @@ export function BaseAuthenticatedPage(): React.ReactElement {
|
|||||||
loadKey="1"
|
loadKey="1"
|
||||||
load={load}
|
load={load}
|
||||||
errMsg="Echec du chargement des informations utilisateur !"
|
errMsg="Echec du chargement des informations utilisateur !"
|
||||||
|
errAdditionalElement={() => (
|
||||||
|
<>
|
||||||
|
<Button onClick={signOut}>Déconnexion</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
build={() => (
|
build={() => (
|
||||||
<UserContextK.Provider
|
<UserContextK.Provider
|
||||||
value={{
|
value={{
|
||||||
|
@ -46,8 +46,8 @@ CREATE TABLE members (
|
|||||||
sex VARCHAR(1) NULL,
|
sex VARCHAR(1) NULL,
|
||||||
time_create BIGINT NOT NULL,
|
time_create BIGINT NOT NULL,
|
||||||
time_update BIGINT NOT NULL,
|
time_update BIGINT NOT NULL,
|
||||||
mother integer NULL REFERENCES members,
|
mother integer NULL REFERENCES members ON DELETE SET NULL,
|
||||||
father integer NULL REFERENCES members,
|
father integer NULL REFERENCES members ON DELETE SET NULL,
|
||||||
birth_year smallint NULL,
|
birth_year smallint NULL,
|
||||||
birth_month smallint NULL,
|
birth_month smallint NULL,
|
||||||
birth_day smallint NULL,
|
birth_day smallint NULL,
|
||||||
|
@ -130,7 +130,7 @@ pub enum Sex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Sex {
|
impl Sex {
|
||||||
pub fn from_str(s: &str) -> Option<Self> {
|
pub fn parse_str(s: &str) -> Option<Self> {
|
||||||
match s {
|
match s {
|
||||||
"M" => Some(Sex::Male),
|
"M" => Some(Sex::Male),
|
||||||
"F" => Some(Sex::Female),
|
"F" => Some(Sex::Female),
|
||||||
@ -184,10 +184,7 @@ impl Member {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sex(&self) -> Option<Sex> {
|
pub fn sex(&self) -> Option<Sex> {
|
||||||
self.sex
|
self.sex.as_deref().map(Sex::parse_str).unwrap_or_default()
|
||||||
.as_deref()
|
|
||||||
.map(|s| Sex::from_str(s))
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_sex(&mut self, s: Option<Sex>) {
|
pub fn set_sex(&mut self, s: Option<Sex>) {
|
||||||
|
@ -5,7 +5,7 @@ use crate::models::{
|
|||||||
Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
|
Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
|
||||||
};
|
};
|
||||||
use crate::schema::{families, memberships};
|
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::string_utils::rand_str;
|
||||||
use crate::utils::time_utils::time;
|
use crate::utils::time_utils::time;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
@ -183,7 +183,10 @@ pub async fn update_family(family: &Family) -> anyhow::Result<()> {
|
|||||||
|
|
||||||
/// Delete a family
|
/// Delete a family
|
||||||
pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> {
|
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
|
// Remove all memberships
|
||||||
db_connection::execute(|conn| {
|
db_connection::execute(|conn| {
|
||||||
|
@ -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))
|
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
|
/// Check whether a member with a given id exists or not
|
||||||
pub async fn exists(family_id: FamilyID, member_id: MemberID) -> anyhow::Result<bool> {
|
pub async fn exists(family_id: FamilyID, member_id: MemberID) -> anyhow::Result<bool> {
|
||||||
db_connection::execute(|conn| {
|
db_connection::execute(|conn| {
|
||||||
@ -89,3 +98,11 @@ pub async fn delete(member: &Member) -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user