import json from .mongo import users_collection from bson.objectid import ObjectId from redis_client import redis_client , CACHE_TTL ALLOWED_FIELDS = {"custom_instruction", "tone", "verbosity"} def get_prompt_by_user(user_id: str) -> dict: cache_key = f"user_prompt:{user_id}" try: cached_data = redis_client.get(cache_key) if cached_data: return json.loads(cached_data) user = users_collection.find_one({"_id": ObjectId(user_id)}) if not user: return {"error": f"No User found for ID {user_id}."} prompt_data = { "custom_instruction": user.get("custom_instruction", ""), "tone": user.get("tone", "Balanced"), "verbosity": user.get("verbosity", "Medium"), } redis_client.setex(cache_key, CACHE_TTL, json.dumps(prompt_data)) return prompt_data except Exception as e: return {"error": str(e)} def update_prompt_for_user(user_id: str, field: str, value: str) -> dict: try: if field not in ALLOWED_FIELDS: return {"error": "Invalid field"} result = users_collection.update_one( {"_id": ObjectId(user_id)}, {"$set": {field: value}}, ) if result.matched_count == 0: return {"error": "User not found"} user = users_collection.find_one({"_id": ObjectId(user_id)}) updated_prompt_data = { "custom_instruction": user.get("custom_instruction", ""), "tone": user.get("tone", "Balanced"), "verbosity": user.get("verbosity", "Medium"), } cache_key = f"user_prompt:{user_id}" redis_client.setex(cache_key, CACHE_TTL, json.dumps(updated_prompt_data)) return updated_prompt_data except Exception as e: return {"error": str(e)}