Skip to content

Commit b088935

Browse files
authored
Merge pull request #10 from CIAT-DAPA/develop
add clone functionality for bulletin masters with optional name and d…
2 parents 4b1cd17 + f429491 commit b088935

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

src/api/bulletins_management.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import APIRouter, HTTPException, Depends, Path
1+
from fastapi import APIRouter, HTTPException, Depends, Path, Body
22
from typing import List, Dict, Set
33
from bson import ObjectId
44
from services.bulletins_master_service import BulletinsMasterService
@@ -176,6 +176,37 @@ def get_bulletin_by_id(
176176
raise HTTPException(status_code=404, detail="Not found or no access")
177177
return bulletins[0]
178178

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+
179210
@router.get("/{bulletin_id}/current-version", response_model=BulletinWithCurrentVersion)
180211
def get_current_version(
181212
bulletin_id: str = Path(..., description="ID del boletín"),

0 commit comments

Comments
 (0)