Merge branch 'main' into builders_in_script
* main: .. ... ... ... ... ... # Conflicts: # herodb/src/cmd/dbexample2/main.rs # herodb/src/models/biz/currency.rs # herodb/src/models/biz/product.rs
This commit is contained in:
2161
herodb/src/cmd/dbexample_governance/Cargo.lock
generated
Normal file
2161
herodb/src/cmd/dbexample_governance/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
herodb/src/cmd/dbexample_governance/Cargo.toml
Normal file
12
herodb/src/cmd/dbexample_governance/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "dbexample_governance"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "dbexample_governance"
|
||||
path = "main.rs"
|
||||
|
||||
[dependencies]
|
||||
herodb = { path = "../../.." }
|
||||
chrono = "0.4"
|
362
herodb/src/cmd/dbexample_governance/main.rs
Normal file
362
herodb/src/cmd/dbexample_governance/main.rs
Normal file
@@ -0,0 +1,362 @@
|
||||
use chrono::{Utc, Duration};
|
||||
use herodb::db::DBBuilder;
|
||||
use herodb::models::governance::{
|
||||
Company, CompanyStatus, BusinessType,
|
||||
Shareholder, ShareholderType,
|
||||
Meeting, Attendee, MeetingStatus, AttendeeRole, AttendeeStatus,
|
||||
User,
|
||||
Vote, VoteOption, Ballot, VoteStatus,
|
||||
Resolution, ResolutionStatus, Approval
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("DB Example: Governance Module");
|
||||
println!("============================");
|
||||
|
||||
// Create a temporary directory for the database
|
||||
let db_path = PathBuf::from("/tmp/dbexample_governance");
|
||||
if db_path.exists() {
|
||||
fs::remove_dir_all(&db_path)?;
|
||||
}
|
||||
fs::create_dir_all(&db_path)?;
|
||||
println!("Database path: {:?}", db_path);
|
||||
|
||||
// Create a database instance with our governance models registered
|
||||
let db = DBBuilder::new(&db_path)
|
||||
.register_model::<Company>()
|
||||
.register_model::<Shareholder>()
|
||||
.register_model::<Meeting>()
|
||||
.register_model::<User>()
|
||||
.register_model::<Vote>()
|
||||
.register_model::<Resolution>()
|
||||
.build()?;
|
||||
|
||||
println!("\n1. Creating a Company");
|
||||
println!("-------------------");
|
||||
|
||||
// Create a company
|
||||
let company = Company::new(
|
||||
1,
|
||||
"Acme Corporation".to_string(),
|
||||
"ACM123456".to_string(),
|
||||
Utc::now(),
|
||||
"December 31".to_string(),
|
||||
"info@acmecorp.com".to_string(),
|
||||
"+1-555-123-4567".to_string(),
|
||||
"https://acmecorp.com".to_string(),
|
||||
"123 Main St, Anytown, USA".to_string(),
|
||||
BusinessType::Coop,
|
||||
"Technology".to_string(),
|
||||
"A leading technology company".to_string(),
|
||||
CompanyStatus::Active,
|
||||
);
|
||||
|
||||
// Insert the company
|
||||
db.insert(&company)?;
|
||||
println!("Company created: {} (ID: {})", company.name, company.id);
|
||||
println!("Status: {:?}, Business Type: {:?}", company.status, company.business_type);
|
||||
|
||||
println!("\n2. Creating Users");
|
||||
println!("---------------");
|
||||
|
||||
// Create users
|
||||
let user1 = User::new(
|
||||
1,
|
||||
"John Doe".to_string(),
|
||||
"john.doe@acmecorp.com".to_string(),
|
||||
"password123".to_string(), // In a real app, this would be hashed
|
||||
"Acme Corporation".to_string(),
|
||||
"CEO".to_string(),
|
||||
);
|
||||
|
||||
let user2 = User::new(
|
||||
2,
|
||||
"Jane Smith".to_string(),
|
||||
"jane.smith@acmecorp.com".to_string(),
|
||||
"password456".to_string(), // In a real app, this would be hashed
|
||||
"Acme Corporation".to_string(),
|
||||
"CFO".to_string(),
|
||||
);
|
||||
|
||||
let user3 = User::new(
|
||||
3,
|
||||
"Bob Johnson".to_string(),
|
||||
"bob.johnson@acmecorp.com".to_string(),
|
||||
"password789".to_string(), // In a real app, this would be hashed
|
||||
"Acme Corporation".to_string(),
|
||||
"CTO".to_string(),
|
||||
);
|
||||
|
||||
// Insert the users
|
||||
db.insert(&user1)?;
|
||||
db.insert(&user2)?;
|
||||
db.insert(&user3)?;
|
||||
|
||||
println!("User created: {} ({})", user1.name, user1.role);
|
||||
println!("User created: {} ({})", user2.name, user2.role);
|
||||
println!("User created: {} ({})", user3.name, user3.role);
|
||||
|
||||
println!("\n3. Creating Shareholders");
|
||||
println!("----------------------");
|
||||
|
||||
// Create shareholders
|
||||
let mut shareholder1 = Shareholder::new(
|
||||
1,
|
||||
company.id,
|
||||
user1.id,
|
||||
user1.name.clone(),
|
||||
1000.0,
|
||||
40.0,
|
||||
ShareholderType::Individual,
|
||||
);
|
||||
|
||||
let mut shareholder2 = Shareholder::new(
|
||||
2,
|
||||
company.id,
|
||||
user2.id,
|
||||
user2.name.clone(),
|
||||
750.0,
|
||||
30.0,
|
||||
ShareholderType::Individual,
|
||||
);
|
||||
|
||||
let mut shareholder3 = Shareholder::new(
|
||||
3,
|
||||
company.id,
|
||||
user3.id,
|
||||
user3.name.clone(),
|
||||
750.0,
|
||||
30.0,
|
||||
ShareholderType::Individual,
|
||||
);
|
||||
|
||||
// Insert the shareholders
|
||||
db.insert(&shareholder1)?;
|
||||
db.insert(&shareholder2)?;
|
||||
db.insert(&shareholder3)?;
|
||||
|
||||
println!("Shareholder created: {} ({} shares, {}%)",
|
||||
shareholder1.name, shareholder1.shares, shareholder1.percentage);
|
||||
println!("Shareholder created: {} ({} shares, {}%)",
|
||||
shareholder2.name, shareholder2.shares, shareholder2.percentage);
|
||||
println!("Shareholder created: {} ({} shares, {}%)",
|
||||
shareholder3.name, shareholder3.shares, shareholder3.percentage);
|
||||
|
||||
// Update shareholder shares
|
||||
shareholder1.update_shares(1100.0, 44.0);
|
||||
db.insert(&shareholder1)?;
|
||||
println!("Updated shareholder: {} ({} shares, {}%)",
|
||||
shareholder1.name, shareholder1.shares, shareholder1.percentage);
|
||||
|
||||
println!("\n4. Creating a Meeting");
|
||||
println!("------------------");
|
||||
|
||||
// Create a meeting
|
||||
let mut meeting = Meeting::new(
|
||||
1,
|
||||
company.id,
|
||||
"Q2 Board Meeting".to_string(),
|
||||
Utc::now() + Duration::days(7), // Meeting in 7 days
|
||||
"Conference Room A".to_string(),
|
||||
"Quarterly board meeting to discuss financial results".to_string(),
|
||||
);
|
||||
|
||||
// Create attendees
|
||||
let attendee1 = Attendee::new(
|
||||
1,
|
||||
meeting.id,
|
||||
user1.id,
|
||||
user1.name.clone(),
|
||||
AttendeeRole::Coordinator,
|
||||
);
|
||||
|
||||
let attendee2 = Attendee::new(
|
||||
2,
|
||||
meeting.id,
|
||||
user2.id,
|
||||
user2.name.clone(),
|
||||
AttendeeRole::Member,
|
||||
);
|
||||
|
||||
let attendee3 = Attendee::new(
|
||||
3,
|
||||
meeting.id,
|
||||
user3.id,
|
||||
user3.name.clone(),
|
||||
AttendeeRole::Member,
|
||||
);
|
||||
|
||||
// Add attendees to the meeting
|
||||
meeting.add_attendee(attendee1);
|
||||
meeting.add_attendee(attendee2);
|
||||
meeting.add_attendee(attendee3);
|
||||
|
||||
// Insert the meeting
|
||||
db.insert(&meeting)?;
|
||||
println!("Meeting created: {} ({})", meeting.title, meeting.date.format("%Y-%m-%d %H:%M"));
|
||||
println!("Status: {:?}, Attendees: {}", meeting.status, meeting.attendees.len());
|
||||
|
||||
// Update attendee status
|
||||
if let Some(attendee) = meeting.find_attendee_by_user_id_mut(user2.id) {
|
||||
attendee.update_status(AttendeeStatus::Confirmed);
|
||||
}
|
||||
if let Some(attendee) = meeting.find_attendee_by_user_id_mut(user3.id) {
|
||||
attendee.update_status(AttendeeStatus::Confirmed);
|
||||
}
|
||||
db.insert(&meeting)?;
|
||||
|
||||
// Get confirmed attendees
|
||||
let confirmed = meeting.confirmed_attendees();
|
||||
println!("Confirmed attendees: {}", confirmed.len());
|
||||
for attendee in confirmed {
|
||||
println!(" - {} ({})", attendee.name, match attendee.role {
|
||||
AttendeeRole::Coordinator => "Coordinator",
|
||||
AttendeeRole::Member => "Member",
|
||||
AttendeeRole::Secretary => "Secretary",
|
||||
AttendeeRole::Participant => "Participant",
|
||||
AttendeeRole::Advisor => "Advisor",
|
||||
AttendeeRole::Admin => "Admin",
|
||||
});
|
||||
}
|
||||
|
||||
println!("\n5. Creating a Resolution");
|
||||
println!("----------------------");
|
||||
|
||||
// Create a resolution
|
||||
let mut resolution = Resolution::new(
|
||||
1,
|
||||
company.id,
|
||||
"Approval of Q1 Financial Statements".to_string(),
|
||||
"Resolution to approve the Q1 financial statements".to_string(),
|
||||
"The Board of Directors hereby approves the financial statements for Q1 2025.".to_string(),
|
||||
user1.id, // Proposed by the CEO
|
||||
);
|
||||
|
||||
// Link the resolution to the meeting
|
||||
resolution.link_to_meeting(meeting.id);
|
||||
|
||||
// Insert the resolution
|
||||
db.insert(&resolution)?;
|
||||
println!("Resolution created: {} (Status: {:?})", resolution.title, resolution.status);
|
||||
|
||||
// Propose the resolution
|
||||
resolution.propose();
|
||||
db.insert(&resolution)?;
|
||||
println!("Resolution proposed on {}", resolution.proposed_at.format("%Y-%m-%d"));
|
||||
|
||||
// Add approvals
|
||||
resolution.add_approval(user1.id, user1.name.clone(), true, "Approved as proposed".to_string());
|
||||
resolution.add_approval(user2.id, user2.name.clone(), true, "Financials look good".to_string());
|
||||
resolution.add_approval(user3.id, user3.name.clone(), true, "No concerns".to_string());
|
||||
db.insert(&resolution)?;
|
||||
|
||||
// Check approval status
|
||||
println!("Approvals: {}, Rejections: {}",
|
||||
resolution.approval_count(),
|
||||
resolution.rejection_count());
|
||||
|
||||
// Approve the resolution
|
||||
resolution.approve();
|
||||
db.insert(&resolution)?;
|
||||
println!("Resolution approved on {}",
|
||||
resolution.approved_at.unwrap().format("%Y-%m-%d"));
|
||||
|
||||
println!("\n6. Creating a Vote");
|
||||
println!("----------------");
|
||||
|
||||
// Create a vote
|
||||
let mut vote = Vote::new(
|
||||
1,
|
||||
company.id,
|
||||
"Vote on New Product Line".to_string(),
|
||||
"Vote to approve investment in new product line".to_string(),
|
||||
Utc::now(),
|
||||
Utc::now() + Duration::days(3), // Voting period of 3 days
|
||||
VoteStatus::Open,
|
||||
);
|
||||
|
||||
// Add voting options
|
||||
vote.add_option("Approve".to_string(), 0);
|
||||
vote.add_option("Reject".to_string(), 0);
|
||||
vote.add_option("Abstain".to_string(), 0);
|
||||
|
||||
// Insert the vote
|
||||
db.insert(&vote)?;
|
||||
println!("Vote created: {} (Status: {:?})", vote.title, vote.status);
|
||||
println!("Voting period: {} to {}",
|
||||
vote.start_date.format("%Y-%m-%d"),
|
||||
vote.end_date.format("%Y-%m-%d"));
|
||||
|
||||
// Cast ballots
|
||||
vote.add_ballot(user1.id, 1, 1000); // User 1 votes "Approve" with 1000 shares
|
||||
vote.add_ballot(user2.id, 1, 750); // User 2 votes "Approve" with 750 shares
|
||||
vote.add_ballot(user3.id, 3, 750); // User 3 votes "Abstain" with 750 shares
|
||||
db.insert(&vote)?;
|
||||
|
||||
// Check voting results
|
||||
println!("Voting results:");
|
||||
for option in &vote.options {
|
||||
println!(" - {}: {} votes", option.text, option.count);
|
||||
}
|
||||
|
||||
// Create a resolution for this vote
|
||||
let mut vote_resolution = Resolution::new(
|
||||
2,
|
||||
company.id,
|
||||
"Investment in New Product Line".to_string(),
|
||||
"Resolution to approve investment in new product line".to_string(),
|
||||
"The Board of Directors hereby approves an investment of $1,000,000 in the new product line.".to_string(),
|
||||
user1.id, // Proposed by the CEO
|
||||
);
|
||||
|
||||
// Link the resolution to the vote
|
||||
vote_resolution.link_to_vote(vote.id);
|
||||
vote_resolution.propose();
|
||||
db.insert(&vote_resolution)?;
|
||||
println!("Created resolution linked to vote: {}", vote_resolution.title);
|
||||
|
||||
println!("\n7. Retrieving Related Objects");
|
||||
println!("---------------------------");
|
||||
|
||||
// Retrieve company and related objects
|
||||
let retrieved_company = db.get::<Company>(&company.id.to_string())?;
|
||||
println!("Company: {} (ID: {})", retrieved_company.name, retrieved_company.id);
|
||||
|
||||
// Get resolutions for this company
|
||||
let company_resolutions = retrieved_company.get_resolutions(&db)?;
|
||||
println!("Company has {} resolutions:", company_resolutions.len());
|
||||
for res in company_resolutions {
|
||||
println!(" - {} (Status: {:?})", res.title, res.status);
|
||||
}
|
||||
|
||||
// Get meeting and its resolutions
|
||||
let retrieved_meeting = db.get::<Meeting>(&meeting.id.to_string())?;
|
||||
println!("Meeting: {} ({})", retrieved_meeting.title, retrieved_meeting.date.format("%Y-%m-%d"));
|
||||
|
||||
let meeting_resolutions = retrieved_meeting.get_resolutions(&db)?;
|
||||
println!("Meeting has {} resolutions:", meeting_resolutions.len());
|
||||
for res in meeting_resolutions {
|
||||
println!(" - {} (Status: {:?})", res.title, res.status);
|
||||
}
|
||||
|
||||
// Get vote and its resolution
|
||||
let retrieved_vote = db.get::<Vote>(&vote.id.to_string())?;
|
||||
println!("Vote: {} (Status: {:?})", retrieved_vote.title, retrieved_vote.status);
|
||||
|
||||
if let Ok(Some(vote_res)) = retrieved_vote.get_resolution(&db) {
|
||||
println!("Vote is linked to resolution: {}", vote_res.title);
|
||||
}
|
||||
|
||||
// Get resolution and its related objects
|
||||
let retrieved_resolution = db.get::<Resolution>(&resolution.id.to_string())?;
|
||||
println!("Resolution: {} (Status: {:?})", retrieved_resolution.title, retrieved_resolution.status);
|
||||
|
||||
if let Ok(Some(res_meeting)) = retrieved_resolution.get_meeting(&db) {
|
||||
println!("Resolution is discussed in meeting: {}", res_meeting.title);
|
||||
}
|
||||
|
||||
println!("\nExample completed successfully!");
|
||||
Ok(())
|
||||
}
|
399
herodb/src/cmd/dbexample_mcc/main.rs
Normal file
399
herodb/src/cmd/dbexample_mcc/main.rs
Normal file
@@ -0,0 +1,399 @@
|
||||
use chrono::{Utc, Duration};
|
||||
use herodb::db::DBBuilder;
|
||||
use herodb::models::mcc::{
|
||||
Calendar, Event,
|
||||
Email, Attachment, Envelope,
|
||||
Contact, Message
|
||||
};
|
||||
use herodb::models::circle::Circle;
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("DB Example MCC: Mail, Calendar, Contacts with Group Support");
|
||||
println!("=======================================================");
|
||||
|
||||
// Create a temporary directory for the database
|
||||
let db_path = PathBuf::from("/tmp/dbexample_mcc");
|
||||
if db_path.exists() {
|
||||
fs::remove_dir_all(&db_path)?;
|
||||
}
|
||||
fs::create_dir_all(&db_path)?;
|
||||
println!("Database path: {:?}", db_path);
|
||||
|
||||
// Create a database instance with our models registered
|
||||
let db = DBBuilder::new(&db_path)
|
||||
.register_model::<Calendar>()
|
||||
.register_model::<Event>()
|
||||
.register_model::<Email>()
|
||||
.register_model::<Contact>()
|
||||
.register_model::<Message>()
|
||||
.register_model::<Circle>()
|
||||
.build()?;
|
||||
|
||||
println!("\n1. Creating Circles (Groups)");
|
||||
println!("---------------------------");
|
||||
|
||||
// Create circles (groups)
|
||||
let work_circle = Circle::new(
|
||||
1,
|
||||
"Work".to_string(),
|
||||
"Work-related communications".to_string()
|
||||
);
|
||||
|
||||
let family_circle = Circle::new(
|
||||
2,
|
||||
"Family".to_string(),
|
||||
"Family communications".to_string()
|
||||
);
|
||||
|
||||
let friends_circle = Circle::new(
|
||||
3,
|
||||
"Friends".to_string(),
|
||||
"Friends communications".to_string()
|
||||
);
|
||||
|
||||
// Insert circles
|
||||
db.set::<Circle>(&work_circle)?;
|
||||
db.set::<Circle>(&family_circle)?;
|
||||
db.set::<Circle>(&friends_circle)?;
|
||||
|
||||
println!("Created circles:");
|
||||
println!(" - Circle #{}: {}", work_circle.id, work_circle.name);
|
||||
println!(" - Circle #{}: {}", family_circle.id, family_circle.name);
|
||||
println!(" - Circle #{}: {}", friends_circle.id, friends_circle.name);
|
||||
|
||||
println!("\n2. Creating Contacts with Group Support");
|
||||
println!("------------------------------------");
|
||||
|
||||
// Create contacts
|
||||
let mut john = Contact::new(
|
||||
1,
|
||||
"John".to_string(),
|
||||
"Doe".to_string(),
|
||||
"john.doe@example.com".to_string(),
|
||||
"work".to_string()
|
||||
);
|
||||
john.add_group(work_circle.id);
|
||||
|
||||
let mut alice = Contact::new(
|
||||
2,
|
||||
"Alice".to_string(),
|
||||
"Smith".to_string(),
|
||||
"alice.smith@example.com".to_string(),
|
||||
"family".to_string()
|
||||
);
|
||||
alice.add_group(family_circle.id);
|
||||
|
||||
let mut bob = Contact::new(
|
||||
3,
|
||||
"Bob".to_string(),
|
||||
"Johnson".to_string(),
|
||||
"bob.johnson@example.com".to_string(),
|
||||
"friends".to_string()
|
||||
);
|
||||
bob.add_group(friends_circle.id);
|
||||
bob.add_group(work_circle.id); // Bob is both a friend and a work contact
|
||||
|
||||
// Insert contacts
|
||||
db.set::<Contact>(&john)?;
|
||||
db.set::<Contact>(&alice)?;
|
||||
db.set::<Contact>(&bob)?;
|
||||
|
||||
println!("Created contacts:");
|
||||
println!(" - {}: {} (Groups: {:?})", john.full_name(), john.email, john.groups);
|
||||
println!(" - {}: {} (Groups: {:?})", alice.full_name(), alice.email, alice.groups);
|
||||
println!(" - {}: {} (Groups: {:?})", bob.full_name(), bob.email, bob.groups);
|
||||
|
||||
println!("\n3. Creating Calendars with Group Support");
|
||||
println!("-------------------------------------");
|
||||
|
||||
// Create calendars
|
||||
let mut work_calendar = Calendar::new(
|
||||
1,
|
||||
"Work Calendar".to_string(),
|
||||
"Work-related events".to_string()
|
||||
);
|
||||
work_calendar.add_group(work_circle.id);
|
||||
|
||||
let mut personal_calendar = Calendar::new(
|
||||
2,
|
||||
"Personal Calendar".to_string(),
|
||||
"Personal events".to_string()
|
||||
);
|
||||
personal_calendar.add_group(family_circle.id);
|
||||
personal_calendar.add_group(friends_circle.id);
|
||||
|
||||
// Insert calendars
|
||||
db.set::<Calendar>(&work_calendar)?;
|
||||
db.set::<Calendar>(&personal_calendar)?;
|
||||
|
||||
println!("Created calendars:");
|
||||
println!(" - {}: {} (Groups: {:?})", work_calendar.id, work_calendar.title, work_calendar.groups);
|
||||
println!(" - {}: {} (Groups: {:?})", personal_calendar.id, personal_calendar.title, personal_calendar.groups);
|
||||
|
||||
println!("\n4. Creating Events with Group Support");
|
||||
println!("----------------------------------");
|
||||
|
||||
// Create events
|
||||
let now = Utc::now();
|
||||
let tomorrow = now + Duration::days(1);
|
||||
let next_week = now + Duration::days(7);
|
||||
|
||||
let mut work_meeting = Event::new(
|
||||
1,
|
||||
work_calendar.id,
|
||||
"Team Meeting".to_string(),
|
||||
"Weekly team sync".to_string(),
|
||||
"Conference Room A".to_string(),
|
||||
tomorrow,
|
||||
tomorrow + Duration::hours(1),
|
||||
"organizer@example.com".to_string()
|
||||
);
|
||||
work_meeting.add_group(work_circle.id);
|
||||
work_meeting.add_attendee(john.email.clone());
|
||||
work_meeting.add_attendee(bob.email.clone());
|
||||
|
||||
let mut family_dinner = Event::new(
|
||||
2,
|
||||
personal_calendar.id,
|
||||
"Family Dinner".to_string(),
|
||||
"Weekly family dinner".to_string(),
|
||||
"Home".to_string(),
|
||||
next_week,
|
||||
next_week + Duration::hours(2),
|
||||
"me@example.com".to_string()
|
||||
);
|
||||
family_dinner.add_group(family_circle.id);
|
||||
family_dinner.add_attendee(alice.email.clone());
|
||||
|
||||
// Insert events
|
||||
db.set::<Event>(&work_meeting)?;
|
||||
db.set::<Event>(&family_dinner)?;
|
||||
|
||||
println!("Created events:");
|
||||
println!(" - {}: {} on {} (Groups: {:?})",
|
||||
work_meeting.id,
|
||||
work_meeting.title,
|
||||
work_meeting.start_time.format("%Y-%m-%d %H:%M"),
|
||||
work_meeting.groups
|
||||
);
|
||||
println!(" - {}: {} on {} (Groups: {:?})",
|
||||
family_dinner.id,
|
||||
family_dinner.title,
|
||||
family_dinner.start_time.format("%Y-%m-%d %H:%M"),
|
||||
family_dinner.groups
|
||||
);
|
||||
|
||||
println!("\n5. Creating Emails with Group Support");
|
||||
println!("----------------------------------");
|
||||
|
||||
// Create emails
|
||||
let mut work_email = Email::new(
|
||||
1,
|
||||
101,
|
||||
1,
|
||||
"INBOX".to_string(),
|
||||
"Here are the meeting notes from yesterday's discussion.".to_string()
|
||||
);
|
||||
work_email.add_group(work_circle.id);
|
||||
|
||||
let work_attachment = Attachment {
|
||||
filename: "meeting_notes.pdf".to_string(),
|
||||
content_type: "application/pdf".to_string(),
|
||||
hash: "abc123def456".to_string(),
|
||||
size: 1024,
|
||||
};
|
||||
work_email.add_attachment(work_attachment);
|
||||
|
||||
let work_envelope = Envelope {
|
||||
date: now.timestamp(),
|
||||
subject: "Meeting Notes".to_string(),
|
||||
from: vec!["john.doe@example.com".to_string()],
|
||||
sender: vec!["john.doe@example.com".to_string()],
|
||||
reply_to: vec!["john.doe@example.com".to_string()],
|
||||
to: vec!["me@example.com".to_string()],
|
||||
cc: vec!["bob.johnson@example.com".to_string()],
|
||||
bcc: vec![],
|
||||
in_reply_to: "".to_string(),
|
||||
message_id: "msg123@example.com".to_string(),
|
||||
};
|
||||
work_email.set_envelope(work_envelope);
|
||||
|
||||
let mut family_email = Email::new(
|
||||
2,
|
||||
102,
|
||||
2,
|
||||
"INBOX".to_string(),
|
||||
"Looking forward to seeing you at dinner next week!".to_string()
|
||||
);
|
||||
family_email.add_group(family_circle.id);
|
||||
|
||||
let family_envelope = Envelope {
|
||||
date: now.timestamp(),
|
||||
subject: "Family Dinner".to_string(),
|
||||
from: vec!["alice.smith@example.com".to_string()],
|
||||
sender: vec!["alice.smith@example.com".to_string()],
|
||||
reply_to: vec!["alice.smith@example.com".to_string()],
|
||||
to: vec!["me@example.com".to_string()],
|
||||
cc: vec![],
|
||||
bcc: vec![],
|
||||
in_reply_to: "".to_string(),
|
||||
message_id: "msg456@example.com".to_string(),
|
||||
};
|
||||
family_email.set_envelope(family_envelope);
|
||||
|
||||
// Insert emails
|
||||
db.set::<Email>(&work_email)?;
|
||||
db.set::<Email>(&family_email)?;
|
||||
|
||||
println!("Created emails:");
|
||||
println!(" - From: {}, Subject: {} (Groups: {:?})",
|
||||
work_email.envelope.as_ref().unwrap().from[0],
|
||||
work_email.envelope.as_ref().unwrap().subject,
|
||||
work_email.groups
|
||||
);
|
||||
println!(" - From: {}, Subject: {} (Groups: {:?})",
|
||||
family_email.envelope.as_ref().unwrap().from[0],
|
||||
family_email.envelope.as_ref().unwrap().subject,
|
||||
family_email.groups
|
||||
);
|
||||
|
||||
println!("\n6. Creating Messages (Chat) with Group Support");
|
||||
println!("-----------------------------------------");
|
||||
|
||||
// Create messages
|
||||
let mut work_chat = Message::new(
|
||||
1,
|
||||
"thread_work_123".to_string(),
|
||||
"john.doe@example.com".to_string(),
|
||||
"Can we move the meeting to 3pm?".to_string()
|
||||
);
|
||||
work_chat.add_group(work_circle.id);
|
||||
work_chat.add_recipient("me@example.com".to_string());
|
||||
work_chat.add_recipient("bob.johnson@example.com".to_string());
|
||||
|
||||
let mut friends_chat = Message::new(
|
||||
2,
|
||||
"thread_friends_456".to_string(),
|
||||
"bob.johnson@example.com".to_string(),
|
||||
"Are we still on for the game this weekend?".to_string()
|
||||
);
|
||||
friends_chat.add_group(friends_circle.id);
|
||||
friends_chat.add_recipient("me@example.com".to_string());
|
||||
friends_chat.add_reaction("👍".to_string());
|
||||
|
||||
// Insert messages
|
||||
db.set::<Message>(&work_chat)?;
|
||||
db.set::<Message>(&friends_chat)?;
|
||||
|
||||
println!("Created messages:");
|
||||
println!(" - From: {}, Content: {} (Groups: {:?})",
|
||||
work_chat.sender_id,
|
||||
work_chat.content,
|
||||
work_chat.groups
|
||||
);
|
||||
println!(" - From: {}, Content: {} (Groups: {:?}, Reactions: {:?})",
|
||||
friends_chat.sender_id,
|
||||
friends_chat.content,
|
||||
friends_chat.groups,
|
||||
friends_chat.meta.reactions
|
||||
);
|
||||
|
||||
println!("\n7. Demonstrating Utility Methods");
|
||||
println!("------------------------------");
|
||||
|
||||
// Filter contacts by group
|
||||
println!("\nFiltering contacts by work group (ID: {}):", work_circle.id);
|
||||
let all_contacts = db.list::<Contact>()?;
|
||||
for contact in all_contacts {
|
||||
if contact.filter_by_groups(&[work_circle.id]) {
|
||||
println!(" - {} ({})", contact.full_name(), contact.email);
|
||||
}
|
||||
}
|
||||
|
||||
// Search emails by subject
|
||||
println!("\nSearching emails with subject containing 'Meeting':");
|
||||
let all_emails = db.list::<Email>()?;
|
||||
for email in all_emails {
|
||||
if email.search_by_subject("Meeting") {
|
||||
println!(" - Subject: {}, From: {}",
|
||||
email.envelope.as_ref().unwrap().subject,
|
||||
email.envelope.as_ref().unwrap().from[0]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Get events for a calendar
|
||||
println!("\nGetting events for Work Calendar (ID: {}):", work_calendar.id);
|
||||
let all_events = db.list::<Event>()?;
|
||||
let work_events: Vec<Event> = all_events
|
||||
.into_iter()
|
||||
.filter(|event| event.calendar_id == work_calendar.id)
|
||||
.collect();
|
||||
for event in work_events {
|
||||
println!(" - {}: {} on {}",
|
||||
event.id,
|
||||
event.title,
|
||||
event.start_time.format("%Y-%m-%d %H:%M")
|
||||
);
|
||||
}
|
||||
|
||||
// Get attendee contacts for an event
|
||||
println!("\nGetting attendee contacts for Team Meeting (ID: {}):", work_meeting.id);
|
||||
let all_contacts = db.list::<Contact>()?;
|
||||
let attendee_contacts: Vec<Contact> = all_contacts
|
||||
.into_iter()
|
||||
.filter(|contact| work_meeting.attendees.contains(&contact.email))
|
||||
.collect();
|
||||
for contact in attendee_contacts {
|
||||
println!(" - {} ({})", contact.full_name(), contact.email);
|
||||
}
|
||||
|
||||
// Convert email to message
|
||||
println!("\nConverting work email to message:");
|
||||
let email_to_message = work_email.to_message(3, "thread_converted_789".to_string());
|
||||
println!(" - Original Email Subject: {}", work_email.envelope.as_ref().unwrap().subject);
|
||||
println!(" - Converted Message Content: {}", email_to_message.content.split('\n').next().unwrap_or(""));
|
||||
println!(" - Converted Message Groups: {:?}", email_to_message.groups);
|
||||
|
||||
// Insert the converted message
|
||||
db.set::<Message>(&email_to_message)?;
|
||||
|
||||
println!("\n8. Relationship Management");
|
||||
println!("------------------------");
|
||||
|
||||
// Get the calendar for an event
|
||||
println!("\nGetting calendar for Family Dinner event (ID: {}):", family_dinner.id);
|
||||
let event_calendar = db.get::<Calendar>(&family_dinner.calendar_id.to_string())?;
|
||||
println!(" - Calendar: {} ({})", event_calendar.title, event_calendar.description);
|
||||
|
||||
// Get events for a contact
|
||||
println!("\nGetting events where John Doe is an attendee:");
|
||||
let all_events = db.list::<Event>()?;
|
||||
let john_events: Vec<Event> = all_events
|
||||
.into_iter()
|
||||
.filter(|event| event.attendees.contains(&john.email))
|
||||
.collect();
|
||||
for event in john_events {
|
||||
println!(" - {}: {} on {}",
|
||||
event.id,
|
||||
event.title,
|
||||
event.start_time.format("%Y-%m-%d %H:%M")
|
||||
);
|
||||
}
|
||||
|
||||
// Get messages in the same thread
|
||||
println!("\nGetting all messages in the work chat thread:");
|
||||
let all_messages = db.list::<Message>()?;
|
||||
let thread_messages: Vec<Message> = all_messages
|
||||
.into_iter()
|
||||
.filter(|message| message.thread_id == work_chat.thread_id)
|
||||
.collect();
|
||||
for message in thread_messages {
|
||||
println!(" - From: {}, Content: {}", message.sender_id, message.content);
|
||||
}
|
||||
|
||||
println!("\nExample completed successfully!");
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user