update
This commit is contained in:
63
backend/routes/subscription_routes.py
Normal file
63
backend/routes/subscription_routes.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from datetime import datetime
|
||||
from pymongo.errors import DuplicateKeyError
|
||||
from database import subscribers_collection
|
||||
|
||||
subscription_bp = Blueprint('subscription', __name__)
|
||||
|
||||
|
||||
@subscription_bp.route('/api/subscribe', methods=['POST'])
|
||||
def subscribe():
|
||||
"""Subscribe a user to the newsletter"""
|
||||
data = request.json
|
||||
email = data.get('email', '').strip().lower()
|
||||
|
||||
if not email or '@' not in email:
|
||||
return jsonify({'error': 'Invalid email address'}), 400
|
||||
|
||||
try:
|
||||
subscriber_doc = {
|
||||
'email': email,
|
||||
'subscribed_at': datetime.utcnow(),
|
||||
'status': 'active'
|
||||
}
|
||||
|
||||
# Try to insert, if duplicate key error, subscriber already exists
|
||||
try:
|
||||
subscribers_collection.insert_one(subscriber_doc)
|
||||
return jsonify({'message': 'Successfully subscribed!'}), 201
|
||||
except DuplicateKeyError:
|
||||
# Check if subscriber is active
|
||||
existing = subscribers_collection.find_one({'email': email})
|
||||
if existing and existing.get('status') == 'active':
|
||||
return jsonify({'message': 'Email already subscribed'}), 200
|
||||
else:
|
||||
# Reactivate if previously unsubscribed
|
||||
subscribers_collection.update_one(
|
||||
{'email': email},
|
||||
{'$set': {'status': 'active', 'subscribed_at': datetime.utcnow()}}
|
||||
)
|
||||
return jsonify({'message': 'Successfully re-subscribed!'}), 200
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@subscription_bp.route('/api/unsubscribe', methods=['POST'])
|
||||
def unsubscribe():
|
||||
"""Unsubscribe a user from the newsletter"""
|
||||
data = request.json
|
||||
email = data.get('email', '').strip().lower()
|
||||
|
||||
try:
|
||||
result = subscribers_collection.update_one(
|
||||
{'email': email},
|
||||
{'$set': {'status': 'inactive'}}
|
||||
)
|
||||
|
||||
if result.matched_count > 0:
|
||||
return jsonify({'message': 'Successfully unsubscribed'}), 200
|
||||
else:
|
||||
return jsonify({'error': 'Email not found in subscribers'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user