Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feedback firebase function #57

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
56 changes: 56 additions & 0 deletions backend/functions/feedback/feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
url for local testing:
http://127.0.0.1:5001/schemessg-v3-dev/asia-southeast1/feedback
"""

from fb_manager.firebaseManager import FirebaseManager
from firebase_functions import https_fn
from datetime import timezone
import datetime

# Firestore client
firebase_manager = FirebaseManager()

@https_fn.on_request(region="asia-southeast1")
def feedback(req: https_fn.Request) -> https_fn.Response:
"""
Handler for logging user feedback

Args:
req (https_fn.Request): request sent from client

Returns:
https_fn.Response: response sent to client
"""

if req.method != "POST":
return https_fn.Response("Only POST requests are allowed.", status=405)

try:
# Parse the request data
request_json = req.get_json()
feedback_text = request_json.get("feedbackText")
userName = request_json.get("userName")
userEmail = request_json.get("userEmail")
timestamp = datetime.datetime.now(timezone.utc)

if not feedback_text or not timestamp:
return {"success": False, "message": "Missing required fields."}, 400

# Prepare the data for Firestore
feedback_data = {
"feedbackText": feedback_text,
"timestamp": timestamp,
"userName": userName,
"userEmail": userEmail,
}

# Add the data to Firestore
firebase_manager.firestore_client.collection("userFeedback").add(feedback_data)

# Return a success response
return {"success": True, "message": "Feedback successfully added."}, 200

except Exception as e:
print(f"Error: {e}")
return {"success": False, "message": "Failed to add feedback."}, 500
1 change: 1 addition & 0 deletions backend/functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from loguru import logger
from schemes.schemes import schemes # noqa: F401
from schemes.search import schemes_search # noqa: F401
from feedback.feedback import feedback


# Initialise logger
Expand Down
Loading