diff --git a/seeds.sql b/seeds.sql index fe34b50..39d2513 100644 --- a/seeds.sql +++ b/seeds.sql @@ -8,6 +8,7 @@ INSERT INTO "role" (name) VALUES ('Rennrudern'); INSERT INTO "role" (name) VALUES ('paid'); INSERT INTO "role" (name) VALUES ('Vorstand'); INSERT INTO "role" (name) VALUES ('Bootsführer'); +INSERT INTO "role" (name) VALUES ('schnupperant'); INSERT INTO "user" (name, pw) VALUES('admin', '$argon2id$v=19$m=19456,t=2,p=1$dS/X5/sPEKTj4Rzs/CuvzQ$4P4NCw4Ukhv80/eQYTsarHhnw61JuL1KMx/L9dm82YM'); INSERT INTO "user_role" (user_id, role_id) VALUES(1,1); INSERT INTO "user_role" (user_id, role_id) VALUES(1,2); diff --git a/src/model/mail.rs b/src/model/mail.rs index dee2bfa..4630e6b 100644 --- a/src/model/mail.rs +++ b/src/model/mail.rs @@ -20,7 +20,7 @@ impl Mail { subject: &str, body: String, smtp_pw: &str, - ) { + ) -> Result<(), String> { let mut email = Message::builder() .from( "ASKÖ Ruderverein Donau Linz " @@ -45,6 +45,9 @@ impl Mail { format!("Mail not sent to {single_rec}, because it could not be parsed"), ) .await; + return Err(format!( + "Mail nicht versandt, da '{single_rec}' keine gültige Mailadresse ist." + )); } } } @@ -64,6 +67,8 @@ impl Mail { // Send the email mailer.send(&email).unwrap(); + + Ok(()) } pub async fn send(db: &SqlitePool, data: MailToSend<'_>, smtp_pw: String) -> bool { diff --git a/src/model/user.rs b/src/model/user.rs index 2d692b8..04b5e7c 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -150,12 +150,15 @@ impl User { }; if self.has_role(db, "Donau Linz").await { - self.send_welcome_mail_full_member(db, mail, smtp_pw).await; + self.send_welcome_mail_full_member(db, mail, smtp_pw) + .await?; } else if self.has_role(db, "scheckbuch").await { - self.send_welcome_mail_scheckbuch(db, mail, smtp_pw).await; + self.send_welcome_mail_scheckbuch(db, mail, smtp_pw).await?; + } else if self.has_role(db, "schnupperant").await { + self.send_welcome_mail_schnupper(db, mail, smtp_pw).await?; } else { return Err(format!( - "Could not send welcome mail, because user {} is not in Donau Linz or scheckbuch group", + "Could not send welcome mail, because user {} is not in Donau Linz or scheckbuch or schnupperant group", self.name )); } @@ -169,7 +172,51 @@ impl User { Ok(()) } - async fn send_welcome_mail_scheckbuch(&self, db: &SqlitePool, mail: &str, smtp_pw: &str) { + async fn send_welcome_mail_schnupper( + &self, + db: &SqlitePool, + mail: &str, + smtp_pw: &str, + ) -> Result<(), String> { + // 2 things to do: + // 1. Send mail to user + Mail::send_single( + db, + mail, + "Schnupperrudern beim ASKÖ Ruderverein Donau Linz", + format!( +"Hallo {0}, + +es freut uns sehr, dich bei unserem Schnupperkurs willkommen heißen zu dürfen. Detaillierte Informationen folgen noch, ich werde sie dir ein paar Tage vor dem Termin zusenden. + +Liebe Grüße, Philipp", self.name), + smtp_pw, + ).await?; + + // 2. Notify all coxes + let coxes = Role::find_by_name(db, "schnupper-betreuer").await.unwrap(); + Notification::create_for_role( + db, + &coxes, + &format!( + "Liebe Schnupper-Betreuer, {} nimmt am Schnupperkurs teil.", + self.name + ), + "Neue(r) Schnupperteilnehmer:in ", + None, + None, + ) + .await; + + Ok(()) + } + + async fn send_welcome_mail_scheckbuch( + &self, + db: &SqlitePool, + mail: &str, + smtp_pw: &str, + ) -> Result<(), String> { // 2 things to do: // 1. Send mail to user Mail::send_single( @@ -190,7 +237,7 @@ Wir freuen uns darauf, Dich bald am Wasser zu sehen und gemeinsam tolle Erfahrun Riemen- & Dollenbruch, ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100), smtp_pw, - ).await; + ).await?; // 2. Notify all coxes let coxes = Role::find_by_name(db, "cox").await.unwrap(); @@ -205,9 +252,16 @@ ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100), None,None ) .await; + + Ok(()) } - async fn send_welcome_mail_full_member(&self, db: &SqlitePool, mail: &str, smtp_pw: &str) { + async fn send_welcome_mail_full_member( + &self, + db: &SqlitePool, + mail: &str, + smtp_pw: &str, + ) -> Result<(), String> { // 2 things to do: // 1. Send mail to user Mail::send_single( @@ -232,7 +286,7 @@ Wir freuen uns darauf, dich bald am Wasser zu sehen und gemeinsam tolle Erfahrun Riemen- & Dollenbruch ASKÖ Ruderverein Donau Linz", self.name), smtp_pw, - ).await; + ).await?; // 2. Notify all coxes let coxes = Role::find_by_name(db, "cox").await.unwrap(); @@ -249,6 +303,8 @@ ASKÖ Ruderverein Donau Linz", self.name), None, ) .await; + + Ok(()) } pub async fn fee(&self, db: &SqlitePool) -> Option {