cala_ledger/entry/
repo.rs1use crate::primitives::{AccountId, DataSourceId, EntryId, JournalId, TransactionId};
2use es_entity::*;
3use sqlx::PgPool;
4
5use super::{entity::*, error::*};
6
7#[derive(EsRepo, Debug, Clone)]
8#[es_repo(
9 entity = "Entry",
10 err = "EntryError",
11 columns(
12 account_id(ty = "AccountId", list_for, update(persist = false)),
13 journal_id(ty = "JournalId", update(persist = false)),
14 transaction_id(ty = "TransactionId", update(persist = false)),
15 data_source_id(
16 ty = "DataSourceId",
17 create(accessor = "data_source().into()"),
18 update(persist = false),
19 ),
20 ),
21 tbl_prefix = "cala"
22)]
23pub(crate) struct EntryRepo {
24 #[allow(dead_code)]
25 pool: PgPool,
26}
27
28impl EntryRepo {
29 pub(crate) fn new(pool: &PgPool) -> Self {
30 Self { pool: pool.clone() }
31 }
32
33 #[cfg(feature = "import")]
34 pub(super) async fn import(
35 &self,
36 op: &mut DbOp<'_>,
37 origin: DataSourceId,
38 entry: &mut Entry,
39 ) -> Result<(), EntryError> {
40 let recorded_at = op.now();
41 sqlx::query!(
42 r#"INSERT INTO cala_entries (data_source_id, id, journal_id, account_id, created_at)
43 VALUES ($1, $2, $3, $4, $5)"#,
44 origin as DataSourceId,
45 entry.values().id as EntryId,
46 entry.values().journal_id as JournalId,
47 entry.values().account_id as AccountId,
48 recorded_at,
49 )
50 .execute(&mut **op.tx())
51 .await?;
52 self.persist_events(op, &mut entry.events).await?;
53 Ok(())
54 }
55}