1
//! `report-activity`: the groups rows and the activity command.
2

            
3
use forms::{FieldError, FormValues, REPORT_ACTIVITY, Row, address};
4
use server::command::report::ActivityReport;
5
use server::command::{ActivityData, ActivityGroup, CmdResult};
6
use server::timezone::Tz;
7
use uuid::Uuid;
8

            
9
use super::Failure;
10
use super::query::field_error;
11
use super::report::{command_failure, grouping, parse_filter, range, scope, unexpected};
12

            
13
/// A row naming a group and its filter; blank cells were already reported as required.
14
2732
fn group(index: usize, row: &Row, errors: &mut Vec<FieldError>) -> Option<ActivityGroup> {
15
5952
    let cell = |key| {
16
5952
        row.get(key)
17
4831
            .map(|text| text.trim())
18
4831
            .filter(|t| !t.is_empty())
19
5952
    };
20
2732
    let (label, text) = (cell("name")?, cell("filter")?);
21
1770
    let filter = parse_filter(text)
22
801
        .map_err(|message| {
23
801
            errors.push(field_error(
24
801
                &address::cell("groups", index, "filter"),
25
801
                message,
26
801
            ));
27
801
        })
28
801
        .ok()?;
29
969
    Some(ActivityGroup {
30
969
        label: label.to_owned(),
31
969
        filter,
32
969
        flip_sign: cell("flip") == Some("yes"),
33
969
    })
34
2732
}
35

            
36
/// `None` when the form has no `groups` field at all: the command's default groups apply.
37
7559
fn groups(values: &FormValues, errors: &mut Vec<FieldError>) -> Option<Vec<ActivityGroup>> {
38
7559
    let rows = values.rows("groups")?;
39
    Some(
40
2567
        rows.iter()
41
2567
            .enumerate()
42
2732
            .filter_map(|(index, row)| group(index, row, errors))
43
            .collect(),
44
    )
45
7559
}
46

            
47
7559
pub(super) async fn activity(
48
7559
    user_id: Uuid,
49
7559
    values: &FormValues,
50
7559
    zone: Tz,
51
7559
) -> Result<ActivityData, Failure> {
52
7559
    let values = &values.normalized(&REPORT_ACTIVITY);
53
7559
    let mut errors = values.check(&REPORT_ACTIVITY);
54
7559
    let scope = scope(user_id, values, &mut errors).await?;
55
7559
    let groups = groups(values, &mut errors);
56
7559
    let (from, to) = range(&REPORT_ACTIVITY, values, ("from", "to"), zone, &mut errors);
57
7559
    let (Some(from), Some(to), true) = (from, to, errors.is_empty()) else {
58
2888
        return Err(Failure::Invalid(errors));
59
    };
60
4671
    let mut cmd = ActivityReport::new()
61
4671
        .user_id(user_id)
62
4671
        .date_from(from)
63
4671
        .date_to(to);
64
4671
    if let Some(target) = scope.target {
65
324
        cmd = cmd.target_commodity_id(target);
66
4347
    }
67
4671
    if let Some(grouping) = grouping(values) {
68
967
        cmd = cmd.period_grouping(grouping);
69
3704
    }
70
4671
    if let Some(groups) = groups {
71
805
        cmd = cmd.groups(groups);
72
3866
    }
73
4671
    if let Some(filter) = scope.filter {
74
484
        cmd = cmd.report_filter(filter);
75
4187
    }
76
4671
    match cmd.run().await.map_err(command_failure)? {
77
4349
        Some(CmdResult::Activity(data)) => Ok(data),
78
        _ => Err(unexpected(&REPORT_ACTIVITY)),
79
    }
80
7559
}