diff --git a/backend/api/models/user_models.py b/backend/api/models/user_models.py index 4b20fc0..aa55399 100644 --- a/backend/api/models/user_models.py +++ b/backend/api/models/user_models.py @@ -1,3 +1,5 @@ +# api/models/user_models.py + from typing import List, Optional, Union from pydantic import BaseModel, EmailStr, Field from datetime import datetime diff --git a/backend/api/routers/docit2mf_build.py b/backend/api/routers/docit2mf_build.py index 7b132b4..75660f4 100644 --- a/backend/api/routers/docit2mf_build.py +++ b/backend/api/routers/docit2mf_build.py @@ -1,18 +1,14 @@ -# routers/docit2mf_build.py +# api/routers/docit2mf_build.py -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, HTTPException from api.models.docit2mf_models import DoCIT2MFMultiRequest from api.services.docit2mf_build_service import build_it2mf_from_level -from api.utils.security import get_current_user router = APIRouter(prefix="/criteria", tags=["criteria"]) @router.post("/doc-it2mf/build") -async def build_doc_it2mf( - request: DoCIT2MFMultiRequest, - current_user: dict = Depends(get_current_user) -): +async def build_doc_it2mf(request: DoCIT2MFMultiRequest): results = [] try: diff --git a/backend/api/routers/history.py b/backend/api/routers/history.py index 0110241..03c3db1 100644 --- a/backend/api/routers/history.py +++ b/backend/api/routers/history.py @@ -1,3 +1,5 @@ +# api/routers/history.py + from fastapi import APIRouter, HTTPException, status, Depends from datetime import datetime from bson import ObjectId @@ -9,6 +11,9 @@ from api.utils.security import get_current_user router = APIRouter(prefix="/history", tags=["history"]) +# ----------------------------- +# 1. AƱadir un elemento al historial +# ----------------------------- @router.post("/add") async def add_history_item( data: HistoryCreateRequest, @@ -21,7 +26,7 @@ async def add_history_item( "_id": history_item_id, "name": data.name, "created_at": datetime.utcnow(), - "results": [r.dict() for r in data.results], # ahora soporta IT2MF + "results": [r.dict() for r in data.results], } await users_collection.update_one( @@ -35,7 +40,9 @@ async def add_history_item( } - +# ----------------------------- +# 2. Eliminar un elemento del historial +# ----------------------------- @router.delete("/delete/{history_item_id}") async def delete_history_item( history_item_id: str, @@ -55,3 +62,75 @@ async def delete_history_item( ) return {"message": "Elemento eliminado del historial"} + + +# ----------------------------- +# 3. Obtener todos los historiales de todos los usuarios (ordenados) +# ----------------------------- +@router.get("/all") +async def get_all_histories(): + users = await users_collection.find().to_list(None) + + all_histories = [] + + for user in users: + user_id = str(user["_id"]) + username = user.get("username", "unknown") + history = user.get("history", []) + + for item in history: + all_histories.append({ + "user_id": user_id, + "username": username, + "history_item": { + "_id": str(item["_id"]), + "name": item["name"], + "created_at": item["created_at"], + "results": item["results"] + } + }) + + # Ordenar por fecha + all_histories_sorted = sorted( + all_histories, + key=lambda h: h["history_item"]["created_at"] + ) + + return {"count": len(all_histories_sorted), "histories": all_histories_sorted} + + +# ----------------------------- +# 4. Obtener historiales del usuario autenticado (ordenados) +# ----------------------------- +@router.get("/user") +async def get_user_histories(current_user: dict = Depends(get_current_user)): + user_id = current_user["_id"] + + user = await users_collection.find_one({"_id": user_id}) + + if not user: + raise HTTPException( + status_code=404, + detail="Usuario no encontrado." + ) + + history = user.get("history", []) + + # Ordenar por fecha + history_sorted = sorted(history, key=lambda h: h["created_at"]) + + formatted_history = [] + for item in history_sorted: + formatted_history.append({ + "_id": str(item["_id"]), + "name": item["name"], + "created_at": item["created_at"], + "results": item["results"] + }) + + return { + "user_id": str(user_id), + "username": user.get("username", "unknown"), + "count": len(formatted_history), + "history": formatted_history + }