feat(admin): allow updating bundle module_ids via plan pricing endpoint
This commit is contained in:
parent
464350cce4
commit
ec3129a889
|
|
@ -78,6 +78,7 @@ class PlanPricingUpdateRequest(BaseModel):
|
|||
badge_text: str | None = None
|
||||
heading: str | None = None
|
||||
subtitle: str | None = None
|
||||
module_ids: list[str] | None = None
|
||||
|
||||
class PlanStatusUpdateRequest(BaseModel):
|
||||
is_active: bool
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
from decimal import Decimal
|
||||
from uuid import UUID
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy import delete, select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.db.enums import BillingPeriod, PlanType, SubscriptionStatus
|
||||
from app.db.models import AccessPlan, AccessPlanModule, AppUser, Module, UserSubscription
|
||||
|
|
@ -297,6 +297,23 @@ class AdminPlanService:
|
|||
meta["pricing_status"] = "configured"
|
||||
p.metadata_json = meta
|
||||
|
||||
if payload.module_ids is not None:
|
||||
plan_ids = [p.id for p in plans]
|
||||
await self.session.execute(
|
||||
delete(AccessPlanModule).where(AccessPlanModule.plan_id.in_(plan_ids))
|
||||
)
|
||||
seen_mids: list[UUID] = []
|
||||
for mid_str in payload.module_ids:
|
||||
try:
|
||||
mid = UUID(mid_str)
|
||||
except Exception:
|
||||
continue
|
||||
if mid not in seen_mids:
|
||||
seen_mids.append(mid)
|
||||
for p in plans:
|
||||
for mid in seen_mids:
|
||||
self.session.add(AccessPlanModule(plan_id=p.id, module_id=mid))
|
||||
|
||||
await self.session.commit()
|
||||
return {"status": "success", "message": f"Pricing and metadata for plan '{base_code}' updated successfully."}
|
||||
|
||||
|
|
|
|||
|
|
@ -129,3 +129,58 @@ async def test_get_public_plans_enriches_module_metadata_and_sort_order():
|
|||
"category": "Therapeutics",
|
||||
"sort_order": 40,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_pricing_syncs_bundle_module_ids():
|
||||
inr_id, usd_id, eur_id = uuid.uuid4(), uuid.uuid4(), uuid.uuid4()
|
||||
m1, m2 = uuid.uuid4(), uuid.uuid4()
|
||||
plans = [
|
||||
SimpleNamespace(
|
||||
id=inr_id,
|
||||
code="ALL_IN_ONE",
|
||||
name="All-in-One",
|
||||
description="",
|
||||
module_fee=Decimal("4000"),
|
||||
currency_code="INR",
|
||||
is_active=True,
|
||||
metadata_json={},
|
||||
),
|
||||
SimpleNamespace(
|
||||
id=usd_id,
|
||||
code="ALL_IN_ONE_USD",
|
||||
name="All-in-One (USD)",
|
||||
description="",
|
||||
module_fee=Decimal("100"),
|
||||
currency_code="USD",
|
||||
is_active=True,
|
||||
metadata_json={},
|
||||
),
|
||||
SimpleNamespace(
|
||||
id=eur_id,
|
||||
code="ALL_IN_ONE_EUR",
|
||||
name="All-in-One (EUR)",
|
||||
description="",
|
||||
module_fee=Decimal("100"),
|
||||
currency_code="EUR",
|
||||
is_active=True,
|
||||
metadata_json={},
|
||||
),
|
||||
]
|
||||
session = AsyncMock()
|
||||
session.add = MagicMock()
|
||||
session.execute.side_effect = [
|
||||
_exec_result(scalars_list=plans),
|
||||
_exec_result(),
|
||||
]
|
||||
|
||||
svc = AdminPlanService(session)
|
||||
res = await svc.update_pricing(
|
||||
"ALL_IN_ONE",
|
||||
PlanPricingUpdateRequest(module_ids=[str(m1), str(m2), str(m1), "invalid-uuid"]),
|
||||
)
|
||||
assert res["status"] == "success"
|
||||
added_links = [(c.args[0].plan_id, c.args[0].module_id) for c in session.add.call_args_list]
|
||||
assert len(added_links) == 6
|
||||
assert (inr_id, m1) in added_links and (eur_id, m2) in added_links
|
||||
session.commit.assert_awaited_once()
|
||||
|
|
|
|||
Loading…
Reference in New Issue