Skip to content

Commit a429b23

Browse files
feat(router): add gateway_status_map interface (#2804)
1 parent 7623ea9 commit a429b23

File tree

11 files changed

+436
-4
lines changed

11 files changed

+436
-4
lines changed

crates/diesel_models/src/gsm.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! Gateway status mapping
2+
3+
use common_utils::custom_serde;
4+
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
5+
use time::PrimitiveDateTime;
6+
7+
use crate::schema::gateway_status_map;
8+
9+
#[derive(
10+
Clone,
11+
Debug,
12+
Eq,
13+
PartialEq,
14+
router_derive::DebugAsDisplay,
15+
Identifiable,
16+
Queryable,
17+
serde::Serialize,
18+
)]
19+
#[diesel(table_name = gateway_status_map, primary_key(connector, flow, sub_flow, code, message))]
20+
pub struct GatewayStatusMap {
21+
pub connector: String,
22+
pub flow: String,
23+
pub sub_flow: String,
24+
pub code: String,
25+
pub message: String,
26+
pub status: String,
27+
pub router_error: Option<String>,
28+
pub decision: String,
29+
#[serde(with = "custom_serde::iso8601")]
30+
pub created_at: PrimitiveDateTime,
31+
#[serde(with = "custom_serde::iso8601")]
32+
pub last_modified: PrimitiveDateTime,
33+
pub step_up_possible: bool,
34+
}
35+
36+
#[derive(Clone, Debug, Eq, PartialEq, Insertable)]
37+
#[diesel(table_name = gateway_status_map)]
38+
pub struct GatewayStatusMappingNew {
39+
pub connector: String,
40+
pub flow: String,
41+
pub sub_flow: String,
42+
pub code: String,
43+
pub message: String,
44+
pub status: String,
45+
pub router_error: Option<String>,
46+
pub decision: String,
47+
pub step_up_possible: bool,
48+
}
49+
50+
#[derive(
51+
Clone,
52+
Debug,
53+
PartialEq,
54+
Eq,
55+
AsChangeset,
56+
router_derive::DebugAsDisplay,
57+
Default,
58+
serde::Deserialize,
59+
)]
60+
#[diesel(table_name = gateway_status_map)]
61+
pub struct GatewayStatusMapperUpdateInternal {
62+
pub connector: Option<String>,
63+
pub flow: Option<String>,
64+
pub sub_flow: Option<String>,
65+
pub code: Option<String>,
66+
pub message: Option<String>,
67+
pub status: Option<String>,
68+
pub router_error: Option<Option<String>>,
69+
pub decision: Option<String>,
70+
pub step_up_possible: Option<bool>,
71+
}
72+
73+
#[derive(Debug)]
74+
pub struct GatewayStatusMappingUpdate {
75+
pub status: Option<String>,
76+
pub router_error: Option<Option<String>>,
77+
pub decision: Option<String>,
78+
pub step_up_possible: Option<bool>,
79+
}
80+
81+
impl From<GatewayStatusMappingUpdate> for GatewayStatusMapperUpdateInternal {
82+
fn from(value: GatewayStatusMappingUpdate) -> Self {
83+
let GatewayStatusMappingUpdate {
84+
decision,
85+
status,
86+
router_error,
87+
step_up_possible,
88+
} = value;
89+
Self {
90+
status,
91+
router_error,
92+
decision,
93+
step_up_possible,
94+
..Default::default()
95+
}
96+
}
97+
}

crates/diesel_models/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod events;
1515
pub mod file;
1616
#[allow(unused)]
1717
pub mod fraud_check;
18+
pub mod gsm;
1819
#[cfg(feature = "kv_store")]
1920
pub mod kv;
2021
pub mod locker_mock_up;

crates/diesel_models/src/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod events;
1111
pub mod file;
1212
pub mod fraud_check;
1313
pub mod generics;
14+
pub mod gsm;
1415
pub mod locker_mock_up;
1516
pub mod mandate;
1617
pub mod merchant_account;

