Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(email): create client every time of sending email (#3105)

Narayan Bhatfc2f16392148
diff --git a/crates/external_services/src/email/ses.rs b/crates/external_services/src/email/ses.rs
index 7e521a5bc1..de4d779491 100644
--- a/crates/external_services/src/email/ses.rs
+++ b/crates/external_services/src/email/ses.rs
@@ -12,14 +12,12 @@ use error_stack::{report, IntoReport, ResultExt};
use hyper::Uri;
use masking::PeekInterface;
use router_env::logger;
-use tokio::sync::OnceCell;
use crate::email::{EmailClient, EmailError, EmailResult, EmailSettings, IntermediateString};
/// Client for AWS SES operation
#[derive(Debug, Clone)]
pub struct AwsSes {
- ses_client: OnceCell<Client>,
sender: String,
settings: EmailSettings,
}
@@ -70,13 +68,13 @@ pub enum AwsSesError {
impl AwsSes {
/// Constructs a new AwsSes client
pub async fn create(conf: &EmailSettings, proxy_url: Option<impl AsRef<str>>) -> Self {
+ // Build the client initially which will help us know if the email configuration is correct
+ Self::create_client(conf, proxy_url)
+ .await
+ .map_err(|error| logger::error!(?error, "Failed to initialize SES Client"))
+ .ok();
+
Self {
- ses_client: OnceCell::new_with(
- Self::create_client(conf, proxy_url)
- .await
- .map_err(|error| logger::error!(?error, "Failed to initialize SES Client"))
- .ok(),
- ),
sender: conf.sender_email.clone(),
settings: conf.clone(),
}
@@ -222,13 +220,13 @@ impl EmailClient for AwsSes {
body: Self::RichText,
proxy_url: Option<&String>,
) -> EmailResult<()> {
- self.ses_client
- .get_or_try_init(|| async {
- Self::create_client(&self.settings, proxy_url)
- .await
- .change_context(EmailError::ClientBuildingFailure)
- })
- .await?
+ // Not using the same email client which was created at startup as the role session would expire
+ // Create a client every time when the email is being sent
+ let email_client = Self::create_client(&self.settings, proxy_url)
+ .await
+ .change_context(EmailError::ClientBuildingFailure)?;
+
+ email_client
.send_email()
.from_email_address(self.sender.to_owned())
.destination(
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index eec8a36ce9..fdfd3fc211 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -897,7 +897,7 @@ impl User {
)
.service(web::resource("/forgot_password").route(web::post().to(forgot_password)))
.service(web::resource("/reset_password").route(web::post().to(reset_password)))
- .service(web::resource("user/invite").route(web::post().to(invite_user)))
+ .service(web::resource("/user/invite").route(web::post().to(invite_user)))
.service(
web::resource("/signup_with_merchant_id")
.route(web::post().to(user_signup_with_merchant_id)),