227
/// Which listing to change; optional when the product has one listing.
228
pub language: Option<String>,
229
pub price: Option<Money>,
230
+ /// Which purchase option a price change re-prices; optional when the
231
+ /// product has one option.
232
+ pub purchase_option: Option<String>,
233
}
234
235
impl ProductChanges {
313
.to_string(),
314
_ => {
315
return Err(Error::Invalid(
313
- "the product has several listings; pass --language".to_string(),
316
+ "pass --language to name the listing to change (a new one also needs \
317
+ --title and --description)"
318
+ .to_string(),
319
))
320
}
321
};
356
.and_then(Value::as_object)
357
.filter(|m| !m.is_empty())
358
.ok_or_else(|| Error::Invalid("Play returned no regional prices".to_string()))?;
354
- let target = pick_purchase_option(product, None)?;
359
+ let target = pick_purchase_option(product, changes.purchase_option.as_deref())?;
360
let mut all: Vec<Value> = options(product).to_vec();
361
for option in all.iter_mut() {
362
if option.get("purchaseOptionId").and_then(Value::as_str) != Some(target.as_str()) {
694
assert_eq!(existing_price(&stored_product()).unwrap().currency, "GBP");
695
}
696
697
+ #[test]
698
+ fn a_price_update_names_the_option_when_the_product_has_several() {
699
+ let converted = json!({
700
+ "regionVersion": { "version": "2022/02" },
701
+ "convertedRegionPrices": {
702
+ "GB": { "price": { "currencyCode": "GBP", "units": "5", "nanos": 0 } }
703
+ }
704
+ });
705
+ let mut product = stored_product();
706
+ product["purchaseOptions"]
707
+ .as_array_mut()
708
+ .unwrap()
709
+ .push(json!({
710
+ "purchaseOptionId": "rent", "state": "ACTIVE",
711
+ "regionalPricingAndAvailabilityConfigs": [
712
+ { "regionCode": "GB", "availability": "AVAILABLE",
713
+ "price": { "currencyCode": "GBP", "units": "2", "nanos": 0 } }
714
+ ]
715
+ }));
716
+ let changes = ProductChanges {
717
+ price: Some(Money::parse("USD", "5").unwrap()),
718
+ ..Default::default()
719
+ };
720
+ assert!(update_body(&product, &changes, Some(&converted))
721
+ .unwrap_err()
722
+ .to_string()
723
+ .contains("--purchase-option"));
724
+ let changes = ProductChanges {
725
+ price: Some(Money::parse("USD", "5").unwrap()),
726
+ purchase_option: Some("rent".into()),
727
+ ..Default::default()
728
+ };
729
+ let body = update_body(&product, &changes, Some(&converted)).unwrap();
730
+ let repriced = body["purchaseOptions"]
731
+ .as_array()
732
+ .unwrap()
733
+ .iter()
734
+ .find(|o| o["purchaseOptionId"] == "rent")
735
+ .unwrap();
736
+ assert_eq!(
737
+ repriced["regionalPricingAndAvailabilityConfigs"][0]["price"]["units"],
738
+ "5"
739
+ );
740
+ }
741
+
742
#[test]
743
fn read_back_differences_are_caught() {
744
let changes = ProductChanges {