|
1 | | -from fastapi import APIRouter, HTTPException, Depends, Path |
| 1 | +from fastapi import APIRouter, HTTPException, Depends, Path, Body |
2 | 2 | from typing import List, Dict, Set |
3 | 3 | from bson import ObjectId |
4 | 4 | from services.bulletins_master_service import BulletinsMasterService |
@@ -176,6 +176,37 @@ def get_bulletin_by_id( |
176 | 176 | raise HTTPException(status_code=404, detail="Not found or no access") |
177 | 177 | return bulletins[0] |
178 | 178 |
|
| 179 | +@router.post("/{bulletin_id}/clone", response_model=BulletinsMasterRead) |
| 180 | +def clone_bulletin( |
| 181 | + bulletin_id: str = Path(..., description="ID of the bulletin master to clone"), |
| 182 | + bulletin_name: str = Body(None, description="New name for the cloned bulletin"), |
| 183 | + description: str = Body(None, description="New description for the cloned bulletin"), |
| 184 | + credentials: HTTPAuthorizationCredentials = Depends(security) |
| 185 | +): |
| 186 | + """ |
| 187 | + Clones an existing bulletin. If name/description are provided, clones the master and its current version. |
| 188 | + """ |
| 189 | + user = get_current_user(credentials) |
| 190 | + user_id = user["user_db"]["id"] |
| 191 | + |
| 192 | + bulletins = bulletins_master_service.get_accessible_resources(user_id, filters={"id": bulletin_id}) |
| 193 | + if not bulletins: |
| 194 | + raise HTTPException(status_code=404, detail="Not found or no access") |
| 195 | + |
| 196 | + bulletin_master = bulletins[0] |
| 197 | + allowed_groups = bulletin_master.access_config.allowed_groups if hasattr(bulletin_master.access_config, "allowed_groups") else [] |
| 198 | + for group_id in allowed_groups: |
| 199 | + if not user_has_permission(user_id, str(group_id), "bulletins_composer", "c"): |
| 200 | + raise HTTPException(status_code=403, detail=f"User does not have permission to clone bulletins in group {group_id}.") |
| 201 | + |
| 202 | + cloned_master, cloned_version = bulletins_master_service.clone_master_with_version( |
| 203 | + bulletin_master, |
| 204 | + user_id, |
| 205 | + bulletin_name=bulletin_name, |
| 206 | + description=description |
| 207 | + ) |
| 208 | + return cloned_master |
| 209 | + |
179 | 210 | @router.get("/{bulletin_id}/current-version", response_model=BulletinWithCurrentVersion) |
180 | 211 | def get_current_version( |
181 | 212 | bulletin_id: str = Path(..., description="ID del boletín"), |
|
0 commit comments