2025-05-06 03:58:49 +00:00
|
|
|
from flask_login import current_user
|
2024-02-06 05:21:13 +00:00
|
|
|
|
2024-07-12 04:25:38 +00:00
|
|
|
from configs import dify_config
|
2026-02-10 04:08:23 +00:00
|
|
|
from enums.cloud_plan import CloudPlan
|
2024-02-06 05:21:13 +00:00
|
|
|
from extensions.ext_database import db
|
2025-03-12 04:56:30 +00:00
|
|
|
from models.account import Tenant, TenantAccountJoin, TenantAccountRole
|
2023-12-18 08:25:37 +00:00
|
|
|
from services.account_service import TenantService
|
2024-01-12 04:34:01 +00:00
|
|
|
from services.feature_service import FeatureService
|
2023-12-18 08:25:37 +00:00
|
|
|
|
2023-05-15 00:51:32 +00:00
|
|
|
|
|
|
|
|
class WorkspaceService:
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_tenant_info(cls, tenant: Tenant):
|
2023-08-15 05:35:47 +00:00
|
|
|
if not tenant:
|
|
|
|
|
return None
|
2025-09-09 17:54:26 +00:00
|
|
|
tenant_info: dict[str, object] = {
|
2024-08-26 05:43:57 +00:00
|
|
|
"id": tenant.id,
|
|
|
|
|
"name": tenant.name,
|
|
|
|
|
"plan": tenant.plan,
|
|
|
|
|
"status": tenant.status,
|
|
|
|
|
"created_at": tenant.created_at,
|
|
|
|
|
"trial_end_reason": None,
|
|
|
|
|
"role": "normal",
|
2023-05-15 00:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-15 05:35:47 +00:00
|
|
|
# Get role of user
|
2024-08-26 05:43:57 +00:00
|
|
|
tenant_account_join = (
|
|
|
|
|
db.session.query(TenantAccountJoin)
|
2025-07-23 16:57:45 +00:00
|
|
|
.where(TenantAccountJoin.tenant_id == tenant.id, TenantAccountJoin.account_id == current_user.id)
|
2024-08-26 05:43:57 +00:00
|
|
|
.first()
|
|
|
|
|
)
|
2024-12-24 10:38:51 +00:00
|
|
|
assert tenant_account_join is not None, "TenantAccountJoin not found"
|
2024-08-26 05:43:57 +00:00
|
|
|
tenant_info["role"] = tenant_account_join.role
|
|
|
|
|
|
2026-01-08 05:17:30 +00:00
|
|
|
feature = FeatureService.get_features(tenant.id)
|
|
|
|
|
can_replace_logo = feature.can_replace_logo
|
2024-08-26 05:43:57 +00:00
|
|
|
|
2025-03-12 04:56:30 +00:00
|
|
|
if can_replace_logo and TenantService.has_roles(tenant, [TenantAccountRole.OWNER, TenantAccountRole.ADMIN]):
|
2024-07-12 04:25:38 +00:00
|
|
|
base_url = dify_config.FILES_URL
|
2024-08-26 05:43:57 +00:00
|
|
|
replace_webapp_logo = (
|
|
|
|
|
f"{base_url}/files/workspaces/{tenant.id}/webapp-logo"
|
|
|
|
|
if tenant.custom_config_dict.get("replace_webapp_logo")
|
|
|
|
|
else None
|
|
|
|
|
)
|
|
|
|
|
remove_webapp_brand = tenant.custom_config_dict.get("remove_webapp_brand", False)
|
|
|
|
|
|
|
|
|
|
tenant_info["custom_config"] = {
|
|
|
|
|
"remove_webapp_brand": remove_webapp_brand,
|
|
|
|
|
"replace_webapp_logo": replace_webapp_logo,
|
2024-02-28 07:17:49 +00:00
|
|
|
}
|
2026-01-08 05:17:30 +00:00
|
|
|
if dify_config.EDITION == "CLOUD":
|
|
|
|
|
tenant_info["next_credit_reset_date"] = feature.next_credit_reset_date
|
|
|
|
|
|
|
|
|
|
from services.credit_pool_service import CreditPoolService
|
|
|
|
|
|
|
|
|
|
paid_pool = CreditPoolService.get_pool(tenant_id=tenant.id, pool_type="paid")
|
2026-02-10 04:08:23 +00:00
|
|
|
# if the tenant is not on the sandbox plan and the paid pool is not full, use the paid pool
|
|
|
|
|
if (
|
|
|
|
|
feature.billing.subscription.plan != CloudPlan.SANDBOX
|
|
|
|
|
and paid_pool is not None
|
|
|
|
|
and (paid_pool.quota_limit == -1 or paid_pool.quota_limit > paid_pool.quota_used)
|
|
|
|
|
):
|
2026-01-08 05:17:30 +00:00
|
|
|
tenant_info["trial_credits"] = paid_pool.quota_limit
|
|
|
|
|
tenant_info["trial_credits_used"] = paid_pool.quota_used
|
|
|
|
|
else:
|
|
|
|
|
trial_pool = CreditPoolService.get_pool(tenant_id=tenant.id, pool_type="trial")
|
|
|
|
|
if trial_pool:
|
|
|
|
|
tenant_info["trial_credits"] = trial_pool.quota_limit
|
|
|
|
|
tenant_info["trial_credits_used"] = trial_pool.quota_used
|
2023-12-18 08:25:37 +00:00
|
|
|
|
2023-05-15 00:51:32 +00:00
|
|
|
return tenant_info
|