Skip to content

Commit

Permalink
feat: velocityControlCreate mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Oct 14, 2024
1 parent dfa049c commit 0b67ae5
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cala-server/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ type Mutation {
txTemplateCreate(input: TxTemplateCreateInput!): TxTemplateCreatePayload!
transactionPost(input: TransactionInput!): TransactionPostPayload!
velocityLimitCreate(input: VelocityLimitCreateInput!): VelocityLimitCreatePayload!
velocityControlCreate(input: VelocityControlCreateInput!): VelocityControlCreatePayload!
}

"""
Expand Down Expand Up @@ -527,6 +528,39 @@ input TxTemplateTransactionInput {

scalar UUID

type VelocityControl {
id: ID!
velocityControlId: UUID!
name: String!
description: String!
enforcement: VelocityEnforcement!
condition: Expression
}

input VelocityControlCreateInput {
velocityControlId: UUID!
name: String!
description: String!
enforcement: VelocityInforcementInput!
condition: Expression
}

type VelocityControlCreatePayload {
velocityControl: VelocityControl!
}

type VelocityEnforcement {
velocityEnforcementAction: VelocityEnforcementAction!
}

enum VelocityEnforcementAction {
REJECT
}

input VelocityInforcementInput {
velocityEnforcementAction: VelocityEnforcementAction! = REJECT
}

type VelocityLimit {
id: ID!
velocityLimitId: UUID!
Expand Down
6 changes: 6 additions & 0 deletions cala-server/src/graphql/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ impl From<UUID> for cala_ledger::VelocityLimitId {
}
}

impl From<UUID> for cala_ledger::VelocityControlId {
fn from(uuid: UUID) -> Self {
cala_ledger::VelocityControlId::from(uuid.0)
}
}

impl From<UUID> for cala_ledger::TxTemplateId {
fn from(uuid: UUID) -> Self {
cala_ledger::TxTemplateId::from(uuid.0)
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 @@ -704,4 +704,30 @@ impl<E: MutationExtensionMarker> CoreMutation<E> {

Ok(velocity_limit.into())
}

async fn velocity_control_create(
&self,
ctx: &Context<'_>,
input: VelocityControlCreateInput,
) -> Result<VelocityControlCreatePayload> {
let app = ctx.data_unchecked::<CalaApp>();
let mut op = ctx
.data_unchecked::<DbOp>()
.try_lock()
.expect("Lock held concurrently");
let mut new_velocity_control_builder = cala_ledger::velocity::NewVelocityControl::builder();
new_velocity_control_builder
.id(input.velocity_control_id)
.name(input.name)
.description(input.description);

let new_velocity_control = new_velocity_control_builder.build()?;
let velocity_control = app
.ledger()
.velocities()
.create_control_in_op(&mut op, new_velocity_control)
.await?;

Ok(velocity_control.into())
}
}
87 changes: 87 additions & 0 deletions cala-server/src/graphql/velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,93 @@ pub(super) struct VelocityLimitCreatePayload {
velocity_limit: VelocityLimit,
}

#[derive(SimpleObject)]
struct VelocityControl {
id: ID,
velocity_control_id: UUID,
name: String,
description: String,
enforcement: VelocityEnforcement,
condition: Option<Expression>,
}

#[derive(SimpleObject)]
struct VelocityEnforcement {
velocity_enforcement_action: VelocityEnforcementAction,
}

#[derive(InputObject)]
pub(super) struct VelocityControlCreateInput {
pub velocity_control_id: UUID,
pub name: String,
pub description: String,
pub enforcement: VelocityInforcementInput,
pub condition: Option<Expression>,
}

#[derive(InputObject)]
pub(super) struct VelocityInforcementInput {
#[graphql(default)]
pub velocity_enforcement_action: VelocityEnforcementAction,
}

#[derive(Enum, Default, Copy, Clone, Eq, PartialEq)]
#[graphql(remote = "cala_ledger::velocity::VelocityEnforcementAction")]
pub(super) enum VelocityEnforcementAction {
#[default]
Reject,
}

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

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

impl From<cala_ledger::velocity::VelocityControl> for VelocityControl {
fn from(velocity_control: cala_ledger::velocity::VelocityControl) -> Self {
let cala_ledger::velocity::VelocityControlValues {
id,
name,
description,
enforcement,
condition,
} = velocity_control.into_values();

let enforcement = VelocityEnforcement::from(enforcement);

Self {
id: id.to_global_id(),
velocity_control_id: UUID::from(id),
name,
description,
enforcement,
condition: condition.map(Expression::from),
}
}
}

impl From<cala_ledger::velocity::VelocityEnforcement> for VelocityEnforcement {
fn from(enforcement: cala_ledger::velocity::VelocityEnforcement) -> Self {
Self {
velocity_enforcement_action: enforcement.action.into(),
}
}
}

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

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

0 comments on commit 0b67ae5

Please sign in to comment.