Created Cally

This commit is contained in:
NPS Agent
2026-05-21 15:49:36 +10:00
parent 98cf813f00
commit be26e8b78d
12 changed files with 772 additions and 3 deletions
+50 -1
View File
@@ -7,11 +7,14 @@ from typing import List, Dict
import uuid
import json
import asyncio
from datetime import date as date_type
from . import models, schemas, auth, database
from . import models, schemas, auth, database, cally_models, cally_database, servicem8
from .database import engine, get_db
from .cally_database import engine as cally_engine, get_cally_db
models.Base.metadata.create_all(bind=engine)
cally_models.Base.metadata.create_all(bind=cally_engine)
class EventNotifier:
def __init__(self):
@@ -310,6 +313,52 @@ async def update_workspace(ws_update: schemas.WorkspaceUpdate, db: Session = Dep
await manager.broadcast(json.dumps({"type": "refresh"}))
return ws
@app.get("/calendar/schedule", response_model=schemas.CalendarScheduleResponse)
def get_calendar_schedule(
start_date: date_type,
end_date: date_type,
db: Session = Depends(get_cally_db),
current_user: models.User = Depends(auth.get_current_user)
):
try:
staff = db.query(cally_models.Sm8Staff).filter(cally_models.Sm8Staff.active == True).all()
# If no staff, trigger an initial sync
if not staff:
servicem8.sync_from_servicem8(db)
staff = db.query(cally_models.Sm8Staff).filter(cally_models.Sm8Staff.active == True).all()
schedule = db.query(cally_models.ScheduleDay).filter(
cally_models.ScheduleDay.date >= start_date,
cally_models.ScheduleDay.date <= end_date
).all()
# Collect all job UUIDs from the schedule to fetch their details
job_uuids = set()
for s in schedule:
if s.job_uuids:
job_uuids.update(s.job_uuids.split(','))
jobs = db.query(cally_models.Sm8Job).filter(cally_models.Sm8Job.uuid.in_(list(job_uuids))).all()
return {"staff": staff, "schedule": schedule, "jobs": jobs}
except Exception as e:
import traceback
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=str(e))
@app.post("/calendar/sync")
async def sync_calendar(
db: Session = Depends(get_cally_db),
current_user: models.User = Depends(auth.get_current_user)
):
success = servicem8.sync_from_servicem8(db)
if not success:
raise HTTPException(status_code=500, detail="Failed to sync from ServiceM8")
await manager.broadcast(json.dumps({"type": "refresh"}))
return {"message": "Sync successful"}
@app.get("/audit", response_model=List[schemas.AuditLog])
def read_audit(db: Session = Depends(get_db), current_user: models.User = Depends(auth.get_current_user)):
return db.query(models.AuditLog).order_by(models.AuditLog.at.desc()).all()