1pub mod config;
2pub mod error;
3
4use sqlx::PgPool;
5use std::sync::{Arc, Mutex};
6pub use tracing::instrument;
7
8pub use config::*;
9use error::*;
10
11use crate::{
12 account::Accounts,
13 account_set::AccountSets,
14 balance::Balances,
15 entry::Entries,
16 journal::Journals,
17 ledger_operation::*,
18 outbox::{server, EventSequence, Outbox, OutboxListener},
19 primitives::TransactionId,
20 transaction::{Transaction, Transactions},
21 tx_template::{Params, TxTemplates},
22 velocity::Velocities,
23};
24#[cfg(feature = "import")]
25mod import_deps {
26 pub use crate::primitives::DataSourceId;
27 pub use cala_types::outbox::OutboxEvent;
28}
29#[cfg(feature = "import")]
30use import_deps::*;
31
32#[derive(Clone)]
33pub struct CalaLedger {
34 pool: PgPool,
35 accounts: Accounts,
36 account_sets: AccountSets,
37 journals: Journals,
38 transactions: Transactions,
39 tx_templates: TxTemplates,
40 entries: Entries,
41 velocities: Velocities,
42 balances: Balances,
43 outbox: Outbox,
44 #[allow(clippy::type_complexity)]
45 outbox_handle: Arc<Mutex<Option<tokio::task::JoinHandle<Result<(), LedgerError>>>>>,
46}
47
48impl CalaLedger {
49 pub async fn init(config: CalaLedgerConfig) -> Result<Self, LedgerError> {
50 let pool = match (config.pool, config.pg_con) {
51 (Some(pool), None) => pool,
52 (None, Some(pg_con)) => {
53 let mut pool_opts = sqlx::postgres::PgPoolOptions::new();
54 if let Some(max_connections) = config.max_connections {
55 pool_opts = pool_opts.max_connections(max_connections);
56 }
57 pool_opts.connect(&pg_con).await?
58 }
59 _ => {
60 return Err(LedgerError::ConfigError(
61 "One of pg_con or pool must be set".to_string(),
62 ))
63 }
64 };
65 if config.exec_migrations {
66 sqlx::migrate!().run(&pool).await?;
67 }
68
69 let outbox = Outbox::init(&pool).await?;
70 let mut outbox_handle = None;
71 if let Some(outbox_config) = config.outbox {
72 outbox_handle = Some(Self::start_outbox_server(outbox_config, outbox.clone()));
73 }
74
75 let accounts = Accounts::new(&pool, outbox.clone());
76 let journals = Journals::new(&pool, outbox.clone());
77 let tx_templates = TxTemplates::new(&pool, outbox.clone());
78 let transactions = Transactions::new(&pool, outbox.clone());
79 let entries = Entries::new(&pool, outbox.clone());
80 let balances = Balances::new(&pool, outbox.clone());
81 let velocities = Velocities::new(&pool, outbox.clone());
82 let account_sets = AccountSets::new(&pool, outbox.clone(), &accounts, &entries, &balances);
83 Ok(Self {
84 accounts,
85 account_sets,
86 journals,
87 tx_templates,
88 outbox,
89 transactions,
90 entries,
91 balances,
92 velocities,
93 outbox_handle: Arc::new(Mutex::new(outbox_handle)),
94 pool,
95 })
96 }
97
98 pub fn pool(&self) -> &PgPool {
99 &self.pool
100 }
101
102 pub async fn begin_operation<'a>(&self) -> Result<LedgerOperation<'a>, LedgerError> {
103 Ok(LedgerOperation::init(&self.pool, &self.outbox).await?)
104 }
105
106 pub fn ledger_operation_from_db_op<'a>(
107 &self,
108 db_op: es_entity::DbOp<'a>,
109 ) -> LedgerOperation<'a> {
110 LedgerOperation::new(db_op, &self.outbox)
111 }
112
113 pub fn accounts(&self) -> &Accounts {
116 &self.accounts
117 }
118
119 pub fn velocities(&self) -> &Velocities {
120 &self.velocities
121 }
122
123 pub fn account_sets(&self) -> &AccountSets {
124 &self.account_sets
125 }
126
127 pub fn journals(&self) -> &Journals {
128 &self.journals
129 }
130
131 pub fn tx_templates(&self) -> &TxTemplates {
132 &self.tx_templates
133 }
134
135 pub fn balances(&self) -> &Balances {
136 &self.balances
137 }
138
139 pub fn entries(&self) -> &Entries {
140 &self.entries
141 }
142
143 pub fn transactions(&self) -> &Transactions {
144 &self.transactions
145 }
146
147 pub async fn post_transaction(
148 &self,
149 tx_id: TransactionId,
150 tx_template_code: &str,
151 params: impl Into<Params> + std::fmt::Debug,
152 ) -> Result<Transaction, LedgerError> {
153 let mut db = LedgerOperation::init(&self.pool, &self.outbox).await?;
154 let transaction = self
155 .post_transaction_in_op(&mut db, tx_id, tx_template_code, params)
156 .await?;
157 db.commit().await?;
158 Ok(transaction)
159 }
160
161 #[instrument(
162 name = "cala_ledger.transaction_post",
163 skip(self, db)
164 fields(transaction_id, external_id)
165 err
166 )]
167 pub async fn post_transaction_in_op(
168 &self,
169 db: &mut LedgerOperation<'_>,
170 tx_id: TransactionId,
171 tx_template_code: &str,
172 params: impl Into<Params> + std::fmt::Debug,
173 ) -> Result<Transaction, LedgerError> {
174 let prepared_tx = self
175 .tx_templates
176 .prepare_transaction(db.op().now(), tx_id, tx_template_code, params.into())
177 .await?;
178
179 let transaction = self
180 .transactions
181 .create_in_op(db, prepared_tx.transaction)
182 .await?;
183
184 let span = tracing::Span::current();
185 span.record("transaction_id", transaction.id().to_string());
186 span.record("external_id", &transaction.values().external_id);
187
188 let entries = self
189 .entries
190 .create_all_in_op(db, prepared_tx.entries)
191 .await?;
192
193 let account_ids = entries
194 .iter()
195 .map(|entry| entry.account_id)
196 .collect::<Vec<_>>();
197 let mappings = self
198 .account_sets
199 .fetch_mappings(transaction.values().journal_id, &account_ids)
200 .await?;
201
202 self.velocities
203 .update_balances_in_op(
204 db,
205 transaction.created_at(),
206 transaction.values(),
207 &entries,
208 &account_ids,
209 )
210 .await?;
211
212 self.balances
213 .update_balances_in_op(
214 db,
215 transaction.created_at(),
216 transaction.journal_id(),
217 entries,
218 mappings,
219 )
220 .await?;
221 Ok(transaction)
222 }
223
224 pub async fn register_outbox_listener(
225 &self,
226 start_after: Option<EventSequence>,
227 ) -> Result<OutboxListener, LedgerError> {
228 Ok(self.outbox.register_listener(start_after).await?)
229 }
230
231 #[cfg(feature = "import")]
232 #[instrument(name = "cala_ledger.sync_outbox_event", skip(self, db))]
233 pub async fn sync_outbox_event(
234 &self,
235 db: sqlx::Transaction<'_, sqlx::Postgres>,
236 origin: DataSourceId,
237 event: OutboxEvent,
238 ) -> Result<(), LedgerError> {
239 use crate::outbox::OutboxEventPayload::*;
240
241 match event.payload {
242 Empty => (),
243 AccountCreated { account, .. } => {
244 let op = es_entity::DbOp::new(db, event.recorded_at);
245 self.accounts
246 .sync_account_creation(op, origin, account)
247 .await?
248 }
249 AccountUpdated {
250 account, fields, ..
251 } => {
252 let op = es_entity::DbOp::new(db, event.recorded_at);
253 self.accounts
254 .sync_account_update(op, account, fields)
255 .await?
256 }
257 AccountSetCreated { account_set, .. } => {
258 let op = es_entity::DbOp::new(db, event.recorded_at);
259 self.account_sets
260 .sync_account_set_creation(op, origin, account_set)
261 .await?
262 }
263 AccountSetUpdated {
264 account_set,
265 fields,
266 ..
267 } => {
268 let op = es_entity::DbOp::new(db, event.recorded_at);
269 self.account_sets
270 .sync_account_set_update(op, account_set, fields)
271 .await?
272 }
273 AccountSetMemberCreated {
274 account_set_id,
275 member_id,
276 ..
277 } => {
278 let op = es_entity::DbOp::new(db, event.recorded_at);
279 self.account_sets
280 .sync_account_set_member_creation(op, origin, account_set_id, member_id)
281 .await?
282 }
283 AccountSetMemberRemoved {
284 account_set_id,
285 member_id,
286 ..
287 } => {
288 let op = es_entity::DbOp::new(db, event.recorded_at);
289 self.account_sets
290 .sync_account_set_member_removal(op, origin, account_set_id, member_id)
291 .await?
292 }
293 JournalCreated { journal, .. } => {
294 let op = es_entity::DbOp::new(db, event.recorded_at);
295 self.journals
296 .sync_journal_creation(op, origin, journal)
297 .await?
298 }
299 JournalUpdated {
300 journal, fields, ..
301 } => {
302 let op = es_entity::DbOp::new(db, event.recorded_at);
303 self.journals
304 .sync_journal_update(op, journal, fields)
305 .await?
306 }
307 TransactionCreated { transaction, .. } => {
308 let op = es_entity::DbOp::new(db, event.recorded_at);
309 self.transactions
310 .sync_transaction_creation(op, origin, transaction)
311 .await?
312 }
313 TxTemplateCreated { tx_template, .. } => {
314 let op = es_entity::DbOp::new(db, event.recorded_at);
315 self.tx_templates
316 .sync_tx_template_creation(op, origin, tx_template)
317 .await?
318 }
319 EntryCreated { entry, .. } => {
320 let op = es_entity::DbOp::new(db, event.recorded_at);
321 self.entries.sync_entry_creation(op, origin, entry).await?
322 }
323 BalanceCreated { balance, .. } => {
324 self.balances
325 .sync_balance_creation(db, origin, balance)
326 .await?
327 }
328 BalanceUpdated { balance, .. } => {
329 self.balances
330 .sync_balance_update(db, origin, balance)
331 .await?
332 }
333 }
334 Ok(())
335 }
336
337 pub async fn await_outbox_handle(&self) -> Result<(), LedgerError> {
338 let handle = { self.outbox_handle.lock().expect("poisened mutex").take() };
339 if let Some(handle) = handle {
340 return handle.await.expect("Couldn't await outbox handle");
341 }
342 Ok(())
343 }
344
345 pub fn shutdown_outbox(&mut self) -> Result<(), LedgerError> {
346 if let Some(handle) = self.outbox_handle.lock().expect("poisened mutex").take() {
347 handle.abort();
348 }
349 Ok(())
350 }
351
352 fn start_outbox_server(
353 config: server::OutboxServerConfig,
354 outbox: Outbox,
355 ) -> tokio::task::JoinHandle<Result<(), LedgerError>> {
356 tokio::spawn(async move {
357 server::start(config, outbox).await?;
358 Ok(())
359 })
360 }
361}