58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
from enum import Enum
|
|
from datetime import datetime, date
|
|
from typing import List, Literal
|
|
|
|
from pydantic import Field, BaseModel
|
|
from beanie import Document, Link
|
|
|
|
|
|
class EntityType(str, Enum):
|
|
individual = 'individual'
|
|
corporation = 'corporation'
|
|
institution = 'institution'
|
|
|
|
|
|
class Individual(BaseModel):
|
|
type: Literal['individual'] = 'individual'
|
|
firstname: str
|
|
middlenames: List[str] = Field(default=[])
|
|
lastname: str
|
|
surnames: List[str] = Field(default=[])
|
|
day_of_birth: date
|
|
job: str
|
|
employer: str
|
|
|
|
|
|
class Employee(BaseModel):
|
|
role: str
|
|
entity_id: str = Field(foreignKey={
|
|
"reference": {
|
|
"resource": "entity",
|
|
"fields": "_id"
|
|
}
|
|
})
|
|
|
|
|
|
class Corporation(BaseModel):
|
|
type: Literal['corporation'] = 'corporation'
|
|
title: str
|
|
activity: str
|
|
employees: List[Employee] = Field(default=[])
|
|
|
|
|
|
class Entity(Document):
|
|
_id: str
|
|
name: str
|
|
address: str
|
|
entity_data: Individual | Corporation = Field(..., discriminator='type')
|
|
|
|
created_at: datetime = Field(default=datetime.utcnow(), nullable=False)
|
|
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
|
|
|
|
class Settings:
|
|
bson_encoders = {
|
|
date: lambda dt: dt if hasattr(dt, 'hour')
|
|
else datetime(year=dt.year, month=dt.month, day=dt.day,
|
|
hour=0, minute=0, second=0)
|
|
}
|