aggiunti trasferte, export excel, miglioramenti generali

This commit is contained in:
2026-02-04 12:55:04 +01:00
parent 17453f5d13
commit 5f4ef6faee
30 changed files with 1558 additions and 325 deletions

52
services/offices.py Normal file
View File

@@ -0,0 +1,52 @@
from sqlalchemy.orm import Session
from database.models import OfficeSpot
from utils.helpers import generate_uuid
def sync_office_spots(office_id: str, quota: int, prefix: str, db: Session):
"""
Synchronize OfficeSpot records with the office quota.
- If active spots < quota: Create new spots
- If active spots > quota: Remove highest numbered spots (Cascade handles assignments)
- If prefix changes: Rename all spots
"""
# Get all current spots sorted by number
current_spots = db.query(OfficeSpot).filter(
OfficeSpot.office_id == office_id
).order_by(OfficeSpot.spot_number).all()
# 1. Handle Prefix Change
# If prefix changed, we need to update names of ALL existing spots
# We do this first to ensure names are correct even if we don't add/remove
if current_spots:
first_spot = current_spots[0]
# Check simple heuristic: does name start with prefix?
# Better: we can't easily know old prefix from here without querying Office,
# but we can just re-generate names for all valid spots.
for spot in current_spots:
expected_name = f"{prefix}{spot.spot_number}"
if spot.name != expected_name:
spot.name = expected_name
current_count = len(current_spots)
# 2. Add Spots
if current_count < quota:
for i in range(current_count + 1, quota + 1):
new_spot = OfficeSpot(
id=generate_uuid(),
office_id=office_id,
spot_number=i,
name=f"{prefix}{i}",
is_unavailable=False
)
db.add(new_spot)
# 3. Remove Spots
elif current_count > quota:
# Identify spots to remove (highest numbers)
spots_to_remove = current_spots[quota:]
for spot in spots_to_remove:
db.delete(spot)
db.commit()