Skip to content

Commit

Permalink
feat(router): add gateway_status_map interface (#2804)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-harsha-vardhan authored Nov 8, 2023
1 parent 7623ea9 commit a429b23
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 4 deletions.
97 changes: 97 additions & 0 deletions crates/diesel_models/src/gsm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! Gateway status mapping
use common_utils::custom_serde;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use time::PrimitiveDateTime;

use crate::schema::gateway_status_map;

#[derive(
Clone,
Debug,
Eq,
PartialEq,
router_derive::DebugAsDisplay,
Identifiable,
Queryable,
serde::Serialize,
)]
#[diesel(table_name = gateway_status_map, primary_key(connector, flow, sub_flow, code, message))]
pub struct GatewayStatusMap {
pub connector: String,
pub flow: String,
pub sub_flow: String,
pub code: String,
pub message: String,
pub status: String,
pub router_error: Option<String>,
pub decision: String,
#[serde(with = "custom_serde::iso8601")]
pub created_at: PrimitiveDateTime,
#[serde(with = "custom_serde::iso8601")]
pub last_modified: PrimitiveDateTime,
pub step_up_possible: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, Insertable)]
#[diesel(table_name = gateway_status_map)]
pub struct GatewayStatusMappingNew {
pub connector: String,
pub flow: String,
pub sub_flow: String,
pub code: String,
pub message: String,
pub status: String,
pub router_error: Option<String>,
pub decision: String,
pub step_up_possible: bool,
}

#[derive(
Clone,
Debug,
PartialEq,
Eq,
AsChangeset,
router_derive::DebugAsDisplay,
Default,
serde::Deserialize,
)]
#[diesel(table_name = gateway_status_map)]
pub struct GatewayStatusMapperUpdateInternal {
pub connector: Option<String>,
pub flow: Option<String>,
pub sub_flow: Option<String>,
pub code: Option<String>,
pub message: Option<String>,
pub status: Option<String>,
pub router_error: Option<Option<String>>,
pub decision: Option<String>,
pub step_up_possible: Option<bool>,
}

#[derive(Debug)]
pub struct GatewayStatusMappingUpdate {
pub status: Option<String>,
pub router_error: Option<Option<String>>,
pub decision: Option<String>,
pub step_up_possible: Option<bool>,
}

impl From<GatewayStatusMappingUpdate> for GatewayStatusMapperUpdateInternal {
fn from(value: GatewayStatusMappingUpdate) -> Self {
let GatewayStatusMappingUpdate {
decision,
status,
router_error,
step_up_possible,
} = value;
Self {
status,
router_error,
decision,
step_up_possible,
..Default::default()
}
}
}
1 change: 1 addition & 0 deletions crates/diesel_models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod events;
pub mod file;
#[allow(unused)]
pub mod fraud_check;
pub mod gsm;
#[cfg(feature = "kv_store")]
pub mod kv;
pub mod locker_mock_up;
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod events;
pub mod file;
pub mod fraud_check;
pub mod generics;
pub mod gsm;
pub mod locker_mock_up;
pub mod mandate;
pub mod merchant_account;
Expand Down
100 changes: 100 additions & 0 deletions crates/diesel_models/src/query/gsm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};
use error_stack::report;

use crate::{
errors, gsm::*, query::generics, schema::gateway_status_map::dsl, PgPooledConn, StorageResult,
};

impl GatewayStatusMappingNew {
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<GatewayStatusMap> {
generics::generic_insert(conn, self).await
}
}

impl GatewayStatusMap {
pub async fn find(
conn: &PgPooledConn,
connector: String,
flow: String,
sub_flow: String,
code: String,
message: String,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::connector
.eq(connector)
.and(dsl::flow.eq(flow))
.and(dsl::sub_flow.eq(sub_flow))
.and(dsl::code.eq(code))
.and(dsl::message.eq(message)),
)
.await
}

pub async fn retrieve_decision(
conn: &PgPooledConn,
connector: String,
flow: String,
sub_flow: String,
code: String,
message: String,
) -> StorageResult<String> {
Self::find(conn, connector, flow, sub_flow, code, message)
.await
.map(|item| item.decision)
}

pub async fn update(
conn: &PgPooledConn,
connector: String,
flow: String,
sub_flow: String,
code: String,
message: String,
gsm: GatewayStatusMappingUpdate,
) -> StorageResult<Self> {
generics::generic_update_with_results::<
<Self as HasTable>::Table,
GatewayStatusMapperUpdateInternal,
_,
_,
>(
conn,
dsl::connector
.eq(connector)
.and(dsl::flow.eq(flow))
.and(dsl::sub_flow.eq(sub_flow))
.and(dsl::code.eq(code))
.and(dsl::message.eq(message)),
gsm.into(),
)
.await?
.first()
.cloned()
.ok_or_else(|| {
report!(errors::DatabaseError::NotFound)
.attach_printable("Error while updating gsm entry")
})
}

pub async fn delete(
conn: &PgPooledConn,
connector: String,
flow: String,
sub_flow: String,
code: String,
message: String,
) -> StorageResult<bool> {
generics::generic_delete::<<Self as HasTable>::Table, _>(
conn,
dsl::connector
.eq(connector)
.and(dsl::flow.eq(flow))
.and(dsl::sub_flow.eq(sub_flow))
.and(dsl::code.eq(code))
.and(dsl::message.eq(message)),
)
.await
}
}
28 changes: 28 additions & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,33 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;

gateway_status_map (connector, flow, sub_flow, code, message) {
#[max_length = 64]
connector -> Varchar,
#[max_length = 64]
flow -> Varchar,
#[max_length = 64]
sub_flow -> Varchar,
#[max_length = 255]
code -> Varchar,
#[max_length = 1024]
message -> Varchar,
#[max_length = 64]
status -> Varchar,
#[max_length = 64]
router_error -> Nullable<Varchar>,
#[max_length = 64]
decision -> Varchar,
created_at -> Timestamp,
last_modified -> Timestamp,
step_up_possible -> Bool,
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
Expand Down Expand Up @@ -909,6 +936,7 @@ diesel::allow_tables_to_appear_in_same_query!(
events,
file_metadata,
fraud_check,
gateway_status_map,
locker_mock_up,
mandate,
merchant_account,
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod ephemeral_key;
pub mod events;
pub mod file;
pub mod fraud_check;
pub mod gsm;
pub mod locker_mock_up;
pub mod mandate;
pub mod merchant_account;
Expand Down Expand Up @@ -80,6 +81,7 @@ pub trait StorageInterface:
+ business_profile::BusinessProfileInterface
+ organization::OrganizationInterface
+ routing_algorithm::RoutingAlgorithmInterface
+ gsm::GsmInterface
+ 'static
{
fn get_scheduler_db(&self) -> Box<dyn scheduler::SchedulerInterface>;
Expand Down
Loading

0 comments on commit a429b23

Please sign in to comment.