summaryrefslogtreecommitdiff
path: root/schist_core/schist_models/src/bucket.rs
blob: debbab73e972fc0a89e5c7e285463cee62400d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::ops::Div;

use derive_builder::Builder;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{Drip, Transaction};

#[derive(
    Builder,
    Clone,
    Queryable,
    Identifiable,
    Selectable,
    Debug,
    PartialEq,
    Serialize,
    Deserialize,
    Insertable,
)]
#[diesel(table_name = crate::schema::buckets)]
pub struct Bucket {
    pub id: i32,
    pub balance: Option<i32>,
    pub balance_cache_key: Option<String>,
    #[diesel(column_name = "bucket_group")]
    pub group: String,
    pub name: String,
}

impl Bucket {
    pub fn is_empty(&self, drips: &[Drip], transactions: &[Transaction]) -> bool {
        drips.iter().all(|drip| drip.bucket_id != self.id)
            && transactions
                .iter()
                .all(|t| t.bucket_id.is_none_or(|id| id != self.id))
    }

    pub fn pretty_balance_per_100(&self) -> String {
        let abs_balance_per_100 = self.balance.unwrap_or(0).div(100).abs();
        if self.balance.is_none_or(|balance| balance >= 0) {
            abs_balance_per_100.to_string()
        } else {
            format!("({abs_balance_per_100})")
        }
    }
}