Skip to content

Commit 5ce6beb

Browse files
sai-harsha-vardhanapoorvdixit88
authored andcommitted
feat(router): add gateway_status_map CRUD APIs (#2809)
1 parent e1f5857 commit 5ce6beb

File tree

16 files changed

+402
-4
lines changed

16 files changed

+402
-4
lines changed

crates/api_models/src/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod customer;
2+
pub mod gsm;
23
pub mod payment;
34
#[cfg(feature = "payouts")]
45
pub mod payouts;

crates/api_models/src/events/gsm.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use common_utils::events::{ApiEventMetric, ApiEventsType};
2+
3+
use crate::gsm;
4+
5+
impl ApiEventMetric for gsm::GsmCreateRequest {
6+
fn get_api_event_type(&self) -> Option<ApiEventsType> {
7+
Some(ApiEventsType::Gsm)
8+
}
9+
}
10+
11+
impl ApiEventMetric for gsm::GsmUpdateRequest {
12+
fn get_api_event_type(&self) -> Option<ApiEventsType> {
13+
Some(ApiEventsType::Gsm)
14+
}
15+
}
16+
17+
impl ApiEventMetric for gsm::GsmRetrieveRequest {
18+
fn get_api_event_type(&self) -> Option<ApiEventsType> {
19+
Some(ApiEventsType::Gsm)
20+
}
21+
}
22+
23+
impl ApiEventMetric for gsm::GsmDeleteRequest {
24+
fn get_api_event_type(&self) -> Option<ApiEventsType> {
25+
Some(ApiEventsType::Gsm)
26+
}
27+
}
28+
29+
impl ApiEventMetric for gsm::GsmDeleteResponse {
30+
fn get_api_event_type(&self) -> Option<ApiEventsType> {
31+
Some(ApiEventsType::Gsm)
32+
}
33+
}

crates/api_models/src/gsm.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::enums;
2+
3+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
4+
pub struct GsmCreateRequest {
5+
pub connector: enums::Connector,
6+
pub flow: String,
7+
pub sub_flow: String,
8+
pub code: String,
9+
pub message: String,
10+
pub status: String,
11+
pub router_error: Option<String>,
12+
pub decision: GsmDecision,
13+
pub step_up_possible: bool,
14+
}
15+
16+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
17+
pub struct GsmRetrieveRequest {
18+
pub connector: enums::Connector,
19+
pub flow: String,
20+
pub sub_flow: String,
21+
pub code: String,
22+
pub message: String,
23+
}
24+
25+
#[derive(
26+
Default,
27+
Clone,
28+
Copy,
29+
Debug,
30+
strum::Display,
31+
PartialEq,
32+
Eq,
33+
serde::Serialize,
34+
serde::Deserialize,
35+
strum::EnumString,
36+
)]
37+
#[serde(rename_all = "snake_case")]
38+
#[strum(serialize_all = "snake_case")]
39+
pub enum GsmDecision {
40+
Retry,
41+
Requeue,
42+
#[default]
43+
DoDefault,
44+
}
45+
46+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
47+
pub struct GsmUpdateRequest {
48+
pub connector: String,
49+
pub flow: String,
50+
pub sub_flow: String,
51+
pub code: String,
52+
pub message: String,
53+
pub status: Option<String>,
54+
pub router_error: Option<String>,
55+
pub decision: Option<GsmDecision>,
56+
pub step_up_possible: Option<bool>,
57+
}
58+
59+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
60+
pub struct GsmDeleteRequest {
61+
pub connector: String,
62+
pub flow: String,
63+
pub sub_flow: String,
64+
pub code: String,
65+
pub message: String,
66+
}
67+
68+
#[derive(Debug, serde::Serialize)]
69+
pub struct GsmDeleteResponse {
70+
pub gsm_rule_delete: bool,
71+
pub connector: String,
72+
pub flow: String,
73+
pub sub_flow: String,
74+
pub code: String,
75+
}

crates/api_models/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod ephemeral_key;
1111
pub mod errors;
1212
pub mod events;
1313
pub mod files;
14+
pub mod gsm;
1415
pub mod mandates;
1516
pub mod organization;
1617
pub mod payment_methods;

crates/common_utils/src/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum ApiEventsType {
4141
Routing,
4242
ResourceListAPI,
4343
PaymentRedirectionResponse,
44+
Gsm,
4445
// TODO: This has to be removed once the corresponding apiEventTypes are created
4546
Miscellaneous,
4647
}

crates/diesel_models/src/gsm.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Gateway status mapping
22
3-
use common_utils::custom_serde;
3+
use common_utils::{
4+
custom_serde,
5+
events::{ApiEventMetric, ApiEventsType},
6+
};
47
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
58
use time::PrimitiveDateTime;
69

@@ -95,3 +98,9 @@ impl From<GatewayStatusMappingUpdate> for GatewayStatusMapperUpdateInternal {
9598
}
9699
}
97100
}
101+
102+
impl ApiEventMetric for GatewayStatusMap {
103+
fn get_api_event_type(&self) -> Option<ApiEventsType> {
104+
Some(ApiEventsType::Gsm)
105+
}
106+
}

crates/router/src/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod customers;
88
pub mod disputes;
99
pub mod errors;
1010
pub mod files;
11+
pub mod gsm;
1112
pub mod mandate;
1213
pub mod metrics;
1314
pub mod payment_link;

