diff --git a/backend/server/src/handler/email_template.rs b/backend/server/src/handler/email_template.rs index e392834f..4150ac98 100644 --- a/backend/server/src/handler/email_template.rs +++ b/backend/server/src/handler/email_template.rs @@ -25,7 +25,14 @@ impl EmailTemplateHandler { State(state): State, Json(request_body): Json, ) -> Result { - EmailTemplate::update(id, request_body.name, request_body.template, &state.db).await?; + EmailTemplate::update( + id, + request_body.name, + request_body.template_subject, + request_body.template_body, + &state.db, + ) + .await?; Ok((StatusCode::OK, "Successfully updated email template")) } diff --git a/backend/server/src/handler/offer.rs b/backend/server/src/handler/offer.rs index 09ea3472..32e4851c 100644 --- a/backend/server/src/handler/offer.rs +++ b/backend/server/src/handler/offer.rs @@ -48,10 +48,10 @@ impl OfferHandler { Path(id): Path, _user: OfferAdmin, ) -> Result { - let string = Offer::preview_email(id, &mut transaction.tx).await?; + let email_parts = Offer::preview_email(id, &mut transaction.tx).await?; transaction.tx.commit().await?; - Ok((StatusCode::OK, string)) + Ok((StatusCode::OK, Json(email_parts))) } pub async fn send_offer( diff --git a/backend/server/src/handler/organisation.rs b/backend/server/src/handler/organisation.rs index 7950f1fb..f43db3c1 100644 --- a/backend/server/src/handler/organisation.rs +++ b/backend/server/src/handler/organisation.rs @@ -199,12 +199,13 @@ impl OrganisationHandler { Path(id): Path, State(state): State, _admin: OrganisationAdmin, - Json(request_body): Json, + Json(request_body): Json, ) -> Result { Organisation::create_email_template( id, request_body.name, - request_body.template, + request_body.template_subject, + request_body.template_body, &state.db, state.snowflake_generator, ) diff --git a/backend/server/src/models/email.rs b/backend/server/src/models/email.rs index b42d843a..72afffe5 100644 --- a/backend/server/src/models/email.rs +++ b/backend/server/src/models/email.rs @@ -3,6 +3,7 @@ use lettre::transport::smtp::authentication::Credentials; use lettre::{ AsyncSmtpTransport, AsyncTransport, Message, SmtpTransport, Tokio1Executor, Transport, }; +use serde::Serialize; use std::env; pub struct ChaosEmail; @@ -13,6 +14,7 @@ pub struct EmailCredentials { pub email_host: String, } +#[derive(Serialize)] pub struct EmailParts { pub subject: String, pub body: String, @@ -41,9 +43,11 @@ impl ChaosEmail { fn new_connection( credentials: EmailCredentials, ) -> Result, ChaosError> { - Ok(AsyncSmtpTransport::relay(&*credentials.email_host)? - .credentials(credentials.credentials) - .build()) + Ok( + AsyncSmtpTransport::::relay(&*credentials.email_host)? + .credentials(credentials.credentials) + .build(), + ) } pub async fn send_message( diff --git a/backend/server/src/models/offer.rs b/backend/server/src/models/offer.rs index 58abdae0..60fa94ee 100644 --- a/backend/server/src/models/offer.rs +++ b/backend/server/src/models/offer.rs @@ -210,7 +210,7 @@ impl Offer { ) -> Result<(), ChaosError> { let offer = Offer::get(id, transaction).await?; let email_parts = EmailTemplate::generate_email( - offer.user_name, + offer.user_name.clone(), offer.role_name, offer.organisation_name, offer.campaign_name, diff --git a/backend/server/src/models/organisation.rs b/backend/server/src/models/organisation.rs index 94824c37..8942ad2f 100644 --- a/backend/server/src/models/organisation.rs +++ b/backend/server/src/models/organisation.rs @@ -439,7 +439,8 @@ impl Organisation { pub async fn create_email_template( organisation_id: i64, name: String, - template: String, + template_subject: String, + template_body: String, pool: &Pool, mut snowflake_generator: SnowflakeIdGenerator, ) -> Result { @@ -447,13 +448,14 @@ impl Organisation { let _ = sqlx::query!( " - INSERT INTO email_templates (id, organisation_id, name, template) - VALUES ($1, $2, $3, $4) + INSERT INTO email_templates (id, organisation_id, name, template_subject, template_body) + VALUES ($1, $2, $3, $4, $5) ", id, organisation_id, name, - template + template_subject, + template_body ) .execute(pool) .await?;