allsource-core 0.19.1

High-performance event store core built in Rust
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use crate::{
    domain::value_objects::{CreatorId, TenantId, WalletAddress},
    error::Result,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Creator status in the paywall system
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum CreatorStatus {
    /// Creator has signed up but not verified email
    #[default]
    Pending,
    /// Creator is active and can receive payments
    Active,
    /// Creator has been temporarily suspended
    Suspended,
    /// Creator has been permanently deactivated
    Deactivated,
}

/// Creator tier determining fees and features
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum CreatorTier {
    /// Free tier: 10% platform fee
    #[default]
    Free,
    /// Creator tier ($29/month): 7% platform fee
    Creator,
    /// Pro tier ($99/month): 5% platform fee
    Pro,
    /// Enterprise tier (custom pricing): Custom fee
    Enterprise,
}

impl CreatorTier {
    /// Get the platform fee percentage for this tier
    pub fn fee_percentage(&self) -> u64 {
        match self {
            CreatorTier::Free => 10,
            CreatorTier::Creator => 7,
            CreatorTier::Pro => 5,
            CreatorTier::Enterprise => 3,
        }
    }
}

/// Creator settings for customization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatorSettings {
    /// Default price for new articles (in cents)
    pub default_price_cents: u64,
    /// Whether to show reading time estimates
    pub show_reading_time: bool,
    /// Custom branding color (hex)
    pub brand_color: Option<String>,
    /// Custom CTA text
    pub unlock_button_text: Option<String>,
}

impl Default for CreatorSettings {
    fn default() -> Self {
        Self {
            default_price_cents: 50, // $0.50 default
            show_reading_time: true,
            brand_color: None,
            unlock_button_text: None,
        }
    }
}

/// Domain Entity: Creator
///
/// Represents a content creator in the paywall system.
/// Creators sign up, configure their paywall settings, and receive payments.
///
/// Domain Rules:
/// - Creator must have a valid email
/// - Creator must have a valid wallet address for receiving payments
/// - Only active creators can receive payments
/// - Creator tier determines platform fee
/// - API key must be kept secure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Creator {
    id: CreatorId,
    tenant_id: TenantId,
    email: String,
    name: Option<String>,
    wallet_address: WalletAddress,
    blog_url: Option<String>,
    status: CreatorStatus,
    tier: CreatorTier,
    settings: CreatorSettings,
    api_key_hash: Option<String>,
    email_verified: bool,
    total_revenue_cents: u64,
    total_articles: u32,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    metadata: serde_json::Value,
}

impl Creator {
    /// Create a new creator with validation
    pub fn new(
        tenant_id: TenantId,
        email: String,
        wallet_address: WalletAddress,
        blog_url: Option<String>,
    ) -> Result<Self> {
        Self::validate_email(&email)?;

        if let Some(ref url) = blog_url {
            Self::validate_url(url)?;
        }

        let now = Utc::now();
        Ok(Self {
            id: CreatorId::new(),
            tenant_id,
            email,
            name: None,
            wallet_address,
            blog_url,
            status: CreatorStatus::Pending,
            tier: CreatorTier::Free,
            settings: CreatorSettings::default(),
            api_key_hash: None,
            email_verified: false,
            total_revenue_cents: 0,
            total_articles: 0,
            created_at: now,
            updated_at: now,
            metadata: serde_json::json!({}),
        })
    }

    /// Reconstruct creator from storage (bypasses validation)
    #[allow(clippy::too_many_arguments)]
    pub fn reconstruct(
        id: CreatorId,
        tenant_id: TenantId,
        email: String,
        name: Option<String>,
        wallet_address: WalletAddress,
        blog_url: Option<String>,
        status: CreatorStatus,
        tier: CreatorTier,
        settings: CreatorSettings,
        api_key_hash: Option<String>,
        email_verified: bool,
        total_revenue_cents: u64,
        total_articles: u32,
        created_at: DateTime<Utc>,
        updated_at: DateTime<Utc>,
        metadata: serde_json::Value,
    ) -> Self {
        Self {
            id,
            tenant_id,
            email,
            name,
            wallet_address,
            blog_url,
            status,
            tier,
            settings,
            api_key_hash,
            email_verified,
            total_revenue_cents,
            total_articles,
            created_at,
            updated_at,
            metadata,
        }
    }

    // Getters

    pub fn id(&self) -> &CreatorId {
        &self.id
    }

    pub fn tenant_id(&self) -> &TenantId {
        &self.tenant_id
    }

    pub fn email(&self) -> &str {
        &self.email
    }

    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn wallet_address(&self) -> &WalletAddress {
        &self.wallet_address
    }

    pub fn blog_url(&self) -> Option<&str> {
        self.blog_url.as_deref()
    }

    pub fn status(&self) -> CreatorStatus {
        self.status
    }

    pub fn tier(&self) -> CreatorTier {
        self.tier
    }

    pub fn settings(&self) -> &CreatorSettings {
        &self.settings
    }

