Skip to content

Commit

Permalink
Merge pull request #114 from bettersg/search_query_func
Browse files Browse the repository at this point in the history
Added function to show user's search queries
  • Loading branch information
yevkim authored Dec 22, 2024
2 parents 7b5fc95 + dfe53ab commit 0e820b5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
from loguru import logger
from schemes.schemes import schemes # noqa: F401
from schemes.search import schemes_search # noqa: F401
from schemes.search_queries import retrieve_search_queries #noqa: F401
from update_scheme.update_scheme import update_scheme # noqa: F401


# Initialise logger
logger.remove()
logger.add(
Expand Down
97 changes: 97 additions & 0 deletions backend/functions/schemes/search_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""
url for local testing:
http://127.0.0.1:5001/schemessg-v3-dev/asia-southeast1/retrieve_search_queries
"""

import json

from fb_manager.firebaseManager import FirebaseManager
from firebase_functions import https_fn, options
from loguru import logger


def create_firebase_manager() -> FirebaseManager:
"""Factory function to create a FirebaseManager instance."""

return FirebaseManager()


@https_fn.on_request(
region="asia-southeast1",
memory=options.MemoryOption.GB_1, # Increases memory to 1GB
)
def retrieve_search_queries(req: https_fn.Request) -> https_fn.Response:
"""
Handler for retrieving user queries (used for telegram bot pagination)
Args:
req (https_fn.Request): request sent from client
Returns:
https_fn.Response: response sent to client
"""
# TODO remove for prod setup
#Set CORS headers for the preflight request
if req.method == 'OPTIONS':
# Allows GET and POST requests from any origin with the Content-Type
# header and caches preflight response for an hour
headers = {
'Access-Control-Allow-Origin': 'http://localhost:3000',
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)

# Set CORS headers for the main request
headers = {
'Access-Control-Allow-Origin': 'http://localhost:3000'
}


firebase_manager = create_firebase_manager()

if not req.method == "GET":
return https_fn.Response(
response=json.dumps({"error": "Invalid request method; only GET is supported"}),
status=405,
mimetype="application/json",
)

splitted_path = req.path.split("/")
session_id = splitted_path[1] if len(splitted_path) == 2 else None

if not session_id:
return https_fn.Response(
response=json.dumps({"error": "Invalid path parameters, please provide session id"}),
status=400,
mimetype="application/json",
headers=headers
)

try:
ref = firebase_manager.firestore_client.collection("userQuery").document(session_id)
doc = ref.get()
except Exception as e:
logger.exception("Unable to fetch schemes search results from firestore", e)
return https_fn.Response(
response=json.dumps({"error": "Internal server error, unable to fetch schemes search results from firestore"}),
status=500,
mimetype="application/json",
headers=headers
)

if not doc.exists:
return https_fn.Response(
response=json.dumps({"error": "Search query with provided session id does not exist"}),
status=404,
mimetype="application/json",
headers=headers
)

results = {"data": doc.to_dict()}
return https_fn.Response(
response=json.dumps(results),
status=200,
mimetype="application/json",
headers=headers)

0 comments on commit 0e820b5

Please sign in to comment.