Skip to main content

actionqueue_platform/
tenant.rs

1//! In-memory tenant registry.
2
3use std::collections::HashMap;
4
5use actionqueue_core::ids::TenantId;
6use actionqueue_core::platform::TenantRegistration;
7use tracing;
8
9/// In-memory projection of organizational tenants.
10///
11/// Reconstructed from WAL events at bootstrap via [`TenantRegistry::register`].
12#[derive(Default)]
13pub struct TenantRegistry {
14    tenants: HashMap<TenantId, TenantRegistration>,
15}
16
17impl TenantRegistry {
18    /// Creates an empty registry.
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    /// Registers a tenant.
24    pub fn register(&mut self, registration: TenantRegistration) {
25        let tenant_id = registration.tenant_id();
26        tracing::debug!(%tenant_id, "tenant registered");
27        self.tenants.insert(tenant_id, registration);
28    }
29
30    /// Returns `true` if the tenant exists in the registry.
31    pub fn exists(&self, tenant_id: TenantId) -> bool {
32        self.tenants.contains_key(&tenant_id)
33    }
34
35    /// Returns the tenant registration, if known.
36    pub fn get(&self, tenant_id: TenantId) -> Option<&TenantRegistration> {
37        self.tenants.get(&tenant_id)
38    }
39
40    /// Returns an iterator over all registered tenants.
41    pub fn all(&self) -> impl Iterator<Item = &TenantRegistration> {
42        self.tenants.values()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use actionqueue_core::ids::TenantId;
49    use actionqueue_core::platform::TenantRegistration;
50
51    use super::TenantRegistry;
52
53    #[test]
54    fn register_and_exists() {
55        let mut reg = TenantRegistry::new();
56        let id = TenantId::new();
57        reg.register(TenantRegistration::new(id, "Acme Corp"));
58        assert!(reg.exists(id));
59        assert_eq!(reg.get(id).map(|r| r.name()), Some("Acme Corp"));
60    }
61
62    #[test]
63    fn unknown_tenant_does_not_exist() {
64        let reg = TenantRegistry::new();
65        assert!(!reg.exists(TenantId::new()));
66    }
67}