2023-09-25 00:05:14 +05:30
|
|
|
import functions_framework
|
|
|
|
|
from google.cloud.firestore_v1.base_query import FieldFilter
|
2023-09-25 01:13:51 +05:30
|
|
|
from firebase_admin import initialize_app, credentials, firestore
|
2023-09-24 23:33:29 +05:30
|
|
|
import os
|
|
|
|
|
|
2023-09-25 01:13:51 +05:30
|
|
|
initialize_app()
|
|
|
|
|
|
2023-09-25 00:05:14 +05:30
|
|
|
@functions_framework.http
|
|
|
|
|
def clear_users(request):
|
|
|
|
|
"""HTTP Cloud Function.
|
|
|
|
|
Args:
|
|
|
|
|
request (flask.Request): The request object.
|
|
|
|
|
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
|
|
|
|
|
Returns:
|
|
|
|
|
The response text, or any set of values that can be turned into a
|
|
|
|
|
Response object using `make_response`
|
|
|
|
|
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
|
|
|
|
|
"""
|
2023-09-25 00:40:42 +05:30
|
|
|
if request.method == 'OPTIONS':
|
|
|
|
|
headers = {
|
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
|
'Access-Control-Allow-Methods': 'GET',
|
|
|
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
|
|
|
# 'Access-Control-Max-Age': '3600'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ('', 204, headers)
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 23:33:29 +05:30
|
|
|
db = firestore.client()
|
2023-09-25 00:05:14 +05:30
|
|
|
source_collection = "patientData"
|
2023-09-24 23:33:29 +05:30
|
|
|
|
|
|
|
|
# Get all documents from the source collection
|
|
|
|
|
source_docs = db.collection(source_collection).where(filter=FieldFilter("testStatus", "==", False)).stream()
|
2023-09-25 00:05:14 +05:30
|
|
|
count = 0
|
2023-09-24 23:33:29 +05:30
|
|
|
for doc in source_docs:
|
|
|
|
|
# Extract the document ID
|
|
|
|
|
doc_id = doc.id
|
|
|
|
|
print(doc_id)
|
|
|
|
|
|
|
|
|
|
# Get the document data
|
|
|
|
|
doc_data = doc.to_dict()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
db.collection(source_collection).document(doc_id).update({"testStatus": True})
|
|
|
|
|
print(f"Document with ID '{doc_id}' updated")
|
2023-09-25 00:05:14 +05:30
|
|
|
count = count + 1
|
2023-09-24 23:33:29 +05:30
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error deleting document with ID '{doc_id}' from the source collection: {e}")
|
2023-09-25 00:05:14 +05:30
|
|
|
|
2023-09-25 01:13:51 +05:30
|
|
|
return ('cleared users: {}!'.format(count), 200, headers)
|