1
//! `(merge-accounts "into-uuid" "from-uuid")`: UUIDs only.
2

            
3
use uuid::Uuid;
4

            
5
use crate::forms::merge_accounts;
6
use crate::natives::forms::{args_error, native_failure};
7

            
8
12
fn account_id(role: &str, text: Option<String>) -> wasmtime::Result<Uuid> {
9
12
    let text = text.unwrap_or_default();
10
12
    Uuid::parse_str(text.trim()).map_err(|_| {
11
1
        args_error(format!(
12
1
            "merge-accounts: {role} must be an account UUID, got '{text}'"
13
1
        ))
14
1
    })
15
12
}
16

            
17
6
pub(super) async fn run_merge_accounts(
18
6
    user_id: Uuid,
19
6
    into: Option<String>,
20
6
    from: Option<String>,
21
6
) -> wasmtime::Result<String> {
22
6
    let into = account_id("into", into)?;
23
6
    let from = account_id("from", from)?;
24
5
    merge_accounts(user_id, into, from)
25
5
        .await
26
2
        .map(|merged| merged.into.to_string())
27
5
        .map_err(native_failure)
28
6
}