|
| 1 | +use diesel::{AsChangeset, Identifiable, Insertable, Queryable}; |
| 2 | +use serde::{self, Deserialize, Serialize}; |
| 3 | +use serde_json; |
| 4 | + |
| 5 | +use crate::schema::authentication; |
| 6 | + |
| 7 | +#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize)] |
| 8 | +#[diesel(table_name = authentication, primary_key(authentication_id))] |
| 9 | +pub struct Authentication { |
| 10 | + pub authentication_id: String, |
| 11 | + pub merchant_id: String, |
| 12 | + pub authentication_connector: String, |
| 13 | + pub connector_authentication_id: Option<String>, |
| 14 | + pub authentication_data: Option<serde_json::Value>, |
| 15 | + pub payment_method_id: String, |
| 16 | + pub authentication_type: Option<common_enums::DecoupledAuthenticationType>, |
| 17 | + pub authentication_status: common_enums::AuthenticationStatus, |
| 18 | + pub authentication_lifecycle_status: common_enums::AuthenticationLifecycleStatus, |
| 19 | + #[serde(with = "common_utils::custom_serde::iso8601")] |
| 20 | + pub created_at: time::PrimitiveDateTime, |
| 21 | + #[serde(with = "common_utils::custom_serde::iso8601")] |
| 22 | + pub modified_at: time::PrimitiveDateTime, |
| 23 | + pub error_message: Option<String>, |
| 24 | + pub error_code: Option<String>, |
| 25 | + pub connector_metadata: Option<serde_json::Value>, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Insertable)] |
| 29 | +#[diesel(table_name = authentication)] |
| 30 | +pub struct AuthenticationNew { |
| 31 | + pub authentication_id: String, |
| 32 | + pub merchant_id: String, |
| 33 | + pub authentication_connector: String, |
| 34 | + pub connector_authentication_id: Option<String>, |
| 35 | + pub authentication_data: Option<serde_json::Value>, |
| 36 | + pub payment_method_id: String, |
| 37 | + pub authentication_type: Option<common_enums::DecoupledAuthenticationType>, |
| 38 | + pub authentication_status: common_enums::AuthenticationStatus, |
| 39 | + pub authentication_lifecycle_status: common_enums::AuthenticationLifecycleStatus, |
| 40 | + pub error_message: Option<String>, |
| 41 | + pub error_code: Option<String>, |
| 42 | + pub connector_metadata: Option<serde_json::Value>, |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Debug)] |
| 46 | +pub enum AuthenticationUpdate { |
| 47 | + AuthenticationDataUpdate { |
| 48 | + authentication_data: Option<serde_json::Value>, |
| 49 | + connector_authentication_id: Option<String>, |
| 50 | + payment_method_id: Option<String>, |
| 51 | + authentication_type: Option<common_enums::DecoupledAuthenticationType>, |
| 52 | + authentication_status: Option<common_enums::AuthenticationStatus>, |
| 53 | + authentication_lifecycle_status: Option<common_enums::AuthenticationLifecycleStatus>, |
| 54 | + connector_metadata: Option<serde_json::Value>, |
| 55 | + }, |
| 56 | + PostAuthorizationUpdate { |
| 57 | + authentication_lifecycle_status: common_enums::AuthenticationLifecycleStatus, |
| 58 | + }, |
| 59 | + ErrorUpdate { |
| 60 | + error_message: Option<String>, |
| 61 | + error_code: Option<String>, |
| 62 | + authentication_status: common_enums::AuthenticationStatus, |
| 63 | + connector_authentication_id: Option<String>, |
| 64 | + }, |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Clone, Debug, Eq, PartialEq, AsChangeset, Serialize, Deserialize)] |
| 68 | +#[diesel(table_name = authentication)] |
| 69 | +pub struct AuthenticationUpdateInternal { |
| 70 | + pub connector_authentication_id: Option<String>, |
| 71 | + pub authentication_data: Option<serde_json::Value>, |
| 72 | + pub payment_method_id: Option<String>, |
| 73 | + pub authentication_type: Option<common_enums::DecoupledAuthenticationType>, |
| 74 | + pub authentication_status: Option<common_enums::AuthenticationStatus>, |
| 75 | + pub authentication_lifecycle_status: Option<common_enums::AuthenticationLifecycleStatus>, |
| 76 | + pub modified_at: time::PrimitiveDateTime, |
| 77 | + pub error_message: Option<String>, |
| 78 | + pub error_code: Option<String>, |
| 79 | + pub connector_metadata: Option<serde_json::Value>, |
| 80 | +} |
| 81 | + |
| 82 | +impl AuthenticationUpdateInternal { |
| 83 | + pub fn apply_changeset(self, source: Authentication) -> Authentication { |
| 84 | + let Self { |
| 85 | + connector_authentication_id, |
| 86 | + authentication_data, |
| 87 | + payment_method_id, |
| 88 | + authentication_type, |
| 89 | + authentication_status, |
| 90 | + authentication_lifecycle_status, |
| 91 | + modified_at: _, |
| 92 | + error_code, |
| 93 | + error_message, |
| 94 | + connector_metadata, |
| 95 | + } = self; |
| 96 | + Authentication { |
| 97 | + connector_authentication_id: connector_authentication_id |
| 98 | + .or(source.connector_authentication_id), |
| 99 | + authentication_data: authentication_data.or(source.authentication_data), |
| 100 | + payment_method_id: payment_method_id.unwrap_or(source.payment_method_id), |
| 101 | + authentication_type: authentication_type.or(source.authentication_type), |
| 102 | + authentication_status: authentication_status.unwrap_or(source.authentication_status), |
| 103 | + authentication_lifecycle_status: authentication_lifecycle_status |
| 104 | + .unwrap_or(source.authentication_lifecycle_status), |
| 105 | + modified_at: common_utils::date_time::now(), |
| 106 | + error_code: error_code.or(source.error_code), |
| 107 | + error_message: error_message.or(source.error_message), |
| 108 | + connector_metadata: connector_metadata.or(source.connector_metadata), |
| 109 | + ..source |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl From<AuthenticationUpdate> for AuthenticationUpdateInternal { |
| 115 | + fn from(auth_update: AuthenticationUpdate) -> Self { |
| 116 | + match auth_update { |
| 117 | + AuthenticationUpdate::AuthenticationDataUpdate { |
| 118 | + authentication_data, |
| 119 | + connector_authentication_id, |
| 120 | + authentication_type, |
| 121 | + authentication_status, |
| 122 | + payment_method_id, |
| 123 | + authentication_lifecycle_status, |
| 124 | + connector_metadata, |
| 125 | + } => Self { |
| 126 | + authentication_data, |
| 127 | + connector_authentication_id, |
| 128 | + authentication_type, |
| 129 | + authentication_status, |
| 130 | + authentication_lifecycle_status, |
| 131 | + modified_at: common_utils::date_time::now(), |
| 132 | + payment_method_id, |
| 133 | + error_message: None, |
| 134 | + error_code: None, |
| 135 | + connector_metadata, |
| 136 | + }, |
| 137 | + AuthenticationUpdate::ErrorUpdate { |
| 138 | + error_message, |
| 139 | + error_code, |
| 140 | + authentication_status, |
| 141 | + connector_authentication_id, |
| 142 | + } => Self { |
| 143 | + error_code, |
| 144 | + error_message, |
| 145 | + authentication_status: Some(authentication_status), |
| 146 | + authentication_data: None, |
| 147 | + connector_authentication_id, |
| 148 | + authentication_type: None, |
| 149 | + authentication_lifecycle_status: None, |
| 150 | + modified_at: common_utils::date_time::now(), |
| 151 | + payment_method_id: None, |
| 152 | + connector_metadata: None, |
| 153 | + }, |
| 154 | + AuthenticationUpdate::PostAuthorizationUpdate { |
| 155 | + authentication_lifecycle_status, |
| 156 | + } => Self { |
| 157 | + connector_authentication_id: None, |
| 158 | + authentication_data: None, |
| 159 | + payment_method_id: None, |
| 160 | + authentication_type: None, |
| 161 | + authentication_status: None, |
| 162 | + authentication_lifecycle_status: Some(authentication_lifecycle_status), |
| 163 | + modified_at: common_utils::date_time::now(), |
| 164 | + error_message: None, |
| 165 | + error_code: None, |
| 166 | + connector_metadata: None, |
| 167 | + }, |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments