a much better camp_form.rs setup

This commit is contained in:
Chris Cochrun 2024-07-01 15:59:44 -05:00
parent 7c12830b2d
commit 4a8325aa35
5 changed files with 551 additions and 85 deletions

View file

@ -1,13 +1,15 @@
use actix_multipart::form::{json, text::Text, MultipartForm};
use actix_web::{post, HttpResponse};
use std::collections::HashMap;
use actix_multipart::form::{text::Text, MultipartForm};
use actix_web::{http::StatusCode, post, HttpResponse, HttpResponseBuilder};
use lettre::{
message::MultiPart,
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
use reqwest::Request;
use super::errors::ApiError;
use maud::html;
use maud::DOCTYPE;
use reqwest::{Client, Error};
#[derive(Debug, MultipartForm, Default)]
struct CampForm {
@ -19,6 +21,7 @@ struct CampForm {
parent_first_name: Option<Text<String>>,
#[multipart(rename = "parent-last-name")]
parent_last_name: Option<Text<String>>,
#[multipart(rename = "birth-date")]
birthdate: Option<Text<String>>,
gender: Option<Text<String>>,
street: Option<Text<String>>,
@ -34,6 +37,8 @@ struct CampForm {
allergies: Option<Text<String>>,
week: Option<Text<String>>,
registration: Option<Text<String>>,
#[multipart(rename = "health-form")]
health_form: Option<Text<String>>,
}
#[post("/camp-form")]
@ -139,13 +144,23 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.unwrap_or(&Text(String::from("")))
.0
.clone();
let health = form
.health_form
.as_ref()
.unwrap_or(&Text(String::from("")))
.0
.clone();
let reg = registration.clone();
log::info!("Sending post to database");
log::info!("{first} {last} signed up for camp!");
let email = markup::new! {
@markup::doctype()
let email = html! {
(DOCTYPE)
meta charset="utf-8";
html {
head {
title { @format!("{} {} signed up for camp!", first, last) }
title { (first) " " (last) " signed up for camp!" }
style {
"table { border-collapse: collapse; width: 100% }"
"td, th { padding: 8px }"
@ -156,68 +171,72 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
}
}
body {
h1 { @format!("Camp form for {} {}!", first, last) }
h1 { "Camp form for " (first) " " (last) }
hr;
table {
tr {
th { "Name" }
td { @format!("{} {}", first, last) }
td { (first) " " (last) }
}
tr {
th { "Parent" }
td { @parent }
td { (parent) }
}
tr {
th { "Birthdate" }
td { @birthdate }
td { (birthdate) }
}
tr {
th { "Gender" }
td { @gender }
td { (gender) }
}
tr {
th { "Street" }
td { @street }
td { (street) }
}
tr {
th { "City" }
td { @city }
td { (city) }
}
tr {
th { "State" }
td { @state }
td { (state) }
}
tr {
th { "Zip" }
td { @zip }
td { (zip) }
}
tr {
th { "Parent Phone" }
td { @parent_phone }
td { (parent_phone) }
}
tr {
th { "Parent Email" }
td { @parent_email }
td { (parent_email) }
}
tr {
th { "Grade" }
td { @grade }
td { (grade) }
}
tr {
th { "Camper Allergies" }
td { @allergies }
td { (allergies) }
}
tr {
th { "T-Shirt Size" }
td { @shirt }
td { (shirt) }
}
tr {
th { "Week Choice" }
td { @week }
td { (week) }
}
tr {
th { "Health Form" }
td { (health) }
}
tr {
th { "Registration" }
td { @registration }
td { (registration) }
}
}
}
@ -225,7 +244,7 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
};
let multi = MultiPart::alternative_plain_html(
String::from("A camp form was filled out!"),
email.to_string(),
email.into_string(),
);
if let Ok(m) = Message::builder()
@ -249,78 +268,139 @@ pub async fn camp_form(MultipartForm(form): MultipartForm<CampForm>) -> HttpResp
.authentication(vec![Mechanism::Plain])
.build();
match sender.send(&m) {
Ok(res) => log::info!("{:?}", res),
Ok(res) => log::info!(
"Successfully sent email to server with this response: {:?}",
res
),
Err(e) => log::error!("{e}"),
}
} else {
log::info!("Email incorrect");
}
match reg.as_str() {
match store_camp_form(form).await {
Ok(_) => log::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 pay for registration now");
log::info!("Sending them to fill out the health form");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
"HX-Redirect",
"https://secure.myvanco.com/L-Z772/campaign/C-13JPJ",
format!(
"https://tfcconnection.org/camp-health-form/?registration={}",
reg.as_str()
),
))
.finish()
}
"full" => {
log::info!("Sending them to pay for the full registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
"HX-Redirect",
"https://secure.myvanco.com/L-Z772/campaign/C-13JQE",
))
.finish()
}
"later" => {
log::info!("{} would like to pay later", full_name);
let html = markup::new! {
div {
class { "mt-8" }
h2 {
@format!("Thank you, {}!", full_name)
"later" => match reg.as_str() {
"now" => {
log::info!("Sending them to pay for registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
"HX-Redirect",
"https://secure.myvanco.com/L-Z772/campaign/C-13JPJ",
))
.finish()
}
"full" => {
log::info!("Sending them to pay for the full registration now");
HttpResponse::Ok()
.insert_header(("Access-Control-Expose-Headers", "*"))
.insert_header((
"HX-Redirect",
"https://secure.myvanco.com/L-Z772/campaign/C-13JQE",
))
.finish()
}
"later" => {
log::info!("{} would like to pay later", full_name);
let html = html! {
div {
class { "mt-8" }
h2 {
"Thank you, " (full_name) "!"
}
p { "Can't wait to see you at camp!" }
p {
class { "" }
"If you'd like to pay for your registration go to the donate tab in the top right when you are ready and find the camp registration option."
}
}
p { "Can't wait to see you at camp!" }
p {
class { "" }
"If you'd like to pay for your registration go to the donate tab in the top right when you are ready and find the camp registration option."
};
HttpResponse::Ok().body(html.into_string())
}
_ => {
log::error!("Got registration error.....");
let html = html! {
div {
class { "mt-8" }
h2 {
"Thank you, " (full_name) "!"
}
p { "Can't wait to see you at camp!" }
p {
class { "" }
"If you'd like to pay for your registration go to the donate tab in the top right when you are ready and find the camp registration option."
}
}
}
};
HttpResponse::Ok().body(html.to_string())
}
};
HttpResponse::Ok().body(html.into_string())
}
},
_ => {
log::error!("Got registration error.....");
let html = markup::new! {
div {
class { "mt-8" }
h2 {
@format!("Thank you, {}!", full_name)
}
p { "Can't wait to see you at camp!" }
p {
class { "" }
"If you'd like to pay for your registration go to the donate tab in the top right when you are ready and find the camp registration option."
}
}
};
HttpResponse::Ok().body(html.to_string())
log::error!("Unknown selection for health. We don't know where to send the user.");
HttpResponseBuilder::new(StatusCode::IM_A_TEAPOT)
.body("Unknown selection for health. We don't know where to send the user.")
}
}
}
async fn store_camp_form(form: CampForm) -> Result<(), ApiError> {
let request = reqwest::Client::new();
let json = json::Json::from(form);
let res = request
.post("https://tbl.tfcconnection.org/")
.header("xc-token", "Ohah24HNGXVvvixv8tvVJW5uNNdWjDJHG1d4t3o9")
.body(json)
async fn store_camp_form(form: CampForm) -> Result<(), Error> {
let request = Client::new();
let mut map = HashMap::new();
map.insert(
63,
format!(
"{} {}",
&form.first_name.unwrap_or(Text(String::new())).0,
&form.last_name.unwrap_or(Text(String::new())).0
),
);
map.insert(
64,
format!(
"{} {}",
form.parent_first_name.unwrap_or(Text(String::new())).0,
form.parent_last_name.unwrap_or(Text(String::new())).0
),
);
map.insert(65, form.parent_phone.unwrap_or(Text(String::new())).0);
map.insert(66, form.parent_email.unwrap_or(Text(String::new())).0);
map.insert(67, form.birthdate.unwrap_or(Text(String::new())).0);
map.insert(69, form.gender.unwrap_or(Text(String::new())).0);
map.insert(70, form.street.unwrap_or(Text(String::new())).0);
map.insert(71, form.city.unwrap_or(Text(String::new())).0);
map.insert(72, form.state.unwrap_or(Text(String::new())).0);
map.insert(73, form.zip.unwrap_or(Text(0)).0.to_string());
map.insert(74, form.grade.unwrap_or(Text(String::new())).0);
map.insert(75, form.week.unwrap_or(Text(String::new())).0);
map.insert(76, form.shirt.unwrap_or(Text(String::new())).0);
map.insert(77, form.registration.unwrap_or(Text(String::new())).0);
map.insert(115, form.health_form.unwrap_or(Text(String::new())).0);
let mut json = HashMap::new();
json.insert("data", map);
request
.post("https://staff.tfcconnection.org/apps/tables/api/1/tables/5/rows")
// .header("xc-token", "Ohah24HNGXVvvixv8tvVJW5uNNdWjDJHG1d4t3o9")
.basic_auth("chris", Some("2VHeGxeC^Zf9KqFK^G@Pt!zu2q^6@b"))
.json(&json)
.send()
.await?;
Ok(())