Skip to main content

koprogo_api/application/ports/
unit_owner_repository.rs

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