Adding multi tenant check and Starting firm initialization

This commit is contained in:
2025-04-12 18:12:56 +02:00
parent 9ef599bbd5
commit c7e946f963
14 changed files with 205 additions and 39 deletions

View File

@@ -0,0 +1,45 @@
from typing import Any
from pydantic import Field
from firm.core.models import CrudDocument
from firm.core.schemas import Writer, Reader
from firm.entity.schemas import EntityIndividualCreate, EntityCorporationCreate
class CurrentFirmModel(CrudDocument):
instance: str
firm: str
name: str = Field(nullable=False)
# primary_color: str = Field()
# secondary_color: str = Field()
def __eq__(self, other: Any) -> bool:
if isinstance(other, dict):
return self.instance == other["instance"] and self.firm == other["firm"]
return super().__eq__(other)
def compute_label(self) -> str:
return self.name
@classmethod
async def get(cls, db):
document = await cls._get_collection(db).find_one({})
if not document:
return None
document["id"] = document.pop("_id")
return cls.model_validate(document)
class CurrentFirmSchemaRead(Reader):
pass
class CurrentFirmSchemaCreate(Writer):
corporation: EntityCorporationCreate = Field(title="Informations sur la firme")
owner: EntityIndividualCreate = Field(title="Informations sur le dirigeant")
class CurrentFirmSchemaUpdate(Writer):
pass

View File

@@ -0,0 +1,22 @@
from fastapi import APIRouter, Depends
from firm.core.depends import get_logged_tenant_db_cursor, get_uninitialized_tenant_db_cursor
from firm.current_firm import CurrentFirmModel, CurrentFirmSchemaRead, CurrentFirmSchemaCreate, CurrentFirmSchemaUpdate
current_firm_router = APIRouter()
@current_firm_router.get("/", response_model=CurrentFirmSchemaRead, response_description=f"Current Firm records retrieved")
async def read(db=Depends(get_logged_tenant_db_cursor)) -> CurrentFirmSchemaRead:
return CurrentFirmSchemaRead.from_model(**CurrentFirmModel.get(db))
@current_firm_router.post("/", response_description=f"Current Firm added to the database")
async def create(schema: CurrentFirmSchemaCreate, db=Depends(get_uninitialized_tenant_db_cursor)) -> CurrentFirmSchemaRead:
await schema.validate_foreign_key(db)
record = await CurrentFirmModel.create(db, schema)
return CurrentFirmSchemaRead.from_model(record)
@current_firm_router.put("/", response_description=f"Current Firm record updated")
async def update(schema: CurrentFirmSchemaUpdate, db=Depends(get_logged_tenant_db_cursor)) -> CurrentFirmSchemaRead:
record = await CurrentFirmModel.get(db)
record = await CurrentFirmModel.update(db, record, schema)
return CurrentFirmSchemaRead.from_model(record)