from typing import Any from beanie import PydanticObjectId from pydantic import Field from firm.core.models import CrudDocument from firm.core.schemas import Writer, Reader from firm.entity.schemas import EntityIndividualCreate, EntityCorporationCreate, EntityRead class CurrentFirmModel(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(Reader): entity: EntityRead partner: EntityRead instance: str firm: str primary_color: str secondary_color: str @classmethod def from_model_and_entities(cls, model, entity, partner): schema = cls(**model.model_dump(mode="json"), entity=entity, partner=partner) 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): 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)