use std::collections::HashMap;
use futures::StreamExt;
use rust_decimal::Decimal;
use serde_json::json;
use std::str::FromStr;
use rutmf::api::{Query, ResolveRef, tmf620::ProductCatalogClient};
use rutmf::core::{Money, Ref, TimePeriod, Timestamp};
use rutmf::mock::MockTmfServer;
use rutmf::product::{ProductOffering, ProductOfferingCreate, ProductSpecification};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = seeded_client()?;
println!("== active offerings ==");
let mut offerings =
client.stream_product_offerings(Query::new().filter("lifecycleStatus", "Active").limit(25));
let mut by_specification: HashMap<String, Vec<String>> = HashMap::new();
while let Some(offering) = offerings.next().await {
let offering: ProductOffering = offering?;
let name = offering.name.as_deref().unwrap_or("<unnamed>");
let specification = match &offering.product_specification {
Some(reference) => {
let spec: ProductSpecification =
reference.resolve(client.inner(), &Query::new()).await?;
format!(
"{} ({})",
spec.name.as_deref().unwrap_or("<unnamed>"),
reference.id
)
}
None => "<none>".to_owned(),
};
println!(" {name:<28} spec={specification}");
by_specification
.entry(specification)
.or_default()
.push(name.to_owned());
}
println!("\n== creating an offering ==");
let body = ProductOfferingCreate::builder()
.name("Business Internet 1G")
.lifecycle_status("Active")
.last_update(Timestamp::now())
.description("Symmetric gigabit fibre for business premises")
.is_sellable(true)
.valid_for(TimePeriod::starting(
"2026-01-01T00:00:00Z".parse::<Timestamp>()?,
))
.product_specification(Ref::<ProductSpecification>::new("9881").with_name("Fibre Access"))
.build();
let created = client.create_product_offering(&body).await?;
println!(
" created id={} name={:?}",
created.id.unwrap_or_default(),
created.name
);
println!("\n== offerings per specification ==");
let mut specs: Vec<_> = by_specification.iter().collect();
specs.sort();
for (specification, names) in specs {
println!(" {specification}: {}", names.join(", "));
}
Ok(())
}
fn seeded_client() -> Result<ProductCatalogClient, Box<dyn std::error::Error>> {
let server = MockTmfServer::new();
for (id, name, status, spec) in [
("7655", "Basic Firewall for Business", "Active", "9881"),
("7656", "Managed Firewall Premium", "Active", "9881"),
("7657", "Legacy DSL", "Retired", "9882"),
("7658", "Business Internet 500M", "Active", "9883"),
] {
server.seed(
"productOffering",
json!({
"id": id,
"href": format!("{}/productOffering/{id}", server.base_url()),
"name": name,
"lifecycleStatus": status,
"isSellable": status == "Active",
"productSpecification": {
"id": spec,
"@type": "ProductSpecificationRef",
"@referredType": "ProductSpecification",
},
"@type": "ProductOffering",
}),
);
}
for (id, name) in [
("9881", "Cisco Firepower NGFW"),
("9882", "Legacy DSL Access"),
("9883", "Fibre Access"),
] {
server.seed(
"productSpecification",
json!({
"id": id,
"href": format!("{}/productSpecification/{id}", server.base_url()),
"name": name,
"lifecycleStatus": "Active",
"@type": "ProductSpecification",
}),
);
}
let monthly = Money::new("EUR", Decimal::from_str("49.99")?);
server.seed(
"productOfferingPrice",
json!({
"id": "4501",
"name": "Monthly recurring",
"priceType": "recurring",
"recurringChargePeriodType": "monthly",
"price": {"unit": monthly.unit, "value": monthly.value},
"@type": "ProductOfferingPrice",
}),
);
Ok(ProductCatalogClient::new(
server.base_url(),
server.transport(),
)?)
}