summaryrefslogtreecommitdiff
path: root/schist_core/schist_models/src/bucket.rs
blob: a28aa8ffa38885e36febb2b7741f80a72b76d99b (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
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))
    }
}