adding the local trip form

This commit is contained in:
Chris Cochrun 2024-01-10 10:25:22 -06:00
parent 0c692b3197
commit 9e5acd6aa5
6 changed files with 596 additions and 1 deletions

335
src/api/local_trip_form.rs Normal file
View file

@ -0,0 +1,335 @@
use actix_multipart::form::{text::Text, MultipartForm};
use actix_web::{post, HttpResponse};
use lettre::{
message::MultiPart,
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
#[derive(Debug, MultipartForm, Default)]
struct LocalForm {
#[multipart(rename = "firstname")]
first_name: Option<Text<String>>,
#[multipart(rename = "lastname")]
last_name: Option<Text<String>>,
#[multipart(rename = "parentfirstname")]
parent_first_name: Option<Text<String>>,
#[multipart(rename = "parentlastname")]
parent_last_name: Option<Text<String>>,
birthdate: Option<Text<String>>,
gender: Option<Text<String>>,
street: Option<Text<String>>,
city: Option<Text<String>>,
state: Option<Text<String>>,
zip: Option<Text<i32>>,
cellphone: Option<Text<String>>,
parentphone: Option<Text<String>>,
email: Option<Text<String>>,
parentemail: Option<Text<String>>,
school: Option<Text<String>>,
grade: Option<Text<String>>,
#[multipart(rename = "pastorfirstname")]
pastor_first_name: Option<Text<String>>,
#[multipart(rename = "pastorlastname")]
pastor_last_name: Option<Text<String>>,
#[multipart(rename = "churchattendance")]
church_attendance: Option<Text<String>>,
#[multipart(rename = "tfcgroup")]
tfc_group: Option<Text<String>>,
shirt: Option<Text<String>>,
}
#[post("/local-trip-form")]
pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpResponse {
let first = form
.first_name
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let last = form
.last_name
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let email_subject = format!("{} {} signed up for the local mission trip!", first, last);
let parent = format!(
"{} {}",
form.parent_first_name
.as_ref()
.unwrap_or(&Text {
0: String::from("")
})
.0
.clone(),
form.parent_last_name
.as_ref()
.unwrap_or(&Text {
0: String::from("")
})
.0
.clone()
);
let birthdate = form
.birthdate
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let gender = form
.gender
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let street = form
.street
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let city = form
.city
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let state = form
.state
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let zip = form.zip.as_ref().unwrap_or(&Text { 0: 0 }).0.clone();
let cellphone = form
.cellphone
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let parentphone = form
.parentphone
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let email = form
.email
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let parentemail = form
.parentemail
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let school = form
.school
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let grade = form
.grade
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let pastor = format!(
"{} {}",
form.pastor_first_name
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone(),
form.pastor_last_name
.as_ref()
.unwrap_or(&Text {
0: String::from("")
})
.0
.clone()
);
let church_attendance = form
.church_attendance
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let tfc_group = form
.tfc_group
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let shirt = form
.shirt
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
log::info!("{first} {last} signed up for the local mission trip!");
let email = markup::new! {
@markup::doctype()
html {
head {
title { @format!("{} {} signed up for the local mission trip!", first, last) }
style {
"table { border-collapse: collapse; width: 100% }"
"td, th { padding: 8px }"
"td { text-align: left; width: 70%; word-wrap: break-word }"
"th { text-align: right; border-right: 1px solid #ddd }"
"tr { border-bottom: 1px solid #ddd }"
"h1 { text-align: center }"
}
}
body {
h1 { @format!("Mission trip form for {} {}!", first, last) }
hr;
table {
tr {
th { "Name" }
td { @format!("{} {}", first, last) }
}
tr {
th { "Parent" }
td { @parent }
}
tr {
th { "Birthdate" }
td { @birthdate }
}
tr {
th { "Gender" }
td { @gender }
}
tr {
th { "Street" }
td { @street }
}
tr {
th { "City" }
td { @city }
}
tr {
th { "State" }
td { @state }
}
tr {
th { "Zip" }
td { @zip }
}
tr {
th { "Phone" }
td { @cellphone }
}
tr {
th { "Parent Phone" }
td { @parentphone }
}
tr {
th { "Email" }
td { @email }
}
tr {
th { "Parent Email" }
td { @parentemail }
}
tr {
th { "School" }
td { @school }
}
tr {
th { "Grade" }
td { @grade }
}
tr {
th { "Pastor" }
td { @pastor }
}
tr {
th { "Church Attendance" }
td { @church_attendance }
}
tr {
th { "TFC Group" }
td { @tfc_group }
}
tr {
th { "T-Shirt Size" }
td { @shirt }
}
}
}
}
};
log::info!("{:?}", form);
let multi = MultiPart::alternative_plain_html(String::from("Testing"), email.to_string());
if let Ok(m) = Message::builder()
.from(
"TFC ADMIN <no-reply@mail.tfcconnection.org>"
.parse()
.unwrap(),
)
.to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap())
.to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap())
.subject(email_subject)
.multipart(multi)
{
let sender = SmtpTransport::relay("mail.tfcconnection.org")
.ok()
.unwrap()
.credentials(Credentials::new(
"no-reply@mail.tfcconnection.org".to_owned(),
"r9f36mNZFtiW4f".to_owned(),
))
.authentication(vec![Mechanism::Plain])
.build();
match sender.send(&m) {
Ok(res) => log::info!("{:?}", res),
Err(e) => log::info!("{e}"),
}
} else {
log::info!("Email incorrect");
}
HttpResponse::Ok().body("thankyou")
}

View file

@ -1,5 +1,6 @@
pub mod church_form;
pub mod health_form;
pub mod local_trip_form;
pub mod mt_form;
pub mod parent_form;
pub mod teacher_form;