myc-core 8.3.0+beta

Provide base features of the Mycelium project as s and Use-cases.
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
use crate::{
    domain::{
        dtos::{
            email::Email,
            message::{FromEmail, Message, MessageSendingEvent},
            tenant::TenantMetaKey,
        },
        entities::{LocalMessageWrite, TenantFetching},
    },
    models::AccountLifeCycle,
    settings::{DEFAULT_TENANT_ID_KEY, TEMPLATES},
};

use mycelium_base::{
    entities::{CreateResponseKind, FetchResponseKind},
    utils::errors::{use_case_err, MappedErrors},
};
use tera::Context;
use uuid::Uuid;

#[tracing::instrument(name = "dispatch_notification", skip_all)]
pub(crate) async fn dispatch_notification<T: ToString>(
    parameters: Vec<(T, String)>,
    template_path_prefix: T,
    config: AccountLifeCycle,
    to: Email,
    cc: Option<Email>,
    local_message_write_repo: Box<&dyn LocalMessageWrite>,
    tenant_fetching_repo: Box<&dyn TenantFetching>,
) -> Result<CreateResponseKind<Option<Uuid>>, MappedErrors> {
    tracing::info!("Dispatching notification");

    let (context, locale) =
        populate_tenant_info(&parameters, &config, tenant_fetching_repo)
            .await?;

    let locale = if let Some(locale) = locale {
        //
        // Use the tenant preferred locale if available
        //
        tracing::trace!("Communicating with tenant locale: {:?}", locale);
        locale
    } else if let Some(locale) = config.locale {
        //
        // Use the account locale if available
        //
        tracing::trace!("Communicating with system locale: {:?}", locale);
        locale.async_get_or_error().await?
    } else {
        //
        // Use the default locale if no locale is available
        //
        tracing::trace!("Communicating with default locale: 'en-us'");
        "en-us".to_string()
    };

    //
    // Verify if the selected locale exists in templates folder
    // If not, use the default en-us one
    //
    let verified_locale = {
        let template_path_prefix_str = template_path_prefix.to_string();
        let body_path = format!(
            "{locale}/{prefix}.jinja",
            locale = locale,
            prefix = template_path_prefix_str
        );

        let template_names: Vec<_> = TEMPLATES.get_template_names().collect();

        if template_names.contains(&body_path.as_str()) {
            locale
        } else {
            tracing::warn!(
                "Locale '{}' not found in templates, falling back to 'en-us'",
                locale
            );
            "en-us".to_string()
        }
    };

    let body_path = format!(
        "{locale}/{path}",
        locale = verified_locale,
        path = format!(
            "{prefix}.jinja",
            prefix = template_path_prefix.to_string()
        )
    );

    let body = match TEMPLATES.render(body_path.as_str(), &context) {
        Ok(res) => res,
        Err(err) => {
            return use_case_err(format!(
                "Unable to render email template: {err}"
            ))
            .as_error();
        }
    };

    let subject_path = format!(
        "{locale}/{path}",
        locale = verified_locale,
        path = format!(
            "{prefix}.subject",
            prefix = template_path_prefix.to_string()
        )
    );

    let subject_ =
        match TEMPLATES.render(subject_path.as_str(), &Context::new()) {
            Ok(res) => res,
            Err(err) => {
                return use_case_err(format!(
                    "Unable to render email subject: {err}"
                ))
                .as_error();
            }
        };

    let from_email =
        Email::from_string(config.noreply_email.async_get_or_error().await?)?;

    let from = if let Some(name) = config.noreply_name {
        FromEmail::NamedEmail(format!(
            "{} <{}>",
            name.async_get_or_error().await?,
            from_email.email()
        ))
    } else {
        FromEmail::Email(from_email)
    };

    local_message_write_repo
        .send(MessageSendingEvent::new(Message {
            from,
            to,
            cc,
            subject: subject_,
            body,
        }))
        .await
}

