use super::business::{BusinessNode, Kind::*, TagSpec};
use super::element::{attributes, elements};
use super::Json;
elements! {
deal = "Deal": children;
deal_move = "DealMove": leaf;
pipeline = "Pipeline": children;
deal_stage = "DealStage": leaf;
}
attributes! {
pub trait DealAttrs {
counterparty; expected; probability; probability_reason;
from; to; at; by;
stale_days; label;
}
}
pub(crate) const DEALS_TAGS: &[TagSpec] = &[
TagSpec {
tag: "Deal",
props: &[
("id", Text),
("title", Text),
("counterparty", Text),
("stage", Text),
("value", Amount),
("currency", Text),
("expected", Text),
("closed", Text),
("owner", Text),
("since", Text),
("probability", Amount),
("probabilityReason", Text),
("source", Text),
("note", Text),
],
required: &["id", "title", "stage"],
one: &[],
many: &[("moves", "DealMove")],
},
TagSpec {
tag: "DealMove",
props: &[("from", Text), ("to", Text), ("at", Text), ("by", Text), ("note", Text)],
required: &["to", "at"],
one: &[],
many: &[],
},
TagSpec { tag: "Pipeline", props: &[("staleDays", Amount)], required: &[], one: &[], many: &[("stages", "DealStage")] },
TagSpec {
tag: "DealStage",
props: &[("id", Text), ("label", Text), ("probability", Amount)],
required: &["id", "probability"],
one: &[],
many: &[],
},
];
pub(crate) fn deals_of(node: &BusinessNode, company: Json) -> Json {
let props = |n: &BusinessNode| Json::Obj(n.props.clone());
let company = match node.one("pipeline") {
Some(p) => company.with("pipeline", props(p).with("stages", p.many("stages").iter().map(props).collect::<Vec<_>>())),
None => company,
};
let deals: Vec<Json> = node
.many("deals")
.iter()
.map(|d| {
let moves: Vec<Json> = d.many("moves").iter().map(props).collect();
if moves.is_empty() {
props(d)
} else {
props(d).with("moves", moves)
}
})
.collect();
if deals.is_empty() {
company
} else {
company.with("deals", deals)
}
}