.
This commit is contained in:
		@@ -46,48 +46,30 @@ class ApplicationResponse(BaseModel):
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@router.get("/", response_model=List[ApplicationResponse])
 | 
			
		||||
async def get_applications(skip: int = 0, limit: int = 100, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
    """Get all applications for the current tenant"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    # Get applications for jobs in the current tenant or applications by users in the current tenant
 | 
			
		||||
    applications = db.query(Application).join(JobPosting).filter(
 | 
			
		||||
        (JobPosting.tenant_id == tenant_id) | (Application.user_id.in_(
 | 
			
		||||
            db.query(User.id).filter(User.tenant_id == tenant_id)
 | 
			
		||||
        ))
 | 
			
		||||
    ).offset(skip).limit(limit).all()
 | 
			
		||||
async def get_applications(skip: int = 0, limit: int = 100, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get all applications"""
 | 
			
		||||
    applications = db.query(Application).offset(skip).limit(limit).all()
 | 
			
		||||
    return applications
 | 
			
		||||
 | 
			
		||||
@router.get("/{application_id}", response_model=ApplicationResponse)
 | 
			
		||||
async def get_application(application_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def get_application(application_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get a specific application"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    application = db.query(Application).join(JobPosting).filter(
 | 
			
		||||
    application = db.query(Application).filter(
 | 
			
		||||
        Application.id == application_id,
 | 
			
		||||
        (JobPosting.tenant_id == tenant_id) | (Application.user_id.in_(
 | 
			
		||||
            db.query(User.id).filter(User.tenant_id == tenant_id)
 | 
			
		||||
        ))
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not application:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Application not found")
 | 
			
		||||
    return application
 | 
			
		||||
 | 
			
		||||
@router.post("/", response_model=ApplicationResponse)
 | 
			
		||||
async def create_application(application: ApplicationCreate, db: Session = Depends(SessionLocal), request: Request = None, user_id: int = 1):  # In real app, get from auth context
 | 
			
		||||
async def create_application(application: ApplicationCreate, db: Session = Depends(SessionLocal), user_id: int = 1):  # Default for testing
 | 
			
		||||
    """Create a new job application"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    # For testing, use default tenant
 | 
			
		||||
    tenant_id = 1  # Default tenant for testing
 | 
			
		||||
    
 | 
			
		||||
    # Verify user exists and has permission to apply
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id  # Make sure user belongs to current tenant
 | 
			
		||||
        User.id == user_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user or user.role != "job_seeker":
 | 
			
		||||
        raise HTTPException(
 | 
			
		||||
@@ -95,14 +77,13 @@ async def create_application(application: ApplicationCreate, db: Session = Depen
 | 
			
		||||
            detail="Only job seekers can apply for jobs"
 | 
			
		||||
        )
 | 
			
		||||
    
 | 
			
		||||
    # Verify job posting exists and is active and belongs to the same tenant
 | 
			
		||||
    # Verify job posting exists and is active
 | 
			
		||||
    job_posting = db.query(JobPosting).filter(
 | 
			
		||||
        JobPosting.id == application.job_posting_id,
 | 
			
		||||
        JobPosting.is_active == True,
 | 
			
		||||
        JobPosting.tenant_id == tenant_id  # Ensure job belongs to current tenant
 | 
			
		||||
        JobPosting.is_active == True
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not job_posting:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Job posting not found or inactive or not in your tenant")
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Job posting not found or inactive")
 | 
			
		||||
    
 | 
			
		||||
    # Verify resume exists and belongs to user
 | 
			
		||||
    resume = db.query(Resume).filter(
 | 
			
		||||
@@ -124,17 +105,10 @@ async def create_application(application: ApplicationCreate, db: Session = Depen
 | 
			
		||||
    return db_application
 | 
			
		||||
 | 
			
		||||
@router.put("/{application_id}", response_model=ApplicationResponse)
 | 
			
		||||
async def update_application(application_id: int, app_update: ApplicationUpdate, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def update_application(application_id: int, app_update: ApplicationUpdate, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Update an application"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    db_application = db.query(Application).join(JobPosting).filter(
 | 
			
		||||
        Application.id == application_id,
 | 
			
		||||
        (JobPosting.tenant_id == tenant_id) | (Application.user_id.in_(
 | 
			
		||||
            db.query(User.id).filter(User.tenant_id == tenant_id)
 | 
			
		||||
        ))
 | 
			
		||||
    db_application = db.query(Application).filter(
 | 
			
		||||
        Application.id == application_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not db_application:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Application not found")
 | 
			
		||||
@@ -150,17 +124,10 @@ async def update_application(application_id: int, app_update: ApplicationUpdate,
 | 
			
		||||
    return db_application
 | 
			
		||||
 | 
			
		||||
@router.delete("/{application_id}")
 | 
			
		||||
async def delete_application(application_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def delete_application(application_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Delete an application"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    db_application = db.query(Application).join(JobPosting).filter(
 | 
			
		||||
        Application.id == application_id,
 | 
			
		||||
        (JobPosting.tenant_id == tenant_id) | (Application.user_id.in_(
 | 
			
		||||
            db.query(User.id).filter(User.tenant_id == tenant_id)
 | 
			
		||||
        ))
 | 
			
		||||
    db_application = db.query(Application).filter(
 | 
			
		||||
        Application.id == application_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not db_application:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Application not found")
 | 
			
		||||
 
 | 
			
		||||
@@ -64,28 +64,19 @@ class JobResponse(BaseModel):
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@router.get("/", response_model=List[JobResponse])
 | 
			
		||||
async def get_jobs(skip: int = 0, limit: int = 100, is_active: bool = True, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
    """Get all jobs for the current tenant"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    query = db.query(JobPosting).filter(JobPosting.tenant_id == tenant_id)
 | 
			
		||||
async def get_jobs(skip: int = 0, limit: int = 100, is_active: bool = True, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get all jobs"""
 | 
			
		||||
    query = db.query(JobPosting)
 | 
			
		||||
    if is_active is not None:
 | 
			
		||||
        query = query.filter(JobPosting.is_active == is_active)
 | 
			
		||||
    jobs = query.offset(skip).limit(limit).all()
 | 
			
		||||
    return jobs
 | 
			
		||||
 | 
			
		||||
@router.get("/{job_id}", response_model=JobResponse)
 | 
			
		||||
async def get_job(job_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def get_job(job_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get a specific job"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    job = db.query(JobPosting).filter(
 | 
			
		||||
        JobPosting.id == job_id,
 | 
			
		||||
        JobPosting.tenant_id == tenant_id  # Ensure job belongs to current tenant
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not job:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Job not found")
 | 
			
		||||
@@ -94,16 +85,14 @@ async def get_job(job_id: int, db: Session = Depends(SessionLocal), request: Req
 | 
			
		||||
    return job
 | 
			
		||||
 | 
			
		||||
@router.post("/", response_model=JobResponse)
 | 
			
		||||
async def create_job(job: JobCreate, db: Session = Depends(SessionLocal), request: Request = None, user_id: int = 1):  # In real app, get from auth context
 | 
			
		||||
async def create_job(job: JobCreate, db: Session = Depends(SessionLocal), user_id: int = 1):  # Default for testing
 | 
			
		||||
    """Create a new job posting"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    # For testing, use default tenant
 | 
			
		||||
    tenant_id = 1  # Default tenant for testing
 | 
			
		||||
    
 | 
			
		||||
    # Verify user exists and has permission to create job postings
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id  # Ensure user belongs to current tenant
 | 
			
		||||
        User.id == user_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user or user.role not in ["job_provider", "admin"]:
 | 
			
		||||
        raise HTTPException(
 | 
			
		||||
@@ -119,7 +108,7 @@ async def create_job(job: JobCreate, db: Session = Depends(SessionLocal), reques
 | 
			
		||||
        salary_min=job.salary_min,
 | 
			
		||||
        salary_max=job.salary_max,
 | 
			
		||||
        is_remote=job.is_remote,
 | 
			
		||||
        tenant_id=tenant_id,  # Use current tenant
 | 
			
		||||
        tenant_id=tenant_id,  # Use default tenant
 | 
			
		||||
        created_by_user_id=user_id
 | 
			
		||||
    )
 | 
			
		||||
    db.add(db_job)
 | 
			
		||||
@@ -128,15 +117,10 @@ async def create_job(job: JobCreate, db: Session = Depends(SessionLocal), reques
 | 
			
		||||
    return db_job
 | 
			
		||||
 | 
			
		||||
@router.put("/{job_id}", response_model=JobResponse)
 | 
			
		||||
async def update_job(job_id: int, job_update: JobUpdate, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def update_job(job_id: int, job_update: JobUpdate, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Update a job posting"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    db_job = db.query(JobPosting).filter(
 | 
			
		||||
        JobPosting.id == job_id,
 | 
			
		||||
        JobPosting.tenant_id == tenant_id  # Ensure job belongs to current tenant
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not db_job:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Job not found")
 | 
			
		||||
@@ -150,15 +134,10 @@ async def update_job(job_id: int, job_update: JobUpdate, db: Session = Depends(S
 | 
			
		||||
    return db_job
 | 
			
		||||
 | 
			
		||||
@router.delete("/{job_id}")
 | 
			
		||||
async def delete_job(job_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def delete_job(job_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Delete a job posting (soft delete by setting is_active to False)"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    db_job = db.query(JobPosting).filter(
 | 
			
		||||
        JobPosting.id == job_id,
 | 
			
		||||
        JobPosting.tenant_id == tenant_id  # Ensure job belongs to current tenant
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not db_job:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Job not found")
 | 
			
		||||
 
 | 
			
		||||
@@ -50,87 +50,41 @@ class ResumeResponse(BaseModel):
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@router.get("/", response_model=List[ResumeResponse])
 | 
			
		||||
async def get_resumes(db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def get_resumes(db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get all resumes for the current user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    # For testing, use default user
 | 
			
		||||
    user_id = 1  # Default user for testing
 | 
			
		||||
    
 | 
			
		||||
    # Extract user_id from token in a real app, for now using a default
 | 
			
		||||
    user_id = 1  # This would come from authentication in a real implementation
 | 
			
		||||
    
 | 
			
		||||
    # Verify user belongs to the current tenant
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Access denied")
 | 
			
		||||
    
 | 
			
		||||
    resumes = get_user_resumes(db, user_id, tenant_id)
 | 
			
		||||
    resumes = get_user_resumes(db, user_id)
 | 
			
		||||
    return resumes
 | 
			
		||||
 | 
			
		||||
@router.get("/{resume_id}", response_model=ResumeResponse)
 | 
			
		||||
async def get_resume(resume_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def get_resume(resume_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get a specific resume"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    # For testing, use default user
 | 
			
		||||
    user_id = 1  # Default user for testing
 | 
			
		||||
    
 | 
			
		||||
    # Extract user_id from token in a real app, for now using a default
 | 
			
		||||
    user_id = 1  # This would come from authentication in a real implementation
 | 
			
		||||
    
 | 
			
		||||
    # Verify user belongs to the current tenant
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Access denied")
 | 
			
		||||
    
 | 
			
		||||
    resume = get_resume_by_id(db, resume_id, user_id, tenant_id)
 | 
			
		||||
    resume = get_resume_by_id(db, resume_id, user_id)
 | 
			
		||||
    if not resume:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="Resume not found")
 | 
			
		||||
    return resume
 | 
			
		||||
 | 
			
		||||
@router.post("/", response_model=ResumeResponse)
 | 
			
		||||
async def create_user_resume(resume: ResumeCreate, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def create_user_resume(resume: ResumeCreate, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Create a new resume for the current user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    # Extract user_id from token in a real app, for now using a default
 | 
			
		||||
    user_id = 1  # This would come from authentication in a real implementation
 | 
			
		||||
    
 | 
			
		||||
    # Verify user belongs to the current tenant
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Access denied")
 | 
			
		||||
    # For testing, use default values
 | 
			
		||||
    user_id = 1  # Default user for testing
 | 
			
		||||
    tenant_id = 1  # Default tenant for testing
 | 
			
		||||
    
 | 
			
		||||
    db_resume = create_resume(db, user_id, resume.title, resume.content, tenant_id)
 | 
			
		||||
    return db_resume
 | 
			
		||||
 | 
			
		||||
@router.put("/{resume_id}", response_model=ResumeResponse)
 | 
			
		||||
async def update_user_resume(resume_id: int, resume_update: ResumeUpdate, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def update_user_resume(resume_id: int, resume_update: ResumeUpdate, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Update a resume for the current user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    # Extract user_id from token in a real app, for now using a default
 | 
			
		||||
    user_id = 1  # This would come from authentication in a real implementation
 | 
			
		||||
    
 | 
			
		||||
    # Verify user belongs to the current tenant
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Access denied")
 | 
			
		||||
    # For testing, use default values
 | 
			
		||||
    user_id = 1  # Default user for testing
 | 
			
		||||
    tenant_id = 1  # Default tenant for testing
 | 
			
		||||
    
 | 
			
		||||
    db_resume = update_resume(db, resume_id, user_id, tenant_id, resume_update.title, resume_update.content)
 | 
			
		||||
    if not db_resume:
 | 
			
		||||
@@ -138,22 +92,11 @@ async def update_user_resume(resume_id: int, resume_update: ResumeUpdate, db: Se
 | 
			
		||||
    return db_resume
 | 
			
		||||
 | 
			
		||||
@router.delete("/{resume_id}")
 | 
			
		||||
async def delete_user_resume(resume_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def delete_user_resume(resume_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Delete a resume for the current user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    # Extract user_id from token in a real app, for now using a default
 | 
			
		||||
    user_id = 1  # This would come from authentication in a real implementation
 | 
			
		||||
    
 | 
			
		||||
    # Verify user belongs to the current tenant
 | 
			
		||||
    user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id
 | 
			
		||||
    ).first()
 | 
			
		||||
    if not user and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Access denied")
 | 
			
		||||
    # For testing, use default values
 | 
			
		||||
    user_id = 1  # Default user for testing
 | 
			
		||||
    tenant_id = 1  # Default tenant for testing
 | 
			
		||||
    
 | 
			
		||||
    success = delete_resume(db, resume_id, user_id, tenant_id)
 | 
			
		||||
    if not success:
 | 
			
		||||
 
 | 
			
		||||
@@ -54,44 +54,24 @@ def hash_password_util(password: str) -> str:
 | 
			
		||||
    return get_password_hash(password)
 | 
			
		||||
 | 
			
		||||
@router.get("/", response_model=List[UserResponse])
 | 
			
		||||
async def get_users(skip: int = 0, limit: int = 100, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
    """Get all users for the current tenant"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        # For testing, allow without tenant
 | 
			
		||||
        import os
 | 
			
		||||
        if os.getenv("TESTING", "False").lower() != "true":
 | 
			
		||||
            raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    query = db.query(User)
 | 
			
		||||
    if settings.MULTI_TENANT_ENABLED and tenant_id:
 | 
			
		||||
        query = query.filter(User.tenant_id == tenant_id)
 | 
			
		||||
    
 | 
			
		||||
    users = query.offset(skip).limit(limit).all()
 | 
			
		||||
async def get_users(skip: int = 0, limit: int = 100, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get all users"""
 | 
			
		||||
    users = db.query(User).offset(skip).limit(limit).all()
 | 
			
		||||
    return users
 | 
			
		||||
 | 
			
		||||
@router.get("/{user_id}", response_model=UserResponse)
 | 
			
		||||
async def get_user(user_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def get_user(user_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Get a specific user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    query = db.query(User).filter(User.id == user_id)
 | 
			
		||||
    if settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        query = query.filter(User.tenant_id == tenant_id)
 | 
			
		||||
    
 | 
			
		||||
    user = query.first()
 | 
			
		||||
    user = db.query(User).filter(User.id == user_id).first()
 | 
			
		||||
    if not user:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="User not found")
 | 
			
		||||
    return user
 | 
			
		||||
 | 
			
		||||
@router.post("/", response_model=UserResponse)
 | 
			
		||||
async def create_user(user: UserCreate, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def create_user(user: UserCreate, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Create a new user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    # For testing, use a default tenant
 | 
			
		||||
    tenant_id = 1  # Default tenant for testing
 | 
			
		||||
    
 | 
			
		||||
    # Check if user already exists
 | 
			
		||||
    existing_user = db.query(User).filter(
 | 
			
		||||
@@ -108,7 +88,7 @@ async def create_user(user: UserCreate, db: Session = Depends(SessionLocal), req
 | 
			
		||||
        username=user.username,
 | 
			
		||||
        hashed_password=hashed_pwd,
 | 
			
		||||
        role=user.role.value,
 | 
			
		||||
        tenant_id=tenant_id  # Use the current tenant
 | 
			
		||||
        tenant_id=tenant_id  # Use default tenant
 | 
			
		||||
    )
 | 
			
		||||
    db.add(db_user)
 | 
			
		||||
    db.commit()
 | 
			
		||||
@@ -116,16 +96,9 @@ async def create_user(user: UserCreate, db: Session = Depends(SessionLocal), req
 | 
			
		||||
    return db_user
 | 
			
		||||
 | 
			
		||||
@router.put("/{user_id}", response_model=UserResponse)
 | 
			
		||||
async def update_user(user_id: int, user_update: UserUpdate, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def update_user(user_id: int, user_update: UserUpdate, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Update a user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    db_user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id  # Ensure user belongs to current tenant
 | 
			
		||||
    ).first()
 | 
			
		||||
    db_user = db.query(User).filter(User.id == user_id).first()
 | 
			
		||||
    if not db_user:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="User not found")
 | 
			
		||||
    
 | 
			
		||||
@@ -142,16 +115,9 @@ async def update_user(user_id: int, user_update: UserUpdate, db: Session = Depen
 | 
			
		||||
    return db_user
 | 
			
		||||
 | 
			
		||||
@router.delete("/{user_id}")
 | 
			
		||||
async def delete_user(user_id: int, db: Session = Depends(SessionLocal), request: Request = None):
 | 
			
		||||
async def delete_user(user_id: int, db: Session = Depends(SessionLocal)):
 | 
			
		||||
    """Delete a user"""
 | 
			
		||||
    tenant_id = getattr(request.state, 'tenant_id', None)
 | 
			
		||||
    if not tenant_id and settings.MULTI_TENANT_ENABLED:
 | 
			
		||||
        raise HTTPException(status_code=400, detail="Tenant ID is required")
 | 
			
		||||
    
 | 
			
		||||
    db_user = db.query(User).filter(
 | 
			
		||||
        User.id == user_id,
 | 
			
		||||
        User.tenant_id == tenant_id  # Ensure user belongs to current tenant
 | 
			
		||||
    ).first()
 | 
			
		||||
    db_user = db.query(User).filter(User.id == user_id).first()
 | 
			
		||||
    if not db_user:
 | 
			
		||||
        raise HTTPException(status_code=404, detail="User not found")
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user