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