    pub fn api_key_hash(&self) -> Option<&str> {
        self.api_key_hash.as_deref()
    }

    pub fn is_email_verified(&self) -> bool {
        self.email_verified
    }

    pub fn total_revenue_cents(&self) -> u64 {
        self.total_revenue_cents
    }

    pub fn total_articles(&self) -> u32 {
        self.total_articles
    }

    pub fn created_at(&self) -> DateTime<Utc> {
        self.created_at
    }

    pub fn updated_at(&self) -> DateTime<Utc> {
        self.updated_at
    }

    pub fn metadata(&self) -> &serde_json::Value {
        &self.metadata
    }

    // Domain behavior methods

    /// Check if creator is active and can receive payments
    pub fn is_active(&self) -> bool {
        self.status == CreatorStatus::Active && self.email_verified
    }

    /// Check if creator can receive payments
    pub fn can_receive_payments(&self) -> Result<()> {
        if !self.email_verified {
            return Err(crate::error::AllSourceError::ValidationError(
                "Email not verified".to_string(),
            ));
        }

        if self.status != CreatorStatus::Active {
            return Err(crate::error::AllSourceError::ValidationError(format!(
                "Creator is not active (status: {:?})",
                self.status
            )));
        }

        Ok(())
    }

    /// Get the platform fee percentage for this creator
    pub fn fee_percentage(&self) -> u64 {
        self.tier.fee_percentage()
    }

    /// Verify the email address
    pub fn verify_email(&mut self) {
        self.email_verified = true;
        if self.status == CreatorStatus::Pending {
            self.status = CreatorStatus::Active;
        }
        self.updated_at = Utc::now();
    }

    /// Update the name
    pub fn update_name(&mut self, name: Option<String>) {
        self.name = name;
        self.updated_at = Utc::now();
    }

    /// Update the wallet address
    pub fn update_wallet_address(&mut self, wallet_address: WalletAddress) {
        self.wallet_address = wallet_address;
        self.updated_at = Utc::now();
    }

    /// Update the blog URL
    pub fn update_blog_url(&mut self, blog_url: Option<String>) -> Result<()> {
        if let Some(ref url) = blog_url {
            Self::validate_url(url)?;
        }
        self.blog_url = blog_url;
        self.updated_at = Utc::now();
        Ok(())
    }

    /// Update settings
    pub fn update_settings(&mut self, settings: CreatorSettings) {
        self.settings = settings;
        self.updated_at = Utc::now();
    }

    /// Set the API key hash
    pub fn set_api_key_hash(&mut self, hash: String) {
        self.api_key_hash = Some(hash);
        self.updated_at = Utc::now();
    }

    /// Upgrade tier
    pub fn upgrade_tier(&mut self, tier: CreatorTier) {
        self.tier = tier;
        self.updated_at = Utc::now();
    }

    /// Suspend the creator
    pub fn suspend(&mut self) {
        self.status = CreatorStatus::Suspended;
        self.updated_at = Utc::now();
    }

    /// Reactivate a suspended creator
    pub fn reactivate(&mut self) -> Result<()> {
        if self.status == CreatorStatus::Deactivated {
            return Err(crate::error::AllSourceError::ValidationError(
                "Cannot reactivate a deactivated creator".to_string(),
            ));
        }

        if self.email_verified {
            self.status = CreatorStatus::Active;
        } else {
            self.status = CreatorStatus::Pending;
        }
        self.updated_at = Utc::now();
        Ok(())
    }

    /// Permanently deactivate the creator
    pub fn deactivate(&mut self) {
        self.status = CreatorStatus::Deactivated;
        self.updated_at = Utc::now();
    }

    /// Record revenue from a transaction
    pub fn record_revenue(&mut self, amount_cents: u64) {
        self.total_revenue_cents += amount_cents;
        self.updated_at = Utc::now();
    }

    /// Increment article count
    pub fn increment_articles(&mut self) {
        self.total_articles += 1;
        self.updated_at = Utc::now();
    }

    /// Decrement article count
    pub fn decrement_articles(&mut self) {
        self.total_articles = self.total_articles.saturating_sub(1);
        self.updated_at = Utc::now();
    }

    /// Update metadata
    pub fn update_metadata(&mut self, metadata: serde_json::Value) {
        self.metadata = metadata;
        self.updated_at = Utc::now();
    }

    // Validation

    fn validate_email(email: &str) -> Result<()> {
        if email.is_empty() {
            return Err(crate::error::AllSourceError::InvalidInput(
                "Email cannot be empty".to_string(),
            ));
        }

        if !email.contains('@') || !email.contains('.') {
            return Err(crate::error::AllSourceError::InvalidInput(
                "Invalid email format".to_string(),
            ));
        }

        if email.len() > 254 {
            return Err(crate::error::AllSourceError::InvalidInput(
                "Email cannot exceed 254 characters".to_string(),
            ));
        }

        Ok(())
    }

