375 lines
12 KiB
Rust
375 lines
12 KiB
Rust
use chrono::Utc;
|
|
use heromodels::db::hero::OurDB;
|
|
use heromodels::db::{Collection, Db}; // Import both Db and Collection traits
|
|
use heromodels::models::calendar::{Calendar, Event};
|
|
use heromodels_core::Model; // Import Model trait to use build method
|
|
use std::env;
|
|
use std::sync::Arc;
|
|
|
|
// Import finance models
|
|
use heromodels::models::finance::account::Account;
|
|
use heromodels::models::finance::asset::{Asset, AssetType};
|
|
use heromodels::models::finance::marketplace::{Listing, ListingType};
|
|
|
|
// Conditionally import other modules based on features
|
|
#[cfg(feature = "flow")]
|
|
use heromodels::models::flow::{Flow, FlowStep, SignatureRequirement};
|
|
|
|
#[cfg(feature = "legal")]
|
|
use heromodels::models::legal::{
|
|
Contract, ContractRevision, ContractSigner, ContractStatus, SignerStatus,
|
|
};
|
|
|
|
#[cfg(feature = "projects")]
|
|
use heromodels::models::projects::{ItemType, Priority, Project, Status as ProjectStatus};
|
|
|
|
/// Create a mock in-memory database for examples
|
|
pub fn create_mock_db() -> Arc<OurDB> {
|
|
// Create a temporary directory for the database files
|
|
let temp_dir = env::temp_dir().join("engine_examples");
|
|
std::fs::create_dir_all(&temp_dir).expect("Failed to create temp directory");
|
|
|
|
// Create a new OurDB instance with reset=true to ensure it's clean
|
|
let db = OurDB::new(temp_dir, true).expect("Failed to create OurDB instance");
|
|
|
|
Arc::new(db)
|
|
}
|
|
|
|
/// Seed the mock database with some initial data for all modules
|
|
pub fn seed_mock_db(db: Arc<OurDB>) {
|
|
// Seed calendar data
|
|
seed_calendar_data(db.clone());
|
|
|
|
// Seed finance data
|
|
seed_finance_data(db.clone());
|
|
|
|
// Seed flow data if the feature is enabled
|
|
#[cfg(feature = "flow")]
|
|
seed_flow_data(db.clone());
|
|
|
|
// Seed legal data if the feature is enabled
|
|
#[cfg(feature = "legal")]
|
|
seed_legal_data(db.clone());
|
|
|
|
// Seed projects data if the feature is enabled
|
|
#[cfg(feature = "projects")]
|
|
seed_projects_data(db.clone());
|
|
|
|
println!("Mock database seeded with initial data for all enabled modules.");
|
|
}
|
|
|
|
/// Seed the mock database with calendar data
|
|
fn seed_calendar_data(db: Arc<OurDB>) {
|
|
// Create a calendar
|
|
let mut calendar = Calendar::new(None, "Work Calendar".to_string());
|
|
calendar.description = Some("My work schedule".to_string());
|
|
|
|
// Store the calendar in the database
|
|
let (_calendar_id, _updated_calendar) = db
|
|
.collection::<Calendar>()
|
|
.expect("Failed to get Calendar collection")
|
|
.set(&calendar)
|
|
.expect("Failed to store calendar");
|
|
|
|
// Create an event
|
|
let now = Utc::now().timestamp();
|
|
let end_time = now + 3600; // Add 1 hour in seconds
|
|
|
|
// Use the builder pattern for Event
|
|
let event = Event::new()
|
|
.title("Team Meeting".to_string())
|
|
.reschedule(now, end_time)
|
|
.location("Conference Room A".to_string())
|
|
.description("Weekly sync".to_string())
|
|
// .add_attendee(Attendee::new(1))
|
|
// .add_attendee(Attendee::new(2))
|
|
.build();
|
|
|
|
// // Add attendees to the event using the builder pattern
|
|
// let attendee1 = Attendee::new(1);
|
|
// let attendee2 = Attendee::new(2);
|
|
|
|
// // Add attendees using the builder pattern
|
|
// event = event.add_attendee(attendee1);
|
|
// event = event.add_attendee(attendee2);
|
|
|
|
// Call build and capture the returned value
|
|
// let event = event.build();
|
|
|
|
// Store the event in the database first to get its ID
|
|
let (event_id, updated_event) = db
|
|
.collection()
|
|
.expect("Failed to get Event collection")
|
|
.set(&event)
|
|
.expect("Failed to store event");
|
|
|
|
// Add the event ID to the calendar
|
|
calendar = calendar.add_event(event_id as i64);
|
|
|
|
// Store the calendar in the database
|
|
let (_calendar_id, updated_calendar) = db
|
|
.collection::<Calendar>()
|
|
.expect("Failed to get Calendar collection")
|
|
.set(&calendar)
|
|
.expect("Failed to store calendar");
|
|
|
|
println!("Mock database seeded with calendar data:");
|
|
println!(
|
|
" - Added calendar: {} (ID: {})",
|
|
updated_calendar.name, updated_calendar.base_data.id
|
|
);
|
|
println!(
|
|
" - Added event: {} (ID: {})",
|
|
updated_event.title, updated_event.base_data.id
|
|
);
|
|
}
|
|
|
|
/// Seed the mock database with flow data
|
|
#[cfg(feature = "flow")]
|
|
fn seed_flow_data(db: Arc<OurDB>) {
|
|
// Create a flow
|
|
let mut flow = Flow::new(0, "Document Approval".to_string());
|
|
|
|
// Set flow properties using the builder pattern
|
|
flow = flow.status("draft".to_string());
|
|
flow = flow.name("Document Approval Flow".to_string());
|
|
|
|
// Create flow steps
|
|
let mut step1 = FlowStep::new(0, 1);
|
|
step1 = step1.description("Initial review by legal team".to_string());
|
|
step1 = step1.status("pending".to_string());
|
|
|
|
let mut step2 = FlowStep::new(0, 2);
|
|
step2 = step2.description("Approval by department head".to_string());
|
|
step2 = step2.status("pending".to_string());
|
|
|
|
// Add signature requirements
|
|
let mut req1 = SignatureRequirement::new(
|
|
0,
|
|
1,
|
|
"Legal Team".to_string(),
|
|
"Please review this document".to_string(),
|
|
);
|
|
let mut req2 = SignatureRequirement::new(
|
|
0,
|
|
2,
|
|
"Department Head".to_string(),
|
|
"Please approve this document".to_string(),
|
|
);
|
|
|
|
// Add steps to flow
|
|
flow = flow.add_step(step1);
|
|
flow = flow.add_step(step2);
|
|
|
|
// Store in the database
|
|
let (_, updated_flow) = db
|
|
.collection::<Flow>()
|
|
.expect("Failed to get Flow collection")
|
|
.set(&flow)
|
|
.expect("Failed to store flow");
|
|
|
|
// Store signature requirements in the database
|
|
let (_, updated_req1) = db
|
|
.collection::<SignatureRequirement>()
|
|
.expect("Failed to get SignatureRequirement collection")
|
|
.set(&req1)
|
|
.expect("Failed to store signature requirement");
|
|
|
|
let (_, updated_req2) = db
|
|
.collection::<SignatureRequirement>()
|
|
.expect("Failed to get SignatureRequirement collection")
|
|
.set(&req2)
|
|
.expect("Failed to store signature requirement");
|
|
|
|
println!("Mock database seeded with flow data:");
|
|
println!(
|
|
" - Added flow: {} (ID: {})",
|
|
updated_flow.name, updated_flow.base_data.id
|
|
);
|
|
println!(" - Added {} steps", updated_flow.steps.len());
|
|
println!(
|
|
" - Added signature requirements with IDs: {} and {}",
|
|
updated_req1.base_data.id, updated_req2.base_data.id
|
|
);
|
|
}
|
|
|
|
/// Seed the mock database with legal data
|
|
#[cfg(feature = "legal")]
|
|
fn seed_legal_data(db: Arc<OurDB>) {
|
|
// Create a contract
|
|
let mut contract = Contract::new(None, "Service Agreement".to_string());
|
|
contract.description = Some("Agreement for software development services".to_string());
|
|
contract.status = ContractStatus::Draft;
|
|
|
|
// Create a revision
|
|
let revision = ContractRevision::new(
|
|
None,
|
|
"Initial draft".to_string(),
|
|
"https://example.com/contract/v1".to_string(),
|
|
);
|
|
|
|
// Create signers
|
|
let signer1 = ContractSigner::new(None, 1, "Client".to_string());
|
|
let signer2 = ContractSigner::new(None, 2, "Provider".to_string());
|
|
|
|
// Add revision and signers to contract
|
|
contract.add_revision(revision);
|
|
contract.add_signer(signer1);
|
|
contract.add_signer(signer2);
|
|
|
|
// Store in the database
|
|
let (_, updated_contract) = db
|
|
.collection::<Contract>()
|
|
.expect("Failed to get Contract collection")
|
|
.set(&contract)
|
|
.expect("Failed to store contract");
|
|
|
|
println!("Mock database seeded with legal data:");
|
|
println!(
|
|
" - Added contract: {} (ID: {})",
|
|
updated_contract.name, updated_contract.base_data.id
|
|
);
|
|
println!(
|
|
" - Added {} revisions and {} signers",
|
|
updated_contract.revisions.len(),
|
|
updated_contract.signers.len()
|
|
);
|
|
}
|
|
|
|
/// Seed the mock database with projects data
|
|
#[cfg(feature = "projects")]
|
|
fn seed_projects_data(db: Arc<OurDB>) {
|
|
// Create a project
|
|
let mut project = Project::new(None, "Website Redesign".to_string());
|
|
project.description = Some("Redesign the company website".to_string());
|
|
project.status = ProjectStatus::InProgress;
|
|
project.priority = Priority::High;
|
|
|
|
// Add members and tags
|
|
project.add_member_id(1);
|
|
project.add_member_id(2);
|
|
project.add_tag("design".to_string());
|
|
project.add_tag("web".to_string());
|
|
|
|
// Store in the database
|
|
let (_, updated_project) = db
|
|
.collection::<Project>()
|
|
.expect("Failed to get Project collection")
|
|
.set(&project)
|
|
.expect("Failed to store project");
|
|
|
|
println!("Mock database seeded with projects data:");
|
|
println!(
|
|
" - Added project: {} (ID: {})",
|
|
updated_project.name, updated_project.base_data.id
|
|
);
|
|
println!(
|
|
" - Status: {}, Priority: {}",
|
|
updated_project.status, updated_project.priority
|
|
);
|
|
println!(
|
|
" - Added {} members and {} tags",
|
|
updated_project.member_ids.len(),
|
|
updated_project.tags.len()
|
|
);
|
|
}
|
|
/// Seed the mock database with finance data
|
|
fn seed_finance_data(db: Arc<OurDB>) {
|
|
// Create a user account
|
|
let mut account = Account::new()
|
|
.name("Demo Account")
|
|
.user_id(1)
|
|
.description("Demo trading account")
|
|
.ledger("ethereum")
|
|
.address("0x1234567890abcdef1234567890abcdef12345678")
|
|
.pubkey("0xabcdef1234567890abcdef1234567890abcdef12");
|
|
|
|
// Store the account in the database
|
|
let (account_id, updated_account) = db
|
|
.collection::<Account>()
|
|
.expect("Failed to get Account collection")
|
|
.set(&account)
|
|
.expect("Failed to store account");
|
|
|
|
// Create an ERC20 token asset
|
|
let token_asset = Asset::new()
|
|
.name("HERO Token")
|
|
.description("Herocode governance token")
|
|
.amount(1000.0)
|
|
.address("0x9876543210abcdef9876543210abcdef98765432")
|
|
.asset_type(AssetType::Erc20)
|
|
.decimals(18);
|
|
|
|
// Store the token asset in the database
|
|
let (token_id, updated_token) = db
|
|
.collection::<Asset>()
|
|
.expect("Failed to get Asset collection")
|
|
.set(&token_asset)
|
|
.expect("Failed to store token asset");
|
|
|
|
// Create an NFT asset
|
|
let nft_asset = Asset::new()
|
|
.name("Herocode #1")
|
|
.description("Unique digital collectible")
|
|
.amount(1.0)
|
|
.address("0xabcdef1234567890abcdef1234567890abcdef12")
|
|
.asset_type(AssetType::Erc721)
|
|
.decimals(0);
|
|
|
|
// Store the NFT asset in the database
|
|
let (nft_id, updated_nft) = db
|
|
.collection::<Asset>()
|
|
.expect("Failed to get Asset collection")
|
|
.set(&nft_asset)
|
|
.expect("Failed to store NFT asset");
|
|
|
|
// Add assets to the account
|
|
account = updated_account.add_asset(token_id);
|
|
account = account.add_asset(nft_id);
|
|
|
|
// Update the account in the database
|
|
let (_, updated_account) = db
|
|
.collection::<Account>()
|
|
.expect("Failed to get Account collection")
|
|
.set(&account)
|
|
.expect("Failed to store updated account");
|
|
|
|
// Create a listing for the NFT
|
|
let listing = Listing::new()
|
|
.seller_id(account_id)
|
|
.asset_id(nft_id)
|
|
.price(0.5)
|
|
.currency("ETH")
|
|
.listing_type(ListingType::Auction)
|
|
.title("Rare Herocode NFT".to_string())
|
|
.description("One of a kind digital collectible".to_string())
|
|
.image_url(Some("hcttps://example.com/nft/1.png".to_string()))
|
|
.add_tag("rare".to_string())
|
|
.add_tag("collectible".to_string());
|
|
|
|
// Store the listing in the database
|
|
let (_listing_id, updated_listing) = db
|
|
.collection::<Listing>()
|
|
.expect("Failed to get Listing collection")
|
|
.set(&listing)
|
|
.expect("Failed to store listing");
|
|
|
|
println!("Mock database seeded with finance data:");
|
|
println!(
|
|
" - Added account: {} (ID: {})",
|
|
updated_account.name, updated_account.base_data.id
|
|
);
|
|
println!(
|
|
" - Added token asset: {} (ID: {})",
|
|
updated_token.name, updated_token.base_data.id
|
|
);
|
|
println!(
|
|
" - Added NFT asset: {} (ID: {})",
|
|
updated_nft.name, updated_nft.base_data.id
|
|
);
|
|
println!(
|
|
" - Added listing: {} (ID: {})",
|
|
updated_listing.title, updated_listing.base_data.id
|
|
);
|
|
}
|