feat: Add increment/decrement endpoints for CF account domain count

- Add POST /internal/{id}/increment endpoint
- Add POST /internal/{id}/decrement endpoint
- Prevent negative domain counts
- Return updated counts and available capacity
- Require internal API key authentication
This commit is contained in:
oguz ozturk 2026-01-12 17:13:57 +03:00
parent d68c4d38ff
commit 0092003a7f
1 changed files with 88 additions and 0 deletions

View File

@ -254,3 +254,91 @@ def get_cf_account_internal(account_id):
'error': str(e)
}), 500
@cf_accounts_bp.route('/internal/<int:account_id>/increment', methods=['POST'])
@internal_api_required
def increment_domain_count_internal(account_id):
"""
Internal API endpoint to increment domain count
Called when Customer Panel creates a domain using this CF account
Requires X-Internal-API-Key header
"""
try:
account = CloudflareAccount.query.get(account_id)
if not account:
return jsonify({
'status': 'error',
'error': 'Account not found'
}), 404
if not account.is_active:
return jsonify({
'status': 'error',
'error': 'Account is not active'
}), 400
# Check capacity
if account.current_domains >= account.max_domains:
return jsonify({
'status': 'error',
'error': f'Account is full ({account.current_domains}/{account.max_domains})'
}), 400
# Increment domain count
old_count = account.current_domains
account.current_domains += 1
db.session.commit()
return jsonify({
'status': 'success',
'message': 'Domain count incremented',
'account_id': account.id,
'old_count': old_count,
'new_count': account.current_domains,
'available_capacity': account.max_domains - account.current_domains
}), 200
except Exception as e:
db.session.rollback()
return jsonify({
'status': 'error',
'error': str(e)
}), 500
@cf_accounts_bp.route('/internal/<int:account_id>/decrement', methods=['POST'])
@internal_api_required
def decrement_domain_count_internal(account_id):
"""
Internal API endpoint to decrement domain count
Called when Customer Panel deletes a domain using this CF account
Requires X-Internal-API-Key header
"""
try:
account = CloudflareAccount.query.get(account_id)
if not account:
return jsonify({
'status': 'error',
'error': 'Account not found'
}), 404
# Decrement domain count (prevent negative)
old_count = account.current_domains
account.current_domains = max(0, account.current_domains - 1)
db.session.commit()
return jsonify({
'status': 'success',
'message': 'Domain count decremented',
'account_id': account.id,
'old_count': old_count,
'new_count': account.current_domains,
'available_capacity': account.max_domains - account.current_domains
}), 200
except Exception as e:
db.session.rollback()
return jsonify({
'status': 'error',
'error': str(e)
}), 500