Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: account set member #122

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cala-ledger-core-types/src/account_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ pub struct AccountSetValues {

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", content = "id")]
pub enum AccountSetMember {
pub enum AccountSetMemberId {
Account(AccountId),
AccountSet(AccountSetId),
}

impl From<AccountId> for AccountSetMember {
impl From<AccountId> for AccountSetMemberId {
fn from(account_id: AccountId) -> Self {
AccountSetMember::Account(account_id)
Self::Account(account_id)
}
}

impl From<AccountSetId> for AccountSetMember {
impl From<AccountSetId> for AccountSetMemberId {
fn from(id: AccountSetId) -> Self {
AccountSetMember::AccountSet(id)
Self::AccountSet(id)
}
}
4 changes: 2 additions & 2 deletions cala-ledger-core-types/src/outbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ pub enum OutboxEventPayload {
AccountSetMemberCreated {
source: DataSource,
account_set_id: AccountSetId,
member: AccountSetMember,
member_id: AccountSetMemberId,
},
AccountSetMemberRemoved {
source: DataSource,
account_set_id: AccountSetId,
member: AccountSetMember,
member_id: AccountSetMemberId,
},
JournalCreated {
source: DataSource,
Expand Down
12 changes: 6 additions & 6 deletions cala-ledger-outbox-client/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ impl TryFrom<proto::cala_ledger_event::Payload> for OutboxEventPayload {
AccountSetMemberCreated {
source: data_source_id.parse()?,
account_set_id: member.account_set_id.parse()?,
member: match member
member_id: match member
.member
.ok_or(CalaLedgerOutboxClientError::MissingField)?
{
proto::account_set_member::Member::MemberAccountId(account_id) => {
cala_types::account_set::AccountSetMember::from(
cala_types::account_set::AccountSetMemberId::from(
account_id.parse::<AccountId>()?,
)
}
proto::account_set_member::Member::MemberAccountSetId(account_set_id) => {
cala_types::account_set::AccountSetMember::from(
cala_types::account_set::AccountSetMemberId::from(
account_set_id.parse::<AccountSetId>()?,
)
}
Expand All @@ -110,17 +110,17 @@ impl TryFrom<proto::cala_ledger_event::Payload> for OutboxEventPayload {
AccountSetMemberRemoved {
source: data_source_id.parse()?,
account_set_id: member.account_set_id.parse()?,
member: match member
member_id: match member
.member
.ok_or(CalaLedgerOutboxClientError::MissingField)?
{
proto::account_set_member::Member::MemberAccountId(account_id) => {
cala_types::account_set::AccountSetMember::from(
cala_types::account_set::AccountSetMemberId::from(
account_id.parse::<AccountId>()?,
)
}
proto::account_set_member::Member::MemberAccountSetId(account_set_id) => {
cala_types::account_set::AccountSetMember::from(
cala_types::account_set::AccountSetMemberId::from(
account_set_id.parse::<AccountSetId>()?,
)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cala-ledger/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ impl Accounts {
self.repo.find_all(account_ids).await
}

#[instrument(name = "cala_ledger.accounts.find_all", skip(self, op), err)]
pub async fn find_all_in_op<T: From<Account>>(
&self,
op: &mut AtomicOperation<'_>,
account_ids: &[AccountId],
) -> Result<HashMap<AccountId, T>, AccountError> {
self.repo.find_all_in_tx(op.tx(), account_ids).await
}

#[instrument(name = "cala_ledger.accounts.find_by_external_id", skip(self), err)]
pub async fn find_by_external_id(&self, external_id: String) -> Result<Account, AccountError> {
self.repo.find_by_external_id(external_id).await
Expand Down
20 changes: 18 additions & 2 deletions cala-ledger/src/account/repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "import")]
use chrono::{DateTime, Utc};
use sqlx::{PgPool, Postgres, Transaction};
use sqlx::{Executor, PgPool, Postgres, Transaction};

use std::collections::HashMap;

Expand Down Expand Up @@ -92,6 +92,22 @@ impl AccountRepo {
pub(super) async fn find_all<T: From<Account>>(
&self,
ids: &[AccountId],
) -> Result<HashMap<AccountId, T>, AccountError> {
self.find_all_in_executor(&self.pool, ids).await
}

pub(super) async fn find_all_in_tx<T: From<Account>>(
&self,
tx: &mut Transaction<'_, Postgres>,
ids: &[AccountId],
) -> Result<HashMap<AccountId, T>, AccountError> {
self.find_all_in_executor(&mut **tx, ids).await
}

async fn find_all_in_executor<T: From<Account>>(
&self,
executor: impl Executor<'_, Database = Postgres>,
ids: &[AccountId],
) -> Result<HashMap<AccountId, T>, AccountError> {
let rows = sqlx::query_as!(
GenericEvent,
Expand All @@ -106,7 +122,7 @@ impl AccountRepo {
ORDER BY a.id, e.sequence"#,
ids as &[AccountId]
)
.fetch_all(&self.pool)
.fetch_all(executor)
.await?;
let n = rows.len();
let ret = EntityEvents::load_n(rows, n)?
Expand Down
5 changes: 5 additions & 0 deletions cala-ledger/src/account_set/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ impl From<&AccountSetValues> for AccountSetByNameCursor {
}
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AccountSetMemberCursor {
pub member_created_at: chrono::DateTime<chrono::Utc>,
}
Loading
Loading