1
//! The category breakdown table as a CSV download.
2

            
3
use std::sync::Arc;
4

            
5
use axum::Extension;
6
use axum::extract::{Query, State};
7
use axum::http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
8
use axum::response::IntoResponse;
9
use server::command::report::view::category_csv;
10

            
11
use super::category_breakdown_query::ask;
12
use crate::pages::forms::unprocessable_fields;
13
use crate::{AppState, jwt_auth::JWTAuthMiddleware, response::WebError};
14

            
15
10
pub async fn category_breakdown_report_csv(
16
10
    Query(pairs): Query<Vec<(String, String)>>,
17
10
    State(_data): State<Arc<AppState>>,
18
10
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
19
10
) -> Result<impl IntoResponse, WebError> {
20
10
    let asked = ask(jwt_auth.user.id, pairs).await?;
21
10
    let data = asked.report.map_err(unprocessable_fields)?;
22
18
    let day = |key| asked.values.text(key).unwrap_or_default();
23
9
    let (from, to) = (day("from"), day("to"));
24
9
    Ok((
25
9
        [
26
9
            (CONTENT_TYPE, "text/csv; charset=utf-8".to_string()),
27
9
            (
28
9
                CONTENT_DISPOSITION,
29
9
                format!("attachment; filename=\"category-report-{from}-{to}.csv\""),
30
9
            ),
31
9
        ],
32
9
        category_csv(&data),
33
9
    ))
34
10
}