tfcconnection/src/api/teacher_form.rs
2024-11-08 07:20:27 -06:00

151 lines
4.9 KiB
Rust

use actix_multipart::form::{text::Text, MultipartForm};
use actix_web::{post, HttpResponse};
use lettre::{message::MultiPart, Message};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)]
struct TeacherForm {
#[multipart(rename = "firstname")]
first_name: Option<Text<String>>,
#[multipart(rename = "lastname")]
last_name: Option<Text<String>>,
#[multipart(rename = "studentfirstname")]
student_first_name: Option<Text<String>>,
#[multipart(rename = "studentlastname")]
student_last_name: Option<Text<String>>,
relationship: Option<Text<String>>,
attitudes: Option<Text<String>>,
positive: Option<Text<String>>,
negative: Option<Text<String>>,
#[multipart(rename = "team-challenges")]
team: Option<Text<String>>,
#[multipart(rename = "extra-info")]
notes: Option<Text<String>>,
}
#[post("/teacher-form")]
pub async fn teacher_form(MultipartForm(form): MultipartForm<TeacherForm>) -> HttpResponse {
let first = form.first_name.as_ref().unwrap().0.clone();
let last = form.last_name.as_ref().unwrap().0.clone();
let student = format!(
"{} {}",
form.student_first_name.as_ref().unwrap().0.clone(),
form.student_last_name.as_ref().unwrap().0.clone()
);
let email_subject = format!(
"{} {} filled out a teacher form for {}!",
first, last, student
);
let relationship = form
.relationship
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
let attitudes = form
.attitudes
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
let positive = form
.positive
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
let negative = form
.negative
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
let team = form
.team
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
let notes = form
.notes
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
info!("{first} {last} filled out a teacher form!");
let email = markup::new! {
@markup::doctype()
html {
head {
title { @format!("{} {} filled out a teacher form for {}!", first, last, student) }
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!("Teacher Reference form for {}!", student) }
hr;
table {
tr {
th { "Name" }
td { @format!("{} {}", first, last) }
}
tr {
th { "Student" }
td { @student }
}
tr {
th { "Relationship to teen" }
td { @relationship }
}
tr {
th { "Attitude in classroom" }
td { @attitudes }
}
tr {
th { "Positive Attributes" }
td { @positive }
}
tr {
th { "Negaitive Attributes" }
td { @negative }
}
tr {
th { "Teamwork" }
td { @team }
}
tr {
th { "Other Notes" }
td { @notes }
}
}
}
}
};
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 _ = send_email(m);
} else {
info!("Email incorrect");
}
HttpResponse::Ok().body("hi")
}