-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05c9174
commit cbd83af
Showing
12 changed files
with
354 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,4 @@ pub mod user; | |
pub mod user_authentication_method; | ||
pub mod user_key_store; | ||
pub mod user_role; | ||
pub mod relay; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::generics; | ||
use crate::{ | ||
errors, | ||
relay::{Relay, RelayNew, RelayUpdate, RelayUpdateInternal}, | ||
schema::relay::dsl, | ||
PgPooledConn, StorageResult, | ||
}; | ||
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods}; | ||
|
||
impl RelayNew { | ||
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<Relay> { | ||
generics::generic_insert(conn, self).await | ||
} | ||
} | ||
|
||
impl Relay { | ||
pub async fn update(self, conn: &PgPooledConn, relay: RelayUpdate) -> StorageResult<Self> { | ||
match generics::generic_update_with_unique_predicate_get_result::< | ||
<Self as HasTable>::Table, | ||
_, | ||
_, | ||
_, | ||
>( | ||
conn, | ||
dsl::id | ||
.eq(self.id.to_owned()) | ||
.and(dsl::merchant_id.eq(self.merchant_id.to_owned())), | ||
RelayUpdateInternal::from(relay), | ||
) | ||
.await | ||
{ | ||
Err(error) => match error.current_context() { | ||
errors::DatabaseError::NoFieldsToUpdate => Ok(self), | ||
_ => Err(error), | ||
}, | ||
result => result, | ||
} | ||
} | ||
|
||
pub async fn find_by_id( | ||
conn: &PgPooledConn, | ||
id: &str, | ||
) -> StorageResult<Self> { | ||
generics::generic_find_one::<<Self as HasTable>::Table, _, _>( | ||
conn, | ||
dsl::id.eq(id.to_owned()), | ||
) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use crate::{enums as storage_enums, schema::relay}; | ||
use common_utils::pii; | ||
use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable}; | ||
use time::PrimitiveDateTime; | ||
|
||
#[derive( | ||
Clone, | ||
Debug, | ||
Eq, | ||
Identifiable, | ||
Queryable, | ||
Selectable, | ||
PartialEq, | ||
serde::Serialize, | ||
serde::Deserialize, | ||
)] | ||
#[diesel(table_name = relay)] | ||
pub struct Relay { | ||
pub id: String, | ||
pub connector_resource_id: String, | ||
pub connector_id: common_utils::id_type::MerchantConnectorAccountId, | ||
pub profile_id: common_utils::id_type::ProfileId, | ||
pub merchant_id: common_utils::id_type::MerchantId, | ||
pub relay_type: storage_enums::RelayType, | ||
pub request_data: Option<pii::SecretSerdeValue>, | ||
pub status: storage_enums::RelayStatus, | ||
pub connector_reference_id: Option<String>, | ||
pub error_code: Option<String>, | ||
pub error_reason: Option<String>, | ||
#[serde(with = "common_utils::custom_serde::iso8601")] | ||
pub created_at: PrimitiveDateTime, | ||
#[serde(with = "common_utils::custom_serde::iso8601")] | ||
pub modified_at: PrimitiveDateTime, | ||
pub response_data: Option<pii::SecretSerdeValue>, | ||
} | ||
|
||
#[derive( | ||
Clone, | ||
Debug, | ||
Eq, | ||
PartialEq, | ||
Insertable, | ||
router_derive::DebugAsDisplay, | ||
serde::Serialize, | ||
serde::Deserialize, | ||
router_derive::Setter, | ||
)] | ||
#[diesel(table_name = relay)] | ||
pub struct RelayNew { | ||
pub id: String, | ||
pub connector_resource_id: String, | ||
pub connector_id: common_utils::id_type::MerchantConnectorAccountId, | ||
pub profile_id: common_utils::id_type::ProfileId, | ||
pub merchant_id: common_utils::id_type::MerchantId, | ||
pub relay_type: storage_enums::RelayType, | ||
pub request_data: Option<pii::SecretSerdeValue>, | ||
pub status: storage_enums::RelayStatus, | ||
pub connector_reference_id: Option<String>, | ||
pub error_code: Option<String>, | ||
pub error_reason: Option<String>, | ||
#[serde(with = "common_utils::custom_serde::iso8601")] | ||
pub created_at: PrimitiveDateTime, | ||
#[serde(with = "common_utils::custom_serde::iso8601")] | ||
pub modified_at: PrimitiveDateTime, | ||
pub response_data: Option<pii::SecretSerdeValue>, | ||
} | ||
|
||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] | ||
pub enum RelayUpdate { | ||
ErrorUpdate { | ||
error_code: String, | ||
error_reason: String, | ||
}, | ||
StatusUpdate { | ||
connector_reference_id: Option<String>, | ||
status: storage_enums::RelayStatus, | ||
}, | ||
} | ||
|
||
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)] | ||
#[table_name = "relay"] | ||
pub struct RelayUpdateInternal { | ||
pub connector_reference_id: Option<String>, | ||
pub status: Option<storage_enums::RelayStatus>, | ||
pub error_code: Option<String>, | ||
pub error_reason: Option<String>, | ||
pub modified_at: PrimitiveDateTime, | ||
} | ||
|
||
impl RelayUpdateInternal { | ||
pub fn create_relay(self, source: Relay) -> Relay { | ||
Relay { | ||
status: self.status.unwrap_or_default(), | ||
modified_at: self.modified_at, | ||
connector_reference_id: self.connector_reference_id, | ||
error_code: self.error_code, | ||
error_reason: self.error_reason, | ||
..source | ||
} | ||
} | ||
} | ||
|
||
impl From<RelayUpdate> for RelayUpdateInternal { | ||
fn from(value: RelayUpdate) -> Self { | ||
match value { | ||
RelayUpdate::ErrorUpdate { | ||
error_code, | ||
error_reason, | ||
} => Self { | ||
error_code: Some(error_code), | ||
error_reason: Some(error_reason), | ||
connector_reference_id: None, | ||
status: None, | ||
modified_at: common_utils::date_time::now(), | ||
}, | ||
RelayUpdate::StatusUpdate { | ||
connector_reference_id, | ||
status, | ||
} => Self { | ||
connector_reference_id, | ||
status: Some(status), | ||
error_code: None, | ||
error_reason: None, | ||
modified_at: common_utils::date_time::now(), | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use diesel_models; | ||
|
||
use crate::core::errors::{self, CustomResult}; | ||
|
||
#[async_trait::async_trait] | ||
pub trait RelayInterface { | ||
async fn insert_relay( | ||
&self, | ||
new: diesel_models::relay::RelayNew, | ||
) -> CustomResult<diesel_models::relay::Relay, errors::StorageError>; | ||
|
||
async fn update_relay( | ||
&self, | ||
this: diesel_models::relay::Relay, | ||
relay: diesel_models::relay::RelayUpdate, | ||
) -> CustomResult<diesel_models::relay::Relay, errors::StorageError>; | ||
|
||
async fn find_relay_by_id( | ||
&self, | ||
id: &str, | ||
) -> CustomResult<diesel_models::relay::Relay, errors::StorageError>; | ||
} | ||
|
||
mod storage { | ||
use error_stack::report; | ||
|
||
use super::RelayInterface; | ||
use crate::{ | ||
connection, | ||
core::errors::{self, CustomResult}, | ||
services::Store, | ||
}; | ||
|
||
#[async_trait::async_trait] | ||
impl RelayInterface for Store { | ||
async fn insert_relay( | ||
&self, | ||
new: diesel_models::relay::RelayNew, | ||
) -> CustomResult<diesel_models::relay::Relay, errors::StorageError> { | ||
let conn = connection::pg_connection_write(self).await?; | ||
new.insert(&conn) | ||
.await | ||
.map_err(|error| report!(errors::StorageError::from(error))) | ||
} | ||
|
||
async fn update_relay( | ||
&self, | ||
this: diesel_models::relay::Relay, | ||
relay: diesel_models::relay::RelayUpdate, | ||
) -> CustomResult<diesel_models::relay::Relay, errors::StorageError> { | ||
let conn = connection::pg_connection_write(self).await?; | ||
this.update(&conn, relay) | ||
.await | ||
.map_err(|error| report!(errors::StorageError::from(error))) | ||
} | ||
|
||
async fn find_relay_by_id( | ||
&self, | ||
id: &str, | ||
) -> CustomResult<diesel_models::relay::Relay, errors::StorageError> { | ||
let conn = connection::pg_connection_read(self).await?; | ||
diesel_models::relay::Relay::find_by_id(&conn, id) | ||
.await | ||
.map_err(|error| report!(errors::StorageError::from(error))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TYPE IF EXISTS "RelayStatus"; | ||
|
||
DROP TYPE IF EXISTS "RelayType"; | ||
|
||
DROP TABLE relay; |
Oops, something went wrong.