from __future__ import annotations from sqlalchemy import delete, select from sqlalchemy.ext.asyncio import AsyncSession from app.db.enums import BillingPeriod, PlanType from app.db.models import AccessPlan, AccessPlanModule, Coupon, Module class CommerceSeedRepository: def __init__(self, session: AsyncSession) -> None: self.session = session async def upsert_plan(self, seed) -> AccessPlan: plan = ( await self.session.execute( select(AccessPlan).where(AccessPlan.code == seed.code) ) ).scalar_one_or_none() values = dict( name=seed.name, description=seed.description, plan_type=PlanType(seed.plan_type), billing_period=BillingPeriod(seed.billing_period), member_fee=seed.member_fee, module_fee=seed.module_fee, currency_code=seed.currency_code, country=seed.country, trial_days=seed.trial_days, is_active=seed.is_active, sort_order=seed.sort_order, metadata_json=dict(seed.metadata), ) if plan is None: plan = AccessPlan( code=seed.code, **values, ) self.session.add(plan) else: for key, value in values.items(): setattr(plan, key, value) await self.session.flush() return plan async def reconcile_plan_modules( self, *, plan: AccessPlan, module_codes: tuple[str, ...], ) -> int: modules = {} for code in module_codes: module = ( await self.session.execute( select(Module).where(Module.code == code) ) ).scalar_one_or_none() if module is None: raise ValueError( f"Unknown content module code for plan {plan.code}: {code}" ) modules[code] = module existing = ( await self.session.execute( select(AccessPlanModule).where(AccessPlanModule.plan_id == plan.id) ) ).scalars().all() existing_by_module_id = {row.module_id: row for row in existing} desired_module_ids = {module.id for module in modules.values()} stale_ids = set(existing_by_module_id) - desired_module_ids if stale_ids: await self.session.execute( delete(AccessPlanModule).where( AccessPlanModule.plan_id == plan.id, AccessPlanModule.module_id.in_(stale_ids), ) ) for module in modules.values(): if module.id not in existing_by_module_id: self.session.add( AccessPlanModule(plan_id=plan.id, module_id=module.id) ) await self.session.flush() return len(desired_module_ids) async def upsert_coupon(self, seed) -> Coupon: coupon = ( await self.session.execute( select(Coupon).where(Coupon.code == seed.code) ) ).scalar_one_or_none() values = dict( discount_type=seed.discount_type, coupon_category=seed.coupon_category, value=seed.value, max_discount_limit=seed.max_discount_limit, start_date=seed.start_date, end_date=seed.end_date, usage_limit=seed.usage_limit, applicable_plan_ids=( list(seed.applicable_plan_ids) if seed.applicable_plan_ids else None ), applicable_module_ids=( list(seed.applicable_module_ids) if seed.applicable_module_ids else None ), min_purchase_amount=seed.min_purchase_amount, is_first_purchase_only=seed.is_first_purchase_only, is_active=seed.is_active, description=seed.description, created_by=seed.created_by, ) if coupon is None: coupon = Coupon( code=seed.code, **values, ) self.session.add(coupon) else: for key, value in values.items(): setattr(coupon, key, value) await self.session.flush() return coupon