Skip to content

Commit

Permalink
fix errors with template_subject introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
KavikaPalletenne committed Dec 2, 2024
1 parent 07680cc commit 7c3d624
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
9 changes: 8 additions & 1 deletion backend/server/src/handler/email_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ impl EmailTemplateHandler {
State(state): State<AppState>,
Json(request_body): Json<EmailTemplate>,
) -> Result<impl IntoResponse, ChaosError> {
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"))
}
Expand Down
4 changes: 2 additions & 2 deletions backend/server/src/handler/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ impl OfferHandler {
Path(id): Path<i64>,
_user: OfferAdmin,
) -> Result<impl IntoResponse, ChaosError> {
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(
Expand Down
5 changes: 3 additions & 2 deletions backend/server/src/handler/organisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ impl OrganisationHandler {
Path(id): Path<i64>,
State(state): State<AppState>,
_admin: OrganisationAdmin,
Json(request_body): Json<models::email_template::EmailTemplate>,
Json(request_body): Json<EmailTemplate>,
) -> Result<impl IntoResponse, ChaosError> {
Organisation::create_email_template(
id,
request_body.name,
request_body.template,
request_body.template_subject,
request_body.template_body,
&state.db,
state.snowflake_generator,
)
Expand Down
10 changes: 7 additions & 3 deletions backend/server/src/models/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,7 @@ pub struct EmailCredentials {
pub email_host: String,
}

#[derive(Serialize)]
pub struct EmailParts {
pub subject: String,
pub body: String,
Expand Down Expand Up @@ -41,9 +43,11 @@ impl ChaosEmail {
fn new_connection(
credentials: EmailCredentials,
) -> Result<AsyncSmtpTransport<Tokio1Executor>, ChaosError> {
Ok(AsyncSmtpTransport::relay(&*credentials.email_host)?
.credentials(credentials.credentials)
.build())
Ok(
AsyncSmtpTransport::<Tokio1Executor>::relay(&*credentials.email_host)?
.credentials(credentials.credentials)
.build(),
)
}

pub async fn send_message(
Expand Down
2 changes: 1 addition & 1 deletion backend/server/src/models/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions backend/server/src/models/organisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,23 @@ impl Organisation {
pub async fn create_email_template(
organisation_id: i64,
name: String,
template: String,
template_subject: String,
template_body: String,
pool: &Pool<Postgres>,
mut snowflake_generator: SnowflakeIdGenerator,
) -> Result<i64, ChaosError> {
let id = snowflake_generator.generate();

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?;
Expand Down

0 comments on commit 7c3d624

Please sign in to comment.