Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webhook): Add is_delivered filter to list initial attempts endpoint #7344

Open
wants to merge 5 commits into
base: return-total-events-count
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-reference-v2/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -8422,7 +8422,7 @@
},
"is_delivery_successful": {
"type": "boolean",
"description": "Indicates whether the webhook delivery attempt was successful."
"description": "Indicates whether the webhook was ultimately delivery."
},
"initial_attempt_id": {
"type": "string",
Expand Down
19 changes: 16 additions & 3 deletions api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5219,6 +5219,16 @@
"type": "string",
"nullable": true
}
},
{
"name": "is_delivered",
"in": "query",
"description": "Only include Events which are ultimately delivered to the merchant.",
"required": false,
"schema": {
"type": "boolean",
"nullable": true
}
}
],
"responses": {
Expand Down Expand Up @@ -11109,7 +11119,7 @@
},
"is_delivery_successful": {
"type": "boolean",
"description": "Indicates whether the webhook delivery attempt was successful."
"description": "Indicates whether the webhook was ultimately delivery."
},
"initial_attempt_id": {
"type": "string",
Expand Down Expand Up @@ -25543,6 +25553,7 @@
},
"TotalEventsResponse": {
"type": "object",
"description": "The response body of list initial delivery attempts api call.",
"required": [
"events",
"total_count"
Expand All @@ -25552,11 +25563,13 @@
"type": "array",
"items": {
"$ref": "#/components/schemas/EventListItemResponse"
}
},
"description": "The list of events"
},
"total_count": {
"type": "integer",
"format": "int64"
"format": "int64",
"description": "Count of total events"
}
}
},
Expand Down
6 changes: 5 additions & 1 deletion crates/api_models/src/webhook_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub struct EventListConstraints {
/// Filter all events associated with the specified business profile ID.
#[schema(value_type = Option<String>)]
pub profile_id: Option<common_utils::id_type::ProfileId>,

// Filter all events by overall_delivery_status.
pub is_delivered: Option<bool>,
}

