2025-10-18 16:03:23 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
2025-10-17 18:14:28 +02:00
|
|
|
"""
|
|
|
|
|
Seed script to create the PILOT2025 coupon for the pilot customer program.
|
|
|
|
|
This coupon provides 3 months (90 days) free trial extension for the first 20 customers.
|
2025-10-18 16:03:23 +02:00
|
|
|
|
|
|
|
|
This script runs as a Kubernetes job inside the tenant-service container.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
python /app/services/tenant/scripts/seed_pilot_coupon.py
|
|
|
|
|
|
|
|
|
|
Environment Variables Required:
|
|
|
|
|
TENANT_DATABASE_URL - PostgreSQL connection string for tenant database
|
|
|
|
|
LOG_LEVEL - Logging level (default: INFO)
|
2025-10-17 18:14:28 +02:00
|
|
|
"""
|
2025-10-18 16:03:23 +02:00
|
|
|
|
|
|
|
|
import asyncio
|
2025-10-17 18:14:28 +02:00
|
|
|
import sys
|
|
|
|
|
import os
|
2025-10-18 16:03:23 +02:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
from pathlib import Path
|
2025-10-17 18:14:28 +02:00
|
|
|
import uuid
|
|
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
# Add app to path
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
import structlog
|
2025-10-17 18:14:28 +02:00
|
|
|
|
|
|
|
|
from app.models.coupon import CouponModel
|
|
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
# Configure logging
|
|
|
|
|
structlog.configure(
|
|
|
|
|
processors=[
|
|
|
|
|
structlog.stdlib.add_log_level,
|
|
|
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
|
|
|
structlog.dev.ConsoleRenderer()
|
|
|
|
|
]
|
|
|
|
|
)
|
2025-10-17 18:14:28 +02:00
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
logger = structlog.get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def seed_pilot_coupon(db: AsyncSession):
|
2025-10-17 18:14:28 +02:00
|
|
|
"""Create or update the PILOT2025 coupon"""
|
|
|
|
|
|
|
|
|
|
coupon_code = "PILOT2025"
|
|
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
logger.info("=" * 80)
|
|
|
|
|
logger.info("🎫 Seeding PILOT2025 Coupon")
|
|
|
|
|
logger.info("=" * 80)
|
|
|
|
|
|
2025-10-17 18:14:28 +02:00
|
|
|
# Check if coupon already exists
|
2025-10-18 16:03:23 +02:00
|
|
|
result = await db.execute(
|
|
|
|
|
select(CouponModel).where(CouponModel.code == coupon_code)
|
|
|
|
|
)
|
|
|
|
|
existing_coupon = result.scalars().first()
|
2025-10-17 18:14:28 +02:00
|
|
|
|
|
|
|
|
if existing_coupon:
|
2025-10-18 16:03:23 +02:00
|
|
|
logger.info(
|
|
|
|
|
"Coupon already exists",
|
|
|
|
|
code=coupon_code,
|
|
|
|
|
current_redemptions=existing_coupon.current_redemptions,
|
|
|
|
|
max_redemptions=existing_coupon.max_redemptions,
|
|
|
|
|
active=existing_coupon.active,
|
|
|
|
|
valid_from=existing_coupon.valid_from,
|
|
|
|
|
valid_until=existing_coupon.valid_until
|
|
|
|
|
)
|
2025-10-17 18:14:28 +02:00
|
|
|
return existing_coupon
|
|
|
|
|
|
|
|
|
|
# Create new coupon
|
2025-10-18 16:03:23 +02:00
|
|
|
now = datetime.now(timezone.utc)
|
2025-10-17 18:14:28 +02:00
|
|
|
valid_until = now + timedelta(days=180) # Valid for 6 months
|
|
|
|
|
|
|
|
|
|
coupon = CouponModel(
|
|
|
|
|
id=uuid.uuid4(),
|
|
|
|
|
code=coupon_code,
|
|
|
|
|
discount_type="trial_extension",
|
|
|
|
|
discount_value=90, # 90 days = 3 months
|
|
|
|
|
max_redemptions=20, # First 20 pilot customers
|
|
|
|
|
current_redemptions=0,
|
|
|
|
|
valid_from=now,
|
|
|
|
|
valid_until=valid_until,
|
|
|
|
|
active=True,
|
|
|
|
|
created_at=now,
|
|
|
|
|
extra_data={
|
|
|
|
|
"program": "pilot_launch_2025",
|
|
|
|
|
"description": "Programa piloto - 3 meses gratis para los primeros 20 clientes",
|
|
|
|
|
"terms": "Válido para nuevos registros únicamente. Un cupón por cliente."
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.add(coupon)
|
2025-10-18 16:03:23 +02:00
|
|
|
await db.commit()
|
|
|
|
|
await db.refresh(coupon)
|
2025-10-17 18:14:28 +02:00
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
logger.info("=" * 80)
|
|
|
|
|
logger.info(
|
|
|
|
|
"✅ Successfully created coupon",
|
|
|
|
|
code=coupon_code,
|
|
|
|
|
type="Trial Extension",
|
|
|
|
|
value="90 days (3 months)",
|
|
|
|
|
max_redemptions=20,
|
|
|
|
|
valid_from=coupon.valid_from,
|
|
|
|
|
valid_until=coupon.valid_until,
|
|
|
|
|
id=str(coupon.id)
|
|
|
|
|
)
|
|
|
|
|
logger.info("=" * 80)
|
2025-10-17 18:14:28 +02:00
|
|
|
|
|
|
|
|
return coupon
|
|
|
|
|
|
|
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
async def main():
|
2025-10-17 18:14:28 +02:00
|
|
|
"""Main execution function"""
|
2025-10-18 16:03:23 +02:00
|
|
|
|
|
|
|
|
logger.info("Pilot Coupon Seeding Script Starting")
|
|
|
|
|
logger.info("Log Level: %s", os.getenv("LOG_LEVEL", "INFO"))
|
|
|
|
|
|
|
|
|
|
# Get database URL from environment
|
|
|
|
|
database_url = os.getenv("TENANT_DATABASE_URL") or os.getenv("DATABASE_URL")
|
|
|
|
|
if not database_url:
|
|
|
|
|
logger.error("❌ TENANT_DATABASE_URL or DATABASE_URL environment variable must be set")
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
# Convert to async URL if needed
|
|
|
|
|
if database_url.startswith("postgresql://"):
|
|
|
|
|
database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
|
|
|
|
|
|
|
|
|
|
logger.info("Connecting to tenant database")
|
|
|
|
|
|
|
|
|
|
# Create engine and session
|
|
|
|
|
engine = create_async_engine(
|
|
|
|
|
database_url,
|
|
|
|
|
echo=False,
|
|
|
|
|
pool_pre_ping=True,
|
|
|
|
|
pool_size=5,
|
|
|
|
|
max_overflow=10
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async_session = sessionmaker(
|
|
|
|
|
engine,
|
|
|
|
|
class_=AsyncSession,
|
|
|
|
|
expire_on_commit=False
|
|
|
|
|
)
|
2025-10-17 18:14:28 +02:00
|
|
|
|
|
|
|
|
try:
|
2025-10-18 16:03:23 +02:00
|
|
|
async with async_session() as session:
|
|
|
|
|
await seed_pilot_coupon(session)
|
2025-10-17 18:14:28 +02:00
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
logger.info("")
|
|
|
|
|
logger.info("🎉 Success! PILOT2025 coupon is ready.")
|
|
|
|
|
logger.info("")
|
|
|
|
|
logger.info("Coupon Details:")
|
|
|
|
|
logger.info(" Code: PILOT2025")
|
|
|
|
|
logger.info(" Type: Trial Extension")
|
|
|
|
|
logger.info(" Value: 90 days (3 months)")
|
|
|
|
|
logger.info(" Max Redemptions: 20")
|
|
|
|
|
logger.info("")
|
2025-10-17 18:14:28 +02:00
|
|
|
|
2025-10-18 16:03:23 +02:00
|
|
|
return 0
|
2025-10-17 18:14:28 +02:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-10-18 16:03:23 +02:00
|
|
|
logger.error("=" * 80)
|
|
|
|
|
logger.error("❌ Pilot Coupon Seeding Failed")
|
|
|
|
|
logger.error("=" * 80)
|
|
|
|
|
logger.error("Error: %s", str(e))
|
|
|
|
|
logger.error("", exc_info=True)
|
|
|
|
|
return 1
|
|
|
|
|
|
2025-10-17 18:14:28 +02:00
|
|
|
finally:
|
2025-10-18 16:03:23 +02:00
|
|
|
await engine.dispose()
|
2025-10-17 18:14:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-10-18 16:03:23 +02:00
|
|
|
exit_code = asyncio.run(main())
|
|
|
|
|
sys.exit(exit_code)
|