crates/diesel_models/src/query/gsm.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};
2+
use error_stack::report;
3+
4+
use crate::{
5+
errors, gsm::*, query::generics, schema::gateway_status_map::dsl, PgPooledConn, StorageResult,
6+
};
7+
8+
impl GatewayStatusMappingNew {
9+
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<GatewayStatusMap> {
10+
generics::generic_insert(conn, self).await
11+
}
12+
}
13+
14+
impl GatewayStatusMap {
15+
pub async fn find(
16+
conn: &PgPooledConn,
17+
connector: String,
18+
flow: String,
19+
sub_flow: String,
20+
code: String,
21+
message: String,
22+
) -> StorageResult<Self> {
23+
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
24+
conn,
25+
dsl::connector
26+
.eq(connector)
27+
.and(dsl::flow.eq(flow))
28+
.and(dsl::sub_flow.eq(sub_flow))
29+
.and(dsl::code.eq(code))
30+
.and(dsl::message.eq(message)),
31+
)
32+
.await
33+
}
34+
35+
pub async fn retrieve_decision(
36+
conn: &PgPooledConn,
37+
connector: String,
38+
flow: String,
39+
sub_flow: String,
40+
code: String,
41+
message: String,
42+
) -> StorageResult<String> {
43+
Self::find(conn, connector, flow, sub_flow, code, message)
44+
.await
45+
.map(|item| item.decision)
46+
}
47+
48+
pub async fn update(
49+
conn: &PgPooledConn,
50+
connector: String,
51+
flow: String,
52+
sub_flow: String,
53+
code: String,
54+
message: String,
55+
gsm: GatewayStatusMappingUpdate,
56+
) -> StorageResult<Self> {
57+
generics::generic_update_with_results::<
58+
<Self as HasTable>::Table,
59+
GatewayStatusMapperUpdateInternal,
60+
_,
61+
_,
62+
>(
63+
conn,
64+
dsl::connector
65+
.eq(connector)
66+
.and(dsl::flow.eq(flow))
67+
.and(dsl::sub_flow.eq(sub_flow))
68+
.and(dsl::code.eq(code))
69+
.and(dsl::message.eq(message)),
70+
gsm.into(),
71+
)
72+
.await?
73+
.first()
74+
.cloned()
75+
.ok_or_else(|| {
76+
report!(errors::DatabaseError::NotFound)
77+
.attach_printable("Error while updating gsm entry")
78+
})
79+
}
80+
81+
pub async fn delete(
82+
conn: &PgPooledConn,
83+
connector: String,
84+
flow: String,
85+
sub_flow: String,
86+
code: String,
87+
message: String,
88+
) -> StorageResult<bool> {
89+
generics::generic_delete::<<Self as HasTable>::Table, _>(
90+
conn,
91+
dsl::connector
92+
.eq(connector)
93+
.and(dsl::flow.eq(flow))
94+
.and(dsl::sub_flow.eq(sub_flow))
95+
.and(dsl::code.eq(code))
96+
.and(dsl::message.eq(message)),
97+
)
98+
.await
99+
}
100+
}

crates/diesel_models/src/schema.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,33 @@ diesel::table! {
332332
}
333333
}
334334

335+
diesel::table! {
336+
use diesel::sql_types::*;
337+
use crate::enums::diesel_exports::*;
338+
339+
gateway_status_map (connector, flow, sub_flow, code, message) {
340+
#[max_length = 64]
341+
connector -> Varchar,
342+
#[max_length = 64]
343+
flow -> Varchar,
344+
#[max_length = 64]
345+
sub_flow -> Varchar,
346+
#[max_length = 255]
347+
code -> Varchar,
348+
#[max_length = 1024]
349+
message -> Varchar,
350+
#[max_length = 64]
351+
status -> Varchar,
352+
#[max_length = 64]
353+
router_error -> Nullable<Varchar>,
354+
#[max_length = 64]
355+
decision -> Varchar,
356+
created_at -> Timestamp,
357+
last_modified -> Timestamp,
358+
step_up_possible -> Bool,
359+
}
360+
}
361+
335362
diesel::table! {
336363
use diesel::sql_types::*;
337364
use crate::enums::diesel_exports::*;
@@ -909,6 +936,7 @@ diesel::allow_tables_to_appear_in_same_query!(
909936
events,
910937
file_metadata,
911938
fraud_check,
939+
gateway_status_map,
912940
locker_mock_up,
913941
mandate,
914942
merchant_account,

crates/router/src/db.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod ephemeral_key;
1212
pub mod events;
1313
pub mod file;
1414
pub mod fraud_check;
15+
pub mod gsm;
1516
pub mod locker_mock_up;
1617
pub mod mandate;
1718
pub mod merchant_account;
@@ -80,6 +81,7 @@ pub trait StorageInterface:
8081
+ business_profile::BusinessProfileInterface
8182
+ organization::OrganizationInterface
8283
+ routing_algorithm::RoutingAlgorithmInterface
84+
+ gsm::GsmInterface
8385
+ 'static
8486
{
8587
fn get_scheduler_db(&self) -> Box<dyn scheduler::SchedulerInterface>;

0 commit comments

Comments
 (0)