From b8225044134820400047b1ef8edfd172bdaf1167 Mon Sep 17 00:00:00 2001 From: UserIsMH Date: Thu, 19 Jun 2025 18:10:48 +0200 Subject: [PATCH] Kontakte - Telefonnummern und E-Mail-Adressen Bearbeiten ist drin --- v2_adminpanel/leads/repositories.py | 29 ++++ v2_adminpanel/leads/routes.py | 18 +++ v2_adminpanel/leads/services.py | 21 +++ .../leads/templates/leads/contact_detail.html | 136 ++++++++++++++++-- 4 files changed, 196 insertions(+), 8 deletions(-) diff --git a/v2_adminpanel/leads/repositories.py b/v2_adminpanel/leads/repositories.py index a08141a..55123d4 100644 --- a/v2_adminpanel/leads/repositories.py +++ b/v2_adminpanel/leads/repositories.py @@ -213,6 +213,35 @@ class LeadRepository: return result + def get_contact_detail_by_id(self, detail_id: UUID) -> Optional[Dict[str, Any]]: + with self.get_db_connection() as conn: + cur = conn.cursor(cursor_factory=RealDictCursor) + + query = "SELECT * FROM lead_contact_details WHERE id = %s" + cur.execute(query, (str(detail_id),)) + result = cur.fetchone() + cur.close() + + return result + + def update_contact_detail(self, detail_id: UUID, detail_value: str, + detail_label: str = None) -> Dict[str, Any]: + with self.get_db_connection() as conn: + cur = conn.cursor(cursor_factory=RealDictCursor) + + query = """ + UPDATE lead_contact_details + SET detail_value = %s, detail_label = %s, updated_at = CURRENT_TIMESTAMP + WHERE id = %s + RETURNING * + """ + + cur.execute(query, (detail_value, detail_label, str(detail_id))) + result = cur.fetchone() + cur.close() + + return result + def delete_contact_detail(self, detail_id: UUID) -> bool: with self.get_db_connection() as conn: cur = conn.cursor() diff --git a/v2_adminpanel/leads/routes.py b/v2_adminpanel/leads/routes.py index 4b60cc7..b0b39bd 100644 --- a/v2_adminpanel/leads/routes.py +++ b/v2_adminpanel/leads/routes.py @@ -165,6 +165,24 @@ def add_email(contact_id): except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 +@leads_bp.route('/api/details/', methods=['PUT']) +@login_required +def update_detail(detail_id): + """Update contact detail (phone/email)""" + try: + data = request.get_json() + detail = lead_service.update_contact_detail( + detail_id, + data['detail_value'], + data.get('detail_label'), + flask_session.get('username') + ) + return jsonify({'success': True, 'detail': detail}) + except ValueError as e: + return jsonify({'success': False, 'error': str(e)}), 400 + except Exception as e: + return jsonify({'success': False, 'error': str(e)}), 500 + @leads_bp.route('/api/details/', methods=['DELETE']) @login_required def delete_detail(detail_id): diff --git a/v2_adminpanel/leads/services.py b/v2_adminpanel/leads/services.py index 77c3b90..64e9265 100644 --- a/v2_adminpanel/leads/services.py +++ b/v2_adminpanel/leads/services.py @@ -114,6 +114,27 @@ class LeadService: return detail + def update_contact_detail(self, detail_id: UUID, detail_value: str, + detail_label: str = None, user: str = None) -> Dict[str, Any]: + """Update a contact detail (phone/email)""" + if not detail_value or len(detail_value.strip()) == 0: + raise ValueError("Detail value cannot be empty") + + # Get current detail to check type + current_detail = self.repo.get_contact_detail_by_id(detail_id) + if not current_detail: + raise ValueError("Contact detail not found") + + # Validation based on type + if current_detail['detail_type'] == 'email' and '@' not in detail_value: + raise ValueError("Invalid email format") + + detail = self.repo.update_contact_detail( + detail_id, detail_value.strip(), detail_label + ) + + return detail + def delete_contact_detail(self, detail_id: UUID, user: str) -> bool: """Delete a contact detail (phone/email)""" success = self.repo.delete_contact_detail(detail_id) diff --git a/v2_adminpanel/leads/templates/leads/contact_detail.html b/v2_adminpanel/leads/templates/leads/contact_detail.html index 199426c..58c8794 100644 --- a/v2_adminpanel/leads/templates/leads/contact_detail.html +++ b/v2_adminpanel/leads/templates/leads/contact_detail.html @@ -51,10 +51,16 @@ {{ phone.detail_label }} {% endif %} - +
+ + +
{% endfor %} @@ -83,10 +89,16 @@ {{ email.detail_label }} {% endif %} - +
+ + +
{% endfor %} @@ -267,6 +279,38 @@ + + +