1use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19use crate::serde_util::null_to_default;
20
21#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase", default)]
25pub struct Plan {
26 pub owner: String,
27 pub name: String,
28 pub created_time: String,
29 pub display_name: String,
30 pub description: String,
31
32 pub price: f64,
33 pub currency: String,
34 pub period: String,
35 pub product: String,
36 #[serde(deserialize_with = "null_to_default")]
38 pub payment_providers: Vec<String>,
39 pub is_enabled: bool,
40 pub is_exclusive: bool,
42
43 pub role: String,
44 #[serde(deserialize_with = "null_to_default")]
45 pub options: Vec<String>,
46}
47
48impl Client {
49 pub async fn get_plans(&self) -> Result<Vec<Plan>> {
51 self.get_list("get-plans", &[("owner", &self.config.organization_name)])
52 .await
53 }
54
55 pub async fn get_pagination_plans(
58 &self,
59 p: i32,
60 page_size: i32,
61 query: &[(&str, &str)],
62 ) -> Result<(Vec<Plan>, i32)> {
63 self.get_pagination(
64 "get-plans",
65 &self.config.organization_name,
66 p,
67 page_size,
68 query,
69 )
70 .await
71 }
72
73 pub async fn get_plan(&self, name: &str) -> Result<Option<Plan>> {
75 self.get_object("get-plan", &[("id", &self.get_id(name))])
76 .await
77 }
78
79 pub async fn add_plan(&self, plan: &Plan) -> Result<bool> {
81 Ok(self.modify_plan("add-plan", plan, &[]).await?.1)
82 }
83
84 pub async fn update_plan(&self, plan: &Plan) -> Result<bool> {
86 Ok(self.modify_plan("update-plan", plan, &[]).await?.1)
87 }
88
89 pub async fn delete_plan(&self, plan: &Plan) -> Result<bool> {
91 Ok(self.modify_plan("delete-plan", plan, &[]).await?.1)
92 }
93
94 async fn modify_plan(
95 &self,
96 action: &str,
97 plan: &Plan,
98 columns: &[&str],
99 ) -> Result<(Response, bool)> {
100 let mut plan = plan.clone();
101 plan.owner = get_owner(&plan.owner, &self.config.organization_name);
102
103 let id = format!("{}/{}", plan.owner, plan.name);
104 self.modify_object(action, &id, &plan, columns).await
105 }
106}