| 1 |
|
| 2 |
|
| 3 |
use chrono::{DateTime, Utc}; |
| 4 |
use serde::Serialize; |
| 5 |
use sqlx::FromRow; |
| 6 |
|
| 7 |
use super::super::id_types::*; |
| 8 |
use super::super::validated_types::*; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#[derive(Debug, Clone)] |
| 13 |
pub struct CompletedTransactionInfo { |
| 14 |
|
| 15 |
pub stripe_payment_intent_id: String, |
| 16 |
|
| 17 |
pub completed_at: DateTime<Utc>, |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#[derive(Debug, Clone, FromRow, Serialize)] |
| 31 |
pub struct DbTransaction { |
| 32 |
|
| 33 |
pub id: TransactionId, |
| 34 |
|
| 35 |
pub buyer_id: Option<UserId>, |
| 36 |
|
| 37 |
pub seller_id: Option<UserId>, |
| 38 |
|
| 39 |
pub item_id: Option<ItemId>, |
| 40 |
|
| 41 |
pub amount_cents: Cents, |
| 42 |
|
| 43 |
pub platform_fee_cents: Cents, |
| 44 |
|
| 45 |
pub currency: String, |
| 46 |
|
| 47 |
pub status: super::super::TransactionStatus, |
| 48 |
|
| 49 |
pub stripe_payment_intent_id: Option<String>, |
| 50 |
|
| 51 |
pub stripe_checkout_session_id: Option<String>, |
| 52 |
|
| 53 |
pub created_at: DateTime<Utc>, |
| 54 |
|
| 55 |
pub completed_at: Option<DateTime<Utc>>, |
| 56 |
|
| 57 |
|
| 58 |
pub item_title: Option<String>, |
| 59 |
|
| 60 |
pub seller_username: Option<String>, |
| 61 |
|
| 62 |
pub share_contact: bool, |
| 63 |
|
| 64 |
pub project_id: Option<ProjectId>, |
| 65 |
|
| 66 |
pub parent_transaction_id: Option<TransactionId>, |
| 67 |
|
| 68 |
pub promo_code_id: Option<PromoCodeId>, |
| 69 |
|
| 70 |
pub guest_email: Option<String>, |
| 71 |
|
| 72 |
pub claim_token: Option<ClaimToken>, |
| 73 |
|
| 74 |
pub claimed_by: Option<UserId>, |
| 75 |
|
| 76 |
pub download_token: Option<DownloadToken>, |
| 77 |
} |
| 78 |
|
| 79 |
impl DbTransaction { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pub fn completed_info(&self) -> Option<CompletedTransactionInfo> { |
| 85 |
Some(CompletedTransactionInfo { |
| 86 |
stripe_payment_intent_id: self.stripe_payment_intent_id.clone()?, |
| 87 |
completed_at: self.completed_at?, |
| 88 |
}) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
#[derive(Debug, Clone, FromRow)] |
| 94 |
pub struct DbTransactionExportRow { |
| 95 |
pub created_at: DateTime<Utc>, |
| 96 |
pub item_id: Option<ItemId>, |
| 97 |
pub item_title: Option<String>, |
| 98 |
pub amount_cents: Cents, |
| 99 |
pub status: super::super::TransactionStatus, |
| 100 |
|
| 101 |
pub buyer_email: Option<String>, |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
#[derive(Debug, Clone, FromRow)] |
| 106 |
pub struct DbPurchaseRow { |
| 107 |
|
| 108 |
pub transaction_id: TransactionId, |
| 109 |
|
| 110 |
pub item_id: ItemId, |
| 111 |
|
| 112 |
pub title: String, |
| 113 |
|
| 114 |
pub creator: String, |
| 115 |
|
| 116 |
pub item_type: super::super::ItemType, |
| 117 |
|
| 118 |
pub purchased_at: DateTime<Utc>, |
| 119 |
|
| 120 |
pub is_free: bool, |
| 121 |
|
| 122 |
pub license_key_code: Option<KeyCode>, |
| 123 |
|
| 124 |
pub has_new_version: bool, |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
#[derive(Debug, Clone, FromRow)] |
| 129 |
#[allow(dead_code)] |
| 130 |
pub struct DbLoginToken { |
| 131 |
|
| 132 |
pub id: LoginTokenId, |
| 133 |
|
| 134 |
pub user_id: UserId, |
| 135 |
|
| 136 |
pub token_hash: String, |
| 137 |
|
| 138 |
pub expires_at: DateTime<Utc>, |
| 139 |
|
| 140 |
pub used_at: Option<DateTime<Utc>>, |
| 141 |
|
| 142 |
pub created_at: DateTime<Utc>, |
| 143 |
} |
| 144 |
|
| 145 |
#[cfg(test)] |
| 146 |
mod tests { |
| 147 |
use super::*; |
| 148 |
|
| 149 |
fn make_transaction( |
| 150 |
status: super::super::super::TransactionStatus, |
| 151 |
pi_id: Option<&str>, |
| 152 |
completed: Option<DateTime<Utc>>, |
| 153 |
) -> DbTransaction { |
| 154 |
DbTransaction { |
| 155 |
id: TransactionId::nil(), |
| 156 |
buyer_id: Some(UserId::nil()), |
| 157 |
seller_id: None, |
| 158 |
item_id: None, |
| 159 |
amount_cents: Cents::ZERO, |
| 160 |
platform_fee_cents: Cents::ZERO, |
| 161 |
currency: "usd".to_string(), |
| 162 |
status, |
| 163 |
stripe_payment_intent_id: pi_id.map(|s| s.to_string()), |
| 164 |
stripe_checkout_session_id: None, |
| 165 |
created_at: Utc::now(), |
| 166 |
completed_at: completed, |
| 167 |
item_title: None, |
| 168 |
seller_username: None, |
| 169 |
share_contact: false, |
| 170 |
project_id: None, |
| 171 |
parent_transaction_id: None, |
| 172 |
promo_code_id: None, |
| 173 |
guest_email: None, |
| 174 |
claim_token: None, |
| 175 |
claimed_by: None, |
| 176 |
download_token: None, |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn completed_info_for_paid_transaction() { |
| 182 |
let now = Utc::now(); |
| 183 |
let tx = make_transaction( |
| 184 |
super::super::super::TransactionStatus::Completed, |
| 185 |
Some("pi_123"), |
| 186 |
Some(now), |
| 187 |
); |
| 188 |
let info = tx.completed_info().unwrap(); |
| 189 |
assert_eq!(info.stripe_payment_intent_id, "pi_123"); |
| 190 |
assert_eq!(info.completed_at, now); |
| 191 |
} |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn completed_info_none_for_pending() { |
| 195 |
let tx = make_transaction( |
| 196 |
super::super::super::TransactionStatus::Pending, |
| 197 |
None, |
| 198 |
None, |
| 199 |
); |
| 200 |
assert!(tx.completed_info().is_none()); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn completed_info_none_for_free_claim() { |
| 205 |
|
| 206 |
let tx = make_transaction( |
| 207 |
super::super::super::TransactionStatus::Completed, |
| 208 |
None, |
| 209 |
Some(Utc::now()), |
| 210 |
); |
| 211 |
assert!(tx.completed_info().is_none()); |
| 212 |
} |
| 213 |
} |
| 214 |
|