192 lines
6.0 KiB
Python
192 lines
6.0 KiB
Python
"""
|
|
Subscription Plans Management Routes
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from app.models import db, SubscriptionPlan, AuditLog
|
|
from app.routes.auth import token_required
|
|
|
|
plans_bp = Blueprint('plans', __name__)
|
|
|
|
@plans_bp.route('', methods=['GET'])
|
|
@token_required
|
|
def get_plans(current_admin):
|
|
"""Get all subscription plans"""
|
|
try:
|
|
plans = SubscriptionPlan.query.order_by(SubscriptionPlan.sort_order).all()
|
|
return jsonify({
|
|
'status': 'success',
|
|
'plans': [plan.to_dict() for plan in plans]
|
|
}), 200
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@plans_bp.route('/<int:plan_id>', methods=['GET'])
|
|
@token_required
|
|
def get_plan(current_admin, plan_id):
|
|
"""Get single plan"""
|
|
try:
|
|
plan = SubscriptionPlan.query.get(plan_id)
|
|
if not plan:
|
|
return jsonify({'error': 'Plan not found'}), 404
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'plan': plan.to_dict()
|
|
}), 200
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@plans_bp.route('', methods=['POST'])
|
|
@token_required
|
|
def create_plan(current_admin):
|
|
"""Create new subscription plan"""
|
|
try:
|
|
data = request.get_json()
|
|
|
|
# Validate required fields
|
|
required = ['name', 'slug']
|
|
for field in required:
|
|
if not data.get(field):
|
|
return jsonify({'error': f'{field} is required'}), 400
|
|
|
|
# Check if slug already exists
|
|
if SubscriptionPlan.query.filter_by(slug=data['slug']).first():
|
|
return jsonify({'error': 'Plan with this slug already exists'}), 400
|
|
|
|
plan = SubscriptionPlan(
|
|
name=data['name'],
|
|
slug=data['slug'],
|
|
description=data.get('description'),
|
|
price_monthly=data.get('price_monthly', 0),
|
|
price_yearly=data.get('price_yearly', 0),
|
|
max_domains=data.get('max_domains', 1),
|
|
max_containers=data.get('max_containers', 1),
|
|
max_storage_gb=data.get('max_storage_gb', 10),
|
|
max_bandwidth_gb=data.get('max_bandwidth_gb', 100),
|
|
features=data.get('features', []),
|
|
is_active=data.get('is_active', True),
|
|
is_visible=data.get('is_visible', True),
|
|
sort_order=data.get('sort_order', 0)
|
|
)
|
|
|
|
db.session.add(plan)
|
|
db.session.commit()
|
|
|
|
# Log action
|
|
log = AuditLog(
|
|
admin_id=current_admin.id,
|
|
action='create_plan',
|
|
resource_type='plan',
|
|
resource_id=plan.id,
|
|
details={'plan_name': plan.name},
|
|
ip_address=request.remote_addr
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': 'Plan created successfully',
|
|
'plan': plan.to_dict()
|
|
}), 201
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@plans_bp.route('/<int:plan_id>', methods=['PUT'])
|
|
@token_required
|
|
def update_plan(current_admin, plan_id):
|
|
"""Update subscription plan"""
|
|
try:
|
|
plan = SubscriptionPlan.query.get(plan_id)
|
|
if not plan:
|
|
return jsonify({'error': 'Plan not found'}), 404
|
|
|
|
data = request.get_json()
|
|
|
|
# Update fields
|
|
if 'name' in data:
|
|
plan.name = data['name']
|
|
if 'description' in data:
|
|
plan.description = data['description']
|
|
if 'price_monthly' in data:
|
|
plan.price_monthly = data['price_monthly']
|
|
if 'price_yearly' in data:
|
|
plan.price_yearly = data['price_yearly']
|
|
if 'max_domains' in data:
|
|
plan.max_domains = data['max_domains']
|
|
if 'max_containers' in data:
|
|
plan.max_containers = data['max_containers']
|
|
if 'max_storage_gb' in data:
|
|
plan.max_storage_gb = data['max_storage_gb']
|
|
if 'max_bandwidth_gb' in data:
|
|
plan.max_bandwidth_gb = data['max_bandwidth_gb']
|
|
if 'features' in data:
|
|
plan.features = data['features']
|
|
if 'is_active' in data:
|
|
plan.is_active = data['is_active']
|
|
if 'is_visible' in data:
|
|
plan.is_visible = data['is_visible']
|
|
if 'sort_order' in data:
|
|
plan.sort_order = data['sort_order']
|
|
|
|
db.session.commit()
|
|
|
|
# Log action
|
|
log = AuditLog(
|
|
admin_id=current_admin.id,
|
|
action='update_plan',
|
|
resource_type='plan',
|
|
resource_id=plan.id,
|
|
details={'plan_name': plan.name},
|
|
ip_address=request.remote_addr
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': 'Plan updated successfully',
|
|
'plan': plan.to_dict()
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@plans_bp.route('/<int:plan_id>', methods=['DELETE'])
|
|
@token_required
|
|
def delete_plan(current_admin, plan_id):
|
|
"""Delete subscription plan"""
|
|
try:
|
|
plan = SubscriptionPlan.query.get(plan_id)
|
|
if not plan:
|
|
return jsonify({'error': 'Plan not found'}), 404
|
|
|
|
plan_name = plan.name
|
|
db.session.delete(plan)
|
|
db.session.commit()
|
|
|
|
# Log action
|
|
log = AuditLog(
|
|
admin_id=current_admin.id,
|
|
action='delete_plan',
|
|
resource_type='plan',
|
|
resource_id=plan_id,
|
|
details={'plan_name': plan_name},
|
|
ip_address=request.remote_addr
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': 'Plan deleted successfully'
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return jsonify({'error': str(e)}), 500
|
|
|