from typing import Any, Optional from beanie import PydanticObjectId from pydantic import Field, BaseModel from firm.core.models import CrudDocument, CrudDocumentConfig from firm.core.schemas import Writer, Reader from firm.entity.schemas import EntityIndividualCreate, EntityCorporationCreate, EntityRead class CurrentFirm(CrudDocument): instance: str = Field() firm: str = Field() entity_id: PydanticObjectId = Field() 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 f"{self.instance} / {self.firm}" @classmethod async def get_current(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(BaseModel): id: Optional[PydanticObjectId] entity: EntityRead partner: EntityRead partner_list: list[EntityRead] instance: str firm: str primary_color: str secondary_color: str @classmethod def from_model_and_entities(cls, model, entity, partner, partner_list): schema = cls(**model.model_dump(mode="json"), entity=entity, partner=partner, partner_list=partner_list) return schema class CurrentFirmSchemaCreate(Writer): corporation: EntityCorporationCreate = Field(title="Informations sur la firme") owner: EntityIndividualCreate = Field(title="Informations sur le dirigeant") position: str = Field(title="Poste") primary_color: str = Field() secondary_color: str = Field() class CurrentFirmSchemaUpdate(Writer): pass class Partner(CrudDocument): document_config = CrudDocumentConfig( indexes=["user_id", "entity_id"], ) user_id: PydanticObjectId = Field() entity_id: PydanticObjectId = Field() @classmethod async def get_by_user_id(cls, db, user_id): document = await cls._get_collection(db).find_one({"user_id": str(user_id)}) if not document: return None document["id"] = document.pop("_id") return cls.model_validate(document)