import asyncio
from utils_v2.database.async_mongo_v2 import AsyncMongo


async def main():

    # MongoDB connections:
    data_mongo = AsyncMongo(
        connection_string = r"mongodb://del.ditscentre.in:27017,wtt.ditscentre.in:27017,mum.arh.001.ditscentre.in:27017/admin?tls=true&tlsCAFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_ca.pem&tlsCertificateKeyFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_cert.pem&replicaSet=dits_mongod_rep&readPreference=primary&authMechanism=MONGODB-X509&authSource=%24external",
        database_name = "converse",
        max_connections = True,
        debug = True
    )
    await data_mongo.connect()

    while True:
        updated = await data_mongo.find_one_and_update(
            collection = "_authTokens",
            filter = {
                "$or": [
                    {"key": None},
                    {"key": {"$exists": False}}
                ]
            },
            update = {
                "$set": {
                    "key": data_mongo.generate_id(as_str = False)
                }
            }
        )
        if not updated: break
        print("UPDATED:", str(updated["_id"]))


asyncio.run(main())
