"""Admin CLI — runs inside the `app` container. Usage from the host:: docker compose exec app python -m app.cli grant-credit docker compose exec app python -m app.cli revoke-credit docker compose exec app python -m app.cli show-status `grant-credit` is idempotent: it extends `users.credit_until` from ``max(now, current_credit_until)``, so granting "1 month" twice gives two months, not one (avoids accidental erosion of an existing grant when re-running the command). This is the manual lever for admin grants. The Stripe webhook applies the same stacking rule via ``referral_service.convert_referral`` for both sides of a referral conversion. """ from __future__ import annotations import argparse import asyncio import sys from datetime import datetime, timedelta, timezone from sqlalchemy import select from app.db import get_engine, get_session_factory from app.models import User from app.services.access import _aware, paid_status def _utcnow() -> datetime: return datetime.now(timezone.utc) async def _get_user_by_email(session, email: str) -> User | None: return (await session.execute( select(User).where(User.email == email) )).scalar_one_or_none() async def grant_credit(email: str, months: float) -> int: if months <= 0: print(f"error: months must be positive (got {months})", file=sys.stderr) return 2 factory = get_session_factory() async with factory() as session: user = await _get_user_by_email(session, email) if user is None: print(f"error: no user with email {email!r}", file=sys.stderr) return 1 anchor = max(_utcnow(), _aware(user.credit_until) or _utcnow()) # 30-day months — simple, predictable, no calendar arithmetic. days = int(round(months * 30)) new_expiry = anchor + timedelta(days=days) user.credit_until = new_expiry await session.commit() # Refresh status snapshot from the just-committed value. st = paid_status(user) print( f"granted {months} month(s) to {email}: " f"credit_until={new_expiry.isoformat()} " f"(~{st.days_remaining} days remaining)" ) return 0 async def revoke_credit(email: str) -> int: factory = get_session_factory() async with factory() as session: user = await _get_user_by_email(session, email) if user is None: print(f"error: no user with email {email!r}", file=sys.stderr) return 1 user.credit_until = None await session.commit() print(f"revoked: credit_until cleared for {email}") return 0 async def show_status(email: str) -> int: factory = get_session_factory() async with factory() as session: user = await _get_user_by_email(session, email) if user is None: print(f"error: no user with email {email!r}", file=sys.stderr) return 1 st = paid_status(user) print(f"email: {user.email}") print(f"tier: {user.tier}") print(f"credit_until: {user.credit_until or '—'}") print(f"paid active: {st.active} (source={st.source or '—'})") if st.expires_at: print(f"expires in: {st.days_remaining} days") return 0 async def send_test_digest(email: str, kind: str) -> int: """Generate a digest and send it to the named user immediately, ignoring opt-in state and idempotency. Useful for previewing copy in your own inbox before a real run lands.""" import httpx from app.jobs._market_context import ( REFERENCE_LINE, latest_quotes_by_group, recent_headlines_by_bucket, ) from app.jobs.email_digest_job import _generate_variants, _send_one from app.services.openrouter import llm_configured if kind not in ("daily", "weekly"): print(f"error: kind must be 'daily' or 'weekly' (got {kind!r})", file=sys.stderr) return 2 if not llm_configured(): print("error: LLM provider not configured (set OPENROUTER_API_KEY)", file=sys.stderr) return 1 factory = get_session_factory() async with factory() as session: user = await _get_user_by_email(session, email) if user is None: print(f"error: no user with email {email!r}", file=sys.stderr) return 1 today = _utcnow() quotes = await latest_quotes_by_group(session) news = await recent_headlines_by_bucket( session, hours=(168 if kind == "weekly" else 24), ) ctx = dict(today=today, quotes_by_group=quotes, headlines_by_bucket=news, reference_line=REFERENCE_LINE) async with httpx.AsyncClient(follow_redirects=True) as client: variants = await _generate_variants(session, client, kind, ctx) tone = (user.digest_tone or "INTERMEDIATE").upper() content = (variants.get(tone) or variants.get("INTERMEDIATE") or next(iter(variants.values()), None)) if content is None: print("error: all LLM variants failed", file=sys.stderr) return 1 date_str = today.strftime("%Y-%m-%d") await _send_one(user, kind, content, date_str, session) print(f"sent {kind} digest to {email} (tone={tone})") return 0 # The genuinely-real accounts. Everything else in the users table is a # leftover from smoke/E2E signup tests that were pointed at the live site # (OTP delivered to the @gilest.ro catch-all). Kept here so the purge is an # explicit allow-list — deny by default — rather than pattern-matching on # test prefixes that a future test run might not follow. DEFAULT_KEEP_EMAILS = ( "giorgio@gilest.ro", "giorgio.gilestro@gmail.com", "ilariodamato@hotmail.com", ) async def purge_test_users(keep: list[str], commit: bool) -> int: """Delete every user whose email is not in `keep`, cascading to child rows. Dry-run by default: prints exactly what would go and changes nothing unless `commit` is True. Deletes child rows explicitly (rather than leaning on DB-level ON DELETE CASCADE) so behaviour is identical on MariaDB and the sqlite test DB. """ from sqlalchemy import delete, or_, update from app.models import ( EmailOTP, EmailSend, PortfolioSync, Referral, StrategicLogFeedback, User, UserAcknowledgement, ) keep_set = {e.strip().lower() for e in keep if e.strip()} factory = get_session_factory() async with factory() as session: rows = (await session.execute( select(User.id, User.email, User.tier, User.created_at) .order_by(User.id) )).all() victims = [r for r in rows if (r.email or "").lower() not in keep_set] kept = [r for r in rows if (r.email or "").lower() in keep_set] print(f"users total: {len(rows)} keep: {len(kept)} " f"to delete: {len(victims)}") print("\nKEEP:") for r in kept: print(f" [{r.id}] {r.email} ({r.tier})") print("\nDELETE:" if victims else "\nDELETE: (none)") for r in victims: print(f" [{r.id}] {r.email} ({r.tier}) {r.created_at}") if not victims: print("\nnothing to do.") return 0 if not commit: print(f"\nDRY RUN — nothing deleted. " f"Re-run with --commit to remove these {len(victims)} " f"account(s).") return 0 ids = [r.id for r in victims] emails = [r.email for r in victims if r.email] # Child rows first (explicit; order matters without cascade). await session.execute(delete(StrategicLogFeedback) .where(StrategicLogFeedback.user_id.in_(ids))) await session.execute(delete(EmailSend) .where(EmailSend.user_id.in_(ids))) await session.execute(delete(PortfolioSync) .where(PortfolioSync.user_id.in_(ids))) await session.execute(delete(UserAcknowledgement) .where(UserAcknowledgement.user_id.in_(ids))) await session.execute(delete(Referral).where(or_( Referral.referrer_user_id.in_(ids), Referral.referred_user_id.in_(ids), ))) # Self-referential FK on survivors that pointed at a victim. await session.execute(update(User) .where(User.referred_by_user_id.in_(ids)) .values(referred_by_user_id=None)) # OTPs are keyed by email, not user_id — no FK to cascade. if emails: await session.execute(delete(EmailOTP) .where(EmailOTP.email.in_(emails))) result = await session.execute(delete(User).where(User.id.in_(ids))) await session.commit() print(f"\ndeleted {result.rowcount} user(s) and their child rows.") return 0 def build_parser() -> argparse.ArgumentParser: p = argparse.ArgumentParser(prog="app.cli", description="Cassandra admin CLI") sub = p.add_subparsers(dest="cmd", required=True) g = sub.add_parser("grant-credit", help="Extend a user's paid-credit window") g.add_argument("email") g.add_argument("months", type=float) r = sub.add_parser("revoke-credit", help="Clear a user's credit_until") r.add_argument("email") s = sub.add_parser("show-status", help="Print paid-tier status for a user") s.add_argument("email") t = sub.add_parser("send-test-digest", help="Send one digest immediately (bypasses opt-in/idempotency)") t.add_argument("email") t.add_argument("kind", choices=("daily", "weekly")) pu = sub.add_parser( "purge-test-users", help="Delete all users except an allow-list (dry-run unless --commit)") pu.add_argument( "--keep", action="append", metavar="EMAIL", default=None, help="Email to keep; repeatable. Defaults to the known real accounts.") pu.add_argument("--commit", action="store_true", help="Actually delete. Without this it is a dry run.") return p async def _dispatch(args) -> int: """Run the chosen sub-command, then dispose the async engine cleanly so aiomysql's __del__ doesn't squawk at interpreter shutdown about a closed event loop.""" try: if args.cmd == "grant-credit": return await grant_credit(args.email, args.months) if args.cmd == "revoke-credit": return await revoke_credit(args.email) if args.cmd == "show-status": return await show_status(args.email) if args.cmd == "send-test-digest": return await send_test_digest(args.email, args.kind) if args.cmd == "purge-test-users": keep = args.keep if args.keep else list(DEFAULT_KEEP_EMAILS) return await purge_test_users(keep, args.commit) return 2 finally: await get_engine().dispose() def main(argv: list[str] | None = None) -> int: args = build_parser().parse_args(argv) return asyncio.run(_dispatch(args)) if __name__ == "__main__": sys.exit(main())