From b33a38d06aaeae025fa351544e9e184cc6b44eaa Mon Sep 17 00:00:00 2001 From: Mark Li Date: Tue, 12 Nov 2024 21:11:33 +0800 Subject: [PATCH 1/3] Add feedback firebase function --- backend/functions/feedback/__init__.py | 0 backend/functions/feedback/feedback.py | 50 ++++++++++++++++++++++++++ backend/functions/main.py | 2 ++ 3 files changed, 52 insertions(+) create mode 100644 backend/functions/feedback/__init__.py create mode 100644 backend/functions/feedback/feedback.py diff --git a/backend/functions/feedback/__init__.py b/backend/functions/feedback/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/functions/feedback/feedback.py b/backend/functions/feedback/feedback.py new file mode 100644 index 0000000..98cd59e --- /dev/null +++ b/backend/functions/feedback/feedback.py @@ -0,0 +1,50 @@ +""" +url for local testing: +http://127.0.0.1:5001/schemessg-v3-dev/asia-southeast1/feedback +""" + +from firebase_functions import https_fn +from firebase_admin import firestore +from uuid import uuid4 +from datetime import timezone +import datetime + +# Firestore client +db = firestore.client() + +# Firestore collection name +COLLECTION_NAME = "schemeEntries" + +@https_fn.on_request(region="asia-southeast1") +def feedback(req: https_fn.Request) -> https_fn.Response: + 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") + timestamp = datetime.datetime.now(timezone.utc) + + if not feedback_text or not timestamp: + return {"success": False, "message": "Missing required fields."}, 400 + + # Generate a UUID + request_uuid = str(uuid4()) + + # Prepare the data for Firestore + feedback_data = { + "feedbackText": feedback_text, + "timestamp": timestamp, + "requestUUID": request_uuid, + } + + # Add the data to Firestore + db.collection(COLLECTION_NAME).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 diff --git a/backend/functions/main.py b/backend/functions/main.py index 63d72ee..5204e59 100644 --- a/backend/functions/main.py +++ b/backend/functions/main.py @@ -22,3 +22,5 @@ @https_fn.on_request(region="asia-southeast1") def main(req: https_fn.Request) -> https_fn.Response: return https_fn.Response("Hello from Firebase!") + +from feedback.feedback import feedback \ No newline at end of file From b143a46f794d17070234fe77ebb4ce1a41bfc914 Mon Sep 17 00:00:00 2001 From: mark Date: Sat, 16 Nov 2024 23:18:36 +0800 Subject: [PATCH 2/3] Corrected feedback post request to the correct collection, added userName and userEmail as per Notion doc, used singleton method however fb_manager does not exist in this branch --- backend/functions/feedback/feedback.py | 34 +++++++++++++++----------- backend/functions/main.py | 2 -- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/functions/feedback/feedback.py b/backend/functions/feedback/feedback.py index 98cd59e..0eddedd 100644 --- a/backend/functions/feedback/feedback.py +++ b/backend/functions/feedback/feedback.py @@ -3,20 +3,26 @@ 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 firebase_admin import firestore -from uuid import uuid4 -from datetime import timezone -import datetime +from datetime import timezone +import datetime # Firestore client -db = firestore.client() - -# Firestore collection name -COLLECTION_NAME = "schemeEntries" +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) @@ -24,23 +30,23 @@ def feedback(req: https_fn.Request) -> https_fn.Response: # Parse the request data request_json = req.get_json() feedback_text = request_json.get("feedbackText") - timestamp = datetime.datetime.now(timezone.utc) + 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 - # Generate a UUID - request_uuid = str(uuid4()) - # Prepare the data for Firestore feedback_data = { "feedbackText": feedback_text, "timestamp": timestamp, - "requestUUID": request_uuid, + "userName": userName, + "userEmail": userEmail, } # Add the data to Firestore - db.collection(COLLECTION_NAME).add(feedback_data) + firebase_manager.firestore_client.collection("userFeedback").add(feedback_data) # Return a success response return {"success": True, "message": "Feedback successfully added."}, 200 diff --git a/backend/functions/main.py b/backend/functions/main.py index 5204e59..63d72ee 100644 --- a/backend/functions/main.py +++ b/backend/functions/main.py @@ -22,5 +22,3 @@ @https_fn.on_request(region="asia-southeast1") def main(req: https_fn.Request) -> https_fn.Response: return https_fn.Response("Hello from Firebase!") - -from feedback.feedback import feedback \ No newline at end of file From 3d533156bd953cc5d4d407ff9668664f6c02f4f0 Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 17 Nov 2024 10:20:50 +0800 Subject: [PATCH 3/3] Add feedback into main.py --- backend/functions/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/functions/main.py b/backend/functions/main.py index f365aca..0f4df29 100644 --- a/backend/functions/main.py +++ b/backend/functions/main.py @@ -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