Spaces:
Sleeping
Sleeping
File size: 89,510 Bytes
74de430 38ac151 74de430 95005e1 38ac151 9dd2bfc 38ac151 5ef066f 38ac151 74de430 c5709e5 38ac151 bff84fe ab19502 04b997c 38ac151 310ce0f aef124d 4835b24 38ac151 04eee22 7b90d84 38ac151 5b13ddd 38ac151 74de430 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 2fb09d0 38ac151 7b90d84 2fb09d0 7b90d84 38ac151 7b90d84 38ac151 2a684b9 38ac151 7b90d84 38ac151 7b90d84 38ac151 7b90d84 38ac151 7b90d84 38ac151 7b90d84 38ac151 1243151 2a684b9 38ac151 ab19502 3522180 ab19502 3522180 ab19502 3522180 ab19502 3522180 8a46bf1 ab19502 a30132c ab19502 a30132c ab19502 19dd95f 38ac151 54c5a41 38ac151 54c5a41 38ac151 867ae6b 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 2a684b9 95005e1 2a684b9 95005e1 2a684b9 38ac151 2a684b9 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 710ac90 38ac151 cd8df9d 9309d79 9dd2bfc 9309d79 9dd2bfc 9309d79 c5fdc5d 9309d79 9dd2bfc 9309d79 c5fdc5d 9309d79 9dd2bfc 9309d79 9dd2bfc 9309d79 9dd2bfc cd8df9d 5b13ddd cd8df9d f9fc05b cd8df9d f9fc05b cd8df9d 310ce0f cd8df9d 9cbadb3 cd8df9d 4835b24 04eee22 4835b24 24bb10d cb9c038 24bb10d 4835b24 95005e1 4835b24 88d4945 4835b24 88d4945 4835b24 88d4945 4835b24 88d4945 4835b24 88d4945 4835b24 6b29be6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 |
"""
Projects API Endpoints - Complete CRUD with nested resources
"""
from fastapi import APIRouter, Depends, HTTPException, status, Query, Request, BackgroundTasks
from sqlalchemy.orm import Session
from sqlalchemy import or_
from typing import Optional, List
from uuid import UUID
from datetime import date
import math
import logging
from app.api.deps import get_db, get_current_active_user, get_current_user
from app.models.user import User
from app.models.project import Project
from app.models.project_team import ProjectTeam
from app.models.notification import Notification
from app.models.enums import AppRole
from app.schemas.project import (
ProjectCreate, ProjectUpdate, ProjectResponse, ProjectListResponse,
ProjectStatusUpdate, ProjectClose, ProjectSetup, ProjectSetupResponse,
ProjectTeamCreate, ProjectTeamUpdate, ProjectTeamResponse,
ProjectRegionCreate, ProjectRegionUpdate, ProjectRegionResponse,
ProjectRoleCreate, ProjectRoleUpdate, ProjectRoleResponse,
ProjectSubcontractorCreate, ProjectSubcontractorUpdate, ProjectSubcontractorResponse
)
from app.schemas.invitation import ProjectInvitationCreate, InvitationCreate, InvitationResponse, InvitationStats
from app.schemas.filters import ProjectFilters
from app.services.project_service import ProjectService
from app.services.invitation_service import InvitationService
from app.services.audit_service import AuditService
from app.core.permissions import require_permission
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/projects", tags=["Projects"])
# ============================================
# CORE PROJECT CRUD
# ============================================
@router.post("", response_model=ProjectResponse, status_code=status.HTTP_201_CREATED)
@require_permission("manage_projects")
async def create_project(
data: ProjectCreate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Create a new project
**Authorization:**
- platform_admin: Can create any project
- client_admin: Can create projects for their client
- contractor_admin: Can create projects for their contractor
**Required Fields:**
- title: Project name (3-500 chars)
- client_id: Client organization UUID
- contractor_id: Contractor organization UUID
**Optional Configuration:**
- activation_requirements: Dynamic form fields for subscription activation
- photo_requirements: Photo evidence requirements
- budget: Budget breakdown by category
- inventory_requirements: Equipment/materials needed
"""
try:
project = ProjectService.create_project(db, data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="create",
entity_type="project",
entity_id=str(project.id),
description=f"Created project '{project.title}'",
changes={"status": "created"},
request=request
)
# Prepare response with nested data
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error creating project: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while creating the project"
)
def parse_project_filters(
client_id: Optional[UUID] = Query(None),
contractor_id: Optional[UUID] = Query(None),
primary_manager_id: Optional[UUID] = Query(None),
status: Optional[str] = Query(None),
project_type: Optional[str] = Query(None),
service_type: Optional[str] = Query(None),
is_closed: Optional[bool] = Query(None),
is_billable: Optional[bool] = Query(None),
planned_start_from: Optional[date] = Query(None),
planned_start_to: Optional[date] = Query(None),
actual_start_from: Optional[date] = Query(None),
actual_start_to: Optional[date] = Query(None),
search: Optional[str] = Query(None),
sort_by: Optional[str] = Query(None),
sort_order: str = Query("desc"),
page: int = Query(1, ge=1),
page_size: int = Query(50, ge=1, le=100),
from_date: Optional[date] = Query(None),
to_date: Optional[date] = Query(None),
) -> ProjectFilters:
"""Parse and convert query parameters to ProjectFilters"""
def parse_csv(value: Optional[str]) -> Optional[List[str]]:
if value is None:
return None
return [item.strip() for item in value.split(',') if item.strip()]
return ProjectFilters(
client_id=client_id,
contractor_id=contractor_id,
primary_manager_id=primary_manager_id,
status=parse_csv(status),
project_type=parse_csv(project_type),
service_type=parse_csv(service_type),
is_closed=is_closed,
is_billable=is_billable,
planned_start_from=planned_start_from,
planned_start_to=planned_start_to,
actual_start_from=actual_start_from,
actual_start_to=actual_start_to,
search=search,
sort_by=sort_by,
sort_order=sort_order,
page=page,
page_size=page_size,
from_date=from_date,
to_date=to_date,
)
@router.get("", response_model=ProjectListResponse)
async def list_projects(
# New pattern (preferred)
filters: ProjectFilters = Depends(parse_project_filters),
# Legacy support for backward compatibility
skip: Optional[int] = Query(None, ge=0, description="DEPRECATED: Use page instead"),
limit: Optional[int] = Query(None, ge=1, le=100, description="DEPRECATED: Use page_size instead"),
status_filter: Optional[str] = Query(None, alias="status", description="DEPRECATED: Use filters.status"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
List projects with comprehensive filtering support.
**NEW FEATURES:**
- Multi-value filters: `?status=active,planning&project_type=customer_service`
- Date ranges: `?planned_start_from=2024-01-01&planned_start_to=2024-12-31`
- Boolean filters: `?is_closed=false&is_billable=true`
- Text search: `?search=fiber network`
- Flexible sorting: `?sort_by=title&sort_order=asc`
- Standardized pagination: `?page=1&page_size=50`
**Authorization:** User sees only projects they have access to
- platform_admin: Sees all projects
- client_admin: Sees their client's projects
- contractor_admin: Sees their contractor's projects
- project team members: See projects they're assigned to (including draft projects where they're primary_manager)
**All Filters:**
- `client_id`: Filter by client UUID
- `contractor_id`: Filter by contractor UUID
- `primary_manager_id`: Filter by primary manager UUID
- `status`: Multi-value (draft,planning,active,on_hold,completed,cancelled)
- `project_type`: Multi-value (customer_service,infrastructure)
- `service_type`: Multi-value (ftth,fttb,fixed_wireless,dsl,etc.)
- `is_closed`: Boolean
- `is_billable`: Boolean
- `planned_start_from/to`: Date range
- `actual_start_from/to`: Date range
- `from_date/to_date`: Created date range
- `search`: Text search across title, description
- `sort_by`: Field to sort by
- `sort_order`: asc or desc
- `page`: Page number (1-based)
- `page_size`: Items per page (1-100)
**Examples:**
```
# Multi-value filters
GET /projects?status=active,planning&project_type=customer_service
# Date range
GET /projects?planned_start_from=2024-01-01&planned_start_to=2024-12-31
# Boolean filters
GET /projects?is_closed=false&is_billable=true
# Search
GET /projects?search=fiber network
# Combined
GET /projects?status=active&is_closed=false&search=network&page=1&page_size=20
```
**DEPRECATION NOTICE:** `skip`, `limit`, and `status` query parameters are deprecated. Use the new filter parameters instead.
"""
# Handle backward compatibility
if skip is not None and filters.page == 1:
filters.page = (skip // filters.page_size) + 1
if limit is not None:
filters.page_size = limit
if status_filter and not filters.status:
filters.status = [status_filter]
# Use new filtering method
projects, total = ProjectService.list_projects_with_filters(
db=db,
filters=filters,
current_user=current_user
)
# Convert to response models with nested data
project_responses = []
for project in projects:
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
project_responses.append(response)
# Calculate pagination
total_pages = math.ceil(total / filters.page_size) if total > 0 else 0
return ProjectListResponse(
projects=project_responses,
total=total,
page=filters.page,
page_size=filters.page_size,
total_pages=total_pages
)
@router.get("/available-managers", response_model=List[dict])
async def get_available_project_managers(
client_id: Optional[UUID] = Query(None, description="Client organization ID"),
contractor_id: Optional[UUID] = Query(None, description="Contractor organization ID"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get users who can be assigned as primary_manager for a project
**Use Case:**
- Frontend dropdown/picker when creating a project
- Shows eligible users (PMs, Sales Managers) from relevant organizations
**Returns users with roles:**
- project_manager
- sales_manager
**From organizations:**
- If client_id provided: users from that client org
- If contractor_id provided: users from that contractor org
- If both provided: users from BOTH organizations
- If neither: uses current user's organization
**Authorization:**
- Org admins can query their org + partner orgs
- Platform admin can query any org
"""
from sqlalchemy import or_
# Build organization filter
org_filters = []
if client_id:
org_filters.append(User.client_id == client_id)
if contractor_id:
org_filters.append(User.contractor_id == contractor_id)
# If no orgs specified, use current user's org
if not org_filters:
if current_user.client_id:
org_filters.append(User.client_id == current_user.client_id)
elif current_user.contractor_id:
org_filters.append(User.contractor_id == current_user.contractor_id)
if not org_filters:
return []
# Query users
query = db.query(User).filter(
User.deleted_at == None,
User.status == 'active',
User.role.in_(['project_manager', 'sales_manager']),
or_(*org_filters)
).order_by(User.name)
users = query.all()
# Format response
return [
{
"id": str(user.id),
"name": user.name,
"email": user.email,
"role": user.role,
"organization_type": "client" if user.client_id else "contractor",
"organization_id": str(user.client_id or user.contractor_id),
"phone": user.phone
}
for user in users
]
@router.get("/awaiting-setup/mine", response_model=ProjectListResponse)
async def get_my_draft_projects(
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get projects in 'draft' status where current user is primary_manager
**Use Case:**
- Shows projects awaiting setup by the primary manager
- Displayed in PM dashboard as "Projects Awaiting Setup"
**Authorization:**
- Returns only projects where user is the primary_manager
- Filtered to 'draft' status only
"""
# Get draft projects where user is primary manager
projects, total = ProjectService.list_projects(
db=db,
current_user=current_user,
skip=0,
limit=100,
status='draft'
)
# Further filter to only projects where user is primary_manager
my_draft_projects = [
p for p in projects
if p.primary_manager_id == current_user.id
]
# Convert to response models
project_responses = []
for project in my_draft_projects:
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
project_responses.append(response)
return ProjectListResponse(
projects=project_responses,
total=len(my_draft_projects),
page=1,
page_size=len(my_draft_projects),
total_pages=1
)
@router.get("/{project_id}", response_model=ProjectResponse)
async def get_project(
project_id: UUID,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get a specific project by ID
**Authorization:** User must have access to this project
"""
project = ProjectService.get_project_by_id(db, project_id, current_user)
# Prepare response with nested data
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
return response
@router.get("/{project_id}/dashboard")
async def get_project_dashboard(
project_id: UUID,
refresh: bool = Query(False, description="Force refresh cache"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get comprehensive project dashboard with all key metrics.
Returns consolidated statistics for:
- Tickets (total, open, in_progress, closed, today)
- Sales Orders (total, pending, processed, cancelled, today)
- Customers (customer_service projects) or Tasks (infrastructure projects)
- Expenses (count, amount, approvals, monthly)
- Team (members by role)
- Finances (revenue, expenses, profit, pending)
- Regions (total, active)
- Notifications (unread, total)
**Performance:**
- Cached for 5 minutes for optimal performance
- Use ?refresh=true to force cache refresh
- Automatically adapts based on project_type (customer_service vs infrastructure)
**Authorization:** Any user with project access
"""
try:
from app.services.dashboard_service import DashboardService
from app.schemas.dashboard import ProjectDashboardResponse
dashboard = DashboardService.get_project_dashboard(
db=db,
project_id=str(project_id),
current_user=current_user,
force_refresh=refresh
)
return dashboard
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to get project dashboard: {str(e)}", exc_info=True)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get project dashboard: {str(e)}"
)
@router.get("/{project_id}/activity-feed")
async def get_project_activity_feed(
project_id: UUID,
limit: int = Query(20, ge=1, le=50, description="Number of items per section"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get project activity feed: notifications, pending actions, recent activity.
Returns:
- **unread_notifications**: Unread notifications for current user in this project
- **pending_actions**: Items requiring action (expense approvals, ticket reviews, etc.)
- **recent_activity**: Recent project activity from audit logs
**Use Cases:**
- Dashboard notification panel
- Activity timeline widget
- Quick action items list
**Authorization:** Any user with project access
"""
try:
from app.services.dashboard_service import DashboardService
from app.services.notification_service import NotificationService
from app.services.audit_service import AuditService
from app.models.ticket_expense import TicketExpense
# Check authorization
if not DashboardService.can_user_access_project(current_user, str(project_id), db):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authorized to access this project"
)
# Get unread notifications (project-related)
# Note: Notification doesn't have deleted_at, priority, or action_url columns
notifications_query = db.query(Notification).filter(
Notification.user_id == current_user.id,
Notification.read_at.is_(None) # Unread means read_at is NULL
).order_by(Notification.created_at.desc()).limit(limit)
notifications = notifications_query.all()
notification_items = [
{
"id": str(n.id),
"type": n.notification_type,
"title": n.title,
"message": n.message,
"status": n.status.value if hasattr(n.status, 'value') else str(n.status),
"created_at": n.created_at.isoformat() + "Z" if n.created_at else None
}
for n in notifications
]
# Get pending actions (expenses pending approval)
pending_actions = []
if current_user.role in ['project_manager', 'platform_admin', 'client_admin', 'contractor_admin']:
# Join through Ticket to get project_id
from app.models.ticket import Ticket
pending_expenses = db.query(TicketExpense).join(
Ticket, TicketExpense.ticket_id == Ticket.id
).filter(
Ticket.project_id == project_id,
TicketExpense.is_approved == False,
TicketExpense.deleted_at.is_(None)
).order_by(TicketExpense.created_at.desc()).limit(limit).all()
for exp in pending_expenses:
pending_actions.append({
"type": "expense_approval",
"id": str(exp.id),
"description": f"Expense: {exp.category} - ${exp.total_cost:.2f}",
"submitted_by": exp.incurred_by_user.name if exp.incurred_by_user else "Unknown",
"submitted_at": exp.created_at.isoformat() + "Z" if exp.created_at else None,
"action_required": "approve_or_reject"
})
# Get recent activity from audit logs
# Note: For now, return empty array as AuditService doesn't have get_project_audit_logs method
# This can be implemented later with proper query filtering by project-related entities
recent_activity = []
return {
"unread_notifications": {
"count": len(notification_items),
"items": notification_items
},
"pending_actions": {
"count": len(pending_actions),
"items": pending_actions
},
"recent_activity": {
"count": len(recent_activity),
"items": recent_activity
}
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to get activity feed: {str(e)}", exc_info=True)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get activity feed: {str(e)}"
)
@router.get("/{project_id}/overview")
async def get_project_overview(
project_id: UUID,
refresh: bool = Query(False, description="Force refresh cache"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get comprehensive project overview (structure, not metrics).
Returns project details, regions, roles, subcontractors, and team information
tailored to the user's role and permissions.
**For Managers/Admins:**
- Full project details
- All regions with details
- All project roles with compensation structures
- All subcontractors
- Team composition summary
**For Field Agents/Sales Agents:**
- Basic project information
- All regions (UI highlights their assigned region)
- Their specific involvement (role, region, subcontractor)
- Project requirements (photo_requirements, activation_requirements)
- No access to other team members, all roles, or all subcontractors
**Performance:**
- Cached for 12 hours for optimal performance
- Use ?refresh=true to force cache refresh
- Structure changes rarely, so long cache is appropriate
**Authorization:** Any user with project access
"""
try:
from app.services.dashboard_service import DashboardService
from app.schemas.project import ProjectOverviewResponse
overview = DashboardService.get_project_overview(
db=db,
project_id=str(project_id),
current_user=current_user,
force_refresh=refresh
)
return overview
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to get project overview: {str(e)}", exc_info=True)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get project overview: {str(e)}"
)
@router.put("/{project_id}", response_model=ProjectResponse)
@router.patch("/{project_id}", response_model=ProjectResponse)
@require_permission("manage_projects")
async def update_project(
project_id: UUID,
data: ProjectUpdate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Update a project (supports both PUT and PATCH)
**Authorization:**
- platform_admin: Can update any project
- client_admin: Can update their client's projects
- contractor_admin: Can update their contractor's projects
- project_manager: Can update projects they manage
**Note:** Cannot change client_id or contractor_id after creation
"""
try:
# Get old state for audit
old_project = ProjectService.get_project_by_id(db, project_id, current_user)
old_data = {
"title": old_project.title,
"status": old_project.status,
"service_type": old_project.service_type
}
# Update project
project = ProjectService.update_project(db, project_id, data, current_user)
# Invalidate requirements cache if photo/activation requirements changed
if data.photo_requirements is not None or data.activation_requirements is not None:
from app.services.project_requirements_cache import ProjectRequirementsCache
ProjectRequirementsCache.invalidate(project_id)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project",
entity_id=str(project.id),
description=f"Updated project '{project.title}'",
changes={
"old": old_data,
"new": {
"title": project.title,
"status": project.status,
"service_type": project.service_type
}
},
request=request
)
# Prepare response
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating project: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while updating the project"
)
@router.post("/{project_id}/setup", response_model=ProjectSetupResponse)
async def complete_project_setup(
project_id: UUID,
setup_data: ProjectSetup,
background_tasks: BackgroundTasks,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Complete project setup (draft → planning)
**Authorization:**
- Only the primary_manager or platform_admin can complete setup
**What this does:**
1. Creates project regions/hubs
2. Creates project roles with compensation structures
3. Adds subcontractors
4. Adds core team members (PMs, dispatchers, sales managers)
5. Transitions project from 'draft' to 'planning' status
**Required:**
- At least 1 team member must be added
**Use Case:**
- Org admin creates project shell with primary_manager_id
- Primary manager logs in, sees "Awaiting Setup" projects
- Primary manager completes this setup wizard
- Project becomes ready for operations
"""
try:
result = ProjectService.complete_project_setup(db, project_id, setup_data, current_user, background_tasks)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project",
entity_id=str(project_id),
description=f"Completed project setup: {result['team_members_added']} team members, {result['regions_created']} regions",
changes={
"status": "draft → planning",
"regions_created": result['regions_created'],
"roles_created": result['roles_created'],
"subcontractors_added": result['subcontractors_added'],
"team_members_added": result['team_members_added']
},
request=request
)
return ProjectSetupResponse(**result)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error completing project setup: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while completing project setup"
)
@router.patch("/{project_id}/status", response_model=ProjectResponse)
@require_permission("manage_projects")
async def update_project_status(
project_id: UUID,
data: ProjectStatusUpdate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Update project status
**Status Values:**
- draft: Awaiting setup by primary manager
- planning: Initial state, project setup
- active: Project is running
- on_hold: Temporarily paused
- completed: Work finished
- cancelled: Project terminated
**Business Rules:**
- Cannot change status of closed projects
- Cannot revert from completed status
- Auto-sets actual_start_date when activating
- Auto-sets actual_end_date when completing
"""
try:
old_status = ProjectService.get_project_by_id(db, project_id, current_user).status
project = ProjectService.update_project_status(db, project_id, data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project",
entity_id=str(project.id),
description=f"Changed project '{project.title}' status from {old_status} to {data.status}",
changes={
"field": "status",
"old": old_status,
"new": data.status,
"reason": data.reason
},
request=request
)
# Prepare response
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating project status: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while updating project status"
)
@router.post("/{project_id}/close", response_model=ProjectResponse)
@require_permission("manage_projects")
async def close_project(
project_id: UUID,
data: ProjectClose,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Close a project (final action)
**Effect:**
- Sets is_closed = true
- Sets closed_at timestamp
- Records closed_by_user_id
- Sets status to completed if not already
- Sets actual_end_date if not already set
**Note:** Closed projects cannot be reopened
"""
try:
project = ProjectService.close_project(
db=db,
project_id=project_id,
current_user=current_user,
reason=data.reason
)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="archive",
entity_type="project",
entity_id=str(project.id),
description=f"Closed project '{project.title}'",
changes={
"action": "closed",
"reason": data.reason
},
request=request
)
# Prepare response
response = ProjectResponse.model_validate(project)
response.client_name = project.client.name if project.client else None
response.contractor_name = project.contractor.name if project.contractor else None
response.primary_manager_name = project.primary_manager.name if project.primary_manager else None
response.is_overdue = project.is_overdue
response.duration_days = project.duration_days
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error closing project: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while closing the project"
)
@router.delete("/{project_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_project(
project_id: UUID,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Soft delete a project
**Authorization:** platform_admin only
**Effect:**
- Sets deleted_at timestamp
- Cascades to child entities (regions, roles, team, subcontractors)
- Cannot be undone via API (requires database intervention)
"""
try:
ProjectService.delete_project(db, project_id, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="delete",
entity_type="project",
entity_id=str(project_id),
description=f"Deleted project (ID: {project_id})",
changes={"action": "soft_deleted"},
request=request
)
return None
except HTTPException:
raise
except Exception as e:
logger.error(f"Error deleting project: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while deleting the project"
)
# ============================================
# PROJECT TEAM MANAGEMENT
# ============================================
@router.get("/{project_id}/team")
async def get_project_team(
project_id: UUID,
role: Optional[str] = Query(None, description="Filter by system role"),
project_role_id: Optional[UUID] = Query(None, description="Filter by project role"),
project_region_id: Optional[UUID] = Query(None, description="Filter by region"),
is_active: Optional[bool] = Query(None, description="Filter by user active status"),
search: Optional[str] = Query(None, description="Search by name or email"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get all team members for a project with optional filters
**Authorization:**
- platform_admin: Can view any project team
- project_manager: Can view their own project teams
- sales_manager: Can view their organization's project teams
- dispatcher: Can view their contractor's project teams
- field_agent: Can view team members for projects they're assigned to (needed for inventory transfers)
**Query Parameters:**
- role: Filter by system role (field_agent, dispatcher, etc.)
- project_role_id: Filter by project role
- project_region_id: Filter by region
- is_active: Filter by user active status (true/false)
- search: Search by user name or email
**Returns:**
- List of team members with user details, roles, and regions
"""
# Get project
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Authorization check
if current_user.role == 'platform_admin':
pass # Can view any project
elif current_user.role == 'project_manager':
if str(project.primary_manager_id) != str(current_user.id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view team members for your own projects"
)
elif current_user.role == 'sales_manager':
# Can view if project belongs to their client organization
if str(current_user.client_id) != str(project.client_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view team members for your organization's projects"
)
elif current_user.role == 'dispatcher':
# Can view if project belongs to their contractor
if str(current_user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view team members for your contractor's projects"
)
elif current_user.role == 'field_agent':
# Field agents can view team members of projects they're assigned to
# This is needed for inventory transfers (selecting recipient agent)
team_member = db.query(ProjectTeam).filter(
ProjectTeam.project_id == project_id,
ProjectTeam.user_id == current_user.id,
ProjectTeam.deleted_at == None,
ProjectTeam.removed_at == None
).first()
if not team_member:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view team members for projects you're assigned to"
)
else:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Insufficient permissions to view project team"
)
# Build query with filters
query = db.query(ProjectTeam).filter(
ProjectTeam.project_id == project_id,
ProjectTeam.deleted_at == None,
ProjectTeam.removed_at == None
)
# Apply filters
if role:
query = query.join(User).filter(User.role == role)
if project_role_id:
query = query.filter(ProjectTeam.project_role_id == project_role_id)
if project_region_id:
query = query.filter(ProjectTeam.project_region_id == project_region_id)
if is_active is not None:
query = query.join(User).filter(User.is_active == is_active)
if search:
query = query.join(User).filter(
or_(
User.name.ilike(f"%{search}%"),
User.email.ilike(f"%{search}%")
)
)
team_members = query.all()
# Build response with nested data
response_list = []
for member in team_members:
member_data = {
**member.__dict__,
"user_name": member.user.name if member.user else None,
"user_email": member.user.email if member.user else None,
"user_role": member.user.role if member.user else None,
}
# Add role name if project_role is assigned
if member.project_role_id and member.project_role:
member_data["role_name"] = member.project_role.role_name
# Add region name if region is assigned
if member.project_region_id and member.region:
member_data["region_name"] = member.region.region_name
response_list.append(ProjectTeamResponse.model_validate(member_data))
return response_list
@router.patch("/{project_id}/team/{member_id}", response_model=ProjectTeamResponse)
async def update_project_team_member(
project_id: UUID,
member_id: UUID,
update_data: ProjectTeamUpdate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Update a project team member's role, region, or other attributes
**Authorization:**
- platform_admin: Can update any team member
- project_manager: Can update their own project's team members
- dispatcher: Can update team members in their contractor's projects
**Use Case:** Change someone's role, region assignment, or lead status
"""
# Verify project exists
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Project {project_id} not found"
)
# Check permission
if current_user.role != 'platform_admin':
if current_user.role == 'project_manager' and str(project.primary_manager_id) != str(current_user.id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only update team members in your own projects"
)
elif current_user.role == 'dispatcher' and str(current_user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only update team members from your contractor"
)
elif current_user.role not in ['project_manager', 'dispatcher']:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to update team members"
)
# Get team member
team_member = db.query(ProjectTeam).filter(
ProjectTeam.id == member_id,
ProjectTeam.project_id == project_id,
ProjectTeam.deleted_at == None
).first()
if not team_member:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Team member {member_id} not found in this project"
)
# Update fields
update_dict = update_data.model_dump(exclude_unset=True)
for field, value in update_dict.items():
setattr(team_member, field, value)
db.commit()
db.refresh(team_member)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project_team",
entity_id=str(team_member.id),
description=f"Updated team member {team_member.user.name if team_member.user else member_id} in project {project.title}",
changes=update_dict,
request=request
)
# Build response with nested data
response_data = {
**team_member.__dict__,
"user_name": team_member.user.name if team_member.user else None,
"user_email": team_member.user.email if team_member.user else None,
"user_role": team_member.user.role if team_member.user else None
}
if team_member.project_role_id and team_member.project_role:
response_data["role_name"] = team_member.project_role.role_name
if team_member.project_region_id and team_member.region:
response_data["region_name"] = team_member.region.region_name
return ProjectTeamResponse.model_validate(response_data)
@router.delete("/{project_id}/team/{member_id}", status_code=status.HTTP_204_NO_CONTENT)
async def remove_project_team_member(
project_id: UUID,
member_id: UUID,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Remove a team member from the project (soft delete)
**Authorization:**
- platform_admin: Can remove any team member
- project_manager: Can remove team members from their own projects
- dispatcher: Can remove team members from their contractor's projects
**Effect:**
- Sets removed_at timestamp (soft removal)
- User account remains active, just removed from this project
- Can be re-added later if needed
"""
# Verify project exists
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Project {project_id} not found"
)
# Check permission
if current_user.role != 'platform_admin':
if current_user.role == 'project_manager' and str(project.primary_manager_id) != str(current_user.id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only remove team members from your own projects"
)
elif current_user.role == 'dispatcher' and str(current_user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only remove team members from your contractor"
)
elif current_user.role not in ['project_manager', 'dispatcher']:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to remove team members"
)
# Get team member
team_member = db.query(ProjectTeam).filter(
ProjectTeam.id == member_id,
ProjectTeam.project_id == project_id,
ProjectTeam.deleted_at == None
).first()
if not team_member:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Team member {member_id} not found in this project"
)
# Soft remove by setting removed_at
from datetime import datetime
team_member.removed_at = datetime.utcnow()
db.commit()
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="delete",
entity_type="project_team",
entity_id=str(team_member.id),
description=f"Removed {team_member.user.name if team_member.user else member_id} from project {project.title}",
changes={"removed_at": team_member.removed_at.isoformat()},
request=request
)
return None
@router.post("/{project_id}/team/invite", status_code=status.HTTP_201_CREATED)
async def invite_to_project_team(
project_id: UUID,
invitation_data: ProjectInvitationCreate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Invite a NEW user to join project team
**Use Case:** PM/Dispatcher inviting external technicians who don't have accounts yet
**Authorization:**
- platform_admin: Can invite to any project
- project_manager: Can invite to their own projects
- dispatcher: Can invite field_agent/driver to projects in their contractor
- sales_manager: Can invite sales_agent to projects in their contractor
**Note:** If user already exists, use POST /projects/{id}/team instead
"""
# Verify project exists
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Project {project_id} not found"
)
# Determine which organization the invited user should belong to based on role
# Contractor roles: dispatcher, field_agent, driver
# Client roles: client_admin
# Can be either: sales_manager, sales_agent, project_manager
contractor_roles = ['dispatcher', 'field_agent', 'driver']
client_roles = ['client_admin']
# Determine org assignment based on invited role
if invitation_data.invited_role in contractor_roles:
# Contractor-specific roles always belong to project's contractor
org_client_id = None
org_contractor_id = project.contractor_id
elif invitation_data.invited_role in client_roles:
# Client-specific roles always belong to project's client
org_client_id = project.client_id
org_contractor_id = None
else:
# For ambiguous roles (sales_manager, sales_agent, project_manager),
# default to contractor (most common case for project invitations)
org_client_id = None
org_contractor_id = project.contractor_id
# Build full invitation with project context
full_invitation = InvitationCreate(
email=invitation_data.email,
phone=invitation_data.phone,
invited_name=invitation_data.invited_name,
invited_role=invitation_data.invited_role,
client_id=org_client_id,
contractor_id=org_contractor_id,
invitation_method=invitation_data.invitation_method,
project_id=project_id,
project_role_id=invitation_data.project_role_id,
project_region_id=invitation_data.project_region_id,
project_subcontractor_id=invitation_data.project_subcontractor_id
)
invitation_service = InvitationService()
invitation = await invitation_service.create_invitation(
invitation_data=full_invitation,
invited_by_user=current_user,
db=db
)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="create",
entity_type="invitation",
entity_id=str(invitation.id),
description=f"Invited {invitation.email} to project {project.title}",
changes={"project_id": str(project_id), "role": invitation.invited_role},
request=request
)
return InvitationResponse.model_validate(invitation)
@router.post("/{project_id}/team")
async def add_existing_user_to_project(
project_id: UUID,
team_data: ProjectTeamCreate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Add an EXISTING user directly to project team (no invitation)
**Use Case:** Adding users who already work for the contractor to this project
**Authorization:**
- platform_admin: Can add to any project
- project_manager: Can add to their own projects
- dispatcher: Can add users from their contractor
**Note:** For new users who need accounts, use POST /projects/{id}/team/invite
"""
from app.models.project_team import ProjectTeam
# Verify project exists
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Project {project_id} not found"
)
# Check permission
if current_user.role != 'platform_admin':
if current_user.role == 'project_manager' and str(project.primary_manager_id) != str(current_user.id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only add team members to your own projects"
)
elif current_user.role == 'dispatcher' and str(current_user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only add team members from your contractor"
)
elif current_user.role not in ['project_manager', 'dispatcher']:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to add team members"
)
# Verify user exists
user = db.query(User).filter(
User.id == team_data.user_id,
User.deleted_at == None
).first()
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User {team_data.user_id} not found. Use the invite endpoint for new users."
)
# Verify user belongs to correct contractor
if str(user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"User must belong to project's contractor organization"
)
# Check if already on team
existing = db.query(ProjectTeam).filter(
ProjectTeam.project_id == project_id,
ProjectTeam.user_id == team_data.user_id,
ProjectTeam.deleted_at == None
).first()
if existing:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="User is already on this project team"
)
# Create team member
from datetime import datetime
team_member = ProjectTeam(
**team_data.model_dump(),
project_id=project_id,
assigned_at=datetime.utcnow()
)
db.add(team_member)
db.commit()
db.refresh(team_member)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="create",
entity_type="project_team",
entity_id=str(team_member.id),
description=f"Added {user.name} to project {project.title}",
changes={"user_id": str(team_data.user_id), "project_id": str(project_id)},
request=request
)
# Build response with nested data
response_data = {
**team_member.__dict__,
"user_name": user.name,
"user_email": user.email,
"user_role": user.role
}
# Add role name if project_role is assigned
if team_member.project_role_id and team_member.project_role:
response_data["role_name"] = team_member.project_role.role_name
# Add region name if region is assigned
if team_member.project_region_id and team_member.region:
response_data["region_name"] = team_member.region.region_name
return ProjectTeamResponse.model_validate(response_data)
@router.get("/{project_id}/team/invitations")
async def list_project_invitations(
project_id: UUID,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db),
status: Optional[str] = Query(None, description="Filter by status: pending, accepted, expired, cancelled")
):
"""
List invitations for a specific project
**Authorization:**
- platform_admin: Can view any project's invitations
- project_manager: Can view their project's invitations
- dispatcher: Can view invitations for projects in their contractor
"""
from app.models.invitation import UserInvitation
# Verify project exists
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Project {project_id} not found"
)
# Check permission
if current_user.role != 'platform_admin':
if current_user.role == 'project_manager' and str(project.primary_manager_id) != str(current_user.id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view invitations for your own projects"
)
elif current_user.role == 'dispatcher' and str(current_user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view invitations for projects in your contractor"
)
elif current_user.role not in ['project_manager', 'dispatcher', 'sales_manager']:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to view project invitations"
)
# Build query
query = db.query(UserInvitation).filter(
UserInvitation.project_id == project_id,
UserInvitation.deleted_at == None
)
if status:
query = query.filter(UserInvitation.status == status)
invitations = query.order_by(UserInvitation.created_at.desc()).all()
return [InvitationResponse.model_validate(inv) for inv in invitations]
@router.get("/{project_id}/team/invitations/stats", response_model=InvitationStats)
async def get_project_invitation_stats(
project_id: UUID,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get invitation statistics for a project
**Returns:**
- Total invitations sent
- Pending invitations (not yet accepted)
- Accepted invitations (user created)
- Expired invitations (past expiry date)
- Cancelled invitations (manually cancelled)
**Authorization:**
- platform_admin: Can view any project's stats
- project_manager: Can view their project's stats
- dispatcher: Can view their contractor's project stats
**Use Case:** Dashboard metrics for invitation management page
"""
from app.models.invitation import UserInvitation
from sqlalchemy import func
# Verify project exists
project = db.query(Project).filter(
Project.id == project_id,
Project.deleted_at == None
).first()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Project {project_id} not found"
)
# Check permission
if current_user.role != 'platform_admin':
if current_user.role == 'project_manager' and str(project.primary_manager_id) != str(current_user.id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view invitation stats for your own projects"
)
elif current_user.role == 'dispatcher' and str(current_user.contractor_id) != str(project.contractor_id):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You can only view invitation stats for your contractor's projects"
)
elif current_user.role not in ['project_manager', 'dispatcher', 'sales_manager']:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to view invitation stats"
)
# Base query for project invitations
base_query = db.query(UserInvitation).filter(
UserInvitation.project_id == project_id,
UserInvitation.deleted_at == None
)
# Get counts by status
total = base_query.count()
pending = base_query.filter(UserInvitation.status == 'pending').count()
accepted = base_query.filter(UserInvitation.status == 'accepted').count()
expired = base_query.filter(UserInvitation.status == 'expired').count()
cancelled = base_query.filter(UserInvitation.status == 'cancelled').count()
return InvitationStats(
total=total,
pending=pending,
accepted=accepted,
expired=expired,
cancelled=cancelled
)
# ============================================
# PROJECT REGIONS MANAGEMENT
# ============================================
@router.post("/{project_id}/regions", response_model=ProjectRegionResponse, status_code=status.HTTP_201_CREATED)
async def create_project_region(
project_id: UUID,
region_data: ProjectRegionCreate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Create a new project region/hub
**Authorization:**
- platform_admin: Can create for any project
- project_manager: Can create for their own projects
**Use Case:**
- PM defines regional hubs/areas for the project
- Regions can have managers and are used for team/subcontractor assignment
**Required Fields:**
- region_name: Name of the region (must be unique within project)
**Optional Fields:**
- region_code: Short code for the region
- description: Region description
- manager_id: Regional manager user ID
- hub_contact_persons: Array of contact persons for public tracking
"""
try:
region = ProjectService.create_region(db, project_id, region_data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="create",
entity_type="project_region",
entity_id=str(region.id),
description=f"Created region '{region.region_name}' for project {project_id}",
changes={"region_name": region.region_name, "region_code": region.region_code},
request=request
)
response = ProjectRegionResponse.model_validate(region)
if region.manager:
response.manager_name = region.manager.name
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error creating region: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while creating the region"
)
@router.get("/{project_id}/regions", response_model=List[ProjectRegionResponse])
async def list_project_regions(
project_id: UUID,
is_active: Optional[bool] = Query(None, description="Filter by active status"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
List all regions for a project
**Authorization:**
- platform_admin: Can view any project's regions
- project_manager: Can view their own project's regions
**Query Parameters:**
- is_active: Filter by active status (true/false)
"""
try:
regions = ProjectService.get_regions(db, project_id, current_user, is_active)
response_list = []
for region in regions:
region_response = ProjectRegionResponse.model_validate(region)
if region.manager:
region_response.manager_name = region.manager.name
response_list.append(region_response)
return response_list
except HTTPException:
raise
except Exception as e:
logger.error(f"Error listing regions: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while listing regions"
)
@router.get("/{project_id}/regions/{region_id}", response_model=ProjectRegionResponse)
async def get_project_region(
project_id: UUID,
region_id: UUID,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Get a specific project region by ID.
**Authorization:**
- PM, Dispatcher, Field Agent: Can view regions in their projects
- Platform Admin: Can view any region
"""
from app.models.project import ProjectRegion
# Get region
region = db.query(ProjectRegion).filter(
ProjectRegion.id == region_id,
ProjectRegion.project_id == project_id,
ProjectRegion.deleted_at.is_(None)
).first()
if not region:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Region not found"
)
# Authorization check
if current_user.role != AppRole.PLATFORM_ADMIN.value:
# Check if user has access to this project
from app.models.project_team import ProjectTeam
has_access = db.query(ProjectTeam).filter(
ProjectTeam.project_id == project_id,
ProjectTeam.user_id == current_user.id,
ProjectTeam.deleted_at.is_(None)
).first() is not None
if not has_access:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authorized to view this region"
)
return region
@router.patch("/{project_id}/regions/{region_id}", response_model=ProjectRegionResponse)
async def update_project_region(
project_id: UUID,
region_id: UUID,
region_data: ProjectRegionUpdate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Update a project region
**Authorization:**
- platform_admin: Can update any region
- project_manager: Can update their own project's regions
**All fields are optional** - only provided fields will be updated
"""
try:
region = ProjectService.update_region(db, region_id, region_data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project_region",
entity_id=str(region.id),
description=f"Updated region '{region.region_name}'",
changes=region_data.model_dump(exclude_unset=True),
request=request
)
response = ProjectRegionResponse.model_validate(region)
if region.manager:
response.manager_name = region.manager.name
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating region: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while updating the region"
)
@router.delete("/{project_id}/regions/{region_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_project_region(
project_id: UUID,
region_id: UUID,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Delete a project region (soft delete)
**Authorization:**
- platform_admin: Can delete any region
- project_manager: Can delete their own project's regions
**Restrictions:**
- Cannot delete if team members are assigned to this region
"""
try:
ProjectService.delete_region(db, region_id, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="delete",
entity_type="project_region",
entity_id=str(region_id),
description=f"Deleted region {region_id}",
changes={},
request=request
)
return None
except HTTPException:
raise
except Exception as e:
logger.error(f"Error deleting region: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while deleting the region"
)
# ============================================
# PROJECT ROLES MANAGEMENT
# ============================================
@router.post("/{project_id}/project-roles", response_model=ProjectRoleResponse, status_code=status.HTTP_201_CREATED)
async def create_project_role(
project_id: UUID,
role_data: ProjectRoleCreate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Create a new project role with compensation structure
**Authorization:**
- platform_admin: Can create for any project
- project_manager: Can create for their own projects
**Use Case:**
- PM defines roles for field workers/casual workers
- Roles include compensation structures for payroll calculation
**Required Fields:**
- role_name: Name of the role (must be unique within project)
- compensation_type: flat_rate, commission, hourly, commission_plus_bonus, or custom
**Compensation Requirements by Type:**
- FIXED_RATE: Requires base_rate + rate_period (HOUR, DAY, WEEK, MONTH)
- PER_UNIT: Requires per_unit_rate
- COMMISSION: Requires commission_percentage
- FIXED_PLUS_COMMISSION: Requires base_rate + rate_period + commission_percentage
**Examples:**
- Kenya daily worker: FIXED_RATE, base_rate=1000, rate_period=DAY
- USA hourly worker: FIXED_RATE, base_rate=25, rate_period=HOUR
- Per-ticket tech: PER_UNIT, per_unit_rate=500
- Sales agent: COMMISSION, commission_percentage=10
- Hybrid: FIXED_PLUS_COMMISSION, base_rate=500, rate_period=DAY, commission_percentage=5
"""
try:
role = ProjectService.create_project_role(db, project_id, role_data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="create",
entity_type="project_role",
entity_id=str(role.id),
description=f"Created role '{role.role_name}' for project {project_id}",
changes={"role_name": role.role_name, "compensation_type": role.compensation_type},
request=request
)
return ProjectRoleResponse.model_validate(role)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error creating role: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while creating the role"
)
@router.get("/{project_id}/project-roles", response_model=List[ProjectRoleResponse])
async def list_project_roles(
project_id: UUID,
is_active: Optional[bool] = Query(None, description="Filter by active status"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
List all roles for a project
**Authorization:**
- platform_admin: Can view any project's roles
- project_manager: Can view their own project's roles
**Query Parameters:**
- is_active: Filter by active status (true/false)
"""
try:
roles = ProjectService.get_project_roles(db, project_id, current_user, is_active)
return [ProjectRoleResponse.model_validate(role) for role in roles]
except HTTPException:
raise
except Exception as e:
logger.error(f"Error listing roles: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while listing roles"
)
@router.patch("/{project_id}/project-roles/{role_id}", response_model=ProjectRoleResponse)
async def update_project_role(
project_id: UUID,
role_id: UUID,
role_data: ProjectRoleUpdate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Update a project role
**Authorization:**
- platform_admin: Can update any role
- project_manager: Can update their own project's roles
**All fields are optional** - only provided fields will be updated
**Note:** When updating compensation_type, ensure required compensation fields are provided
"""
try:
role = ProjectService.update_project_role(db, role_id, role_data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project_role",
entity_id=str(role.id),
description=f"Updated role '{role.role_name}'",
changes=role_data.model_dump(exclude_unset=True),
request=request
)
return ProjectRoleResponse.model_validate(role)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating role: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while updating the role"
)
@router.delete("/{project_id}/project-roles/{role_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_project_role(
project_id: UUID,
role_id: UUID,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Delete a project role (soft delete)
**Authorization:**
- platform_admin: Can delete any role
- project_manager: Can delete their own project's roles
**Restrictions:**
- Cannot delete if team members are using this role
"""
try:
ProjectService.delete_project_role(db, role_id, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="delete",
entity_type="project_role",
entity_id=str(role_id),
description=f"Deleted role {role_id}",
changes={},
request=request
)
return None
except HTTPException:
raise
except Exception as e:
logger.error(f"Error deleting role: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while deleting the role"
)
# ============================================
# PROJECT SUBCONTRACTORS MANAGEMENT
# ============================================
@router.post("/{project_id}/subcontractors", response_model=ProjectSubcontractorResponse, status_code=status.HTTP_201_CREATED)
async def add_project_subcontractor(
project_id: UUID,
subcontractor_data: ProjectSubcontractorCreate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Add a subcontractor to a project
**Authorization:**
- platform_admin: Can add to any project
- project_manager: Can add to their own projects
**Use Case:**
- PM adds subcontractors who will work on the project
- Subcontractors can be assigned to specific regions
- When inviting team members, PM can specify which subcontractor they work for
**Required Fields:**
- subcontractor_id: ID of the contractor organization
**Optional Fields:**
- scope_description: What work is this subcontractor responsible for
- project_region_id: Limit subcontractor to specific region
- contract_start_date, contract_end_date: Contract period
- contract_value: Contract value
**Restrictions:**
- Cannot add the main contractor as a subcontractor
- Subcontractor must be an active contractor organization
"""
try:
project_subcon = ProjectService.create_subcontractor(db, project_id, subcontractor_data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="create",
entity_type="project_subcontractor",
entity_id=str(project_subcon.id),
description=f"Added subcontractor {subcontractor_data.subcontractor_id} to project {project_id}",
changes={"subcontractor_id": str(subcontractor_data.subcontractor_id)},
request=request
)
response = ProjectSubcontractorResponse.model_validate(project_subcon)
if project_subcon.subcontractor:
response.subcontractor_name = project_subcon.subcontractor.name
if project_subcon.region:
response.region_name = project_subcon.region.region_name
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error adding subcontractor: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while adding the subcontractor"
)
@router.get("/{project_id}/subcontractors", response_model=List[ProjectSubcontractorResponse])
async def list_project_subcontractors(
project_id: UUID,
is_active: Optional[bool] = Query(None, description="Filter by active status"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
List all subcontractors for a project
**Authorization:**
- platform_admin: Can view any project's subcontractors
- project_manager: Can view their own project's subcontractors
**Query Parameters:**
- is_active: Filter by active status (true/false)
"""
try:
subcontractors = ProjectService.get_subcontractors(db, project_id, current_user, is_active)
response_list = []
for subcon in subcontractors:
subcon_response = ProjectSubcontractorResponse.model_validate(subcon)
if subcon.subcontractor:
subcon_response.subcontractor_name = subcon.subcontractor.name
if subcon.region:
subcon_response.region_name = subcon.region.region_name
response_list.append(subcon_response)
return response_list
except HTTPException:
raise
except Exception as e:
logger.error(f"Error listing subcontractors: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while listing subcontractors"
)
@router.patch("/{project_id}/subcontractors/{subcontractor_id}", response_model=ProjectSubcontractorResponse)
async def update_project_subcontractor(
project_id: UUID,
subcontractor_id: UUID,
subcontractor_data: ProjectSubcontractorUpdate,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Update a project subcontractor
**Authorization:**
- platform_admin: Can update any subcontractor
- project_manager: Can update their own project's subcontractors
**All fields are optional** - only provided fields will be updated
**Deactivation:**
- Set is_active=false to deactivate a subcontractor
- Provide deactivation_reason when deactivating
"""
try:
project_subcon = ProjectService.update_subcontractor(db, subcontractor_id, subcontractor_data, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project_subcontractor",
entity_id=str(project_subcon.id),
description=f"Updated subcontractor relationship {subcontractor_id}",
changes=subcontractor_data.model_dump(exclude_unset=True),
request=request
)
response = ProjectSubcontractorResponse.model_validate(project_subcon)
if project_subcon.subcontractor:
response.subcontractor_name = project_subcon.subcontractor.name
if project_subcon.region:
response.region_name = project_subcon.region.region_name
return response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating subcontractor: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while updating the subcontractor"
)
@router.delete("/{project_id}/subcontractors/{subcontractor_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_project_subcontractor(
project_id: UUID,
subcontractor_id: UUID,
request: Request,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Delete a project subcontractor (soft delete)
**Authorization:**
- platform_admin: Can delete any subcontractor
- project_manager: Can delete their own project's subcontractors
**Restrictions:**
- Cannot delete if team members are assigned to this subcontractor
"""
try:
ProjectService.delete_subcontractor(db, subcontractor_id, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="delete",
entity_type="project_subcontractor",
entity_id=str(subcontractor_id),
description=f"Deleted subcontractor relationship {subcontractor_id}",
changes={},
request=request
)
return None
except HTTPException:
raise
except Exception as e:
logger.error(f"Error deleting subcontractor: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while deleting the subcontractor"
)
# ============================================
# PROJECT FINALIZATION
# ============================================
@router.post("/{project_id}/finalize-setup")
async def finalize_project_setup(
project_id: UUID,
setup_notes: Optional[str] = None,
request: Request = None,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Finalize project setup and activate the project (draft/planning → active)
**Authorization:**
- platform_admin: Can finalize any project
- project_manager: Can finalize their own projects
**Use Case:**
- PM completes the setup wizard and activates the project
- Project moves from 'draft' or 'planning' to 'active' status
- Returns summary of all configured items
- Safe to call on both draft and planning projects
**Validation:**
- Project must be in 'draft' or 'planning' status
- Cannot finalize projects that are already 'active', 'on_hold', 'completed', or 'cancelled'
- At least 1 team member must exist
**Optional:**
- setup_notes: Any final notes about the setup
**Returns:**
- Project status (will be 'active')
- Summary counts: regions, roles, subcontractors, team members
- Configuration flags: budget, activation requirements, photo requirements, inventory
"""
try:
result = ProjectService.finalize_project_setup(db, project_id, setup_notes, current_user)
# Audit log
AuditService.log_action(
db=db,
user=current_user,
action="update",
entity_type="project",
entity_id=str(project_id),
description=f"Finalized project setup: draft → planning",
changes={"status": "draft → planning", "summary": result['summary']},
request=request
)
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Error finalizing project setup: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while finalizing project setup"
)
# ============================================
# INVENTORY REQUIREMENTS
# ============================================
@router.get("/{project_id}/inventory-requirements")
async def get_project_inventory_requirements(
project_id: UUID,
usage_type: Optional[str] = Query(None, description="Filter by usage type: 'installed' or 'consumed'"),
category: Optional[str] = Query(None, description="Filter by category"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get inventory requirements for a project
Returns the inventory types that can be received and tracked for this project.
Used when receiving inventory batches to populate dropdown options.
**Authorization:**
- Any user on the project team can view
**Query Parameters:**
- usage_type: Filter by 'installed' (equipment installed at customer sites) or 'consumed' (materials used up)
- category: Filter by category (e.g., 'Equipment', 'Cable', 'Tools')
**Response:**
- Dictionary of inventory requirements keyed by code
- Each requirement includes: code, name, description, usage_type, unit, requires_serial_number, category
"""
from app.services.inventory_requirements_service import InventoryRequirementsService
try:
# Get inventory requirements
inventory_reqs = InventoryRequirementsService.get_project_inventory_requirements(
project_id=project_id,
db=db,
usage_type=usage_type,
category=category
)
return {
"project_id": str(project_id),
"inventory_requirements": inventory_reqs,
"total_count": len(inventory_reqs)
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting inventory requirements: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve inventory requirements"
)
@router.get("/{project_id}/inventory-requirements/dropdown")
async def get_inventory_dropdown_options(
project_id: UUID,
usage_type: Optional[str] = Query(None, description="Filter by usage type: 'installed' or 'consumed'"),
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get inventory options formatted for dropdown selection
Used when receiving inventory batches - provides a clean list of options
with code, name, description, and metadata.
**Authorization:**
- Any user on the project team can view
**Response:**
- Array of dropdown options sorted by category and name
- Each option includes: code, name, description, category, unit, requires_serial_number, usage_type
"""
from app.services.inventory_requirements_service import InventoryRequirementsService
try:
options = InventoryRequirementsService.get_inventory_dropdown_options(
project_id=project_id,
db=db,
usage_type=usage_type
)
return {
"project_id": str(project_id),
"options": options,
"total_count": len(options)
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting inventory dropdown options: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve inventory dropdown options"
)
@router.get("/{project_id}/inventory-requirements/completion-items")
async def get_inventory_completion_items(
project_id: UUID,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get inventory items that appear in ticket completion forms
Returns only 'installed' items with include_in_completion=True.
These items will be dynamically added to ticket completion checklists.
**Authorization:**
- Any user on the project team can view
**Response:**
- Array of inventory items that require completion data
- Each item includes: code, name, description, requires_serial_number, completion_field_label, completion_required
"""
from app.services.inventory_requirements_service import InventoryRequirementsService
try:
completion_items = InventoryRequirementsService.get_installed_inventory_for_completion(
project_id=project_id,
db=db
)
return {
"project_id": str(project_id),
"completion_items": completion_items,
"total_count": len(completion_items)
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting inventory completion items: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve inventory completion items"
)
@router.get("/{project_id}/inventory-requirements/categories")
async def get_inventory_categories(
project_id: UUID,
current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""
Get list of unique inventory categories in project
Useful for filtering and grouping inventory items.
**Authorization:**
- Any user on the project team can view
**Response:**
- Array of category names (sorted alphabetically)
"""
from app.services.inventory_requirements_service import InventoryRequirementsService
try:
categories = InventoryRequirementsService.get_inventory_categories(
project_id=project_id,
db=db
)
return {
"project_id": str(project_id),
"categories": categories,
"total_count": len(categories)
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting inventory categories: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve inventory categories"
)
|