moving all forms to use new logger and email abstraction

This commit is contained in:
Chris Cochrun 2024-11-08 07:14:07 -06:00
parent e8dbb324f3
commit ebaf770d81
7 changed files with 55 additions and 103 deletions

View file

@ -10,6 +10,7 @@ use lettre::{
use maud::html;
use maud::DOCTYPE;
use reqwest::{Client, Error};
use tracing::info;
use crate::email::send_email;
@ -45,7 +46,7 @@ struct CampForm {
#[post("/camp-form")]
pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResponse {
log::info!("a new form");
info!("a new form");
let first = form
.first_name
.as_ref()
@ -154,9 +155,9 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.clone();
let reg = registration.clone();
log::info!("Sending post to database");
info!("Sending post to database");
log::info!("{first} {last} signed up for camp!");
info!("{first} {last} signed up for camp!");
let email = html! {
(DOCTYPE)
meta charset="utf-8";
@ -262,17 +263,17 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
{
let _ = send_email(m);
} else {
log::info!("Email incorrect");
info!("Email incorrect");
}
match store_camp_form(form).await {
Ok(_) => log::info!("Successfully posted to nextcloud tables"),
Ok(_) => info!("Successfully posted to nextcloud tables"),
Err(e) => log::error!("Error in posting camp data: {:?}", e),
}
match health.as_str() {
"now" => {
log::info!("Sending them to fill out the health form");
info!("Sending them to fill out the health form");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
@ -286,7 +287,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
}
"later" => match reg.as_str() {
"now" => {
log::info!("Sending them to pay for registration now");
info!("Sending them to pay for registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
@ -296,7 +297,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.finish()
}
"full" => {
log::info!("Sending them to pay for the full registration now");
info!("Sending them to pay for the full registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
@ -306,7 +307,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.finish()
}
"later" => {
log::info!("{} would like to pay later", full_name);
info!("{} would like to pay later", full_name);
let html = html! {
div class="mt-8" {
h2 {

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)]
struct ChurchForm {
@ -84,7 +87,7 @@ pub async fn church_form(MultipartForm(form): MultipartForm<ChurchForm>) -> Http
.unwrap_or(&Text(String::from("")))
.0
.clone();
log::info!("{first} {last} filled out a Church Reference form!");
info!("{first} {last} filled out a Church Reference form!");
let email = markup::new! {
@markup::doctype()
html {
@ -156,21 +159,9 @@ pub async fn church_form(MultipartForm(form): MultipartForm<ChurchForm>) -> Http
.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}"),
}
let _ = send_email(m);
} else {
log::info!("Email incorrect");
info!("Email incorrect");
}
HttpResponse::Ok().body("hi")

View file

@ -9,6 +9,9 @@ use lettre::{
};
use maud::html;
use reqwest::{Client, Error};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)]
struct HealthForm {
@ -254,7 +257,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.unwrap_or(&Text(String::from("")))
.0
.clone();
log::info!("{first} {last} filled out a health form!");
info!("{first} {last} filled out a health form!");
let email = html! {
html {
head {
@ -387,10 +390,10 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
let mut path: Option<String> = Some(String::from(""));
let mut file_exists = false;
let mut filename = String::from("");
log::info!("Does the file exist? {:?}", file_exists);
info!("Does the file exist? {:?}", file_exists);
match store_health_form(&form).await {
Ok(_) => log::info!("Successfully posted the health form in nextcloud tables"),
Ok(_) => info!("Successfully posted the health form in nextcloud tables"),
Err(e) => log::error!("Error posting health form to nextcloud tables: {:?}", e),
}
@ -403,10 +406,10 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
path = Some(format!("./tmp/{}", file));
}
// let path = format!("./tmp/{}", file);
log::info!("saving to {}", path.clone().unwrap());
info!("saving to {}", path.clone().unwrap());
match f.file.persist(path.clone().unwrap()) {
Ok(f) => {
log::info!(
info!(
"Hey! We got a file! {:?}",
f.metadata().expect("Something bad happened mate")
);
@ -414,7 +417,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
file_exists = true;
}
}
Err(e) => log::info!("{:?}: Probably a missing image", e),
Err(e) => info!("{:?}: Probably a missing image", e),
}
}
}
@ -423,7 +426,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
let filebody = fs::read(path.clone().unwrap_or_default());
let content_type = ContentType::parse("image/jpg").unwrap();
let attachment = Attachment::new(filename).body(filebody.unwrap(), content_type);
// log::info!("The attachment is: {:?}", attachment.headers());
// info!("The attachment is: {:?}", attachment.headers());
MultiPart::mixed()
.singlepart(SinglePart::html(email.into_string()))
.singlepart(attachment)
@ -431,7 +434,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
MultiPart::alternative_plain_html(String::from("Testing"), email.into_string())
};
// log::info!("{:?}", multi);
// info!("{:?}", multi);
if let Ok(m) = Message::builder()
.from(
@ -444,26 +447,14 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.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}"),
}
let _ = send_email(m);
} else {
log::info!("Email incorrect");
info!("Email incorrect");
}
match registration.as_str() {
"now" => {
log::info!("Sending them to pay for registration now");
info!("Sending them to pay for registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
@ -473,7 +464,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.finish()
}
"full" => {
log::info!("Sending them to pay for the full registration now");
info!("Sending them to pay for the full registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
@ -483,7 +474,7 @@ pub async fn health_form(MultipartForm(form): MultipartForm<HealthForm>) -> Http
.finish()
}
"later" => {
log::info!("{} would like to pay later", full_name);
info!("{} would like to pay later", full_name);
let html = html! {
div class="mt-8" {
h2 {

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)]
struct LocalForm {
@ -165,7 +168,7 @@ pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpRe
.unwrap_or(&Text(String::from("")))
.0
.clone();
log::info!("{first} {last} signed up for the local mission trip!");
info!("{first} {last} signed up for the local mission trip!");
let email = markup::new! {
@markup::doctype()
html {
@ -260,7 +263,7 @@ pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpRe
}
}
};
log::info!("{:?}", form);
info!("{:?}", form);
let multi = MultiPart::alternative_plain_html(String::from("Testing"), email.to_string());
if let Ok(m) = Message::builder()
@ -274,21 +277,9 @@ pub async fn local_form(MultipartForm(form): MultipartForm<LocalForm>) -> HttpRe
.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}"),
}
let _ = send_email(m);
} else {
log::info!("Email incorrect");
info!("Email incorrect");
}
HttpResponse::Ok().body("thankyou")

View file

@ -1,17 +1,13 @@
use std::fs;
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
use actix_rt::Runtime;
use actix_web::{post, HttpResponse};
use color_eyre::Result;
use lettre::{
message::{header::ContentType, Attachment, MultiPart, SinglePart},
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
Message,
};
use markup::DynRender;
use maud::{html, PreEscaped, DOCTYPE};
use tracing::{error, info, warn};
use tracing::{error, info};
#[derive(Debug, MultipartForm)]
struct MtForm {
@ -228,7 +224,7 @@ pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> HttpResponse
let last = form.last_name.clone();
let email_subject = format!("{} {} signed up for mission trip!", first, last);
let filename_noext = format!("{}_{}", first, last);
log::info!("{first} {last} signed up for mission trip!");
info!("{first} {last} signed up for mission trip!");
let email = form.build_email().await;
let mut path = String::from("");
let mut file_exists = false;
@ -250,7 +246,7 @@ pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> HttpResponse
file_exists = true;
}
}
Err(e) => log::info!("{:?}: Probably a missing image", e),
Err(e) => info!("{:?}: Probably a missing image", e),
}
}

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)]
struct ParentForm {
@ -91,7 +94,7 @@ pub async fn parent_form(MultipartForm(form): MultipartForm<ParentForm>) -> Http
.unwrap_or(&Text(String::from("")))
.0
.clone();
log::info!("{first} {last} filled out a parent form!");
info!("{first} {last} filled out a parent form!");
let email = markup::new! {
@markup::doctype()
html {
@ -167,21 +170,9 @@ pub async fn parent_form(MultipartForm(form): MultipartForm<ParentForm>) -> Http
.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}"),
}
let _ = send_email(m);
} else {
log::info!("Email incorrect");
info!("Email incorrect");
}
HttpResponse::Ok().body("hi")

View file

@ -5,6 +5,9 @@ use lettre::{
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
use tracing::info;
use crate::email::send_email;
#[derive(Debug, MultipartForm, Default)]
struct TeacherForm {
@ -75,7 +78,7 @@ pub async fn teacher_form(MultipartForm(form): MultipartForm<TeacherForm>) -> Ht
.unwrap_or(&Text(String::from("")))
.0
.clone();
log::info!("{first} {last} filled out a teacher form!");
info!("{first} {last} filled out a teacher form!");
let email = markup::new! {
@markup::doctype()
html {
@ -143,21 +146,9 @@ pub async fn teacher_form(MultipartForm(form): MultipartForm<TeacherForm>) -> Ht
.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}"),
}
let _ = send_email(m);
} else {
log::info!("Email incorrect");
info!("Email incorrect");
}
HttpResponse::Ok().body("hi")