| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 17 |
pub enum StripeConnectionStatus { |
| 18 |
|
| 19 |
NotConnected, |
| 20 |
|
| 21 |
Onboarding, |
| 22 |
|
| 23 |
PayoutsPending, |
| 24 |
|
| 25 |
Active, |
| 26 |
} |
| 27 |
|
| 28 |
impl StripeConnectionStatus { |
| 29 |
|
| 30 |
pub fn text(&self) -> &'static str { |
| 31 |
match self { |
| 32 |
Self::NotConnected => "Not connected", |
| 33 |
Self::Onboarding => "Onboarding incomplete", |
| 34 |
Self::PayoutsPending => "Payouts pending", |
| 35 |
Self::Active => "Active", |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
pub fn css_class(&self) -> &'static str { |
| 41 |
match self { |
| 42 |
Self::NotConnected => "inactive", |
| 43 |
Self::Onboarding | Self::PayoutsPending => "pending", |
| 44 |
Self::Active => "active", |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[derive(Debug, Clone, FromRow, Serialize)] |
| 57 |
pub struct DbUser { |
| 58 |
|
| 59 |
pub id: UserId, |
| 60 |
|
| 61 |
pub username: Username, |
| 62 |
|
| 63 |
|
| 64 |
pub email: Email, |
| 65 |
|
| 66 |
pub password_hash: String, |
| 67 |
|
| 68 |
pub display_name: Option<String>, |
| 69 |
|
| 70 |
pub bio: Option<String>, |
| 71 |
|
| 72 |
pub avatar_url: Option<String>, |
| 73 |
|
| 74 |
pub created_at: DateTime<Utc>, |
| 75 |
|
| 76 |
pub updated_at: DateTime<Utc>, |
| 77 |
|
| 78 |
|
| 79 |
pub stripe_account_id: Option<String>, |
| 80 |
|
| 81 |
pub stripe_onboarding_complete: bool, |
| 82 |
|
| 83 |
pub stripe_payouts_enabled: bool, |
| 84 |
|
| 85 |
pub stripe_charges_enabled: bool, |
| 86 |
|
| 87 |
pub stripe_tax_enabled: bool, |
| 88 |
|
| 89 |
|
| 90 |
pub email_verified: bool, |
| 91 |
|
| 92 |
pub email_verification_token: Option<String>, |
| 93 |
|
| 94 |
pub email_verification_sent_at: Option<DateTime<Utc>>, |
| 95 |
|
| 96 |
|
| 97 |
pub failed_login_attempts: i32, |
| 98 |
|
| 99 |
pub locked_until: Option<DateTime<Utc>>, |
| 100 |
|
| 101 |
pub last_failed_login_at: Option<DateTime<Utc>>, |
| 102 |
|
| 103 |
|
| 104 |
pub can_create_projects: bool, |
| 105 |
|
| 106 |
pub upload_trusted: bool, |
| 107 |
|
| 108 |
|
| 109 |
pub login_notification_enabled: bool, |
| 110 |
|
| 111 |
|
| 112 |
pub totp_secret: Option<String>, |
| 113 |
|
| 114 |
pub totp_enabled: bool, |
| 115 |
|
| 116 |
|
| 117 |
pub suspended_at: Option<DateTime<Utc>>, |
| 118 |
|
| 119 |
pub suspension_reason: Option<String>, |
| 120 |
|
| 121 |
pub appeal_text: Option<String>, |
| 122 |
|
| 123 |
pub appeal_submitted_at: Option<DateTime<Utc>>, |
| 124 |
|
| 125 |
pub appeal_decision: Option<String>, |
| 126 |
|
| 127 |
pub appeal_response: Option<String>, |
| 128 |
|
| 129 |
pub appeal_decided_at: Option<DateTime<Utc>>, |
| 130 |
|
| 131 |
|
| 132 |
pub notify_sale: bool, |
| 133 |
|
| 134 |
pub notify_follower: bool, |
| 135 |
|
| 136 |
pub notify_release: bool, |
| 137 |
|
| 138 |
pub notify_issues: bool, |
| 139 |
|
| 140 |
pub last_broadcast_at: Option<DateTime<Utc>>, |
| 141 |
|
| 142 |
|
| 143 |
pub onboarding_email_step: i16, |
| 144 |
|
| 145 |
pub onboarding_email_sent_at: Option<DateTime<Utc>>, |
| 146 |
|
| 147 |
pub cache_generation: i64, |
| 148 |
|
| 149 |
pub creator_tier: Option<String>, |
| 150 |
|
| 151 |
pub storage_used_bytes: i64, |
| 152 |
|
| 153 |
pub max_file_override_bytes: Option<i64>, |
| 154 |
|
| 155 |
pub grandfathered_until: Option<DateTime<Utc>>, |
| 156 |
|
| 157 |
pub tips_enabled: bool, |
| 158 |
|
| 159 |
pub notify_tip: bool, |
| 160 |
|
| 161 |
pub notify_status: bool, |
| 162 |
|
| 163 |
pub deactivated_at: Option<DateTime<Utc>>, |
| 164 |
|
| 165 |
pub is_sandbox: bool, |
| 166 |
|
| 167 |
pub sandbox_expires_at: Option<DateTime<Utc>>, |
| 168 |
|
| 169 |
|
| 170 |
pub terminated_at: Option<DateTime<Utc>>, |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
pub content_removal_at: Option<DateTime<Utc>>, |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
pub creator_paused_at: Option<DateTime<Utc>>, |
| 179 |
|
| 180 |
pub jwt_invalidated_at: Option<DateTime<Utc>>, |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
pub is_founder: bool, |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
pub founder_locked_at: Option<DateTime<Utc>>, |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
pub feed_key_version: i32, |
| 197 |
} |
| 198 |
|
| 199 |
impl DbUser { |
| 200 |
|
| 201 |
pub fn is_suspended(&self) -> bool { |
| 202 |
self.suspended_at.is_some() |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
pub fn is_deactivated(&self) -> bool { |
| 207 |
self.deactivated_at.is_some() |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
pub fn is_creator_paused(&self) -> bool { |
| 212 |
self.creator_paused_at.is_some() |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
pub fn is_founder_locked(&self) -> bool { |
| 218 |
self.founder_locked_at.is_some() |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
impl DbUser { |
| 223 |
|
| 224 |
pub fn stripe_connection_status(&self) -> StripeConnectionStatus { |
| 225 |
if self.stripe_account_id.is_none() { |
| 226 |
StripeConnectionStatus::NotConnected |
| 227 |
} else if !self.stripe_onboarding_complete { |
| 228 |
StripeConnectionStatus::Onboarding |
| 229 |
} else if !self.stripe_payouts_enabled { |
| 230 |
StripeConnectionStatus::PayoutsPending |
| 231 |
} else { |
| 232 |
StripeConnectionStatus::Active |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
#[cfg(test)] |
| 238 |
mod tests { |
| 239 |
use super::*; |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn stripe_status_not_connected() { |
| 243 |
let status = StripeConnectionStatus::NotConnected; |
| 244 |
assert_eq!(status.text(), "Not connected"); |
| 245 |
assert_eq!(status.css_class(), "inactive"); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn stripe_status_onboarding() { |
| 250 |
let status = StripeConnectionStatus::Onboarding; |
| 251 |
assert_eq!(status.text(), "Onboarding incomplete"); |
| 252 |
assert_eq!(status.css_class(), "pending"); |
| 253 |
} |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn stripe_status_payouts_pending() { |
| 257 |
let status = StripeConnectionStatus::PayoutsPending; |
| 258 |
assert_eq!(status.text(), "Payouts pending"); |
| 259 |
assert_eq!(status.css_class(), "pending"); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn stripe_status_active() { |
| 264 |
let status = StripeConnectionStatus::Active; |
| 265 |
assert_eq!(status.text(), "Active"); |
| 266 |
assert_eq!(status.css_class(), "active"); |
| 267 |
} |
| 268 |
|
| 269 |
fn make_user(account_id: Option<&str>, onboarding: bool, payouts: bool) -> DbUser { |
| 270 |
DbUser { |
| 271 |
id: UserId::nil(), |
| 272 |
username: Username::from_trusted("test".to_string()), |
| 273 |
email: Email::from_trusted("test@example.com".to_string()), |
| 274 |
password_hash: String::new(), |
| 275 |
display_name: None, |
| 276 |
bio: None, |
| 277 |
avatar_url: None, |
| 278 |
created_at: Utc::now(), |
| 279 |
updated_at: Utc::now(), |
| 280 |
stripe_account_id: account_id.map(|s| s.to_string()), |
| 281 |
stripe_onboarding_complete: onboarding, |
| 282 |
stripe_payouts_enabled: payouts, |
| 283 |
stripe_charges_enabled: false, |
| 284 |
stripe_tax_enabled: false, |
| 285 |
email_verified: false, |
| 286 |
email_verification_token: None, |
| 287 |
email_verification_sent_at: None, |
| 288 |
failed_login_attempts: 0, |
| 289 |
locked_until: None, |
| 290 |
last_failed_login_at: None, |
| 291 |
can_create_projects: false, |
| 292 |
upload_trusted: false, |
| 293 |
login_notification_enabled: true, |
| 294 |
totp_secret: None, |
| 295 |
totp_enabled: false, |
| 296 |
suspended_at: None, |
| 297 |
suspension_reason: None, |
| 298 |
appeal_text: None, |
| 299 |
appeal_submitted_at: None, |
| 300 |
appeal_decision: None, |
| 301 |
appeal_response: None, |
| 302 |
appeal_decided_at: None, |
| 303 |
notify_sale: true, |
| 304 |
notify_follower: true, |
| 305 |
notify_release: true, |
| 306 |
notify_issues: true, |
| 307 |
last_broadcast_at: None, |
| 308 |
onboarding_email_step: 0, |
| 309 |
onboarding_email_sent_at: None, |
| 310 |
cache_generation: 0, |
| 311 |
creator_tier: None, |
| 312 |
storage_used_bytes: 0, |
| 313 |
max_file_override_bytes: None, |
| 314 |
grandfathered_until: None, |
| 315 |
tips_enabled: false, |
| 316 |
notify_tip: true, |
| 317 |
notify_status: false, |
| 318 |
deactivated_at: None, |
| 319 |
is_sandbox: false, |
| 320 |
sandbox_expires_at: None, |
| 321 |
terminated_at: None, |
| 322 |
content_removal_at: None, |
| 323 |
creator_paused_at: None, |
| 324 |
jwt_invalidated_at: None, |
| 325 |
is_founder: false, |
| 326 |
founder_locked_at: None, |
| 327 |
feed_key_version: 0, |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn db_user_stripe_status_not_connected() { |
| 333 |
let u = make_user(None, false, false); |
| 334 |
assert_eq!(u.stripe_connection_status(), StripeConnectionStatus::NotConnected); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn db_user_stripe_status_onboarding() { |
| 339 |
let u = make_user(Some("acct_123"), false, false); |
| 340 |
assert_eq!(u.stripe_connection_status(), StripeConnectionStatus::Onboarding); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn db_user_stripe_status_payouts_pending() { |
| 345 |
let u = make_user(Some("acct_123"), true, false); |
| 346 |
assert_eq!(u.stripe_connection_status(), StripeConnectionStatus::PayoutsPending); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn db_user_stripe_status_active() { |
| 351 |
let u = make_user(Some("acct_123"), true, true); |
| 352 |
assert_eq!(u.stripe_connection_status(), StripeConnectionStatus::Active); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn is_suspended_true_when_set() { |
| 357 |
let mut u = make_user(None, false, false); |
| 358 |
u.suspended_at = Some(Utc::now()); |
| 359 |
assert!(u.is_suspended()); |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn is_suspended_false_when_none() { |
| 364 |
let u = make_user(None, false, false); |
| 365 |
assert!(!u.is_suspended()); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn is_deactivated_true_when_set() { |
| 370 |
let mut u = make_user(None, false, false); |
| 371 |
u.deactivated_at = Some(Utc::now()); |
| 372 |
assert!(u.is_deactivated()); |
| 373 |
} |
| 374 |
|
| 375 |
#[test] |
| 376 |
fn is_deactivated_false_when_none() { |
| 377 |
let u = make_user(None, false, false); |
| 378 |
assert!(!u.is_deactivated()); |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn is_creator_paused_true_when_set() { |
| 383 |
let mut u = make_user(None, false, false); |
| 384 |
u.creator_paused_at = Some(Utc::now()); |
| 385 |
assert!(u.is_creator_paused()); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn is_creator_paused_false_when_none() { |
| 390 |
let u = make_user(None, false, false); |
| 391 |
assert!(!u.is_creator_paused()); |
| 392 |
} |
| 393 |
} |
| 394 |
|