1use async_graphql::*;
2
3use super::{convert::ToGlobalId, primitives::*};
4
5#[derive(Clone, SimpleObject)]
6pub struct TxTemplate {
7 id: ID,
8 tx_template_id: UUID,
9 version: u32,
10 code: String,
11 params: Option<Vec<ParamDefinition>>,
12 transaction: TxTemplateTransaction,
13 entries: Vec<TxTemplateEntry>,
14 description: Option<String>,
15 metadata: Option<JSON>,
16 created_at: Timestamp,
17 modified_at: Timestamp,
18}
19
20#[derive(Clone, SimpleObject)]
21pub(super) struct ParamDefinition {
22 name: String,
23 r#type: ParamDataType,
24 default: Option<Expression>,
25 description: Option<String>,
26}
27
28#[derive(Clone, SimpleObject)]
29pub(super) struct TxTemplateEntry {
30 entry_type: Expression,
31 account_id: Expression,
32 layer: Expression,
33 direction: Expression,
34 units: Expression,
35 currency: Expression,
36 description: Option<Expression>,
37 metadata: Option<Expression>,
38}
39
40#[derive(Clone, SimpleObject)]
41pub(super) struct TxTemplateTransaction {
42 effective: Expression,
43 journal_id: Expression,
44 correlation_id: Option<Expression>,
45 external_id: Option<Expression>,
46 description: Option<Expression>,
47 metadata: Option<Expression>,
48}
49
50#[derive(InputObject)]
51pub(super) struct TxTemplateCreateInput {
52 pub tx_template_id: UUID,
53 pub code: String,
54 pub params: Option<Vec<ParamDefinitionInput>>,
55 pub transaction: TxTemplateTransactionInput,
56 pub entries: Vec<TxTemplateEntryInput>,
57 pub description: Option<String>,
58 pub metadata: Option<JSON>,
59}
60
61#[derive(InputObject)]
62pub(super) struct TxTemplateTransactionInput {
63 pub effective: Expression,
64 pub journal_id: Expression,
65 pub correlation_id: Option<Expression>,
66 pub external_id: Option<Expression>,
67 pub description: Option<Expression>,
68 pub metadata: Option<Expression>,
69}
70
71#[derive(InputObject)]
72pub(super) struct TxTemplateEntryInput {
73 pub entry_type: Expression,
74 pub account_id: Expression,
75 pub layer: Expression,
76 pub direction: Expression,
77 pub units: Expression,
78 pub currency: Expression,
79 pub description: Option<Expression>,
80}
81
82#[derive(InputObject)]
83pub(super) struct ParamDefinitionInput {
84 pub name: String,
85 pub r#type: ParamDataType,
86 pub default: Option<Expression>,
87 pub description: Option<String>,
88}
89
90#[derive(SimpleObject)]
91pub(super) struct TxTemplateCreatePayload {
92 pub tx_template: TxTemplate,
93}
94
95impl ToGlobalId for cala_ledger::TxTemplateId {
96 fn to_global_id(&self) -> async_graphql::types::ID {
97 async_graphql::types::ID::from(format!("tx_template:{}", self))
98 }
99}
100
101impl From<cala_ledger::tx_template::TxTemplate> for TxTemplate {
102 fn from(entity: cala_ledger::tx_template::TxTemplate) -> Self {
103 let created_at = entity.created_at();
104 let modified_at = entity.modified_at();
105 let values = entity.into_values();
106 let transaction = TxTemplateTransaction::from(values.transaction);
107 let entries = values
108 .entries
109 .into_iter()
110 .map(TxTemplateEntry::from)
111 .collect();
112 let params = values
113 .params
114 .map(|params| params.into_iter().map(ParamDefinition::from).collect());
115 Self {
116 id: values.id.to_global_id(),
117 version: values.version,
118 tx_template_id: UUID::from(values.id),
119 code: values.code,
120 transaction,
121 entries,
122 params,
123 description: values.description,
124 metadata: values.metadata.map(JSON::from),
125 created_at: Timestamp::from(created_at),
126 modified_at: Timestamp::from(modified_at),
127 }
128 }
129}
130
131impl From<cala_ledger::tx_template::TxTemplateTransaction> for TxTemplateTransaction {
132 fn from(
133 cala_ledger::tx_template::TxTemplateTransaction {
134 effective,
135 journal_id,
136 correlation_id,
137 external_id,
138 description,
139 metadata,
140 }: cala_ledger::tx_template::TxTemplateTransaction,
141 ) -> Self {
142 Self {
143 effective: Expression::from(effective),
144 journal_id: Expression::from(journal_id),
145 correlation_id: correlation_id.map(Expression::from),
146 external_id: external_id.map(Expression::from),
147 description: description.map(Expression::from),
148 metadata: metadata.map(Expression::from),
149 }
150 }
151}
152
153impl From<cala_ledger::tx_template::TxTemplateEntry> for TxTemplateEntry {
154 fn from(
155 cala_ledger::tx_template::TxTemplateEntry {
156 entry_type,
157 account_id,
158 layer,
159 direction,
160 units,
161 currency,
162 description,
163 metadata,
164 }: cala_ledger::tx_template::TxTemplateEntry,
165 ) -> Self {
166 Self {
167 entry_type: Expression::from(entry_type),
168 account_id: Expression::from(account_id),
169 layer: Expression::from(layer),
170 direction: Expression::from(direction),
171 units: Expression::from(units),
172 currency: Expression::from(currency),
173 description: description.map(Expression::from),
174 metadata: metadata.map(Expression::from),
175 }
176 }
177}
178
179impl From<cala_ledger::tx_template::ParamDefinition> for ParamDefinition {
180 fn from(value: cala_ledger::tx_template::ParamDefinition) -> Self {
181 let default = value.default.map(Expression::from);
182 Self {
183 name: value.name,
184 r#type: ParamDataType::from(value.r#type),
185 default,
186 description: value.description,
187 }
188 }
189}
190
191impl From<cala_ledger::tx_template::TxTemplate> for TxTemplateCreatePayload {
192 fn from(entity: cala_ledger::tx_template::TxTemplate) -> Self {
193 Self {
194 tx_template: TxTemplate::from(entity),
195 }
196 }
197}