koprogo_api/application/ports/
owner_credit_balance_repository.rs

1use crate::domain::entities::OwnerCreditBalance;
2use async_trait::async_trait;
3use uuid::Uuid;
4
5/// Repository trait for OwnerCreditBalance entity (SEL credit tracking)
6#[async_trait]
7pub trait OwnerCreditBalanceRepository: Send + Sync {
8    /// Create a new credit balance (starts at 0)
9    async fn create(&self, balance: &OwnerCreditBalance) -> Result<OwnerCreditBalance, String>;
10
11    /// Find a credit balance by owner and building
12    async fn find_by_owner_and_building(
13        &self,
14        owner_id: Uuid,
15        building_id: Uuid,
16    ) -> Result<Option<OwnerCreditBalance>, String>;
17
18    /// Find all balances for a building
19    async fn find_by_building(&self, building_id: Uuid) -> Result<Vec<OwnerCreditBalance>, String>;
20
21    /// Find all balances for an owner (across all buildings)
22    async fn find_by_owner(&self, owner_id: Uuid) -> Result<Vec<OwnerCreditBalance>, String>;
23
24    /// Get or create a balance (ensures balance exists)
25    async fn get_or_create(
26        &self,
27        owner_id: Uuid,
28        building_id: Uuid,
29    ) -> Result<OwnerCreditBalance, String>;
30
31    /// Update a credit balance
32    async fn update(&self, balance: &OwnerCreditBalance) -> Result<OwnerCreditBalance, String>;
33
34    /// Delete a credit balance
35    async fn delete(&self, owner_id: Uuid, building_id: Uuid) -> Result<bool, String>;
36
37    /// Get leaderboard (top contributors by balance)
38    async fn get_leaderboard(
39        &self,
40        building_id: Uuid,
41        limit: i32,
42    ) -> Result<Vec<OwnerCreditBalance>, String>;
43
44    /// Get active participants count (owners with at least 1 exchange)
45    async fn count_active_participants(&self, building_id: Uuid) -> Result<i64, String>;
46
47    /// Get total credits in circulation for a building
48    async fn get_total_credits_in_circulation(&self, building_id: Uuid) -> Result<i32, String>;
49}