koprogo_api/application/ports/
unit_owner_repository.rs

1use crate::domain::entities::UnitOwner;
2use async_trait::async_trait;
3use uuid::Uuid;
4
5#[async_trait]
6pub trait UnitOwnerRepository: Send + Sync {
7    /// Create a new unit-owner relationship
8    async fn create(&self, unit_owner: &UnitOwner) -> Result<UnitOwner, String>;
9
10    /// Find a unit-owner relationship by ID
11    async fn find_by_id(&self, id: Uuid) -> Result<Option<UnitOwner>, String>;
12
13    /// Get all current owners of a unit (end_date IS NULL)
14    async fn find_current_owners_by_unit(&self, unit_id: Uuid) -> Result<Vec<UnitOwner>, String>;
15
16    /// Get all current units of an owner (end_date IS NULL)
17    async fn find_current_units_by_owner(&self, owner_id: Uuid) -> Result<Vec<UnitOwner>, String>;
18
19    /// Get ownership history of a unit (including past owners)
20    async fn find_all_owners_by_unit(&self, unit_id: Uuid) -> Result<Vec<UnitOwner>, String>;
21
22    /// Get ownership history of an owner (including past units)
23    async fn find_all_units_by_owner(&self, owner_id: Uuid) -> Result<Vec<UnitOwner>, String>;
24
25    /// Update a unit-owner relationship
26    async fn update(&self, unit_owner: &UnitOwner) -> Result<UnitOwner, String>;
27
28    /// Delete a unit-owner relationship
29    async fn delete(&self, id: Uuid) -> Result<(), String>;
30
31    /// Check if a unit has any active owners
32    async fn has_active_owners(&self, unit_id: Uuid) -> Result<bool, String>;
33
34    /// Get the total ownership percentage for a unit (should be <= 1.0)
35    async fn get_total_ownership_percentage(&self, unit_id: Uuid) -> Result<f64, String>;
36
37    /// Find active unit-owner relationship by unit and owner IDs
38    async fn find_active_by_unit_and_owner(
39        &self,
40        unit_id: Uuid,
41        owner_id: Uuid,
42    ) -> Result<Option<UnitOwner>, String>;
43
44    /// Get all active unit-owner relationships for a building
45    /// Returns tuples of (unit_id, owner_id, ownership_percentage)
46    /// Useful for calculating charge distributions
47    async fn find_active_by_building(
48        &self,
49        building_id: Uuid,
50    ) -> Result<Vec<(Uuid, Uuid, f64)>, String>;
51}