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

fix(router): validate schedule time before scheduling API key expiry reminder (#1776)

Tuhin Tarafder7b1dc78de5b4
diff --git a/crates/router/src/core/api_keys.rs b/crates/router/src/core/api_keys.rs
index b26a67d9fe..129b1f96e2 100644
--- a/crates/router/src/core/api_keys.rs
+++ b/crates/router/src/core/api_keys.rs
@@ -179,6 +179,7 @@ pub async fn create_api_key(
);
// Add process to process_tracker for email reminder, only if expiry is set to future date
+ // If the `api_key` is set to expire in less than 7 days, the merchant is not notified about it's expiry
#[cfg(feature = "email")]
{
if api_key.expires_at.is_some() {
@@ -200,6 +201,7 @@ pub async fn create_api_key(
// Add api_key_expiry task to the process_tracker table.
// Construct ProcessTrackerNew struct with all required fields, and schedule the first email.
// After first email has been sent, update the schedule_time based on retry_count in execute_workflow().
+// A task is not scheduled if the time for the first email is in the past.
#[cfg(feature = "email")]
#[instrument(skip_all)]
pub async fn add_api_key_expiry_task(
@@ -208,6 +210,21 @@ pub async fn add_api_key_expiry_task(
expiry_reminder_days: Vec<u8>,
) -> Result<(), errors::ProcessTrackerError> {
let current_time = common_utils::date_time::now();
+
+ let schedule_time = expiry_reminder_days
+ .first()
+ .and_then(|expiry_reminder_day| {
+ api_key.expires_at.map(|expires_at| {
+ expires_at.saturating_sub(time::Duration::days(i64::from(*expiry_reminder_day)))
+ })
+ });
+
+ if let Some(schedule_time) = schedule_time {
+ if schedule_time <= current_time {
+ return Ok(());
+ }
+ }
+
let api_key_expiry_tracker = &storage::ApiKeyExpiryWorkflow {
key_id: api_key.key_id.clone(),
merchant_id: api_key.merchant_id.clone(),
@@ -223,14 +240,6 @@ pub async fn add_api_key_expiry_task(
format!("unable to serialize API key expiry tracker: {api_key_expiry_tracker:?}")
})?;
- let schedule_time = expiry_reminder_days
- .first()
- .and_then(|expiry_reminder_day| {
- api_key.expires_at.map(|expires_at| {
- expires_at.saturating_sub(time::Duration::days(i64::from(*expiry_reminder_day)))
- })
- });
-
let process_tracker_entry = storage::ProcessTrackerNew {
id: generate_task_id_for_api_key_expiry_workflow(api_key.key_id.as_str()),
name: Some(String::from(API_KEY_EXPIRY_NAME)),
@@ -356,6 +365,7 @@ pub async fn update_api_key(
// Update api_key_expiry task in the process_tracker table.
// Construct Update variant of ProcessTrackerUpdate with new tracking_data.
+// A task is not scheduled if the time for the first email is in the past.
#[cfg(feature = "email")]
#[instrument(skip_all)]
pub async fn update_api_key_expiry_task(
@@ -365,10 +375,6 @@ pub async fn update_api_key_expiry_task(
) -> Result<(), errors::ProcessTrackerError> {
let current_time = common_utils::date_time::now();
- let task_id = generate_task_id_for_api_key_expiry_workflow(api_key.key_id.as_str());
-
- let task_ids = vec![task_id.clone()];
-
let schedule_time = expiry_reminder_days
.first()
.and_then(|expiry_reminder_day| {
@@ -377,6 +383,16 @@ pub async fn update_api_key_expiry_task(
})
});
+ if let Some(schedule_time) = schedule_time {
+ if schedule_time <= current_time {
+ return Ok(());
+ }
+ }
+
+ let task_id = generate_task_id_for_api_key_expiry_workflow(api_key.key_id.as_str());
+
+ let task_ids = vec![task_id.clone()];
+
let updated_tracking_data = &storage::ApiKeyExpiryWorkflow {
key_id: api_key.key_id.clone(),
merchant_id: api_key.merchant_id.clone(),