RABC fejlesztése és beélesítése hibák kijavításával
This commit is contained in:
@@ -51,18 +51,14 @@ class PricingCalculator:
|
||||
|
||||
# RBAC rank discounts (higher rank = bigger discount)
|
||||
# Map the actual UserRole enum values to discount percentages
|
||||
# RBAC Phase 1: Tisztított rendszerszintű szerepkörök
|
||||
RBAC_DISCOUNTS = {
|
||||
UserRole.superadmin: 0.5, # 50% discount
|
||||
UserRole.admin: 0.3, # 30% discount
|
||||
UserRole.fleet_manager: 0.2, # 20% discount
|
||||
UserRole.user: 0.0, # 0% discount
|
||||
# Add other roles as needed
|
||||
UserRole.region_admin: 0.25, # 25% discount
|
||||
UserRole.country_admin: 0.25, # 25% discount
|
||||
UserRole.moderator: 0.15, # 15% discount
|
||||
UserRole.sales_agent: 0.1, # 10% discount
|
||||
UserRole.service_owner: 0.1, # 10% discount
|
||||
UserRole.driver: 0.0, # 0% discount
|
||||
UserRole.SUPERADMIN: 0.5, # 50% discount
|
||||
UserRole.ADMIN: 0.3, # 30% discount
|
||||
UserRole.MODERATOR: 0.15, # 15% discount
|
||||
UserRole.SERVICE_MGR: 0.1, # 10% discount
|
||||
UserRole.SALES_REP: 0.1, # 10% discount
|
||||
UserRole.USER: 0.0, # 0% discount
|
||||
}
|
||||
|
||||
@classmethod
|
||||
@@ -71,7 +67,7 @@ class PricingCalculator:
|
||||
db: AsyncSession,
|
||||
base_amount: float,
|
||||
country_code: str = "HU",
|
||||
user_role: UserRole = UserRole.user,
|
||||
user_role: UserRole = UserRole.USER,
|
||||
individual_discounts: Optional[List[Dict[str, Any]]] = None
|
||||
) -> float:
|
||||
"""
|
||||
@@ -663,7 +659,7 @@ async def calculate_price(
|
||||
db: AsyncSession,
|
||||
base_amount: float,
|
||||
country_code: str = "HU",
|
||||
user_role: UserRole = UserRole.user,
|
||||
user_role: UserRole = UserRole.USER,
|
||||
individual_discounts: Optional[List[Dict[str, Any]]] = None
|
||||
) -> float:
|
||||
"""
|
||||
@@ -806,6 +802,95 @@ async def upgrade_subscription(
|
||||
}
|
||||
|
||||
|
||||
async def upgrade_org_subscription(
|
||||
db: AsyncSession,
|
||||
org_id: int,
|
||||
tier_id: int,
|
||||
actor_user_id: int
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Szervezet előfizetési csomagjának beállítása (org-level subscription assignment).
|
||||
|
||||
P0 Feature: Subscription & Package Assignment Bridge.
|
||||
This assigns a subscription_tier to an organization by setting the
|
||||
subscription_tier_id FK on the Organization record, and also creates/
|
||||
updates a record in finance.org_subscriptions for audit trail.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
org_id: Organization ID
|
||||
tier_id: SubscriptionTier ID
|
||||
actor_user_id: User ID performing the action (for audit)
|
||||
|
||||
Returns:
|
||||
Dict: Result with tier details
|
||||
"""
|
||||
from app.models.core_logic import SubscriptionTier, OrganizationSubscription
|
||||
from app.models.marketplace.organization import Organization
|
||||
|
||||
# 1. Ellenőrizze, hogy a tier létezik-e
|
||||
stmt = select(SubscriptionTier).where(SubscriptionTier.id == tier_id)
|
||||
result = await db.execute(stmt)
|
||||
tier = result.scalar_one_or_none()
|
||||
|
||||
if not tier:
|
||||
raise ValueError(f"Subscription tier id={tier_id} not found")
|
||||
|
||||
# 2. Ellenőrizze, hogy a szervezet létezik-e
|
||||
stmt = select(Organization).where(Organization.id == org_id)
|
||||
result = await db.execute(stmt)
|
||||
org = result.scalar_one_or_none()
|
||||
|
||||
if not org:
|
||||
raise ValueError(f"Organization id={org_id} not found")
|
||||
|
||||
# 3. Állítsa be a subscription_tier_id-t a szervezeten
|
||||
org.subscription_tier_id = tier_id
|
||||
org.subscription_plan = tier.name
|
||||
|
||||
# 4. Extract max_vehicles from tier rules to update base_asset_limit
|
||||
max_vehicles = tier.rules.get("allowances", {}).get("max_vehicles", 1) if tier.rules else 1
|
||||
org.base_asset_limit = max_vehicles
|
||||
|
||||
# 5. Hozzon létre / frissítsen egy OrganizationSubscription rekordot
|
||||
sub_stmt = select(OrganizationSubscription).where(
|
||||
OrganizationSubscription.org_id == org_id,
|
||||
OrganizationSubscription.is_active == True
|
||||
)
|
||||
sub_result = await db.execute(sub_stmt)
|
||||
existing_sub = sub_result.scalar_one_or_none()
|
||||
|
||||
if existing_sub:
|
||||
# Deaktiváljuk a régit
|
||||
existing_sub.is_active = False
|
||||
existing_sub.valid_until = datetime.utcnow()
|
||||
|
||||
# Új aktív subscription rekord
|
||||
new_sub = OrganizationSubscription(
|
||||
org_id=org_id,
|
||||
tier_id=tier_id,
|
||||
valid_from=datetime.utcnow(),
|
||||
is_active=True
|
||||
)
|
||||
db.add(new_sub)
|
||||
|
||||
logger.info(
|
||||
f"Org subscription upgraded: org_id={org_id}, "
|
||||
f"tier={tier.name} (id={tier_id}), "
|
||||
f"max_vehicles={max_vehicles}, "
|
||||
f"actor={actor_user_id}"
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"organization_id": org_id,
|
||||
"tier_id": tier_id,
|
||||
"tier_name": tier.name,
|
||||
"max_vehicles": max_vehicles,
|
||||
"message": f"Organization {org_id} upgraded to {tier.name}"
|
||||
}
|
||||
|
||||
|
||||
async def record_ledger_entry(
|
||||
db: AsyncSession,
|
||||
user_id: int,
|
||||
|
||||
Reference in New Issue
Block a user