From b25d00efe8c171a8727333f851ffd8c3dc0b1c79 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Tue, 15 Oct 2024 13:42:38 +0530 Subject: [PATCH] chore: velocityControlAttach mutation (#253) --- cala-ledger/src/velocity/control/entity.rs | 4 ++++ cala-ledger/src/velocity/mod.rs | 13 ++++++----- cala-server/schema.graphql | 11 +++++++++ cala-server/src/graphql/schema.rs | 26 ++++++++++++++++++++++ cala-server/src/graphql/velocity.rs | 20 +++++++++++++++++ 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/cala-ledger/src/velocity/control/entity.rs b/cala-ledger/src/velocity/control/entity.rs index 1ddaac38..23b05c36 100644 --- a/cala-ledger/src/velocity/control/entity.rs +++ b/cala-ledger/src/velocity/control/entity.rs @@ -37,6 +37,10 @@ impl VelocityControl { self.values } + pub fn values(&self) -> VelocityControlValues { + self.values.clone() + } + pub fn created_at(&self) -> chrono::DateTime { self.events .entity_first_persisted_at diff --git a/cala-ledger/src/velocity/mod.rs b/cala-ledger/src/velocity/mod.rs index c5d20c60..6c491e56 100644 --- a/cala-ledger/src/velocity/mod.rs +++ b/cala-ledger/src/velocity/mod.rs @@ -106,12 +106,13 @@ impl Velocities { control: VelocityControlId, account_id: AccountId, params: impl Into + std::fmt::Debug, - ) -> Result<(), VelocityError> { + ) -> Result { 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( @@ -120,7 +121,7 @@ impl Velocities { control_id: VelocityControlId, account_id: AccountId, params: impl Into + std::fmt::Debug, - ) -> Result<(), VelocityError> { + ) -> Result { let control = self.controls.find_by_id(op.tx(), control_id).await?; let limits = self .limits @@ -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( diff --git a/cala-server/schema.graphql b/cala-server/schema.graphql index 01caf936..74c46489 100644 --- a/cala-server/schema.graphql +++ b/cala-server/schema.graphql @@ -337,6 +337,7 @@ type Mutation { velocityLimitCreate(input: VelocityLimitCreateInput!): VelocityLimitCreatePayload! velocityControlCreate(input: VelocityControlCreateInput!): VelocityControlCreatePayload! velocityControlAddLimit(input: VelocityControlAddLimitInput!): VelocityControlAddLimitPayload! + velocityControlAttach(input: VelocityControlAttachInput!): VelocityControlAttachPayload! } """ @@ -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! diff --git a/cala-server/src/graphql/schema.rs b/cala-server/src/graphql/schema.rs index edc64e0a..857c276a 100644 --- a/cala-server/src/graphql/schema.rs +++ b/cala-server/src/graphql/schema.rs @@ -766,4 +766,30 @@ impl CoreMutation { Ok(velocity_limit.into()) } + + async fn velocity_control_attach( + &self, + ctx: &Context<'_>, + input: VelocityControlAttachInput, + ) -> Result { + let app = ctx.data_unchecked::(); + let mut op = ctx + .data_unchecked::() + .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()) + } } diff --git a/cala-server/src/graphql/velocity.rs b/cala-server/src/graphql/velocity.rs index 67dc0da9..bbca993c 100644 --- a/cala-server/src/graphql/velocity.rs +++ b/cala-server/src/graphql/velocity.rs @@ -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 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))