1
use askama::Template;
2
use axum::{
3
    Extension, Json,
4
    extract::{Path, State},
5
    http::StatusCode,
6
    response::IntoResponse,
7
};
8
use finance::account::Account;
9
use serde::Deserialize;
10
use server::command::{CmdResult, FinanceEntity, account::GetAccount};
11
use sqlx::types::Uuid;
12
use std::sync::Arc;
13

            
14
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate};
15

            
16
struct ScriptView {
17
    id: Uuid,
18
    name: Option<String>,
19
}
20

            
21
#[derive(Template)]
22
#[template(path = "pages/account/edit.html")]
23
struct AccountEditPage {
24
    account_id: Uuid,
25
    account_name: String,
26
    tags: Vec<finance::tag::Tag>,
27
    scripting_enabled: bool,
28
    scripts: Vec<ScriptView>,
29
}
30

            
31
pub async fn account_edit_page(
32
    Path(id): Path<Uuid>,
33
    State(_data): State<Arc<AppState>>,
34
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
35
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
36
    let user = &jwt_auth.user;
37

            
38
    let account_result = GetAccount::new()
39
        .user_id(user.id)
40
        .account_id(id)
41
        .run()
42
        .await
43
        .map_err(|e| {
44
            let error_response = serde_json::json!({
45
                "status": "fail",
46
                "message": format!("Failed to get account: {e:?}"),
47
            });
48
            (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
49
        })?;
50

            
51
    let (account, name) = if let Some(CmdResult::TaggedEntities { entities, .. }) = account_result
52
        && let Some((FinanceEntity::Account(account), tags)) = entities.into_iter().next()
53
    {
54
        let name = if let Some(FinanceEntity::Tag(name_tag)) = tags.get("name") {
55
            name_tag.tag_value.clone()
56
        } else {
57
            String::new()
58
        };
59
        (account, name)
60
    } else {
61
        let error_response = serde_json::json!({
62
            "status": "fail",
63
            "message": "Account not found",
64
        });
65
        return Err((StatusCode::NOT_FOUND, Json(error_response)));
66
    };
67

            
68
    let server_user = server::user::User { id: user.id };
69
    let tags: Vec<finance::tag::Tag> = server_user
70
        .get_account_tags(&account)
71
        .await
72
        .unwrap_or_default()
73
        .into_iter()
74
        .filter(|t| t.tag_name != "name")
75
        .collect();
76

            
77
    #[cfg(feature = "scripting")]
78
    let scripts: Vec<ScriptView> = server_user
79
        .list_scripts()
80
        .await
81
        .unwrap_or_default()
82
        .into_iter()
83
        .map(|s| ScriptView {
84
            id: s.id,
85
            name: s.name,
86
        })
87
        .collect();
88

            
89
    #[cfg(not(feature = "scripting"))]
90
    let scripts: Vec<ScriptView> = Vec::new();
91

            
92
    let template = AccountEditPage {
93
        account_id: account.id,
94
        account_name: name,
95
        tags,
96
        scripting_enabled: cfg!(feature = "scripting"),
97
        scripts,
98
    };
99

            
100
    Ok(HtmlTemplate(template))
101
}
102

            
103
#[derive(Deserialize)]
104
pub struct RenameForm {
105
    account_id: Uuid,
106
    name: String,
107
}
108

            
109
#[derive(Deserialize)]
110
struct AccountTagData {
111
    name: String,
112
    value: String,
113
    description: Option<String>,
114
}
115

            
116
#[derive(Deserialize)]
117
pub struct AccountTagsForm {
118
    tags: Vec<AccountTagData>,
119
}
120

            
121
pub async fn rename_account(
122
    State(_data): State<Arc<AppState>>,
123
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
124
    Json(form): Json<RenameForm>,
125
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
126
    let user = &jwt_auth.user;
127
    let server_user = server::user::User { id: user.id };
128

            
129
    let account = Account {
130
        id: form.account_id,
131
        parent: None,
132
    };
133

            
134
    let name_tag = finance::tag::Tag {
135
        id: Uuid::new_v4(),
136
        tag_name: "name".to_string(),
137
        tag_value: form.name,
138
        description: None,
139
    };
140

            
141
    server_user
142
        .set_account_tag(&account, &name_tag)
143
        .await
144
        .map_err(|e| {
145
            let error_response = serde_json::json!({
146
                "status": "fail",
147
                "message": t!("Failed to rename account"),
148
            });
149
            log::error!("Failed to rename account: {e:?}");
150
            (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
151
        })?;
152

            
153
    Ok(t!("Account renamed").to_string())
154
}
155

            
156
pub async fn account_tags_submit(
157
    Path(id): Path<Uuid>,
158
    State(_data): State<Arc<AppState>>,
159
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
160
    Json(form): Json<AccountTagsForm>,
161
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
162
    let user = &jwt_auth.user;
163
    let server_user = server::user::User { id: user.id };
164

            
165
    let account = Account { id, parent: None };
166

            
167
    let existing_tags = server_user
168
        .get_account_tags(&account)
169
        .await
170
        .unwrap_or_default();
171

            
172
    for tag in &existing_tags {
173
        if tag.tag_name != "name" {
174
            let _ = server_user.delete_tag(tag.id).await;
175
        }
176
    }
177

            
178
    for tag_data in form.tags {
179
        if tag_data.name == "name" {
180
            continue;
181
        }
182
        server_user
183
            .create_account_tag(id, tag_data.name, tag_data.value, tag_data.description)
184
            .await
185
            .map_err(|e| {
186
                let error_response = serde_json::json!({
187
                    "status": "fail",
188
                    "message": format!("Failed to create account tag: {:?}", e),
189
                });
190
                log::error!("Failed to create account tag: {e:?}");
191
                (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
192
            })?;
193
    }
194

            
195
    Ok(t!("Account tags saved").to_string())
196
}
197

            
198
#[cfg(feature = "scripting")]
199
pub async fn run_account_script(
200
    Path((account_id, script_id)): Path<(Uuid, Uuid)>,
201
    State(_data): State<Arc<AppState>>,
202
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
203
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
204
    use scripting::ScriptExecutor;
205
    use server::script::TransactionState;
206

            
207
    let user = &jwt_auth.user;
208
    let server_user = server::user::User { id: user.id };
209

            
210
    let script = server_user.get_script(script_id).await.map_err(|e| {
211
        let error_response = serde_json::json!({
212
            "status": "fail",
213
            "message": format!("Failed to get script: {e:?}"),
214
        });
215
        (StatusCode::NOT_FOUND, Json(error_response))
216
    })?;
217

            
218
    let transaction_ids = server_user
219
        .list_transaction_ids_by_account(account_id)
220
        .await
221
        .map_err(|e| {
222
            let error_response = serde_json::json!({
223
                "status": "fail",
224
                "message": format!("Failed to fetch transactions: {e:?}"),
225
            });
226
            (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
227
        })?;
228

            
229
    let executor = ScriptExecutor::new();
230
    let mut processed = 0u64;
231

            
232
    for tx_id in &transaction_ids {
233
        let tx_result = server::command::transaction::GetTransaction::new()
234
            .user_id(user.id)
235
            .transaction_id(*tx_id)
236
            .run()
237
            .await
238
            .map_err(|e| {
239
                let error_response = serde_json::json!({
240
                    "status": "fail",
241
                    "message": format!("Failed to get transaction {tx_id}: {e:?}"),
242
                });
243
                (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
244
            })?;
245

            
246
        let (transaction, note) = if let Some(CmdResult::TaggedEntities { mut entities, .. }) =
247
            tx_result
248
            && let Some((FinanceEntity::Transaction(tx), tags)) = entities.pop()
249
        {
250
            let note = tags.get("note").and_then(|entity| {
251
                if let FinanceEntity::Tag(tag) = entity {
252
                    Some(tag.tag_value.clone())
253
                } else {
254
                    None
255
                }
256
            });
257
            (tx, note)
258
        } else {
259
            continue;
260
        };
261

            
262
        let splits_result = server::command::split::ListSplits::new()
263
            .user_id(user.id)
264
            .transaction(*tx_id)
265
            .run()
266
            .await
267
            .map_err(|e| {
268
                let error_response = serde_json::json!({
269
                    "status": "fail",
270
                    "message": format!("Failed to get splits for {tx_id}: {e:?}"),
271
                });
272
                (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
273
            })?;
274

            
275
        let mut split_entities = Vec::new();
276
        if let Some(CmdResult::TaggedEntities {
277
            entities: split_data,
278
            ..
279
        }) = splits_result
280
        {
281
            for (entity, _tags) in split_data {
282
                split_entities.push(entity);
283
            }
284
        }
285

            
286
        let state = TransactionState::new(transaction)
287
            .with(split_entities)
288
            .with_note(note);
289

            
290
        let state = state
291
            .run_scripts(&executor, &[script.bytecode.clone()])
292
            .map_err(|e| {
293
                let error_response = serde_json::json!({
294
                    "status": "fail",
295
                    "message": format!("Script execution failed on {tx_id}: {e:?}"),
296
                });
297
                (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
298
            })?;
299

            
300
        for tag in &state.transaction_tags {
301
            let _ = server_user
302
                .create_transaction_tag(*tx_id, tag.tag_name.clone(), tag.tag_value.clone(), None)
303
                .await;
304
        }
305

            
306
        for (split_id, tag) in &state.split_tags {
307
            let _ = server_user
308
                .create_split_tag(*split_id, tag.tag_name.clone(), tag.tag_value.clone(), None)
309
                .await;
310
        }
311

            
312
        processed += 1;
313
    }
314

            
315
    Ok(format!("{}: {processed}", t!("Processed transactions")))
316
}