use super::types::{
AssignSeatRequest, EnterpriseKycMyStatusView, EnterpriseMember, EnterpriseSummary,
InviteMemberRequest, OrgConsumeReport, OrgSeat, OrgSubscription,
};
use crate::billing::entitlements::urlencoding;
use crate::core::client::Client;
use crate::shared::{Error, Result};
use tokio_util::sync::CancellationToken;
impl Client {
pub async fn list_my_enterprises(
&self,
signal: Option<CancellationToken>,
) -> Result<Vec<EnterpriseSummary>> {
self.commerce_get_list("/me/enterprises", signal).await
}
pub async fn get_enterprise(
&self,
id: i64,
signal: Option<CancellationToken>,
) -> Result<EnterpriseSummary> {
self.commerce_get_opt::<EnterpriseSummary>(&format!("/api/admin/enterprises/{id}"), signal)
.await?
.ok_or_else(|| Error::other(format!("enterprise {id} not found")))
}
pub async fn invite_member(
&self,
req: &InviteMemberRequest,
signal: Option<CancellationToken>,
) -> Result<EnterpriseMember> {
let body = serde_json::to_string(req)
.map_err(|e| Error::other(format!("inviteMember: marshal: {e}")))?;
self.commerce_post_opt::<EnterpriseMember>(
"/api/admin/enterprise-members",
Some(&body),
signal,
)
.await?
.ok_or_else(|| Error::other("invite member failed"))
}
pub async fn list_enterprise_members(
&self,
enterprise_id: i64,
signal: Option<CancellationToken>,
) -> Result<Vec<EnterpriseMember>> {
self.commerce_get_list(
&format!("/api/admin/enterprise-members/by-enterprise/{enterprise_id}"),
signal,
)
.await
}
pub async fn list_org_subscriptions(
&self,
enterprise_id: i64,
signal: Option<CancellationToken>,
) -> Result<Vec<OrgSubscription>> {
self.commerce_get_list(
&format!("/api/admin/org-subscriptions/by-enterprise/{enterprise_id}"),
signal,
)
.await
}
pub async fn list_seats(
&self,
org_subscription_id: i64,
signal: Option<CancellationToken>,
) -> Result<Vec<OrgSeat>> {
self.commerce_get_list(
&format!("/api/admin/org-seats/by-subscription/{org_subscription_id}"),
signal,
)
.await
}
pub async fn assign_seat(
&self,
req: &AssignSeatRequest,
signal: Option<CancellationToken>,
) -> Result<OrgSeat> {
let body = serde_json::to_string(req)
.map_err(|e| Error::other(format!("assignSeat: marshal: {e}")))?;
self.commerce_post_opt::<OrgSeat>("/api/admin/org-seats/assign", Some(&body), signal)
.await?
.ok_or_else(|| Error::other("assign seat failed"))
}
pub async fn revoke_seat(
&self,
seat_id: i64,
note: Option<&str>,
signal: Option<CancellationToken>,
) -> Result<()> {
let path = match note {
Some(n) => format!(
"/api/admin/org-seats/{seat_id}/revoke?note={}",
urlencoding(n)
),
None => format!("/api/admin/org-seats/{seat_id}/revoke"),
};
self.commerce_post_discard(&path, None, signal).await
}
pub async fn get_org_consume_report(
&self,
enterprise_id: i64,
signal: Option<CancellationToken>,
) -> Result<OrgConsumeReport> {
match self
.commerce_get_opt::<OrgConsumeReport>(
&format!("/api/admin/enterprise-settlements/overview/{enterprise_id}"),
signal,
)
.await?
{
Some(r) => Ok(r),
None => Ok(OrgConsumeReport {
enterprise_id,
subscription_count: 0,
total_pool_tk: 0,
total_used_tk: 0,
total_price_fen: 0,
note: None,
}),
}
}
pub async fn get_my_enterprise_kyc_status(
&self,
signal: Option<CancellationToken>,
) -> Result<EnterpriseKycMyStatusView> {
Ok(self
.commerce_get_opt::<EnterpriseKycMyStatusView>(
"/api/distribution/enterprise/kyc/my",
signal,
)
.await?
.unwrap_or_default())
}
}