|
| 1 | +from typing import List |
| 2 | +from backend.models.dtos.team_dto import ProjectTeamPairDTOList |
1 | 3 | from databases import Database |
2 | 4 | from fastapi import APIRouter, BackgroundTasks, Body, Depends, Request |
3 | 5 | from fastapi.responses import JSONResponse |
@@ -407,3 +409,213 @@ async def message_team( |
407 | 409 | ) |
408 | 410 | except ValueError as e: |
409 | 411 | return JSONResponse(content={"Error": str(e)}, status_code=400) |
| 412 | + |
| 413 | + |
| 414 | +@router.delete("/projects/{project_id}/teams/{team_id}/") |
| 415 | +async def remove_team_from_project( |
| 416 | + project_id: int, |
| 417 | + team_id: int, |
| 418 | + request: Request, |
| 419 | + user: AuthUserDTO = Depends(login_required), |
| 420 | + db: Database = Depends(get_db), |
| 421 | +): |
| 422 | + """ |
| 423 | + Unlink a Team from a Project. |
| 424 | + """ |
| 425 | + permitted = await TeamService.is_user_team_manager(team_id, user.id, db) |
| 426 | + if not permitted: |
| 427 | + return JSONResponse( |
| 428 | + { |
| 429 | + "Error": "User is not a manager of the team", |
| 430 | + "SubCode": "UserPermissionError", |
| 431 | + }, |
| 432 | + status_code=403, |
| 433 | + ) |
| 434 | + |
| 435 | + deny_resp = await TeamService.ensure_unlink_allowed(project_id, team_id, db) |
| 436 | + if deny_resp: |
| 437 | + return deny_resp |
| 438 | + |
| 439 | + try: |
| 440 | + deleted = await TeamService.unlink_team(project_id, team_id, db) |
| 441 | + if not deleted: |
| 442 | + return JSONResponse( |
| 443 | + {"Error": "No such team linked to project", "SubCode": "NotFoundError"}, |
| 444 | + status_code=404, |
| 445 | + ) |
| 446 | + return JSONResponse({"Success": True}, status_code=200) |
| 447 | + except Exception as e: |
| 448 | + return JSONResponse( |
| 449 | + {"Error": "Internal server error", "Details": str(e)}, |
| 450 | + status_code=500, |
| 451 | + ) |
| 452 | + |
| 453 | + |
| 454 | +@router.delete("/projects/teams/{team_id}/unlink") |
| 455 | +async def remove_team_from_all_projects( |
| 456 | + team_id: int, |
| 457 | + request: Request, |
| 458 | + user: AuthUserDTO = Depends(login_required), |
| 459 | + db: Database = Depends(get_db), |
| 460 | +): |
| 461 | + """ |
| 462 | + Unlink the given team from all projects it is assigned to. |
| 463 | +
|
| 464 | + Steps: |
| 465 | + - ensure caller is a manager of the team |
| 466 | + - fetch all project_ids for the team from project_teams |
| 467 | + - run ensure_unlink_allowed(project_id, team_id, db) for every project |
| 468 | + - if all checks pass, unlink each (inside one DB transaction) |
| 469 | + """ |
| 470 | + permitted = await TeamService.is_user_team_manager(team_id, user.id, db) |
| 471 | + if not permitted: |
| 472 | + return JSONResponse( |
| 473 | + { |
| 474 | + "Error": ( |
| 475 | + f"Cannot unlink team with team id-{team_id}: " |
| 476 | + f"user {user.id} is not a manager of the team" |
| 477 | + ), |
| 478 | + "SubCode": "UserPermissionError", |
| 479 | + }, |
| 480 | + status_code=403, |
| 481 | + ) |
| 482 | + |
| 483 | + rows = await db.fetch_all( |
| 484 | + "SELECT project_id FROM project_teams WHERE team_id = :tid", |
| 485 | + {"tid": team_id}, |
| 486 | + ) |
| 487 | + project_ids: List[int] = [r["project_id"] for r in rows] if rows else [] |
| 488 | + |
| 489 | + if not project_ids: |
| 490 | + return JSONResponse( |
| 491 | + { |
| 492 | + "Error": ( |
| 493 | + f"Cannot unlink team with team id-{team_id}: " |
| 494 | + "team is not linked to any projects" |
| 495 | + ), |
| 496 | + "SubCode": "NotFoundError", |
| 497 | + }, |
| 498 | + status_code=404, |
| 499 | + ) |
| 500 | + |
| 501 | + for pid in project_ids: |
| 502 | + deny_resp = await TeamService.ensure_unlink_allowed(pid, team_id, db) |
| 503 | + if deny_resp: |
| 504 | + return deny_resp |
| 505 | + |
| 506 | + try: |
| 507 | + async with db.transaction(): |
| 508 | + for pid in project_ids: |
| 509 | + deleted = await TeamService.unlink_team(pid, team_id, db) |
| 510 | + if not deleted: |
| 511 | + raise RuntimeError(f"NOT_FOUND:{pid}:{team_id}") |
| 512 | + |
| 513 | + projects_str = ", ".join(str(p) for p in project_ids) |
| 514 | + return JSONResponse( |
| 515 | + { |
| 516 | + "Success": True, |
| 517 | + "Message": ( |
| 518 | + f"Team id-{team_id} unlinked from projects: {projects_str}" |
| 519 | + ), |
| 520 | + }, |
| 521 | + status_code=200, |
| 522 | + ) |
| 523 | + |
| 524 | + except Exception as e: |
| 525 | + return JSONResponse( |
| 526 | + { |
| 527 | + "Error": ( |
| 528 | + f"Cannot unlink team with team id-{team_id}: internal server error - {str(e)}" |
| 529 | + ), |
| 530 | + "SubCode": "InternalServerError", |
| 531 | + }, |
| 532 | + status_code=500, |
| 533 | + ) |
| 534 | + |
| 535 | + |
| 536 | +@router.delete("/projects/unlink") |
| 537 | +async def remove_teams_from_projects( |
| 538 | + payload: ProjectTeamPairDTOList, |
| 539 | + request: Request, |
| 540 | + user: AuthUserDTO = Depends(login_required), |
| 541 | + db: Database = Depends(get_db), |
| 542 | +): |
| 543 | + """ |
| 544 | + Bulk unlink teams from projects. |
| 545 | +
|
| 546 | + Body: |
| 547 | + { |
| 548 | + "items": [ |
| 549 | + {"project_id": 1442, "team_id": 43}, |
| 550 | + {"project_id": 2000, "team_id": 55} |
| 551 | + ] |
| 552 | + } |
| 553 | +
|
| 554 | + First: run all checks for all items (manager check + ensure_unlink_allowed). |
| 555 | + If any check fails, return error immediately and do NOT modify DB. |
| 556 | + If all checks pass, perform unlink operations inside a single DB transaction. |
| 557 | + """ |
| 558 | + items = payload.items or [] |
| 559 | + if not items: |
| 560 | + return JSONResponse( |
| 561 | + {"Error": "No project/team pairs provided", "SubCode": "InvalidRequest"}, |
| 562 | + status_code=400, |
| 563 | + ) |
| 564 | + |
| 565 | + seen = set() |
| 566 | + pairs = [] |
| 567 | + for it in items: |
| 568 | + key = (it.project_id, it.team_id) |
| 569 | + if key in seen: |
| 570 | + continue |
| 571 | + seen.add(key) |
| 572 | + pairs.append(it) |
| 573 | + |
| 574 | + for it in pairs: |
| 575 | + pid = it.project_id |
| 576 | + tid = it.team_id |
| 577 | + |
| 578 | + permitted = await TeamService.is_user_team_manager(tid, user.id, db) |
| 579 | + if not permitted: |
| 580 | + return JSONResponse( |
| 581 | + { |
| 582 | + "Error": ( |
| 583 | + f"Cannot unlink team with team id-{tid}: user {user.id} is not a manager of the team" |
| 584 | + ), |
| 585 | + "SubCode": "UserPermissionError", |
| 586 | + }, |
| 587 | + status_code=403, |
| 588 | + ) |
| 589 | + |
| 590 | + deny_resp = await TeamService.ensure_unlink_allowed(pid, tid, db) |
| 591 | + if deny_resp: |
| 592 | + return deny_resp |
| 593 | + |
| 594 | + try: |
| 595 | + async with db.transaction(): |
| 596 | + for it in pairs: |
| 597 | + pid = it.project_id |
| 598 | + tid = it.team_id |
| 599 | + |
| 600 | + deleted = await TeamService.unlink_team(pid, tid, db) |
| 601 | + if not deleted: |
| 602 | + raise RuntimeError(f"NOT_FOUND:{pid}:{tid}") |
| 603 | + |
| 604 | + pairs_str = ", ".join( |
| 605 | + [f"(project {p.project_id}, team {p.team_id})" for p in pairs] |
| 606 | + ) |
| 607 | + return JSONResponse( |
| 608 | + { |
| 609 | + "Success": True, |
| 610 | + "Message": f"Unlinked teams: {pairs_str}", |
| 611 | + }, |
| 612 | + status_code=200, |
| 613 | + ) |
| 614 | + except Exception as e: |
| 615 | + return JSONResponse( |
| 616 | + { |
| 617 | + "Error": f"Cannot unlink teams: internal server error - {str(e)}", |
| 618 | + "SubCode": "InternalServerError", |
| 619 | + }, |
| 620 | + status_code=500, |
| 621 | + ) |
0 commit comments