automate-schnupper-mails #533

Merged
philipp merged 2 commits from automate-schnupper-mails into staging 2024-05-22 08:51:41 +02:00
3 changed files with 70 additions and 8 deletions

View File

@ -8,6 +8,7 @@ INSERT INTO "role" (name) VALUES ('Rennrudern');
INSERT INTO "role" (name) VALUES ('paid'); INSERT INTO "role" (name) VALUES ('paid');
INSERT INTO "role" (name) VALUES ('Vorstand'); INSERT INTO "role" (name) VALUES ('Vorstand');
INSERT INTO "role" (name) VALUES ('Bootsführer'); 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" (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,1);
INSERT INTO "user_role" (user_id, role_id) VALUES(1,2); INSERT INTO "user_role" (user_id, role_id) VALUES(1,2);

View File

@ -20,7 +20,7 @@ impl Mail {
subject: &str, subject: &str,
body: String, body: String,
smtp_pw: &str, smtp_pw: &str,
) { ) -> Result<(), String> {
let mut email = Message::builder() let mut email = Message::builder()
.from( .from(
"ASKÖ Ruderverein Donau Linz <no-reply@rudernlinz.at>" "ASKÖ Ruderverein Donau Linz <no-reply@rudernlinz.at>"
@ -45,6 +45,9 @@ impl Mail {
format!("Mail not sent to {single_rec}, because it could not be parsed"), format!("Mail not sent to {single_rec}, because it could not be parsed"),
) )
.await; .await;
return Err(format!(
"Mail nicht versandt, da '{single_rec}' keine gültige Mailadresse ist."
));
} }
} }
} }
@ -64,6 +67,8 @@ impl Mail {
// Send the email // Send the email
mailer.send(&email).unwrap(); mailer.send(&email).unwrap();
Ok(())
} }
pub async fn send(db: &SqlitePool, data: MailToSend<'_>, smtp_pw: String) -> bool { pub async fn send(db: &SqlitePool, data: MailToSend<'_>, smtp_pw: String) -> bool {

View File

@ -150,12 +150,15 @@ impl User {
}; };
if self.has_role(db, "Donau Linz").await { 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 { } 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 { } else {
return Err(format!( 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 self.name
)); ));
} }
@ -169,7 +172,51 @@ impl User {
Ok(()) 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: // 2 things to do:
// 1. Send mail to user // 1. Send mail to user
Mail::send_single( Mail::send_single(
@ -190,7 +237,7 @@ Wir freuen uns darauf, Dich bald am Wasser zu sehen und gemeinsam tolle Erfahrun
Riemen- & Dollenbruch, Riemen- & Dollenbruch,
ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100), ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100),
smtp_pw, smtp_pw,
).await; ).await?;
// 2. Notify all coxes // 2. Notify all coxes
let coxes = Role::find_by_name(db, "cox").await.unwrap(); let coxes = Role::find_by_name(db, "cox").await.unwrap();
@ -205,9 +252,16 @@ ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100),
None,None None,None
) )
.await; .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: // 2 things to do:
// 1. Send mail to user // 1. Send mail to user
Mail::send_single( Mail::send_single(
@ -232,7 +286,7 @@ Wir freuen uns darauf, dich bald am Wasser zu sehen und gemeinsam tolle Erfahrun
Riemen- & Dollenbruch Riemen- & Dollenbruch
ASKÖ Ruderverein Donau Linz", self.name), ASKÖ Ruderverein Donau Linz", self.name),
smtp_pw, smtp_pw,
).await; ).await?;
// 2. Notify all coxes // 2. Notify all coxes
let coxes = Role::find_by_name(db, "cox").await.unwrap(); let coxes = Role::find_by_name(db, "cox").await.unwrap();
@ -249,6 +303,8 @@ ASKÖ Ruderverein Donau Linz", self.name),
None, None,
) )
.await; .await;
Ok(())
} }
pub async fn fee(&self, db: &SqlitePool) -> Option<Fee> { pub async fn fee(&self, db: &SqlitePool) -> Option<Fee> {