1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
mod create;
pub use create::CreateSchema;
use tracing::debug;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use time::OffsetDateTime;
use warden_core::state::AppState;
use crate::ConfigurationError;
/// Transaction to monitor
#[derive(Deserialize, Debug, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "utoipa", schema(example = json!({
"schemaType": "custom.schema",
"schemaVersion": "1.0.0",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "FinancialTransaction",
"type": "object",
"required": ["transactionId", "amount", "currency", "timestamp"],
"properties": {
"transactionId": {
"type": "string",
"format": "uuid"
},
"amount": {
"type": "number",
"exclusiveMinimum": 0
},
"currency": {
"type": "string",
"pattern": "^[A-Z]{3}$",
"description": "ISO 4217 Alpha-3 code (e.g., USD, EUR)"
},
"timestamp": {
"type": "string",
"format": "date-time"
},
}
},
"createdAt": time::OffsetDateTime::now_utc().format(&time::format_description::well_known::Rfc3339).unwrap(),
"updatedAt": time::OffsetDateTime::now_utc().format(&time::format_description::well_known::Rfc3339).unwrap(),
})))]
#[serde(rename_all = "camelCase")]
pub struct TransactionSchema {
/// Transaction schema type
pub schema_type: String,
/// The schema's version
pub schema_version: String,
/// JSON schema for transcation
pub schema: serde_json::Value,
/// When the schema was created
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
/// When the schema was last updated
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
}
#[async_trait]
pub trait SchemaDriver {
async fn create_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
schema: &serde_json::Value,
) -> Result<TransactionSchema, ConfigurationError>;
async fn delete_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
) -> Result<(), ConfigurationError>;
async fn get_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
) -> Result<Option<TransactionSchema>, ConfigurationError>;
async fn update_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
schema: &serde_json::Value,
) -> Result<Option<TransactionSchema>, ConfigurationError>;
async fn get_schemas(
&self,
limit: i64,
first: Option<i64>,
after: Option<impl AsRef<str> + Send + Sync + Debug>,
) -> Result<Vec<TransactionSchema>, ConfigurationError>;
}
#[async_trait]
impl SchemaDriver for AppState {
#[tracing::instrument(skip(self, schema))]
async fn create_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
schema: &serde_json::Value,
) -> Result<TransactionSchema, crate::ConfigurationError> {
debug!("creating transaction schema");
sqlx::query_as!(
TransactionSchema,
"insert into transaction_schema (schema_type, schema_version, schema) values ($1, $2, $3)
returning *
",
kind.as_ref(),
version.as_ref(),
sqlx::types::Json(&schema) as _
)
.fetch_one(&self.database)
.await
.map_err(|e| e.into())
}
#[tracing::instrument(skip(self))]
async fn delete_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
) -> Result<(), crate::ConfigurationError> {
debug!("deleting transaction schema");
sqlx::query!(
"delete from transaction_schema where schema_type = $1 and schema_version = $2",
kind.as_ref(),
version.as_ref(),
)
.execute(&self.database)
.await?;
Ok(())
}
#[tracing::instrument(skip(self))]
async fn get_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
) -> Result<Option<TransactionSchema>, crate::ConfigurationError> {
debug!("getting transaction schema");
let result = sqlx::query_as!(
TransactionSchema,
"select
*
from transaction_schema where schema_type = $1 and schema_version = $2",
kind.as_ref(),
version.as_ref(),
)
.fetch_optional(&self.database)
.await?;
Ok(result)
}
#[tracing::instrument(skip(self, schema))]
async fn update_schema(
&self,
kind: impl AsRef<str> + Send + Sync + Debug,
version: impl AsRef<str> + Send + Sync + Debug,
schema: &serde_json::Value,
) -> Result<Option<TransactionSchema>, crate::ConfigurationError> {
debug!("updating transaction schema");
sqlx::query_as!(
TransactionSchema,
"
update
transaction_schema
set
schema = $3
where
schema_type = $1
and schema_version = $2
returning *
",
kind.as_ref(),
version.as_ref(),
sqlx::types::Json(&schema) as _
)
.fetch_optional(&self.database)
.await
.map_err(|e| e.into())
}
#[tracing::instrument(skip(self))]
async fn get_schemas(
&self,
limit: i64,
first: Option<i64>,
after: Option<impl AsRef<str> + Send + Sync + Debug>,
) -> Result<Vec<TransactionSchema>, ConfigurationError> {
debug!("getting transaction schemas");
let limit = first.unwrap_or(limit);
let mut last_type = String::default();
let mut last_version = String::default();
if let Some(s) = after {
let parts: Vec<&str> = s.as_ref().split(',').collect();
if parts.len() == 2 {
last_type = parts[0].to_string();
last_version = parts[1].to_string();
}
}
let rows = sqlx::query_as!(
TransactionSchema,
"
select *
from transaction_schema
where ($1 = '' or (schema_type, schema_version) > ($1, $2))
order by schema_type asc, schema_version asc
limit $3
",
&last_type,
&last_version,
limit + 1
)
.fetch_all(&self.database)
.await?;
Ok(rows)
}
}
|