actionqueue_platform/
tenant.rs1use std::collections::HashMap;
4
5use actionqueue_core::ids::TenantId;
6use actionqueue_core::platform::TenantRegistration;
7use tracing;
8
9#[derive(Default)]
13pub struct TenantRegistry {
14 tenants: HashMap<TenantId, TenantRegistration>,
15}
16
17impl TenantRegistry {
18 pub fn new() -> Self {
20 Self::default()
21 }
22
23 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 pub fn exists(&self, tenant_id: TenantId) -> bool {
32 self.tenants.contains_key(&tenant_id)
33 }
34
35 pub fn get(&self, tenant_id: TenantId) -> Option<&TenantRegistration> {
37 self.tenants.get(&tenant_id)
38 }
39
40 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}