Adding multi tenant check and Starting firm initialization
This commit is contained in:
34
api/rpk-api/firm/core/depends.py
Normal file
34
api/rpk-api/firm/core/depends.py
Normal file
@@ -0,0 +1,34 @@
|
||||
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.")
|
||||
@@ -9,9 +9,9 @@ from pydantic import BaseModel, Field, computed_field
|
||||
class CrudDocument(BaseModel):
|
||||
id: Optional[PydanticObjectId] = Field(default=None)
|
||||
created_at: datetime = Field(default=datetime.now(UTC), nullable=False, title="Créé le")
|
||||
# created_by: str
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False, title="Modifié le")
|
||||
# updated_by: str
|
||||
created_by: str = Field(nullable=False, title="Créé par")
|
||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC), nullable=False, title="Modifié le")
|
||||
updated_by: str = Field(nullable=False, title="Modifié par")
|
||||
|
||||
@property
|
||||
def _id(self):
|
||||
@@ -37,8 +37,9 @@ class CrudDocument(BaseModel):
|
||||
|
||||
@classmethod
|
||||
async def create(cls, db, create_schema):
|
||||
values = cls.model_validate(create_schema.model_dump()).model_dump(mode="json")
|
||||
result = await cls._get_collection(db).insert_one(values)
|
||||
model_dict = create_schema.model_dump() | {"created_by": db.user, "updated_by":db.user}
|
||||
document = cls.model_validate(model_dict).model_dump(mode="json")
|
||||
result = await cls._get_collection(db).insert_one(document)
|
||||
|
||||
return await cls.get(db, result.inserted_id)
|
||||
|
||||
@@ -56,12 +57,12 @@ class CrudDocument(BaseModel):
|
||||
|
||||
@classmethod
|
||||
async def get(cls, db, model_id):
|
||||
value = await cls._get_collection(db).find_one({"_id": model_id})
|
||||
if not value:
|
||||
document = await cls._get_collection(db).find_one({"_id": model_id})
|
||||
if not document:
|
||||
return None
|
||||
|
||||
value["id"] = value.pop("_id")
|
||||
return cls.model_validate(value)
|
||||
document["id"] = document.pop("_id")
|
||||
return cls.model_validate(document)
|
||||
|
||||
@classmethod
|
||||
async def update(cls, db, model, update_schema):
|
||||
|
||||
@@ -5,26 +5,9 @@ from fastapi_filter import FilterDepends
|
||||
from fastapi_pagination import Page, add_pagination
|
||||
from fastapi_pagination.ext.motor import paginate
|
||||
|
||||
from hub.auth import get_current_user
|
||||
from firm.core.depends import get_logged_tenant_db_cursor
|
||||
from firm.core.models import CrudDocument
|
||||
from firm.core.schemas import Writer, Reader
|
||||
from firm.db import get_db_client
|
||||
|
||||
|
||||
|
||||
#instance: str="westside", firm: str="cht",
|
||||
def get_tenant_db_cursor(db_client=Depends(get_db_client)):
|
||||
instance = "westside"
|
||||
firm = "cht"
|
||||
return db_client[f"tenant_{instance}_{firm}"]
|
||||
|
||||
#instance: str="westside", firm: str="cht",
|
||||
def get_logged_tenant_db_cursor(db_client=Depends(get_db_client), user=Depends(get_current_user)):
|
||||
instance = "westside"
|
||||
firm = "cht"
|
||||
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
||||
db_cursor.user = user
|
||||
return db_cursor
|
||||
|
||||
def get_crud_router(model: CrudDocument, model_create: Writer, model_read: Reader, model_update: Writer, model_filter):
|
||||
model_name = model.__name__
|
||||
|
||||
Reference in New Issue
Block a user