Spaces:
Runtime error
Runtime error
Commit ·
0af87ef
1
Parent(s): 1b5947e
fix(tier): require active, non-revoked, and non-expired subscription for pro/vip tiers
Browse files609 unmodified lines610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656129 unmodified lines786787788789790791792793794795796797798799800801802803804805806807808809609 unmodified linesif tier and tier.strip() and tier.strip().upper() != "ALL":t = tier.strip().upper().replace(" ", "_")if t in ("20_ADFREE", "AD_FREE", "ADFREE", "20"):tier_clause = {"$or": [{"is_ad_free_admob": True},{"subscription.plan_id": "sahih_ad_free_monthly"},{"subscription.amount": 20.0},{"subscription.tier": "AD_FREE"},]}elif t in ("159_PRO", "PRO_MONTHLY", "159"):tier_clause = {"$or": [{"subscription.plan_id": "sahih_pro_monthly"},{"subscription.amount": 159.0},{"subscription.tier": "159_PRO"},]}elif t in ("800_PRO", "PRO_6MONTH", "800"):tier_clause = {"$or": [{"subscription.plan_id": "sahih_pro_6month"},{"subscription.amount": 800.0},{"subscription.tier": "800_PRO"},]}elif t in ("1500_PRO", "PRO_ANNUAL", "1500"):tier_clause = {"$or": [{"subscription.plan_id": "sahih_pro_annual"},{"subscription.amount": 1500.0},{"subscription.tier": "1500_PRO"},]}elif t in ("VIP_PRO", "VIP", "VIP_OFFER"):tier_clause = {"$or": [{"subscription.tag": "VIP offer"},{"subscription.payment_method": "VIP offer"},{"subscription.plan_id": {"$in": ["vip_pro", "vip_offer", "pro_lifetime"]}},{"subscription.tier": "VIP_PRO"},{"is_vip": True},]}else:129 unmodified lines# Canonical tier resolution across the 5 tiers:# 20 adfree, 159 pro, 800 pro, 1500 pro, vip pro (or FREE)sub = u.get("subscription") if isinstance(u.get("subscription"), dict) else {}plan_id = str(sub.get("plan_id") or "").lower()tag = str(sub.get("tag") or "").lower()pay_method = str(sub.get("payment_method") or "").lower()tier_val = str(sub.get("tier") or "").upper()amt = float(sub.get("amount") or u.get("amount") or 0.0)is_pro = bool(u.get("is_pro") or sub.get("is_pro", False))is_ad_free = bool(u.get("is_ad_free_admob") or "ad_free" in plan_id or tier_val == "AD_FREE")resolved_tier = "FREE"if "vip" in tag or "vip" in pay_method or "vip" in plan_id or u.get("is_vip") or tier_val in ("VIP", "VIP_PRO"):resolved_tier = "VIP_PRO"elif plan_id == "sahih_pro_annual" or "annual" in plan_id or amt == 1500.0 or tier_val in ("1500_PRO", "ANNUAL"):resolved_tier = "1500_PRO"elif plan_id == "sahih_pro_6month" or "6month" in plan_id or amt == 800.0 or tier_val in ("800_PRO",):resolved_tier = "800_PRO"elif plan_id == "sahih_pro_monthly" or amt == 159.0 or tier_val in ("159_PRO", "PRO") or is_pro:resolved_tier = "159_PRO"elif is_ad_free or plan_id == "sahih_ad_free_monthly" or amt == 20.0 or tier_val == "AD_FREE":resolved_tier = "20_ADFREE"u["tier"] = resolved_tier609 unmodified lines610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706129 unmodified lines836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882609 unmodified linesif tier and tier.strip() and tier.strip().upper() != "ALL":t = tier.strip().upper().replace(" ", "_")now_dt = datetime.datetime.utcnow()not_revoked_clause = {"subscription.status": {"$nin": ["REVOKED", "CANCELLED", "EXPIRED", "revoked", "cancelled", "expired"]}}not_expired_clause = {"$or": [{"subscription.expires_at": None},{"subscription.expires_at": {"$gte": now_dt}},{"subscription.expires_at": {"$exists": False}},]}pro_active_clause = {"$or": [{"is_pro": True}, {"subscription.is_pro": True}]}if t in ("20_ADFREE", "AD_FREE", "ADFREE", "20"):tier_clause = {"$and": [not_revoked_clause,not_expired_clause,{"$or": [{"is_ad_free_admob": True},{"subscription.plan_id": "sahih_ad_free_monthly"},{"subscription.amount": 20.0},{"subscription.tier": "AD_FREE"},]}]}elif t in ("VIP_PRO", "VIP", "VIP_OFFER"):tier_clause = {"$and": [pro_active_clause,not_revoked_clause,not_expired_clause,{"$or": [{"subscription.tag": "VIP offer"},{"subscription.payment_method": "VIP offer"},{"subscription.plan_id": {"$in": ["vip_pro", "vip_offer", "pro_lifetime"]}},{"subscription.tier": "VIP_PRO"},{"is_vip": True},]}]}elif t in ("1500_PRO", "PRO_ANNUAL", "1500"):tier_clause = {"$and": [pro_active_clause,not_revoked_clause,not_expired_clause,{"$or": [{"subscription.plan_id": "sahih_pro_annual"},{"subscription.amount": 1500.0},{"subscription.tier": "1500_PRO"},]}]}elif t in ("800_PRO", "PRO_6MONTH", "800"):tier_clause = {"$and": [pro_active_clause,not_revoked_clause,not_expired_clause,{"$or": [{"subscription.plan_id": "sahih_pro_6month"},{"subscription.amount": 800.0},{"subscription.tier": "800_PRO"},]}]}elif t in ("159_PRO", "PRO_MONTHLY", "159"):tier_clause = {"$and": [pro_active_clause,not_revoked_clause,not_expired_clause,{"$or": [{"subscription.plan_id": "sahih_pro_monthly"},{"subscription.amount": 159.0},{"subscription.tier": "159_PRO"},{"subscription.tier": "PRO"},]}]}else:129 unmodified lines# Canonical tier resolution across the 5 tiers:# 20 adfree, 159 pro, 800 pro, 1500 pro, vip pro (or FREE)sub = u.get("subscription") if isinstance(u.get("subscription"), dict) else {}sub_status = str(sub.get("status") or "").upper()is_revoked = sub_status in ("REVOKED", "CANCELLED", "EXPIRED")is_expired = Falseexpires_at = sub.get("expires_at")if expires_at:try:if isinstance(expires_at, str):exp_dt = datetime.datetime.fromisoformat(expires_at.replace("Z", "+00:00"))elif isinstance(expires_at, datetime.datetime):exp_dt = expires_atelse:exp_dt = Noneif exp_dt:if exp_dt.tzinfo is not None:exp_dt = exp_dt.astimezone(datetime.timezone.utc).replace(tzinfo=None)if exp_dt < datetime.datetime.utcnow():is_expired = Trueexcept Exception:passis_pro_active = (bool(u.get("is_pro")) or bool(sub.get("is_pro"))) and not is_revoked and not is_expiredis_ad_free_active = bool(u.get("is_ad_free_admob")) and not is_revoked and not is_expiredplan_id = str(sub.get("plan_id") or "").lower()tag = str(sub.get("tag") or "").lower()pay_method = str(sub.get("payment_method") or "").lower()tier_val = str(sub.get("tier") or "").upper()amt = float(sub.get("amount") or u.get("amount") or 0.0)resolved_tier = "FREE"if is_pro_active:if "vip" in tag or "vip" in pay_method or "vip" in plan_id or u.get("is_vip") or tier_val in ("VIP", "VIP_PRO"):resolved_tier = "VIP_PRO"elif plan_id == "sahih_pro_annual" or "annual" in plan_id or amt == 1500.0 or tier_val in ("1500_PRO", "ANNUAL"):resolved_tier = "1500_PRO"elif plan_id == "sahih_pro_6month" or "6month" in plan_id or amt == 800.0 or tier_val in ("800_PRO",):resolved_tier = "800_PRO"else:resolved_tier = "159_PRO"elif is_ad_free_active or (("ad_free" in plan_id or amt == 20.0 or tier_val == "AD_FREE") and not is_revoked and not is_expired):resolved_tier = "20_ADFREE"u["tier"] = resolved_tier
app/superadmin_routes.py+111-38