cala_server/graphql/
velocity.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use async_graphql::*;

use cala_ledger::VelocityControlId;

use crate::app::CalaApp;

use super::{
    convert::ToGlobalId,
    primitives::*,
    tx_template::{ParamDefinition, ParamDefinitionInput},
    DbOp,
};

#[derive(SimpleObject, Clone)]
pub struct VelocityLimit {
    id: ID,
    velocity_limit_id: UUID,
    name: String,
    description: String,
    condition: Option<Expression>,
    window: Vec<PartitionKey>,
    currency: Option<CurrencyCode>,
    params: Option<Vec<ParamDefinition>>,
    limit: Limit,
}

#[derive(SimpleObject, Clone)]
struct Limit {
    timestamp_source: Option<Expression>,
    balance: Vec<BalanceLimit>,
}

#[derive(SimpleObject, Clone)]
struct BalanceLimit {
    layer: Expression,
    amount: Expression,
    normal_balance_type: Expression,
    start: Option<Expression>,
    end: Option<Expression>,
}

#[derive(SimpleObject, Clone)]
struct PartitionKey {
    alias: String,
    value: Expression,
}

#[derive(InputObject)]
pub(super) struct VelocityLimitCreateInput {
    pub velocity_limit_id: UUID,
    pub name: String,
    pub description: String,
    pub window: Vec<PartitionKeyInput>,
    pub condition: Option<Expression>,
    pub limit: LimitInput,
    pub currency: Option<CurrencyCode>,
    pub params: Option<Vec<ParamDefinitionInput>>,
}

#[derive(InputObject)]
pub(super) struct PartitionKeyInput {
    pub alias: String,
    pub value: Expression,
}

#[derive(InputObject)]
pub(super) struct LimitInput {
    pub timestamp_source: Option<Expression>,
    pub balance: Vec<BalanceLimitInput>,
}

#[derive(InputObject)]
pub(super) struct BalanceLimitInput {
    #[graphql(default)]
    pub limit_type: BalanceLimitType,
    pub layer: Expression,
    pub amount: Expression,
    pub normal_balance_type: Expression,
    pub start: Option<Expression>,
    pub end: Option<Expression>,
}

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

#[derive(SimpleObject)]
pub(super) struct VelocityLimitCreatePayload {
    velocity_limit: VelocityLimit,
}

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

#[ComplexObject]
impl VelocityControl {
    async fn limits(&self, ctx: &Context<'_>) -> Result<Vec<VelocityLimit>> {
        let app = ctx.data_unchecked::<CalaApp>();
        let control_id = VelocityControlId::from(self.velocity_control_id);

        let res = match ctx.data_opt::<DbOp>() {
            Some(op) => {
                let mut op = op.try_lock().expect("Lock held concurrently");
                app.ledger()
                    .velocities()
                    .list_limits_for_control_in_op(&mut op, control_id)
                    .await?
            }
            None => {
                app.ledger()
                    .velocities()
                    .list_limits_for_control(control_id)
                    .await?
            }
        };

        let limits = res.into_iter().map(VelocityLimit::from).collect();
        Ok(limits)
    }
}

#[derive(SimpleObject, Clone)]
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: VelocityEnforcementInput,
    pub condition: Option<Expression>,
}

#[derive(InputObject)]
pub(super) struct VelocityEnforcementInput {
    #[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,
}

#[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))
    }
}

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),
        }
    }
}

#[derive(InputObject)]
pub(super) struct VelocityControlAddLimitInput {
    pub velocity_control_id: UUID,
    pub velocity_limit_id: UUID,
}

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

impl From<cala_ledger::velocity::VelocityControl> for VelocityControlAddLimitPayload {
    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))
    }
}

impl From<cala_ledger::velocity::VelocityLimit> for VelocityLimit {
    fn from(velocity_limit: cala_ledger::velocity::VelocityLimit) -> Self {
        let cala_ledger::velocity::VelocityLimitValues {
            id,
            name,
            description,
            condition,
            currency,
            params,
            limit,
            window,
        } = velocity_limit.into_values();

        let params = params.map(|params| params.into_iter().map(ParamDefinition::from).collect());
        let window = window.into_iter().map(PartitionKey::from).collect();

        Self {
            id: id.to_global_id(),
            velocity_limit_id: UUID::from(id),
            name,
            description,
            condition: condition.map(Expression::from),
            currency: currency.map(CurrencyCode::from),
            params,
            window,
            limit: Limit::from(limit),
        }
    }
}

impl From<cala_ledger::velocity::Limit> for Limit {
    fn from(limit: cala_ledger::velocity::Limit) -> Self {
        let balance = limit.balance.into_iter().map(BalanceLimit::from).collect();

        Self {
            timestamp_source: limit.timestamp_source.map(Expression::from),
            balance,
        }
    }
}

impl From<cala_ledger::velocity::BalanceLimit> for BalanceLimit {
    fn from(balance_limit: cala_ledger::velocity::BalanceLimit) -> Self {
        Self {
            layer: balance_limit.layer.into(),
            amount: balance_limit.amount.into(),
            normal_balance_type: balance_limit.enforcement_direction.into(),
            start: balance_limit.start.map(Expression::from),
            end: balance_limit.end.map(Expression::from),
        }
    }
}

impl From<cala_ledger::velocity::PartitionKey> for PartitionKey {
    fn from(partition_key: cala_ledger::velocity::PartitionKey) -> Self {
        Self {
            alias: partition_key.alias,
            value: Expression::from(partition_key.value),
        }
    }
}

impl From<cala_ledger::velocity::VelocityLimit> for VelocityLimitCreatePayload {
    fn from(entity: cala_ledger::velocity::VelocityLimit) -> Self {
        Self {
            velocity_limit: VelocityLimit::from(entity),
        }
    }
}