feat: Enhance idea retrieval to include the latest generation and support user-specific ideas not tied to a project, while also improving asset storage uniqueness and adjusting generation cancellation timeout.

This commit is contained in:
xds
2026-02-16 16:35:26 +03:00
parent 5e7dc19bf3
commit 68a3f529cb
15 changed files with 67 additions and 30 deletions

View File

@@ -20,14 +20,43 @@ class IdeaRepo:
return Idea(**res)
return None
async def get_ideas(self, project_id: str, limit: int = 20, offset: int = 0) -> List[Idea]:
filter = {"project_id": project_id, "is_deleted": False}
res = await self.collection.find(filter).sort("updated_at", -1).skip(offset).limit(limit).to_list(None)
ideas = []
for doc in res:
doc["id"] = str(doc.pop("_id"))
ideas.append(Idea(**doc))
return ideas
async def get_ideas(self, project_id: Optional[str], user_id: str, limit: int = 20, offset: int = 0) -> List[dict]:
if project_id:
match_stage = {"project_id": project_id, "is_deleted": False}
else:
match_stage = {"created_by": user_id, "project_id": None, "is_deleted": False}
pipeline = [
{"$match": match_stage},
{"$sort": {"updated_at": -1}},
{"$skip": offset},
{"$limit": limit},
# Add string id field for lookup
{"$addFields": {"str_id": {"$toString": "$_id"}}},
# Lookup generations
{
"$lookup": {
"from": "generations",
"let": {"idea_id": "$str_id"},
"pipeline": [
{"$match": {"$expr": {"$eq": ["$idea_id", "$$idea_id"]}}},
{"$sort": {"created_at": -1}}, # Ensure we get the latest
{"$limit": 1}
],
"as": "generations"
}
},
# Unwind generations array (preserve ideas without generations)
{"$unwind": {"path": "$generations", "preserveNullAndEmptyArrays": True}},
# Rename for clarity
{"$addFields": {
"last_generation": "$generations",
"id": "$str_id"
}},
{"$project": {"generations": 0, "str_id": 0, "_id": 0}}
]
return await self.collection.aggregate(pipeline).to_list(None)
async def delete_idea(self, idea_id: str) -> bool:
if not ObjectId.is_valid(idea_id):