crates/router/src/core/gsm.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use api_models::gsm as gsm_api_types;
2+
use diesel_models::gsm as storage;
3+
use error_stack::{IntoReport, ResultExt};
4+
use router_env::{instrument, tracing};
5+
6+
use crate::{
7+
core::{
8+
errors,
9+
errors::{RouterResponse, StorageErrorExt},
10+
},
11+
db::gsm::GsmInterface,
12+
services,
13+
types::{self, transformers::ForeignInto},
14+
AppState,
15+
};
16+
17+
#[instrument(skip_all)]
18+
pub async fn create_gsm_rule(
19+
state: AppState,
20+
gsm_rule: gsm_api_types::GsmCreateRequest,
21+
) -> RouterResponse<types::GsmResponse> {
22+
let db = state.store.as_ref();
23+
GsmInterface::add_gsm_rule(db, gsm_rule.foreign_into())
24+
.await
25+
.to_duplicate_response(errors::ApiErrorResponse::GenericDuplicateError {
26+
message: "GSM with given key already exists in our records".to_string(),
27+
})
28+
.map(services::ApplicationResponse::Json)
29+
}
30+
31+
#[instrument(skip_all)]
32+
pub async fn retrieve_gsm_rule(
33+
state: AppState,
34+
gsm_request: gsm_api_types::GsmRetrieveRequest,
35+
) -> RouterResponse<types::GsmResponse> {
36+
let db = state.store.as_ref();
37+
let gsm_api_types::GsmRetrieveRequest {
38+
connector,
39+
flow,
40+
sub_flow,
41+
code,
42+
message,
43+
} = gsm_request;
44+
GsmInterface::find_gsm_rule(db, connector.to_string(), flow, sub_flow, code, message)
45+
.await
46+
.to_not_found_response(errors::ApiErrorResponse::GenericNotFoundError {
47+
message: "GSM with given key does not exist in our records".to_string(),
48+
})
49+
.map(services::ApplicationResponse::Json)
50+
}
51+
52+
#[instrument(skip_all)]
53+
pub async fn update_gsm_rule(
54+
state: AppState,
55+
gsm_request: gsm_api_types::GsmUpdateRequest,
56+
) -> RouterResponse<types::GsmResponse> {
57+
let db = state.store.as_ref();
58+
let gsm_api_types::GsmUpdateRequest {
59+
connector,
60+
flow,
61+
sub_flow,
62+
code,
63+
message,
64+
decision,
65+
status,
66+
router_error,
67+
step_up_possible,
68+
} = gsm_request;
69+
GsmInterface::update_gsm_rule(
70+
db,
71+
connector.to_string(),
72+
flow,
73+
sub_flow,
74+
code,
75+
message,
76+
storage::GatewayStatusMappingUpdate {
77+
decision: decision.map(|d| d.to_string()),
78+
status,
79+
router_error: Some(router_error),
80+
step_up_possible,
81+
},
82+
)
83+
.await
84+
.to_not_found_response(errors::ApiErrorResponse::GenericNotFoundError {
85+
message: "GSM with given key does not exist in our records".to_string(),
86+
})
87+
.attach_printable("Failed while updating Gsm rule")
88+
.map(services::ApplicationResponse::Json)
89+
}
90+
91+
#[instrument(skip_all)]
92+
pub async fn delete_gsm_rule(
93+
state: AppState,
94+
gsm_request: gsm_api_types::GsmDeleteRequest,
95+
) -> RouterResponse<gsm_api_types::GsmDeleteResponse> {
96+
let db = state.store.as_ref();
97+
let gsm_api_types::GsmDeleteRequest {
98+
connector,
99+
flow,
100+
sub_flow,
101+
code,
102+
message,
103+
} = gsm_request;
104+
match GsmInterface::delete_gsm_rule(
105+
db,
106+
connector.to_string(),
107+
flow.to_owned(),
108+
sub_flow.to_owned(),
109+
code.to_owned(),
110+
message.to_owned(),
111+
)
112+
.await
113+
.to_not_found_response(errors::ApiErrorResponse::GenericNotFoundError {
114+
message: "GSM with given key does not exist in our records".to_string(),
115+
})
116+
.attach_printable("Failed while Deleting Gsm rule")
117+
{
118+
Ok(is_deleted) => {
119+
if is_deleted {
120+
Ok(services::ApplicationResponse::Json(
121+
gsm_api_types::GsmDeleteResponse {
122+
gsm_rule_delete: true,
123+
connector,
124+
flow,
125+
sub_flow,
126+
code,
127+
},
128+
))
129+
} else {
130+
Err(errors::ApiErrorResponse::InternalServerError)
131+
.into_report()
132+
.attach_printable("Failed while Deleting Gsm rule, got response as false")
133+
}
134+
}
135+
Err(err) => Err(err),
136+
}
137+
}

crates/router/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn mk_app(
142142
.service(routes::Files::server(state.clone()))
143143
.service(routes::Disputes::server(state.clone()))
144144
.service(routes::Routing::server(state.clone()))
145+
.service(routes::Gsm::server(state.clone()))
145146
}
146147

147148
#[cfg(all(feature = "olap", feature = "kms"))]

crates/router/src/routes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod disputes;
1010
pub mod dummy_connector;
1111
pub mod ephemeral_key;
1212
pub mod files;
13+
pub mod gsm;
1314
pub mod health;
1415
pub mod lock_utils;
1516
pub mod mandates;
@@ -36,7 +37,7 @@ pub use self::app::Routing;
3637
pub use self::app::Verify;
3738
pub use self::app::{
3839
ApiKeys, AppState, BusinessProfile, Cache, Cards, Configs, Customers, Disputes, EphemeralKey,
39-
Files, Health, Mandates, MerchantAccount, MerchantConnectorAccount, PaymentLink,
40+
Files, Gsm, Health, Mandates, MerchantAccount, MerchantConnectorAccount, PaymentLink,
4041
PaymentMethods, Payments, Refunds, Webhooks,
4142
};
4243
#[cfg(feature = "stripe")]

0 commit comments

Comments
 (0)