def add_shipping_rate(backend: "ShippingMethodsBackend", obj: dict, action: types.ShippingMethodAddShippingRateAction): new = copy.deepcopy(obj) draft = action.shipping_rate shipping_rate = create_shipping_rate_from_draft(draft) target_zone_rate = None for zone_rate in new["zoneRates"]: if zone_rate["zone"]["id"] == action.zone.id: target_zone_rate = zone_rate break if not target_zone_rate: new["zoneRates"].append( schemas.ZoneRateSchema().dump(types.ZoneRate( zone=action.zone, shipping_rates=[shipping_rate] )) ) else: if not target_zone_rate.get("shippingRates"): target_zone_rate["shippingRates"] = [] target_zone_rate["shippingRates"].append( schemas.ShippingRateSchema().dump(shipping_rate) ) return new
def remove_shipping_rate(backend: "ShippingMethodsBackend", obj: dict, action: types.ShippingMethodRemoveShippingRateAction): target_zone_rate = None for zone_rate in obj["zoneRates"]: if zone_rate["zone"]["id"] == action.zone.id: target_zone_rate = zone_rate break if not target_zone_rate: raise InternalUpdateError("Zone does not exist") rate_to_delete = create_shipping_rate_from_draft(action.shipping_rate) for shipping_rate in target_zone_rate["shippingRates"]: existing_rate = schemas.ShippingRateSchema().load(shipping_rate) if existing_rate == rate_to_delete: target_zone_rate["shippingRates"].remove(shipping_rate) return copy.deepcopy(obj) raise InternalUpdateError("Shipping rate does not exist")