#[tracing::instrument(name = "populate_tenant_info", skip_all)]
async fn populate_tenant_info<T: ToString>(
    parameters: &Vec<(T, String)>,
    config: &AccountLifeCycle,
    tenant_fetching_repo: Box<&dyn TenantFetching>,
) -> Result<(Context, Option<String>), MappedErrors> {
    let mut context = Context::new();
    let mut optional_locale = None;

    let tenant_found = if let Some((_, tenant_id)) = parameters
        .iter()
        .find(|(key, _)| key.to_string() == DEFAULT_TENANT_ID_KEY)
    {
        if let Ok(tenant_id) = tenant_id.parse::<Uuid>() {
            if let FetchResponseKind::Found(tenant) = tenant_fetching_repo
                .get_tenant_public_by_id(tenant_id)
                .await?
            {
                //
                // Inject the tenant name
                //
                context.insert("domain_name", tenant.name.as_str());

                if let Some(meta) = &tenant.meta {
                    //
                    // Inject the tenant website URL
                    //
                    if let Some(website_url) =
                        meta.get(&TenantMetaKey::WebsiteUrl)
                    {
                        context.insert("domain_url", website_url.as_str());
                    }

                    //
                    // Inject the tenant support email
                    //
                    if let Some(support_email) =
                        meta.get(&TenantMetaKey::SupportEmail)
                    {
                        context.insert("support_email", support_email.as_str());
                    }

                    //
                    // Populate the tenant preferred locale
                    //
                    optional_locale = meta
                        .get(&TenantMetaKey::Locale)
                        .map(|locale| locale.to_owned());
                }
                true
            } else {
                false
            }
        } else {
            false
        }
    } else {
        false
    };

    // If tenant was not found or not provided, use config values as fallback
    if !tenant_found {
        context.insert(
            "domain_name",
            config.domain_name.async_get_or_error().await?.as_str(),
        );

        if let Some(domain_url) = &config.domain_url {
            context.insert(
                "domain_url",
                domain_url.async_get_or_error().await?.as_str(),
            );
        }

        context.insert(
            "support_email",
            &config.support_email.async_get_or_error().await?,
        );
    }

    for (key, value) in parameters {
        context.insert(key.to_string(), &value.to_string());
    }

    Ok((context, optional_locale))
}

