db/heromodels/src/models/biz/company.rs
2025-05-22 23:57:33 +03:00

179 lines
4.6 KiB
Rust

use serde::{Deserialize, Serialize};
use heromodels_core::{BaseModelData, Model, IndexKey, IndexKeyBuilder, Index};
use rhai::{CustomType, TypeBuilder}; // For #[derive(CustomType)]
// --- Enums ---
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CompanyStatus {
Active,
Inactive,
Suspended,
}
impl Default for CompanyStatus {
fn default() -> Self {
CompanyStatus::Inactive
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BusinessType {
Coop,
Single,
Twin,
Starter,
Global,
}
impl Default for BusinessType {
fn default() -> Self {
BusinessType::Single
}
}
// --- Company Struct ---
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, CustomType)] // Added CustomType
pub struct Company {
pub base_data: BaseModelData,
pub name: String,
pub registration_number: String,
pub incorporation_date: i64, // Changed to i64 // Timestamp
pub fiscal_year_end: String, // e.g., "MM-DD"
pub email: String,
pub phone: String,
pub website: String,
pub address: String,
pub business_type: BusinessType,
pub industry: String,
pub description: String,
pub status: CompanyStatus,
}
// --- Model Trait Implementation ---
impl Model for Company {
fn db_prefix() -> &'static str {
"company"
}
fn get_id(&self) -> u32 {
self.base_data.id
}
fn base_data_mut(&mut self) -> &mut BaseModelData {
&mut self.base_data
}
// Override db_keys to provide custom indexes if needed
fn db_keys(&self) -> Vec<IndexKey> {
vec![
IndexKeyBuilder::new("name").value(self.name.clone()).build(),
IndexKeyBuilder::new("registration_number").value(self.registration_number.clone()).build(),
// Add other relevant keys, e.g., by status or type if frequently queried
]
}
}
// --- Index Implementations (Example) ---
pub struct CompanyNameIndex;
impl Index for CompanyNameIndex {
type Model = Company;
type Key = str;
fn key() -> &'static str {
"name"
}
}
pub struct CompanyRegistrationNumberIndex;
impl Index for CompanyRegistrationNumberIndex {
type Model = Company;
type Key = str;
fn key() -> &'static str {
"registration_number"
}
}
// --- Builder Pattern ---
impl Company {
pub fn new(name: String, registration_number: String, incorporation_date: i64) -> Self { // incorporation_date to i64
Self {
base_data: BaseModelData::new(),
name,
registration_number,
incorporation_date, // This is i64 now
fiscal_year_end: String::new(),
email: String::new(),
phone: String::new(),
website: String::new(),
address: String::new(),
business_type: BusinessType::default(),
industry: String::new(),
description: String::new(),
status: CompanyStatus::default(),
}
}
pub fn fiscal_year_end(mut self, fiscal_year_end: String) -> Self {
self.fiscal_year_end = fiscal_year_end;
self
}
pub fn email(mut self, email: String) -> Self {
self.email = email;
self
}
pub fn phone(mut self, phone: String) -> Self {
self.phone = phone;
self
}
pub fn website(mut self, website: String) -> Self {
self.website = website;
self
}
pub fn address(mut self, address: String) -> Self {
self.address = address;
self
}
pub fn business_type(mut self, business_type: BusinessType) -> Self {
self.business_type = business_type;
self
}
pub fn industry(mut self, industry: String) -> Self {
self.industry = industry;
self
}
pub fn description(mut self, description: String) -> Self {
self.description = description;
self
}
pub fn status(mut self, status: CompanyStatus) -> Self {
self.status = status;
self
}
// Setter for base_data fields if needed directly, though usually handled by Model trait or new()
// Builder methods for created_at and modified_at can be more specific if needed,
// but Model::build() updates modified_at, and BaseModelData::new() sets created_at.
// If direct setting is required for tests or specific scenarios:
pub fn set_base_created_at(mut self, created_at: i64) -> Self {
self.base_data.created_at = created_at;
self
}
pub fn set_base_modified_at(mut self, modified_at: i64) -> Self {
self.base_data.modified_at = modified_at;
self
}
}