Skip to content

Commit

Permalink
chore: velocityControlAttach mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Oct 15, 2024
1 parent 5c40dd3 commit 5d78834
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cala-ledger/src/velocity/control/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl VelocityControl {
self.values
}

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

pub fn created_at(&self) -> chrono::DateTime<chrono::Utc> {
self.events
.entity_first_persisted_at
Expand Down
13 changes: 7 additions & 6 deletions cala-ledger/src/velocity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ impl Velocities {
control: VelocityControlId,
account_id: AccountId,
params: impl Into<Params> + std::fmt::Debug,
) -> Result<(), VelocityError> {
) -> Result<VelocityControl, VelocityError> {
let mut op = AtomicOperation::init(&self.pool, &self.outbox).await?;
self.attach_control_to_account_in_op(&mut op, control, account_id, params)
let control = self
.attach_control_to_account_in_op(&mut op, control, account_id, params)
.await?;
op.commit().await?;
Ok(())
Ok(control)
}

pub async fn attach_control_to_account_in_op(
Expand All @@ -120,7 +121,7 @@ impl Velocities {
control_id: VelocityControlId,
account_id: AccountId,
params: impl Into<Params> + std::fmt::Debug,
) -> Result<(), VelocityError> {
) -> Result<VelocityControl, VelocityError> {
let control = self.controls.find_by_id(op.tx(), control_id).await?;
let limits = self
.limits
Expand All @@ -134,13 +135,13 @@ impl Velocities {
.attach_control_in_op(
op,
control.created_at(),
control.into_values(),
control.values(),
account_id,
limits,
params,
)
.await?;
Ok(())
Ok(control)
}

pub(crate) async fn update_balances_in_op(
Expand Down
11 changes: 11 additions & 0 deletions cala-server/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ type Mutation {
velocityLimitCreate(input: VelocityLimitCreateInput!): VelocityLimitCreatePayload!
velocityControlCreate(input: VelocityControlCreateInput!): VelocityControlCreatePayload!
velocityControlAddLimit(input: VelocityControlAddLimitInput!): VelocityControlAddLimitPayload!
velocityControlAttach(input: VelocityControlAttachInput!): VelocityControlAttachPayload!
}

"""
Expand Down Expand Up @@ -548,6 +549,16 @@ type VelocityControlAddLimitPayload {
velocityControl: VelocityControl!
}

input VelocityControlAttachInput {
velocityControlId: UUID!
accountId: UUID!
params: JSON!
}

type VelocityControlAttachPayload {
velocityControl: VelocityControl!
}

input VelocityControlCreateInput {
velocityControlId: UUID!
name: String!
Expand Down
26 changes: 26 additions & 0 deletions cala-server/src/graphql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,30 @@ impl<E: MutationExtensionMarker> CoreMutation<E> {

Ok(velocity_limit.into())
}

async fn velocity_control_attach(
&self,
ctx: &Context<'_>,
input: VelocityControlAttachInput,
) -> Result<VelocityControlAttachPayload> {
let app = ctx.data_unchecked::<CalaApp>();
let mut op = ctx
.data_unchecked::<DbOp>()
.try_lock()
.expect("Lock held concurrently");
let params = cala_ledger::tx_template::Params::from(input.params);

let velocity_control = app
.ledger()
.velocities()
.attach_control_to_account_in_op(
&mut op,
input.velocity_control_id.into(),
input.account_id.into(),
params,
)
.await?;

Ok(velocity_control.into())
}
}
20 changes: 20 additions & 0 deletions cala-server/src/graphql/velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ pub(super) struct VelocityControlCreatePayload {
velocity_control: VelocityControl,
}

#[derive(InputObject)]
pub(super) struct VelocityControlAttachInput {
pub velocity_control_id: UUID,
pub account_id: UUID,
pub params: JSON,
}

#[derive(SimpleObject)]
pub(super) struct VelocityControlAttachPayload {
velocity_control: VelocityControl,
}

impl From<cala_ledger::velocity::VelocityControl> for VelocityControlAttachPayload {
fn from(entity: cala_ledger::velocity::VelocityControl) -> Self {
Self {
velocity_control: VelocityControl::from(entity),
}
}
}

impl ToGlobalId for cala_ledger::VelocityControlId {
fn to_global_id(&self) -> async_graphql::types::ID {
async_graphql::types::ID::from(format!("velocity_control:{}", self))
Expand Down

0 comments on commit 5d78834

Please sign in to comment.