// * ---------------------------------------------------------------------------
// * TESTS
// *
// * IMPORTANT: These tests require TEMPLATES_DIR environment variable to be
// * set to an absolute path before running. The TEMPLATES lazy_static is
// * initialized when the module is first loaded, so TEMPLATES_DIR must be set
// * before cargo test runs.
// *
// * Run tests with: TEMPLATES_DIR=/workspaces/mycelium/templates cargo test
// * Or from workspace root: TEMPLATES_DIR=$(pwd)/templates cargo test
// * ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::dtos::{
        email::Email,
        profile::Owner,
        tenant::{Tenant, TenantMeta, TenantMetaKey},
    };
    use async_trait::async_trait;
    use chrono::Local;
    use myc_config::secret_resolver::SecretResolver;
    use mycelium_base::{dtos::Children, entities::FetchManyResponseKind};
    use std::collections::HashMap;
    use std::env;

    // ? -----------------------------------------------------------------------
    // ? Setup function to ensure TEMPLATES_DIR is set
    // ? -----------------------------------------------------------------------

    fn setup_templates_dir() {
        if env::var("TEMPLATES_DIR").is_err() {
            // Use current working directory (equivalent to ${PWD}/templates)
            if let Ok(current_dir) = env::current_dir() {
                let templates_path = current_dir.join("templates");
                if let Some(abs_path) = templates_path.to_str() {
                    env::set_var("TEMPLATES_DIR", abs_path);
                    return;
                }
            }

            // Fallback: try CARGO_MANIFEST_DIR (set by cargo test)
            if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
                let templates_path = format!("{}/templates", manifest_dir);
                env::set_var("TEMPLATES_DIR", &templates_path);
                return;
            }

            // Last resort: use current directory (equivalent to ${PWD}/templates)
            if let Ok(pwd) = env::current_dir() {
                if let Some(pwd_str) = pwd.to_str() {
                    env::set_var(
                        "TEMPLATES_DIR",
                        &format!("{}/templates", pwd_str),
                    );
                }
            }
        }
    }

    // ? -----------------------------------------------------------------------
    // ? Mock repositories
    // ? -----------------------------------------------------------------------

    struct MockLocalMessageWrite {
        should_fail: bool,
        message_id: Option<Uuid>,
    }

    impl MockLocalMessageWrite {
        fn new() -> Self {
            Self {
                should_fail: false,
                message_id: Some(Uuid::new_v4()),
            }
        }

        fn with_error() -> Self {
            Self {
                should_fail: true,
                message_id: None,
            }
        }
    }

    #[async_trait]
    impl LocalMessageWrite for MockLocalMessageWrite {
        async fn send(
            &self,
            _message_event: MessageSendingEvent,
        ) -> Result<CreateResponseKind<Option<Uuid>>, MappedErrors> {
            if self.should_fail {
                return use_case_err("Failed to send message".to_string())
                    .as_error();
            }

            Ok(CreateResponseKind::Created(self.message_id))
        }

        async fn update_message_event(
            &self,
            _message_event: MessageSendingEvent,
        ) -> Result<(), MappedErrors> {
            unimplemented!()
        }

        async fn delete_message_event(
            &self,
            _id: Uuid,
        ) -> Result<(), MappedErrors> {
            unimplemented!()
        }

        async fn ping(&self) -> Result<(), MappedErrors> {
            unimplemented!()
        }
    }

    struct MockTenantFetching {
        tenant: Option<Tenant>,
        should_fail: bool,
    }

    impl MockTenantFetching {
        fn with_tenant(tenant: Tenant) -> Self {
            Self {
                tenant: Some(tenant),
                should_fail: false,
            }
        }

        fn not_found() -> Self {
            Self {
                tenant: None,
                should_fail: false,
            }
        }

        fn with_error() -> Self {
            Self {
                tenant: None,
                should_fail: true,
            }
        }
    }

    #[async_trait]
    impl TenantFetching for MockTenantFetching {
        async fn get_tenant_owned_by_me(
            &self,
            _id: Uuid,
            _owners_ids: Vec<Uuid>,
        ) -> Result<FetchResponseKind<Tenant, String>, MappedErrors> {
            unimplemented!()
        }

        async fn get_tenant_public_by_id(
            &self,
            _id: Uuid,
        ) -> Result<FetchResponseKind<Tenant, String>, MappedErrors> {
            if self.should_fail {
                return use_case_err("Failed to fetch tenant".to_string())
                    .as_error();
            }

            match &self.tenant {
                Some(tenant) => Ok(FetchResponseKind::Found(tenant.clone())),
                None => Ok(FetchResponseKind::NotFound(Some(
                    "tenant_id".to_string(),
                ))),
            }
        }

        async fn get_tenants_by_manager_account(
            &self,
            _id: Uuid,
            _manager_ids: Vec<Uuid>,
        ) -> Result<FetchResponseKind<Tenant, String>, MappedErrors> {
            unimplemented!()
        }

        async fn filter_tenants_as_manager(
            &self,
            _name: Option<String>,
            _owner: Option<Uuid>,
            _metadata: Option<(TenantMetaKey, String)>,
            _tag: Option<(String, String)>,
            _page_size: Option<i32>,
            _skip: Option<i32>,
        ) -> Result<FetchManyResponseKind<Tenant>, MappedErrors> {
            unimplemented!()
        }
    }

    // ? -----------------------------------------------------------------------
    // ? Helper functions
    // ? -----------------------------------------------------------------------

    fn create_test_config() -> AccountLifeCycle {
        AccountLifeCycle {
            domain_name: SecretResolver::Value("Test Domain".to_string()),
            domain_url: Some(SecretResolver::Value(
                "https://test.com".to_string(),
            )),
            locale: Some(SecretResolver::Value("en-us".to_string())),
            token_expiration: SecretResolver::Value(3600),
            noreply_name: Some(SecretResolver::Value(
                "Test System".to_string(),
            )),
            noreply_email: SecretResolver::Value(
                "noreply@test.com".to_string(),
            ),
            support_name: None,
            support_email: SecretResolver::Value(
                "support@test.com".to_string(),
            ),
            token_secret: SecretResolver::Value("test-secret".to_string()),
        }
    }

    fn create_test_email() -> Email {
        Email::from_string("test@example.com".to_string()).unwrap()
    }

    fn create_test_tenant_with_meta(meta: Option<TenantMeta>) -> Tenant {
        let owner = Owner {
            id: Uuid::new_v4(),
            email: "owner@test.com".to_string(),
            first_name: Some("Test".to_string()),
            last_name: Some("Owner".to_string()),
            username: Some("testowner".to_string()),
            is_principal: true,
        };

        Tenant {
            id: Some(Uuid::new_v4()),
            name: "Test Tenant".to_string(),
            description: Some("Test tenant description".to_string()),
            owners: Children::Records(vec![owner]),
            manager: None,
            tags: None,
            meta,
            status: None,
            created: Local::now(),
            updated: None,
        }
    }

    // ? -----------------------------------------------------------------------
    // ? Test cases
    // ? -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dispatch_notification_with_tenant_id_and_meta() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let mut meta = HashMap::new();
        meta.insert(
            TenantMetaKey::WebsiteUrl,
            "https://tenant.example.com".to_string(),
        );
        meta.insert(
            TenantMetaKey::SupportEmail,
            "support@tenant.example.com".to_string(),
        );
        meta.insert(TenantMetaKey::Locale, "en-us".to_string()); // Use en-us for testing

        let tenant = create_test_tenant_with_meta(Some(meta));
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::with_tenant(tenant.clone());

        let result = dispatch_notification(
            vec![
                (DEFAULT_TENANT_ID_KEY, tenant_id.to_string()),
                ("verification_code", "123456".to_string()),
            ],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(id)) => assert!(id.is_some()),
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_without_tenant_id() {
        setup_templates_dir();
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::not_found();

        let result = dispatch_notification(
            vec![("verification_code", "123456".to_string())],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(id)) => assert!(id.is_some()),
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_tenant_not_found() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::not_found();

        let result = dispatch_notification(
            vec![
                (DEFAULT_TENANT_ID_KEY, tenant_id.to_string()),
                ("verification_code", "123456".to_string()),
            ],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        // Should still succeed, but use config values instead of tenant values
        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_locale_existing() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let mut meta = HashMap::new();
        meta.insert(TenantMetaKey::Locale, "en-us".to_string()); // Use en-us for testing

        let tenant = create_test_tenant_with_meta(Some(meta));
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::with_tenant(tenant);

        let result = dispatch_notification(
            vec![
                (DEFAULT_TENANT_ID_KEY, tenant_id.to_string()),
                ("verification_code", "123456".to_string()),
            ],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_locale_fallback() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let mut meta = HashMap::new();
        // Use a locale that doesn't exist in templates (e.g., "fr-fr")
        meta.insert(TenantMetaKey::Locale, "fr-fr".to_string());

        let tenant = create_test_tenant_with_meta(Some(meta));
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::with_tenant(tenant);

        let result = dispatch_notification(
            vec![
                (DEFAULT_TENANT_ID_KEY, tenant_id.to_string()),
                ("verification_code", "123456".to_string()),
            ],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        // Should succeed with fallback to en-us
        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_tenant_without_meta() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let tenant = create_test_tenant_with_meta(None);
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::with_tenant(tenant);

        let result = dispatch_notification(
            vec![
                (DEFAULT_TENANT_ID_KEY, tenant_id.to_string()),
                ("verification_code", "123456".to_string()),
            ],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_send_error() {
        setup_templates_dir();
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::with_error();
        let tenant_repo = MockTenantFetching::not_found();

        let result = dispatch_notification(
            vec![("verification_code", "123456".to_string())],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_dispatch_notification_with_cc() {
        setup_templates_dir();
        let config = create_test_config();
        let email = create_test_email();
        let cc_email =
            Email::from_string("cc@example.com".to_string()).unwrap();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::not_found();

        let result = dispatch_notification(
            vec![("verification_code", "123456".to_string())],
            "email/activation-code",
            config,
            email,
            Some(cc_email),
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_with_config_locale() {
        setup_templates_dir();
        let mut config = create_test_config();
        config.locale = Some(SecretResolver::Value("es".to_string()));

        let email = create_test_email();
        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::not_found();

        let result = dispatch_notification(
            vec![("verification_code", "123456".to_string())],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_dispatch_notification_tenant_fetching_error() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::with_error();

        let result = dispatch_notification(
            vec![(DEFAULT_TENANT_ID_KEY, tenant_id.to_string())],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_dispatch_notification_with_tenant_partial_meta() {
        setup_templates_dir();
        let tenant_id = Uuid::new_v4();
        let mut meta = HashMap::new();
        // Only website URL, no support email or locale
        meta.insert(
            TenantMetaKey::WebsiteUrl,
            "https://tenant.example.com".to_string(),
        );

        let tenant = create_test_tenant_with_meta(Some(meta));
        let config = create_test_config();
        let email = create_test_email();

        let message_repo = MockLocalMessageWrite::new();
        let tenant_repo = MockTenantFetching::with_tenant(tenant);

        let result = dispatch_notification(
            vec![
                (DEFAULT_TENANT_ID_KEY, tenant_id.to_string()),
                ("verification_code", "123456".to_string()),
            ],
            "email/activation-code",
            config,
            email,
            None,
            Box::new(&message_repo),
            Box::new(&tenant_repo),
        )
        .await;

        match result {
            Ok(CreateResponseKind::Created(_)) => {}
            Ok(_) => panic!("Expected Created response"),
            Err(err) => panic!("Expected success but got error: {:?}", err),
        }
    }
}