From 4df845919313a766c8b8f02e2f3dcfbe9bc52383 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Mon, 25 Sep 2023 00:05:14 +0530 Subject: [PATCH] add key to clearusers --- .../python/functions/clearusers.py | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/cloud-functions/python/functions/clearusers.py b/cloud-functions/python/functions/clearusers.py index 35604fd..ef915d4 100644 --- a/cloud-functions/python/functions/clearusers.py +++ b/cloud-functions/python/functions/clearusers.py @@ -1,17 +1,29 @@ +import functions_framework +from google.cloud.firestore_v1.base_query import FieldFilter import firebase_admin from firebase_admin import credentials, firestore import os -from google.cloud.firestore_v1.base_query import FieldFilter -key_path = os.getcwd() + '/scripts/keys/hpos-preprod-firebase-adminsdk-d6jjt-ae42ad0f67.json' -# Replace 'path/to/your/serviceAccountKey.json' with the actual path to your service account JSON file -cred = credentials.Certificate(key_path) -firebase_admin.initialize_app(cred) -def move_documents(source_collection): +@functions_framework.http +def clear_users(request): + """HTTP Cloud Function. + Args: + request (flask.Request): The request object. + + Returns: + The response text, or any set of values that can be turned into a + Response object using `make_response` + . + """ + key_path = os.getcwd() + '/hpos-preprod-firebase-adminsdk-d6jjt-ae42ad0f67.json' + cred = credentials.Certificate(key_path) + firebase_admin.initialize_app(cred) db = firestore.client() + source_collection = "patientData" # Get all documents from the source collection source_docs = db.collection(source_collection).where(filter=FieldFilter("testStatus", "==", False)).stream() + count = 0 for doc in source_docs: # Extract the document ID doc_id = doc.id @@ -24,9 +36,10 @@ def move_documents(source_collection): # Delete the document from the source collection db.collection(source_collection).document(doc_id).update({"testStatus": True}) print(f"Document with ID '{doc_id}' updated") + count = count + 1 except Exception as e: print(f"Error deleting document with ID '{doc_id}' from the source collection: {e}") + + firebase_admin.delete_app(firebase_admin.get_app()) -if __name__ == "__main__": - # Replace 'source_collection' and 'destination_collection' with your collection names - move_documents("patientData") + return 'cleared users: {}!'.format(count)