forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.rs
More file actions
106 lines (100 loc) · 3.46 KB
/
Copy pathsubscription.rs
File metadata and controls
106 lines (100 loc) · 3.46 KB
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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
/// Status of a recurring-payment subscription.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "subscription_status", rename_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum SubscriptionStatus {
Active,
Cancelled,
Failed,
Completed,
}
/// Raw database row for a subscription.
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct SubscriptionRow {
pub id: Uuid,
pub payer_id: Uuid,
pub payer_account: String,
pub recipient_account: String,
pub asset_code: String,
pub asset_issuer: Option<String>,
pub amount: String,
pub interval_seconds: i64,
pub next_execution_at: DateTime<Utc>,
pub status: SubscriptionStatus,
pub onchain_subscription_id: Option<String>,
pub last_execution_at: Option<DateTime<Utc>>,
pub last_tx_hash: Option<String>,
pub failure_count: i32,
pub last_error: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
/// Public-facing subscription representation.
#[derive(Debug, Clone, Serialize)]
pub struct Subscription {
pub id: Uuid,
pub payer_id: Uuid,
pub payer_account: String,
pub recipient_account: String,
pub asset_code: String,
pub asset_issuer: Option<String>,
pub amount: String,
pub interval_seconds: i64,
pub next_execution_at: DateTime<Utc>,
pub status: SubscriptionStatus,
pub onchain_subscription_id: Option<String>,
pub last_execution_at: Option<DateTime<Utc>>,
pub last_tx_hash: Option<String>,
pub failure_count: i32,
pub last_error: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl From<SubscriptionRow> for Subscription {
fn from(row: SubscriptionRow) -> Self {
Self {
id: row.id,
payer_id: row.payer_id,
payer_account: row.payer_account,
recipient_account: row.recipient_account,
asset_code: row.asset_code,
asset_issuer: row.asset_issuer,
amount: row.amount,
interval_seconds: row.interval_seconds,
next_execution_at: row.next_execution_at,
status: row.status,
onchain_subscription_id: row.onchain_subscription_id,
last_execution_at: row.last_execution_at,
last_tx_hash: row.last_tx_hash,
failure_count: row.failure_count,
last_error: row.last_error,
created_at: row.created_at,
updated_at: row.updated_at,
}
}
}
/// Request body for POST /api/subscriptions.
///
/// Mirrors an on-chain `create_subscription` call the client has already
/// made (or is about to make) with their own wallet; this just indexes it.
#[derive(Debug, Deserialize)]
pub struct CreateSubscriptionRequest {
pub payer_account: String,
pub recipient_account: String,
pub asset_code: String,
pub asset_issuer: Option<String>,
pub amount: String,
pub interval_seconds: i64,
/// When the first execution should occur. Defaults to `now + interval`.
pub first_execution_at: Option<DateTime<Utc>>,
/// The id the Soroban contract assigned to this subscription, if the
/// client already submitted the on-chain `create_subscription` call.
pub onchain_subscription_id: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct ListSubscriptionsParams {
pub status: Option<SubscriptionStatus>,
}