Skip to content

Commit

Permalink
chore: velocityLimit and velocityControl queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Oct 16, 2024
1 parent 1139d98 commit 23148ba
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 25 deletions.

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

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

6 changes: 3 additions & 3 deletions cala-ledger/src/velocity/account_control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl AccountControls {
&self,
op: &mut AtomicOperation<'_>,
created_at: DateTime<Utc>,
control: VelocityControlValues,
control: &VelocityControlValues,
account_id: AccountId,
limits: Vec<VelocityLimitValues>,
params: impl Into<Params> + std::fmt::Debug,
Expand Down Expand Up @@ -91,8 +91,8 @@ impl AccountControls {
let control = AccountVelocityControl {
account_id,
control_id: control.id,
condition: control.condition,
enforcement: control.enforcement,
condition: control.condition.clone(),
enforcement: control.enforcement.clone(),
velocity_limits,
};

Expand Down
4 changes: 2 additions & 2 deletions cala-ledger/src/velocity/control/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl VelocityControl {
self.values
}

pub fn values(&self) -> VelocityControlValues {
self.values.clone()
pub fn values(&self) -> &VelocityControlValues {
&self.values
}

pub fn created_at(&self) -> chrono::DateTime<chrono::Utc> {
Expand Down
37 changes: 33 additions & 4 deletions cala-ledger/src/velocity/control/repo.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use sqlx::{PgPool, Postgres, Transaction};

use std::collections::HashMap;

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

#[derive(Debug, Clone)]
pub struct VelocityControlRepo {
_pool: PgPool,
pool: PgPool,
}

impl VelocityControlRepo {
pub fn new(pool: &PgPool) -> Self {
Self {
_pool: pool.clone(),
}
Self { pool: pool.clone() }
}

pub async fn create_in_tx(
Expand Down Expand Up @@ -62,4 +62,33 @@ impl VelocityControlRepo {
Err(e) => Err(e.into()),
}
}

pub async fn find_all<T: From<VelocityControl>>(
&self,
ids: &[VelocityControlId],
) -> Result<HashMap<VelocityControlId, T>, VelocityError> {
let rows = sqlx::query_as!(
GenericEvent,
r#"SELECT v.id, e.sequence, e.event,
v.created_at AS entity_created_at, e.recorded_at AS event_recorded_at
FROM cala_velocity_controls v
JOIN cala_velocity_control_events e
ON v.data_source_id = e.data_source_id
AND v.id = e.id
WHERE v.data_source_id = '00000000-0000-0000-0000-000000000000'
AND v.id = ANY($1)
ORDER BY v.id, e.sequence"#,
ids as &[VelocityControlId]
)
.fetch_all(&self.pool)
.await?;
let n = rows.len();

let ret = EntityEvents::load_n::<VelocityControl>(rows, n)?
.0
.into_iter()
.map(|limit: VelocityControl| (limit.values().id, T::from(limit)))
.collect();
Ok(ret)
}
}
4 changes: 4 additions & 0 deletions cala-ledger/src/velocity/limit/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ impl VelocityLimit {
pub fn into_values(self) -> VelocityLimitValues {
self.values
}

pub fn values(&self) -> &VelocityLimitValues {
&self.values
}
}

impl Entity for VelocityLimit {
Expand Down
40 changes: 35 additions & 5 deletions cala-ledger/src/velocity/limit/repo.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use sqlx::{PgPool, Postgres, Transaction};

use super::{super::error::*, entity::*};
use std::collections::HashMap;

use crate::primitives::VelocityControlId;

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

#[derive(Debug, Clone)]
pub struct VelocityLimitRepo {
_pool: PgPool,
pool: PgPool,
}

impl VelocityLimitRepo {
pub fn new(pool: &PgPool) -> Self {
Self {
_pool: pool.clone(),
}
Self { pool: pool.clone() }
}

pub async fn create_in_tx(
Expand Down Expand Up @@ -80,4 +81,33 @@ impl VelocityLimitRepo {
let ret = EntityEvents::load_n::<VelocityLimit>(rows, n)?.0;
Ok(ret)
}

pub async fn find_all<T: From<VelocityLimit>>(
&self,
ids: &[VelocityLimitId],
) -> Result<HashMap<VelocityLimitId, T>, VelocityError> {
let rows = sqlx::query_as!(
GenericEvent,
r#"SELECT v.id, e.sequence, e.event,
v.created_at AS entity_created_at, e.recorded_at AS event_recorded_at
FROM cala_velocity_limits v
JOIN cala_velocity_limit_events e
ON v.data_source_id = e.data_source_id
AND v.id = e.id
WHERE v.data_source_id = '00000000-0000-0000-0000-000000000000'
AND v.id = ANY($1)
ORDER BY v.id, e.sequence"#,
ids as &[VelocityLimitId]
)
.fetch_all(&self.pool)
.await?;
let n = rows.len();

let ret = EntityEvents::load_n::<VelocityLimit>(rows, n)?
.0
.into_iter()
.map(|limit: VelocityLimit| (limit.values().id, T::from(limit)))
.collect();
Ok(ret)
}
}
16 changes: 16 additions & 0 deletions cala-ledger/src/velocity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod limit;
use chrono::{DateTime, Utc};
use sqlx::PgPool;

use std::collections::HashMap;

use cala_types::{entry::EntryValues, transaction::TransactionValues};

pub use crate::param::Params;
Expand Down Expand Up @@ -181,4 +183,18 @@ impl Velocities {
) -> Result<Vec<VelocityLimit>, VelocityError> {
self.limits.list_for_control(op.tx(), control_id).await
}

pub async fn find_all_limits<T: From<VelocityLimit>>(
&self,
limit_ids: &[VelocityLimitId],
) -> Result<HashMap<VelocityLimitId, T>, VelocityError> {
self.limits.find_all(limit_ids).await
}

pub async fn find_all_controls<T: From<VelocityControl>>(
&self,
control_ids: &[VelocityControlId],
) -> Result<HashMap<VelocityControlId, T>, VelocityError> {
self.controls.find_all(control_ids).await
}
}

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

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

2 changes: 2 additions & 0 deletions cala-server/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ type Query {
transactionByExternalId(externalId: String!): Transaction
txTemplate(id: UUID!): TxTemplate
txTemplateByCode(code: String!): TxTemplate
velocityLimit(id: UUID!): VelocityLimit
velocityControl(id: UUID!): VelocityControl
}

type RangedBalance {
Expand Down
Loading

0 comments on commit 23148ba

Please sign in to comment.