Hello Admin,
+You have received a new contact message from a customer.
+ +Customer Details
+Name: {customer_name}
+Email: {customer_email}
+Phone: {phone or 'Not provided'}
+Subject: {subject}
+diff --git a/backend/app/main.py b/backend/app/main.py index e482a9b..56a600a 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -92,6 +92,7 @@ app.include_router(orders.router) app.include_router(wishlist.router) app.include_router(contact.router) app.include_router(contact.admin_router) # Admin contact messages endpoints +app.include_router(contact.user_router) # User messages endpoints # Mount static files for uploads app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads") diff --git a/backend/app/routers/contact.py b/backend/app/routers/contact.py index ef38a90..22fb12e 100644 --- a/backend/app/routers/contact.py +++ b/backend/app/routers/contact.py @@ -4,7 +4,9 @@ from typing import List, Optional from app.database.database import get_db from app.models import ContactMessage, User from app.schemas.contact import ContactMessageCreate, ContactMessageResponse, ContactMessageUpdate -from app.services.auth import get_current_admin_user +from app.services.auth import get_current_admin_user, get_current_user +from app.services.email import send_contact_notification_to_admin, send_admin_response_to_customer +from app.config import settings router = APIRouter(prefix="/api/contact", tags=["contact"]) @@ -17,6 +19,20 @@ def send_contact_message(message: ContactMessageCreate, db: Session = Depends(ge db.add(db_message) db.commit() db.refresh(db_message) + + # Send email notification to admin + try: + send_contact_notification_to_admin( + admin_email=settings.admin_email, + customer_name=db_message.full_name, + customer_email=db_message.email, + subject=db_message.subject, + message=db_message.message, + phone=db_message.phone + ) + except Exception as e: + print(f"Failed to send admin notification email: {e}") + return db_message @@ -81,11 +97,32 @@ def update_message( raise HTTPException(status_code=404, detail="Message not found") update_data = message_update.model_dump(exclude_unset=True) if hasattr(message_update, 'model_dump') else message_update.dict(exclude_unset=True) + + # Check if admin_notes is being added/updated + send_email_to_customer = False + if 'admin_notes' in update_data and update_data['admin_notes']: + # Only send if there's actually content and it's different from before + if not message.admin_notes or update_data['admin_notes'] != message.admin_notes: + send_email_to_customer = True + for field, value in update_data.items(): setattr(message, field, value) db.commit() db.refresh(message) + + # Send email to customer if admin responded + if send_email_to_customer: + try: + send_admin_response_to_customer( + customer_email=message.email, + customer_name=message.full_name, + original_subject=message.subject, + admin_notes=message.admin_notes + ) + except Exception as e: + print(f"Failed to send customer response email: {e}") + return message @@ -103,3 +140,38 @@ def delete_message( db.delete(message) db.commit() return {"message": "Contact message deleted successfully"} + + +# User/Customer endpoints +user_router = APIRouter(prefix="/api/my-messages", tags=["user-messages"]) + + +@user_router.get("", response_model=List[ContactMessageResponse]) +def get_my_messages( + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Get all contact messages sent by the current user""" + messages = db.query(ContactMessage).filter( + ContactMessage.email == current_user.email + ).order_by(ContactMessage.created_at.desc()).all() + return messages + + +@user_router.get("/{message_id}", response_model=ContactMessageResponse) +def get_my_message( + message_id: int, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Get a specific contact message by ID (only if sent by current user)""" + message = db.query(ContactMessage).filter( + ContactMessage.id == message_id, + ContactMessage.email == current_user.email + ).first() + + if not message: + raise HTTPException(status_code=404, detail="Message not found") + + return message + diff --git a/backend/app/services/email.py b/backend/app/services/email.py index 136485a..3ddfe3c 100644 --- a/backend/app/services/email.py +++ b/backend/app/services/email.py @@ -231,3 +231,178 @@ Brand Master Team """ return send_email(email, subject, body, html_body) + + +def send_contact_notification_to_admin(admin_email: str, customer_name: str, customer_email: str, subject: str, message: str, phone: str = None) -> bool: + """ + Send notification to admin when a customer submits a contact message. + + Args: + admin_email: Admin's email address + customer_name: Customer's full name + customer_email: Customer's email + subject: Message subject + message: Message content + phone: Customer's phone number (optional) + + Returns: + bool: True if email sent successfully + """ + email_subject = f"New Contact Message from {customer_name}" + + # Plain text version + body = f""" +Hello Admin, + +You have received a new contact message from a customer. + +Customer Details: +- Name: {customer_name} +- Email: {customer_email} +- Phone: {phone or 'Not provided'} + +Subject: {subject} + +Message: +{message} + +Please log in to the admin dashboard to view and respond to this message. +https://brand-master.dvirlabs.com/admin + +Best regards, +Brand Master System +""" + + # HTML version + html_body = f""" + + +
+ + + +Hello Admin,
+You have received a new contact message from a customer.
+ +Name: {customer_name}
+Email: {customer_email}
+Phone: {phone or 'Not provided'}
+Subject: {subject}
+Re: {original_subject}
+Hello {customer_name},
+Thank you for contacting Brand Master. We have reviewed your message and here is our response:
+ +{admin_notes.replace(chr(10), '
')}
If you have any additional questions, please don't hesitate to contact us again.
+ ++ View your contact messages and admin responses +
++ You haven't sent any contact messages yet. +
+ ++ Sent on {formatDate(message.created_at)} +
++ {message.message} +
++ {message.admin_notes} +
++ ⏳ Waiting for admin response... +
+{message.full_name}
+{message.email}
+{message.phone}
++ {message.is_read ? 'Read' : 'Unread'} +
+