acosmi/enterprise/
client.rs1use super::types::{
6 AssignSeatRequest, EnterpriseKycMyStatusView, EnterpriseMember, EnterpriseSummary,
7 InviteMemberRequest, OrgConsumeReport, OrgSeat, OrgSubscription,
8};
9use crate::billing::entitlements::urlencoding;
10use crate::core::client::Client;
11use crate::shared::{Error, Result};
12use tokio_util::sync::CancellationToken;
13
14impl Client {
15 pub async fn list_my_enterprises(
21 &self,
22 signal: Option<CancellationToken>,
23 ) -> Result<Vec<EnterpriseSummary>> {
24 self.commerce_get_list("/me/enterprises", signal).await
25 }
26
27 pub async fn get_enterprise(
29 &self,
30 id: i64,
31 signal: Option<CancellationToken>,
32 ) -> Result<EnterpriseSummary> {
33 self.commerce_get_opt::<EnterpriseSummary>(&format!("/api/admin/enterprises/{id}"), signal)
34 .await?
35 .ok_or_else(|| Error::other(format!("enterprise {id} not found")))
36 }
37
38 pub async fn invite_member(
46 &self,
47 req: &InviteMemberRequest,
48 signal: Option<CancellationToken>,
49 ) -> Result<EnterpriseMember> {
50 let body = serde_json::to_string(req)
51 .map_err(|e| Error::other(format!("inviteMember: marshal: {e}")))?;
52 self.commerce_post_opt::<EnterpriseMember>(
53 "/api/admin/enterprise-members",
54 Some(&body),
55 signal,
56 )
57 .await?
58 .ok_or_else(|| Error::other("invite member failed"))
59 }
60
61 pub async fn list_enterprise_members(
63 &self,
64 enterprise_id: i64,
65 signal: Option<CancellationToken>,
66 ) -> Result<Vec<EnterpriseMember>> {
67 self.commerce_get_list(
68 &format!("/api/admin/enterprise-members/by-enterprise/{enterprise_id}"),
69 signal,
70 )
71 .await
72 }
73
74 pub async fn list_org_subscriptions(
80 &self,
81 enterprise_id: i64,
82 signal: Option<CancellationToken>,
83 ) -> Result<Vec<OrgSubscription>> {
84 self.commerce_get_list(
85 &format!("/api/admin/org-subscriptions/by-enterprise/{enterprise_id}"),
86 signal,
87 )
88 .await
89 }
90
91 pub async fn list_seats(
93 &self,
94 org_subscription_id: i64,
95 signal: Option<CancellationToken>,
96 ) -> Result<Vec<OrgSeat>> {
97 self.commerce_get_list(
98 &format!("/api/admin/org-seats/by-subscription/{org_subscription_id}"),
99 signal,
100 )
101 .await
102 }
103
104 pub async fn assign_seat(
108 &self,
109 req: &AssignSeatRequest,
110 signal: Option<CancellationToken>,
111 ) -> Result<OrgSeat> {
112 let body = serde_json::to_string(req)
113 .map_err(|e| Error::other(format!("assignSeat: marshal: {e}")))?;
114 self.commerce_post_opt::<OrgSeat>("/api/admin/org-seats/assign", Some(&body), signal)
115 .await?
116 .ok_or_else(|| Error::other("assign seat failed"))
117 }
118
119 pub async fn revoke_seat(
121 &self,
122 seat_id: i64,
123 note: Option<&str>,
124 signal: Option<CancellationToken>,
125 ) -> Result<()> {
126 let path = match note {
127 Some(n) => format!(
128 "/api/admin/org-seats/{seat_id}/revoke?note={}",
129 urlencoding(n)
130 ),
131 None => format!("/api/admin/org-seats/{seat_id}/revoke"),
132 };
133 self.commerce_post_discard(&path, None, signal).await
134 }
135
136 pub async fn get_org_consume_report(
144 &self,
145 enterprise_id: i64,
146 signal: Option<CancellationToken>,
147 ) -> Result<OrgConsumeReport> {
148 match self
149 .commerce_get_opt::<OrgConsumeReport>(
150 &format!("/api/admin/enterprise-settlements/overview/{enterprise_id}"),
151 signal,
152 )
153 .await?
154 {
155 Some(r) => Ok(r),
156 None => Ok(OrgConsumeReport {
157 enterprise_id,
158 subscription_count: 0,
159 total_pool_tk: 0,
160 total_used_tk: 0,
161 total_price_fen: 0,
162 note: None,
163 }),
164 }
165 }
166
167 pub async fn get_my_enterprise_kyc_status(
175 &self,
176 signal: Option<CancellationToken>,
177 ) -> Result<EnterpriseKycMyStatusView> {
178 Ok(self
179 .commerce_get_opt::<EnterpriseKycMyStatusView>(
180 "/api/distribution/enterprise/kyc/my",
181 signal,
182 )
183 .await?
184 .unwrap_or_default())
185 }
186}