cala_ledger/entry/
repo.rs

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
48
49
50
51
52
53
54
55
use crate::primitives::{AccountId, DataSourceId, EntryId, JournalId, TransactionId};
use es_entity::*;
use sqlx::PgPool;

use super::{entity::*, error::*};

#[derive(EsRepo, Debug, Clone)]
#[es_repo(
    entity = "Entry",
    err = "EntryError",
    columns(
        account_id(ty = "AccountId", list_for, update(persist = false)),
        journal_id(ty = "JournalId", update(persist = false)),
        transaction_id(ty = "TransactionId", update(persist = false)),
        data_source_id(
            ty = "DataSourceId",
            create(accessor = "data_source().into()"),
            update(persist = false),
        ),
    ),
    tbl_prefix = "cala"
)]
pub(crate) struct EntryRepo {
    #[allow(dead_code)]
    pool: PgPool,
}

impl EntryRepo {
    pub(crate) fn new(pool: &PgPool) -> Self {
        Self { pool: pool.clone() }
    }

    #[cfg(feature = "import")]
    pub(super) async fn import(
        &self,
        op: &mut DbOp<'_>,
        origin: DataSourceId,
        entry: &mut Entry,
    ) -> Result<(), EntryError> {
        let recorded_at = op.now();
        sqlx::query!(
            r#"INSERT INTO cala_entries (data_source_id, id, journal_id, account_id, created_at)
            VALUES ($1, $2, $3, $4, $5)"#,
            origin as DataSourceId,
            entry.values().id as EntryId,
            entry.values().journal_id as JournalId,
            entry.values().account_id as AccountId,
            recorded_at,
        )
        .execute(&mut **op.tx())
        .await?;
        self.persist_events(op, &mut entry.events).await?;
        Ok(())
    }
}