    fn validate_url(url: &str) -> Result<()> {
        if url.is_empty() {
            return Err(crate::error::AllSourceError::InvalidInput(
                "URL cannot be empty".to_string(),
            ));
        }

        if !url.starts_with("http://") && !url.starts_with("https://") {
            return Err(crate::error::AllSourceError::InvalidInput(
                "URL must start with http:// or https://".to_string(),
            ));
        }

        if url.len() > 2048 {
            return Err(crate::error::AllSourceError::InvalidInput(
                "URL cannot exceed 2048 characters".to_string(),
            ));
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const VALID_WALLET: &str = "9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM";

    fn test_tenant_id() -> TenantId {
        TenantId::new("test-tenant".to_string()).unwrap()
    }

    fn test_wallet() -> WalletAddress {
        WalletAddress::new(VALID_WALLET.to_string()).unwrap()
    }

    #[test]
    fn test_create_creator() {
        let creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            Some("https://blog.example.com".to_string()),
        );

        assert!(creator.is_ok());
        let creator = creator.unwrap();
        assert_eq!(creator.email(), "test@example.com");
        assert_eq!(creator.status(), CreatorStatus::Pending);
        assert!(!creator.is_email_verified());
    }

    #[test]
    fn test_reject_invalid_email() {
        let result = Creator::new(
            test_tenant_id(),
            "invalid-email".to_string(),
            test_wallet(),
            None,
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_reject_empty_email() {
        let result = Creator::new(test_tenant_id(), String::new(), test_wallet(), None);

        assert!(result.is_err());
    }

    #[test]
    fn test_reject_invalid_url() {
        let result = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            Some("not-a-url".to_string()),
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_verify_email_activates_creator() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        assert_eq!(creator.status(), CreatorStatus::Pending);
        assert!(!creator.is_active());

        creator.verify_email();

        assert_eq!(creator.status(), CreatorStatus::Active);
        assert!(creator.is_active());
    }

    #[test]
    fn test_can_receive_payments() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        // Cannot receive payments when not verified
        assert!(creator.can_receive_payments().is_err());

        // Verify and check again
        creator.verify_email();
        assert!(creator.can_receive_payments().is_ok());

        // Suspend and check again
        creator.suspend();
        assert!(creator.can_receive_payments().is_err());
    }

    #[test]
    fn test_fee_percentage() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        // Free tier: 10%
        assert_eq!(creator.fee_percentage(), 10);

        // Upgrade to Creator tier: 7%
        creator.upgrade_tier(CreatorTier::Creator);
        assert_eq!(creator.fee_percentage(), 7);

        // Upgrade to Pro tier: 5%
        creator.upgrade_tier(CreatorTier::Pro);
        assert_eq!(creator.fee_percentage(), 5);
    }

    #[test]
    fn test_suspend_and_reactivate() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        creator.verify_email();
        assert!(creator.is_active());

        creator.suspend();
        assert_eq!(creator.status(), CreatorStatus::Suspended);
        assert!(!creator.is_active());

        creator.reactivate().unwrap();
        assert_eq!(creator.status(), CreatorStatus::Active);
        assert!(creator.is_active());
    }

    #[test]
    fn test_cannot_reactivate_deactivated() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        creator.deactivate();
        assert!(creator.reactivate().is_err());
    }

    #[test]
    fn test_record_revenue() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        assert_eq!(creator.total_revenue_cents(), 0);

        creator.record_revenue(1000);
        assert_eq!(creator.total_revenue_cents(), 1000);

        creator.record_revenue(500);
        assert_eq!(creator.total_revenue_cents(), 1500);
    }

    #[test]
    fn test_article_count() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        assert_eq!(creator.total_articles(), 0);

        creator.increment_articles();
        assert_eq!(creator.total_articles(), 1);

        creator.increment_articles();
        assert_eq!(creator.total_articles(), 2);

        creator.decrement_articles();
        assert_eq!(creator.total_articles(), 1);
    }

    #[test]
    fn test_update_settings() {
        let mut creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        let new_settings = CreatorSettings {
            default_price_cents: 100,
            show_reading_time: false,
            brand_color: Some("#FF0000".to_string()),
            unlock_button_text: Some("Read Now".to_string()),
        };

        creator.update_settings(new_settings);

        assert_eq!(creator.settings().default_price_cents, 100);
        assert!(!creator.settings().show_reading_time);
        assert_eq!(creator.settings().brand_color, Some("#FF0000".to_string()));
    }

    #[test]
    fn test_serde_serialization() {
        let creator = Creator::new(
            test_tenant_id(),
            "test@example.com".to_string(),
            test_wallet(),
            None,
        )
        .unwrap();

        let json = serde_json::to_string(&creator);
        assert!(json.is_ok());

        let deserialized: Creator = serde_json::from_str(&json.unwrap()).unwrap();
        assert_eq!(deserialized.email(), "test@example.com");
    }
}