35 lines
1.5 KiB
Python
35 lines
1.5 KiB
Python
from fastapi import HTTPException, Depends
|
|
from hub.auth import get_current_user
|
|
|
|
from firm.db import get_db_client
|
|
from firm.current_firm import CurrentFirmModel
|
|
|
|
async def get_tenant_db_cursor(instance: str, firm: str, db_client=Depends(get_db_client)):
|
|
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
|
current_firm = await CurrentFirmModel.get(db_cursor)
|
|
if current_firm is None:
|
|
raise HTTPException(status_code=405, detail=f"Firm needs to be instantiated first")
|
|
db_cursor.firm = current_firm
|
|
return db_cursor
|
|
|
|
def get_logged_tenant_db_cursor(db_cursor=Depends(get_tenant_db_cursor), user=Depends(get_current_user)):
|
|
for firm in user.firms:
|
|
if firm == db_cursor.firm:
|
|
db_cursor.user = user
|
|
return db_cursor
|
|
|
|
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
|
|
|
async def get_uninitialized_tenant_db_cursor(instance: str, firm: str, db_client=Depends(get_db_client), user=Depends(get_current_user)):
|
|
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
|
current_firm = await CurrentFirmModel.get(db_cursor)
|
|
if current_firm is not None:
|
|
HTTPException(status_code=409, detail="Firm configuration already exists")
|
|
|
|
for firm in user.firms:
|
|
if firm == db_cursor.firm:
|
|
db_cursor.user = user
|
|
return db_cursor
|
|
|
|
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|