Compare commits

...

1 Commits

Author SHA1 Message Date
872e27e7e4 Initializable Firm 2025-04-16 22:39:09 +02:00
3 changed files with 43 additions and 21 deletions

View File

@@ -12,7 +12,7 @@ class Registry:
self.instance = instance self.instance = instance
self.firm = firm self.firm = firm
self.current_firm = CurrentFirmModel.get(self.db) self.current_firm = CurrentFirmModel.get_current(self.db)
def set_user(self, user): def set_user(self, user):
for firm in user.firms: for firm in user.firms:

View File

@@ -1,5 +1,6 @@
from typing import Any from typing import Any
from beanie import PydanticObjectId
from pydantic import Field from pydantic import Field
from firm.core.models import CrudDocument from firm.core.models import CrudDocument
@@ -8,12 +9,11 @@ from firm.entity.schemas import EntityIndividualCreate, EntityCorporationCreate
class CurrentFirmModel(CrudDocument): class CurrentFirmModel(CrudDocument):
instance: str instance: str = Field()
firm: str firm: str = Field()
name: str = Field(nullable=False) entity_id: PydanticObjectId = Field()
primary_color: str = Field()
# primary_color: str = Field() secondary_color: str = Field()
# secondary_color: str = Field()
def __eq__(self, other: Any) -> bool: def __eq__(self, other: Any) -> bool:
if isinstance(other, dict): if isinstance(other, dict):
@@ -21,10 +21,10 @@ class CurrentFirmModel(CrudDocument):
return super().__eq__(other) return super().__eq__(other)
def compute_label(self) -> str: def compute_label(self) -> str:
return self.name return f"{self.instance} / {self.firm}"
@classmethod @classmethod
async def get(cls, db): async def get_current(cls, db):
document = await cls._get_collection(db).find_one({}) document = await cls._get_collection(db).find_one({})
if not document: if not document:
return None return None
@@ -39,7 +39,15 @@ class CurrentFirmSchemaRead(Reader):
class CurrentFirmSchemaCreate(Writer): class CurrentFirmSchemaCreate(Writer):
corporation: EntityCorporationCreate = Field(title="Informations sur la firme") corporation: EntityCorporationCreate = Field(title="Informations sur la firme")
owner: EntityIndividualCreate = Field(title="Informations sur le dirigeant") owner: EntityIndividualCreate = Field(title="Informations sur le dirigeant")
position: str = Field(title="Poste")
primary_color: str = Field()
secondary_color: str = Field()
class CurrentFirmSchemaUpdate(Writer): class CurrentFirmSchemaUpdate(Writer):
pass pass
class Partner(CrudDocument):
user_id: PydanticObjectId = Field()
entity_id: PydanticObjectId = Field()

View File

@@ -1,22 +1,36 @@
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from firm.core.depends import get_logged_tenant_db_cursor, get_uninitialized_tenant_db_cursor from firm.core.depends import get_authed_tenant_registry, get_uninitialized_registry
from firm.current_firm import CurrentFirmModel, CurrentFirmSchemaRead, CurrentFirmSchemaCreate, CurrentFirmSchemaUpdate from firm.current_firm import CurrentFirmModel, CurrentFirmSchemaRead, CurrentFirmSchemaCreate, CurrentFirmSchemaUpdate, Partner
from firm.entity.models import Entity, Employee
current_firm_router = APIRouter() current_firm_router = APIRouter()
@current_firm_router.get("/", response_model=CurrentFirmSchemaRead, response_description=f"Current Firm records retrieved") @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: async def read(reg=Depends(get_authed_tenant_registry)) -> CurrentFirmSchemaRead:
return CurrentFirmSchemaRead.from_model(**CurrentFirmModel.get(db)) document = await CurrentFirmModel.get_current(reg.db)
return CurrentFirmSchemaRead.from_model(document)
@current_firm_router.post("/", response_description=f"Current Firm added to the database") @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: async def create(schema: CurrentFirmSchemaCreate, reg=Depends(get_uninitialized_registry)) -> CurrentFirmSchemaRead:
await schema.validate_foreign_key(db) owner_entity = await Entity.create(reg.db, schema.owner)
record = await CurrentFirmModel.create(db, schema) Partner.create(reg.db, Partner(user_id=reg.user.id, entity_id=owner_entity.id))
return CurrentFirmSchemaRead.from_model(record)
corporation_schema = schema.corporation
corporation_schema.entity_data.employees.append(Employee(entity_id=owner_entity.id, position=schema.position))
corp = await Entity.create(reg.db, corporation_schema)
document = await CurrentFirmModel.create(reg.db, CurrentFirmModel(
instance=reg.instance,
firm=reg.firm,
entity_id=corp.id,
primary_color=schema.primary_color,
secondary_color=schema.secondary_color,
))
return await CurrentFirmSchemaRead.from_model(document)
@current_firm_router.put("/", response_description=f"Current Firm record updated") @current_firm_router.put("/", response_description=f"Current Firm record updated")
async def update(schema: CurrentFirmSchemaUpdate, db=Depends(get_logged_tenant_db_cursor)) -> CurrentFirmSchemaRead: async def update(schema: CurrentFirmSchemaUpdate, reg=Depends(get_authed_tenant_registry)) -> CurrentFirmSchemaRead:
record = await CurrentFirmModel.get(db) document = await CurrentFirmModel.get_current(reg.db)
record = await CurrentFirmModel.update(db, record, schema) document = await CurrentFirmModel.update(reg.db, document, schema)
return CurrentFirmSchemaRead.from_model(record) return CurrentFirmSchemaRead.from_model(document)