Skip to main content

acosmi/enterprise/
client.rs

1//! 企业席位(P6a)业务方法。端口自 `enterprise/client.ts`(declaration-merging → `impl Client` 块)。
2//!
3//! 端点路径形如 `/api/admin/enterprises/...`(后台管理)/ `/me/enterprises/...`(登录态)。
4
5use 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    // =====================================================================
16    // 企业组织(登录态:我所在的企业)
17    // =====================================================================
18
19    /// 列出我所在的企业。对应 TS `listMyEnterprises`。空 data → []。
20    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    /// 获取企业详情。对应 TS `getEnterprise`。空 data → Err(`enterprise {id} not found`)。
28    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    // =====================================================================
39    // 成员管理
40    // =====================================================================
41
42    /// 邀请成员加入(P6a 简化:直接 add,OWNER/ADMIN 可调用)。对应 TS `inviteMember`。
43    ///
44    /// 空 data → Err(`invite member failed`)。
45    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    /// 列出企业成员。对应 TS `listEnterpriseMembers`。空 data → []。
62    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    // =====================================================================
75    // 订阅 + 席位
76    // =====================================================================
77
78    /// 列出企业订阅。对应 TS `listOrgSubscriptions`。空 data → []。
79    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    /// 列出订阅下的席位(1 订阅 N 席,seat_no 1..N)。对应 TS `listSeats`。空 data → []。
92    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    /// 分配席位(月度变更次数 +1,超 3 次返 41xxx 业务码)。对应 TS `assignSeat`。
105    ///
106    /// 空 data → Err(`assign seat failed`)。
107    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    /// 收回席位(席位状态 → AVAILABLE,累计用量保留)。对应 TS `revokeSeat`(返回 void)。
120    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    // =====================================================================
137    // 用量报表
138    // =====================================================================
139
140    /// 企业消耗汇总(订阅维度池子合计)。对应 TS `getOrgConsumeReport`。
141    ///
142    /// 空 data → 回退全零 report(TS 默认对象)。
143    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    // =====================================================================
168    // KYC 自查(v2.0.0+)
169    // =====================================================================
170
171    /// 企业 OWNER 自查 KYC 状态。对应 TS `getMyEnterpriseKycStatus`。
172    ///
173    /// 用户不是任何企业 OWNER 时返空 view(`enterpriseId` 缺省)。空 data → 默认(`{}`)。
174    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}