From a4ee01439774d1eabd3e10e917d281f19a394dd9 Mon Sep 17 00:00:00 2001 From: oguz ozturk Date: Mon, 12 Jan 2026 17:59:06 +0300 Subject: [PATCH] feat: Add customer management endpoints (edit, delete, suspend, activate) --- backend/app/routes/admin.py | 228 ++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) diff --git a/backend/app/routes/admin.py b/backend/app/routes/admin.py index 377e34c..f141c0a 100644 --- a/backend/app/routes/admin.py +++ b/backend/app/routes/admin.py @@ -539,6 +539,234 @@ def update_customer_plan(customer_id): }), 500 +@admin_bp.route('/customers/', methods=['PUT']) +def update_customer(customer_id): + """Müşteri bilgilerini güncelle""" + try: + data = request.json + customer = Customer.query.get(customer_id) + + if not customer: + return jsonify({ + "status": "error", + "message": "Müşteri bulunamadı" + }), 404 + + user = User.query.get(customer.user_id) + + # Update user fields + if 'full_name' in data: + user.full_name = data['full_name'] + if 'email' in data: + # Check if email already exists + existing = User.query.filter(User.email == data['email'], User.id != user.id).first() + if existing: + return jsonify({ + "status": "error", + "message": "Bu email adresi zaten kullanılıyor" + }), 400 + user.email = data['email'] + + # Update customer fields + if 'company_name' in data: + customer.company_name = data['company_name'] + if 'phone' in data: + customer.phone = data['phone'] + if 'billing_address' in data: + customer.billing_address = data['billing_address'] + if 'billing_city' in data: + customer.billing_city = data['billing_city'] + if 'billing_country' in data: + customer.billing_country = data['billing_country'] + if 'billing_postal_code' in data: + customer.billing_postal_code = data['billing_postal_code'] + + db.session.commit() + + return jsonify({ + "status": "success", + "message": "Müşteri bilgileri başarıyla güncellendi", + "customer": { + **user.to_dict(), + **customer.to_dict() + } + }) + + except Exception as e: + db.session.rollback() + return jsonify({ + "status": "error", + "message": f"Müşteri güncellenirken hata: {str(e)}" + }), 500 + + +@admin_bp.route('/customers/', methods=['DELETE']) +def delete_customer(customer_id): + """Müşteriyi sil (tüm domainleri ile birlikte)""" + try: + customer = Customer.query.get(customer_id) + + if not customer: + return jsonify({ + "status": "error", + "message": "Müşteri bulunamadı" + }), 404 + + user = User.query.get(customer.user_id) + + # Get all domains + domains = Domain.query.filter_by(customer_id=customer.id).all() + domain_count = len(domains) + + # Delete all domains and their DNS records + from app.models.domain import DNSRecord + for domain in domains: + # Delete DNS records + DNSRecord.query.filter_by(domain_id=domain.id).delete() + # Delete domain + db.session.delete(domain) + + # Delete customer + db.session.delete(customer) + + # Delete user + db.session.delete(user) + + db.session.commit() + + return jsonify({ + "status": "success", + "message": f"Müşteri ve {domain_count} domain başarıyla silindi" + }) + + except Exception as e: + db.session.rollback() + return jsonify({ + "status": "error", + "message": f"Müşteri silinirken hata: {str(e)}" + }), 500 + + +@admin_bp.route('/customers//suspend', methods=['POST']) +def suspend_customer(customer_id): + """Müşteriyi askıya al (suspend)""" + try: + customer = Customer.query.get(customer_id) + + if not customer: + return jsonify({ + "status": "error", + "message": "Müşteri bulunamadı" + }), 404 + + user = User.query.get(customer.user_id) + + # Suspend user and subscription + user.is_active = False + customer.subscription_status = 'suspended' + + # Optionally suspend all domains + Domain.query.filter_by(customer_id=customer.id).update({ + 'status': 'suspended' + }) + + db.session.commit() + + return jsonify({ + "status": "success", + "message": "Müşteri başarıyla askıya alındı" + }) + + except Exception as e: + db.session.rollback() + return jsonify({ + "status": "error", + "message": f"Müşteri askıya alınırken hata: {str(e)}" + }), 500 + + +@admin_bp.route('/customers//activate', methods=['POST']) +def activate_customer(customer_id): + """Müşteriyi aktif et""" + try: + customer = Customer.query.get(customer_id) + + if not customer: + return jsonify({ + "status": "error", + "message": "Müşteri bulunamadı" + }), 404 + + user = User.query.get(customer.user_id) + + # Activate user and subscription + user.is_active = True + customer.subscription_status = 'active' + + # Optionally activate all domains + Domain.query.filter_by(customer_id=customer.id, status='suspended').update({ + 'status': 'active' + }) + + db.session.commit() + + return jsonify({ + "status": "success", + "message": "Müşteri başarıyla aktif edildi" + }) + + except Exception as e: + db.session.rollback() + return jsonify({ + "status": "error", + "message": f"Müşteri aktif edilirken hata: {str(e)}" + }), 500 + + +@admin_bp.route('/customers//plan', methods=['PUT']) +def update_customer_plan(customer_id): + """Müşteri paketini güncelle""" + try: + data = request.json + customer = Customer.query.get(customer_id) + + if not customer: + return jsonify({ + "status": "error", + "message": "Müşteri bulunamadı" + }), 404 + + # Update subscription plan + if 'subscription_plan' in data: + customer.subscription_plan = data['subscription_plan'] + + # Update limits + if 'max_domains' in data: + customer.max_domains = data['max_domains'] + if 'max_containers' in data: + customer.max_containers = data['max_containers'] + + # Update subscription dates + if 'subscription_expires' in data: + from datetime import datetime + customer.subscription_expires = datetime.fromisoformat(data['subscription_expires']) + + db.session.commit() + + return jsonify({ + "status": "success", + "message": "Paket başarıyla güncellendi", + "customer": customer.to_dict() + }) + + except Exception as e: + db.session.rollback() + return jsonify({ + "status": "error", + "message": f"Paket güncellenirken hata: {str(e)}" + }), 500 + + @admin_bp.route('/customers//status', methods=['PUT']) def update_customer_status(customer_id): """Müşteri durumunu güncelle (aktif/pasif)"""