#[derive(Debug)]
Expand All @@ -37,6 +40,7 @@ pub enum EventListConstraintsInternal {
created_before: Option<PrimitiveDateTime>,
limit: Option<i64>,
offset: Option<i64>,
is_delivered: Option<bool>,
},
ObjectIdFilter {
object_id: String,
Expand Down Expand Up @@ -68,7 +72,7 @@ pub struct EventListItemResponse {
/// Specifies the class of event (the type of object: Payment, Refund, etc.)
pub event_class: EventClass,

/// Indicates whether the webhook delivery attempt was successful.
/// Indicates whether the webhook was ultimately delivery.
pub is_delivery_successful: bool,

/// The identifier for the initial delivery attempt. This will be the same as `event_id` for
Expand Down
3 changes: 3 additions & 0 deletions crates/diesel_models/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ pub struct EventNew {
pub response: Option<Encryption>,
pub delivery_attempt: Option<storage_enums::WebhookDeliveryAttempt>,
pub metadata: Option<EventMetadata>,
pub is_overall_delivery_successful: bool,
}

#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
#[diesel(table_name = events)]
pub struct EventUpdateInternal {
pub is_webhook_notified: Option<bool>,
pub response: Option<Encryption>,
pub is_overall_delivery_successful: Option<bool>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Identifiable, Queryable, Selectable)]
Expand All @@ -57,6 +59,7 @@ pub struct Event {
pub response: Option<Encryption>,
pub delivery_attempt: Option<storage_enums::WebhookDeliveryAttempt>,
pub metadata: Option<EventMetadata>,
pub is_overall_delivery_successful: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize, AsExpression, diesel::FromSqlRow)]
Expand Down
15 changes: 15 additions & 0 deletions crates/diesel_models/src/query/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Event {
created_before: time::PrimitiveDateTime,
limit: Option<i64>,
offset: Option<i64>,
is_delivered: Option<bool>,
) -> StorageResult<Vec<Self>> {
use async_bb8_diesel::AsyncRunQueryDsl;
use diesel::{debug_query, pg::Pg, QueryDsl};
Expand All @@ -81,6 +82,7 @@ impl Event {
(dsl::created_at, created_after, created_before),
limit,
offset,
is_delivered,
);

logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
Expand Down Expand Up @@ -134,6 +136,7 @@ impl Event {
created_before: time::PrimitiveDateTime,
limit: Option<i64>,
offset: Option<i64>,
is_delivered: Option<bool>,
) -> StorageResult<Vec<Self>> {
use async_bb8_diesel::AsyncRunQueryDsl;
use diesel::{debug_query, pg::Pg, QueryDsl};
Expand All @@ -159,6 +162,7 @@ impl Event {
(dsl::created_at, created_after, created_before),
limit,
offset,
is_delivered,
);

logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
Expand Down Expand Up @@ -217,6 +221,7 @@ impl Event {
),
limit: Option<i64>,
offset: Option<i64>,
is_delivered: Option<bool>,
) -> T
where
T: diesel::query_dsl::methods::LimitDsl<Output = T>
Expand All @@ -233,6 +238,10 @@ impl Event {
diesel::dsl::Eq<dsl::business_profile_id, common_utils::id_type::ProfileId>,
Output = T,
>,
T: diesel::query_dsl::methods::FilterDsl<
diesel::dsl::Eq<dsl::is_overall_delivery_successful, bool>,
Output = T,
>,
{
if let Some(profile_id) = profile_id {
query = query.filter(dsl::business_profile_id.eq(profile_id));
Expand All @@ -250,6 +259,10 @@ impl Event {
query = query.offset(offset);
}

if let Some(is_delivered) = is_delivered {
query = query.filter(dsl::is_overall_delivery_successful.eq(is_delivered));
}

query
}

Expand All @@ -259,6 +272,7 @@ impl Event {
profile_id: Option<common_utils::id_type::ProfileId>,
created_after: time::PrimitiveDateTime,
created_before: time::PrimitiveDateTime,
is_delivered: Option<bool>,
) -> StorageResult<i64> {
use async_bb8_diesel::AsyncRunQueryDsl;
use diesel::{debug_query, pg::Pg, QueryDsl};
Expand All @@ -284,6 +298,7 @@ impl Event {
(dsl::created_at, created_after, created_before),
None,
None,
is_delivered,
);

logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ diesel::table! {
response -> Nullable<Bytea>,
delivery_attempt -> Nullable<WebhookDeliveryAttempt>,
metadata -> Nullable<Jsonb>,
is_overall_delivery_successful -> Bool,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ diesel::table! {
response -> Nullable<Bytea>,
delivery_attempt -> Nullable<WebhookDeliveryAttempt>,
metadata -> Nullable<Jsonb>,
is_overall_delivery_successful -> Bool,
}
}

Expand Down
5 changes: 5 additions & 0 deletions crates/openapi/src/routes/webhook_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
Query,
description = "Only include Events associated with the Profile identified by the specified Profile ID."
),
(
"is_delivered" = Option<bool>,
Query,
description = "Only include Events which are ultimately delivered to the merchant."
),
),
responses(
(status = 200, description = "List of Events retrieved successfully", body = TotalEventsResponse),
Expand Down
34 changes: 32 additions & 2 deletions crates/router/src/core/webhooks/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub(crate) async fn create_event_and_trigger_outgoing_webhook(
response: None,
delivery_attempt: Some(delivery_attempt),
metadata: Some(event_metadata),
is_overall_delivery_successful: false,
};

let event_insert_result = state
Expand Down Expand Up @@ -822,7 +823,7 @@ async fn update_event_in_storage(
.attach_printable("Failed to encrypt outgoing webhook response content")?,
),
};
state
let current_event = state
.store
.update_event_by_merchant_id_event_id(
key_manager_state,
Expand All @@ -832,7 +833,36 @@ async fn update_event_in_storage(
&merchant_key_store,
)
.await
.change_context(errors::WebhooksFlowError::WebhookEventUpdationFailed)
.change_context(errors::WebhooksFlowError::WebhookEventUpdationFailed)?;

let parent_update = domain::EventUpdate::ParentUpdate {
is_overall_delivery_successful: status_code.is_success(),
};

let parent_event_id = current_event.initial_attempt_id.clone();
let delivery_attempt = current_event.delivery_attempt;

if let Some((
parent_event_id,
enums::WebhookDeliveryAttempt::InitialAttempt
| enums::WebhookDeliveryAttempt::AutomaticRetry,
)) = parent_event_id.zip(delivery_attempt)
{
state
.store
.update_event_by_merchant_id_event_id(
key_manager_state,
merchant_id,
parent_event_id.as_str(),
parent_update,
&merchant_key_store,
)
.await
.change_context(errors::WebhooksFlowError::WebhookEventUpdationFailed)
.attach_printable("Failed to update parent event")?;
}

Ok(current_event)
}

fn increment_webhook_outgoing_received_count(merchant_id: &common_utils::id_type::MerchantId) {
Expand Down
7 changes: 7 additions & 0 deletions crates/router/src/core/webhooks/webhook_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub async fn list_initial_delivery_attempts(
created_before,
limit,
offset,
is_delivered
} => {
let limit = match limit {
Some(limit) if limit <= INITIAL_DELIVERY_ATTEMPTS_LIST_MAX_LIMIT => Ok(Some(limit)),
Expand Down Expand Up @@ -114,6 +115,7 @@ pub async fn list_initial_delivery_attempts(
created_before,
limit,
offset,
is_delivered,
&key_store,
)
.await,
Expand All @@ -124,6 +126,7 @@ pub async fn list_initial_delivery_attempts(
created_before,
limit,
offset,
is_delivered,
&key_store,
)
.await,
Expand All @@ -143,12 +146,15 @@ pub async fn list_initial_delivery_attempts(
.unwrap_or(events_list_begin_time);
let created_before = api_constraints.created_before.unwrap_or(now);

let is_delivered = api_constraints.is_delivered;

let total_count = store
.count_initial_events_by_constraints(
&merchant_id,
profile_id,
created_after,
created_before,
is_delivered,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
Expand Down Expand Up @@ -269,6 +275,7 @@ pub async fn retry_delivery_attempt(
response: None,
delivery_attempt: Some(delivery_attempt),
metadata: event_to_retry.metadata,
is_overall_delivery_successful: false,
};

let event = store
Expand Down
Loading
Loading