Lines
100 %
Functions
22.73 %
Branches
//! The report pages' one reader: query pairs to the form's values, then the report.
use chrono::Utc;
use forms::{FieldError, Form, FormValues};
use rpc::forms::{Failure, FormAction, Outcome, apply_report_defaults, run_report_in, user_zone};
use server::timezone::local_day;
use uuid::Uuid;
use super::{ChartParams, TableControlParams, encode_query};
use crate::form_render::References;
use crate::pages::forms::{failure_error, request::Request};
use crate::response::WebError;
/// Client-only controls: never part of the form, never passed to the report.
const PRESENTATION: [&str; 6] = [
"commodity_columns",
"collapsed_depth",
"sort_order",
"chart_kind",
"chart_series",
"renderer",
];
pub struct Presentation {
pub shown: Vec<(String, String)>,
pub table: TableControlParams,
pub chart: ChartParams,
pub sort_order: Option<String>,
}
impl Presentation {
fn from_pairs(pairs: &[(String, String)]) -> Self {
let get = |name: &str| {
pairs
.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value.clone())
.filter(|value| !value.is_empty())
};
Self {
shown: pairs
.filter(|(_, value)| !value.is_empty())
.cloned()
.collect(),
table: TableControlParams {
collapsed_depth: get("collapsed_depth"),
commodity_columns: get("commodity_columns"),
},
chart: ChartParams {
chart_kind: get("chart_kind"),
chart_series: get("chart_series"),
renderer: get("renderer"),
sort_order: get("sort_order"),
/// A report request: the values it carried and what the report made of them.
pub struct Asked<D> {
pub values: FormValues,
pub references: References,
pub presentation: Presentation,
pub report: Result<D, Vec<FieldError>>,
impl<D> Asked<D> {
/// The chart endpoints read the form's values from the same query as the page.
pub fn chart_query(&self, form: &Form, controls: &[(&str, &str)]) -> String {
let pairs = self.values.to_pairs(form);
let mut query: Vec<(&str, &str)> = pairs
.map(|(name, value)| (name.as_str(), value.as_str()))
.collect();
query.extend_from_slice(controls);
encode_query(&query)
/// The empty dates take the report defaults the form then shows; `pick` takes
/// the form's report out of the outcome.
pub async fn ask_form<D>(
user_id: Uuid,
pairs: Vec<(String, String)>,
form: &'static Form,
pick: fn(Outcome) -> Option<D>,
) -> Result<Asked<D>, WebError> {
let failed = format!("{} failed", form.title);
let (shown, pairs): (Vec<_>, Vec<_>) = pairs
.into_iter()
.partition(|(name, _)| PRESENTATION.contains(&name.as_str()));
let presentation = Presentation::from_pairs(&shown);
let now = Utc::now();
let zone = user_zone(user_id)
.await
.map_err(|err| failure_error(&failed, err))?;
let request = Request::read_prefilled(form, FormAction::Query, user_id, pairs, |values| {
apply_report_defaults(form, values, local_day(now, zone));
})
.await?;
let answered = request
.settle(async |values| run_report_in(user_id, form, values, (zone, now)).await)
.await;
let report = match answered {
Ok(outcome) => {
Ok(pick(outcome).ok_or_else(|| WebError::internal(&failed, "unexpected outcome"))?)
Err(Failure::Invalid(errors)) => Err(errors),
Err(failure) => return Err(failure_error(&failed, failure)),
Ok(Asked {
values: request.values,
references: request.references,
presentation,
report,