Lines
97.06 %
Functions
58.33 %
Branches
100 %
use axum::{
Extension,
extract::{Path, State},
response::IntoResponse,
};
use server::command::{CmdResult, script::RunScriptOnAccount};
use server::script_run::TransactionFailure;
use sqlx::types::Uuid;
use std::sync::Arc;
use crate::{AppState, jwt_auth::JWTAuthMiddleware, response::WebError};
pub async fn run_account_script(
Path((account_id, script_id)): Path<(Uuid, Uuid)>,
State(_data): State<Arc<AppState>>,
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
) -> Result<impl IntoResponse, WebError> {
let answer = RunScriptOnAccount::new()
.user_id(jwt_auth.user.id)
.script_id(script_id)
.account_id(account_id)
.run()
.await?;
let Some(CmdResult::ScriptRun(run)) = answer else {
return Err(WebError::internal("Script run answered", answer));
if let Some(failure) = run.failures.first() {
return Err(script_failure(failure));
}
Ok(report(&run.ran, &run.committed))
/// The committed report: one line per transaction the script ran on.
fn report(ran: &[Uuid], committed: &[Uuid]) -> String {
let outcome = |id: &Uuid| match committed.contains(id) {
true => t!("committed"),
false => t!("unchanged"),
[
format!("{}: {}", t!("Processed transactions"), ran.len()),
format!("{}: {}", t!("Committed transactions"), committed.len()),
]
.into_iter()
.chain(ran.iter().map(|id| format!("{id}: {}", outcome(id))))
.collect::<Vec<_>>()
.join("\n")
fn script_failure(failure: &TransactionFailure) -> WebError {
WebError::unprocessable(format!(
"{}: failed: script {}: {}: {} (nothing was written)",
failure.transaction_id, failure.script_id, failure.code, failure.message
))
#[cfg(test)]
#[path = "script_tests.rs"]
mod tests;