use serde::{Deserialize, Serialize};
use crate::rest::{ResourceOperation, ResourcePath, RestResource};
use crate::HttpMethod;
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct FulfillmentService {
#[serde(skip_serializing)]
pub id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub handle: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub callback_url: Option<String>,
#[serde(skip_serializing)]
pub location_id: Option<u64>,
#[serde(skip_serializing)]
pub provider_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tracking_support: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inventory_management: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub requires_shipping_method: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fulfillment_orders_opt_in: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub permits_sku_sharing: Option<bool>,
#[serde(skip_serializing)]
pub admin_graphql_api_id: Option<String>,
}
impl RestResource for FulfillmentService {
type Id = u64;
type FindParams = FulfillmentServiceFindParams;
type AllParams = FulfillmentServiceListParams;
type CountParams = ();
const NAME: &'static str = "FulfillmentService";
const PLURAL: &'static str = "fulfillment_services";
const PATHS: &'static [ResourcePath] = &[
ResourcePath::new(
HttpMethod::Get,
ResourceOperation::Find,
&["id"],
"fulfillment_services/{id}",
),
ResourcePath::new(
HttpMethod::Get,
ResourceOperation::All,
&[],
"fulfillment_services",
),
ResourcePath::new(
HttpMethod::Post,
ResourceOperation::Create,
&[],
"fulfillment_services",
),
ResourcePath::new(
HttpMethod::Put,
ResourceOperation::Update,
&["id"],
"fulfillment_services/{id}",
),
ResourcePath::new(
HttpMethod::Delete,
ResourceOperation::Delete,
&["id"],
"fulfillment_services/{id}",
),
];
fn get_id(&self) -> Option<Self::Id> {
self.id
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct FulfillmentServiceFindParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct FulfillmentServiceListParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub scope: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rest::{get_path, ResourceOperation};
#[test]
fn test_fulfillment_service_serialization() {
let service = FulfillmentService {
id: Some(61629186),
name: Some("My Fulfillment".to_string()),
handle: Some("my-fulfillment".to_string()),
callback_url: Some("https://myapp.com/fulfillment".to_string()),
location_id: Some(655441491),
provider_id: Some("provider_123".to_string()),
tracking_support: Some(true),
inventory_management: Some(true),
requires_shipping_method: Some(false),
fulfillment_orders_opt_in: Some(true),
permits_sku_sharing: Some(false),
admin_graphql_api_id: Some("gid://shopify/FulfillmentService/61629186".to_string()),
};
let json = serde_json::to_string(&service).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(parsed["name"], "My Fulfillment");
assert_eq!(parsed["handle"], "my-fulfillment");
assert_eq!(parsed["callback_url"], "https://myapp.com/fulfillment");
assert_eq!(parsed["tracking_support"], true);
assert_eq!(parsed["inventory_management"], true);
assert_eq!(parsed["requires_shipping_method"], false);
assert_eq!(parsed["fulfillment_orders_opt_in"], true);
assert_eq!(parsed["permits_sku_sharing"], false);
assert!(parsed.get("id").is_none());
assert!(parsed.get("location_id").is_none());
assert!(parsed.get("provider_id").is_none());
assert!(parsed.get("admin_graphql_api_id").is_none());
}
#[test]
fn test_fulfillment_service_deserialization() {
let json = r#"{
"id": 61629186,
"name": "My Fulfillment Service",
"handle": "my-fulfillment-service",
"callback_url": "https://myapp.com/fulfillment",
"location_id": 655441491,
"provider_id": null,
"tracking_support": true,
"inventory_management": true,
"requires_shipping_method": false,
"fulfillment_orders_opt_in": true,
"permits_sku_sharing": false,
"admin_graphql_api_id": "gid://shopify/FulfillmentService/61629186"
}"#;
let service: FulfillmentService = serde_json::from_str(json).unwrap();
assert_eq!(service.id, Some(61629186));
assert_eq!(service.name, Some("My Fulfillment Service".to_string()));
assert_eq!(service.handle, Some("my-fulfillment-service".to_string()));
assert_eq!(
service.callback_url,
Some("https://myapp.com/fulfillment".to_string())
);
assert_eq!(service.location_id, Some(655441491));
assert!(service.provider_id.is_none());
assert_eq!(service.tracking_support, Some(true));
assert_eq!(service.inventory_management, Some(true));
assert_eq!(service.requires_shipping_method, Some(false));
assert_eq!(service.fulfillment_orders_opt_in, Some(true));
assert_eq!(service.permits_sku_sharing, Some(false));
assert_eq!(
service.admin_graphql_api_id,
Some("gid://shopify/FulfillmentService/61629186".to_string())
);
}
#[test]
fn test_fulfillment_service_crud_without_count() {
let find_path = get_path(FulfillmentService::PATHS, ResourceOperation::Find, &["id"]);
assert!(find_path.is_some());
assert_eq!(find_path.unwrap().template, "fulfillment_services/{id}");
let all_path = get_path(FulfillmentService::PATHS, ResourceOperation::All, &[]);
assert!(all_path.is_some());
assert_eq!(all_path.unwrap().template, "fulfillment_services");
let count_path = get_path(FulfillmentService::PATHS, ResourceOperation::Count, &[]);
assert!(count_path.is_none());
let create_path = get_path(FulfillmentService::PATHS, ResourceOperation::Create, &[]);
assert!(create_path.is_some());
assert_eq!(create_path.unwrap().template, "fulfillment_services");
let update_path = get_path(FulfillmentService::PATHS, ResourceOperation::Update, &["id"]);
assert!(update_path.is_some());
assert_eq!(update_path.unwrap().template, "fulfillment_services/{id}");
let delete_path = get_path(FulfillmentService::PATHS, ResourceOperation::Delete, &["id"]);
assert!(delete_path.is_some());
assert_eq!(delete_path.unwrap().template, "fulfillment_services/{id}");
}
#[test]
fn test_fulfillment_service_list_params() {
let params = FulfillmentServiceListParams {
scope: Some("current_client".to_string()),
};
let json = serde_json::to_value(¶ms).unwrap();
assert_eq!(json["scope"], "current_client");
let params_all = FulfillmentServiceListParams {
scope: Some("all".to_string()),
};
let json_all = serde_json::to_value(¶ms_all).unwrap();
assert_eq!(json_all["scope"], "all");
let empty_params = FulfillmentServiceListParams::default();
let empty_json = serde_json::to_value(&empty_params).unwrap();
assert_eq!(empty_json, serde_json::json!({}));
}
#[test]
fn test_fulfillment_service_constants() {
assert_eq!(FulfillmentService::NAME, "FulfillmentService");
assert_eq!(FulfillmentService::PLURAL, "fulfillment_services");
}
#[test]
fn test_fulfillment_service_get_id() {
let service_with_id = FulfillmentService {
id: Some(61629186),
name: Some("Test Service".to_string()),
..Default::default()
};
assert_eq!(service_with_id.get_id(), Some(61629186));
let service_without_id = FulfillmentService::default();
assert_eq!(service_without_id.get_id(), None);
}
}