db/heromodels/examples/biz_rhai/biz.rs
Mahmoud-Emad af83035d89 feat: Add new Rhai example demonstrating payment flow
- Add a new Rhai example showcasing complete payment flow
  for company registration. This includes company creation,
  payment record creation, successful payment processing,
  status updates, and handling failed and refunded payments.
- Add new example demonstrating payment integration in Rhai
  scripting.  This example showcases the usage of various
  payment status methods and verifies data from the database
  after processing payments.
- Add new examples to Cargo.toml to facilitate building and
  running the examples.  This makes it easier to integrate
  and test payment functionality using Rhai scripting.
2025-06-25 18:39:47 +03:00

307 lines
13 KiB
Rust

// Hero Models - Biz Rhai Example
print("Hero Models - Biz Rhai Example");
print("===============================");
print("DB instance will be implicitly passed to DB functions.");
// --- Enum Constants ---
print("\n--- Enum Constants ---");
print(`CompanyStatus PendingPayment: ${CompanyStatusConstants::PendingPayment}`);
print(`CompanyStatus Active: ${CompanyStatusConstants::Active}`);
print(`CompanyStatus Suspended: ${CompanyStatusConstants::Suspended}`);
print(`CompanyStatus Inactive: ${CompanyStatusConstants::Inactive}`);
print(`BusinessType Coop: ${BusinessTypeConstants::Coop}`);
print(`BusinessType Global: ${BusinessTypeConstants::Global}`);
// --- Testing Company Model ---
print("\n--- Testing Company Model ---");
let company1_uuid = "comp-uuid-alpha-001";
let company1_name = "Innovatech Solutions Ltd.";
let company1_reg = "REG-ISL-2024";
let company1_inc_date = 1672531200; // Jan 1, 2023
print(`Creating a new company (UUID: ${company1_uuid})...`);
let company1 = new_company(company1_name, company1_reg, company1_inc_date)
.email("contact@innovatech.com")
.phone("+1-555-0100")
.website("https://innovatech.com")
.address("123 Innovation Drive, Tech City, TC 54321")
.business_type(BusinessTypeConstants::Global)
.industry("Technology")
.description("Leading provider of innovative tech solutions.")
// Note: status defaults to PendingPayment for new companies
.fiscal_year_end("12-31")
.set_base_created_at(1672531200)
.set_base_modified_at(1672531205);
print(`Company 1 Name: ${company1.name}, Status: ${company1.status} (default for new companies)`);
print(`Company 1 Email: ${company1.email}, Industry: ${company1.industry}`);
// Save the company to the database
print("\nSaving company1 to database...");
company1 = set_company(company1); // Capture the company with the DB-assigned ID
print("Company1 saved.");
// Demonstrate payment flow for the company
print("\n--- Payment Processing for Company ---");
print("Creating payment record for company registration...");
let payment_intent_id = `pi_demo_${company1.id}`;
let payment = new_payment(
payment_intent_id,
company1.id,
"yearly",
500.0, // Setup fee
99.0, // Monthly fee
1688.0 // Total amount (setup + 12 months)
);
payment = set_payment(payment);
print(`Payment created: ${payment.payment_intent_id}, Status: ${payment.status}`);
// Simulate successful payment processing
print("Processing payment...");
payment = payment.complete_payment(`cus_demo_${company1.id}`);
payment = set_payment(payment);
print(`Payment completed: ${payment.status}`);
// Update company status to Active after successful payment
print("Updating company status to Active after payment...");
company1 = company1.status(CompanyStatusConstants::Active);
company1 = set_company(company1);
print(`Company status updated: ${company1.status}`);
// Retrieve the company
print(`\nRetrieving company by ID (${company1.id})...`);
let retrieved_company = get_company_by_id(company1.id);
print(`Retrieved Company: ${retrieved_company.name}, Status: ${retrieved_company.status}`);
print(`Retrieved Company Reg No: ${retrieved_company.registration_number}, Website: ${retrieved_company.website}`);
// --- Testing Shareholder Model ---
print("\n--- Testing Shareholder Model ---");
let sh1_user_id = 3001; // Example user ID
let sh1_name = "Alice Wonderland";
let sh1_since = 1672617600; // Example timestamp (Jan 2, 2023)
print(`Creating shareholder 1 for company ${company1.id}...`);
let shareholder1 = new_shareholder()
.company_id(company1.id)
.user_id(sh1_user_id)
.name(sh1_name)
.shares(1000.0)
.percentage(10.0) // CALCULATED
.type_("Individual")
.since(sh1_since)
.set_base_created_at(1672617600);
shareholder1 = set_shareholder(shareholder1);
print("Shareholder 1 saved.");
let retrieved_sh1 = get_shareholder_by_id(shareholder1.id);
print(`Retrieved Shareholder 1: ${retrieved_sh1.name}, Type: ${retrieved_sh1.type_}, Shares: ${retrieved_sh1.shares}`);
let sh2_entity_id = 4001; // Example corporate entity ID
let sh2_name = "Mad Hatter Inc.";
let sh2_since = 1672704000; // Example timestamp (Jan 3, 2023)
print(`\nCreating shareholder 2 for company ${company1.id}...`);
let shareholder2 = new_shareholder()
.company_id(company1.id)
.user_id(sh2_entity_id) // Using user_id field for entity_id for simplicity in example
.name(sh2_name)
.shares(5000.0)
.percentage(50.0)
.type_(ShareholderTypeConstants::Corporate)
.since(sh2_since)
.set_base_created_at(1672704000);
shareholder2 = set_shareholder(shareholder2);
print("Shareholder 2 saved.");
let retrieved_sh2 = get_shareholder_by_id(shareholder2.id);
print(`Retrieved Shareholder 2: ${retrieved_sh2.name}, Type: ${retrieved_sh2.type_}, Percentage: ${retrieved_sh2.percentage}`);
// --- Testing Update for Company (Example - if setters were fully implemented for complex updates) ---
print("\n--- Testing Update for Company ---");
let updated_company = retrieved_company
.description("Leading global provider of cutting-edge technology solutions and services.")
// Note: Company is already Active from payment processing above
.phone("+1-555-0199"); // Assume modified_at would be updated by set_company
print(`Updated Company - Name: ${updated_company.name}, New Phone: ${updated_company.phone}`);
print(`Company Status: ${updated_company.status} (already Active from payment)`);
set_company(updated_company);
print("Updated Company saved.");
let final_retrieved_company = get_company_by_id(company1.id);
print(`Final Retrieved Company - Description: '${final_retrieved_company.description}', Phone: ${final_retrieved_company.phone}`);
print("\n--- Testing Product Model ---");
// Print ProductType constants
print("\n--- ProductType Constants ---");
print(`Product Type Product: ${ProductTypeConstants::Product}`);
print(`Product Type Service: ${ProductTypeConstants::Service}`);
// Print ProductStatus constants
print("\n--- ProductStatus Constants ---");
print(`Product Status Available: ${ProductStatusConstants::Available}`);
print(`Product Status Unavailable: ${ProductStatusConstants::Unavailable}`);
// Create a product component
let component1 = new_product_component("Super Capacitor")
.description("High-capacity energy storage unit")
.quantity(2);
print(`\nCreated Product Component: ${component1.name}, Qty: ${component1.quantity}`);
// Create Product 1 (a physical product with a component)
let product1_name = "Advanced Gadget X";
let product1_creation_time = 1672876800; // Example timestamp (Jan 5, 2023)
print(`\nCreating Product 1: ${product1_name}...`);
let product1 = new_product()
.name(product1_name)
.description("A revolutionary gadget with cutting-edge features.")
.price(299.99)
.type_(ProductTypeConstants::Product)
.category("Electronics")
.status(ProductStatusConstants::Available)
.max_amount(1000)
.purchase_till(1704067199) // Dec 31, 2023, 23:59:59
.active_till(1735689599) // Dec 31, 2024, 23:59:59
.add_component(component1)
.set_base_created_at(product1_creation_time);
print("Saving Product 1...");
product1 = set_product(product1);
print("Product 1 saved.");
print(`\nRetrieving Product 1 (ID: ${product1.id})...`);
let retrieved_product1 = get_product_by_id(product1.id);
print(`Retrieved Product 1: ${retrieved_product1.name}, Price: ${retrieved_product1.price}, Type: ${retrieved_product1.type_}`);
if retrieved_product1.components.len() > 0 {
print(`Product 1 Component 1: ${retrieved_product1.components[0].name}, Desc: ${retrieved_product1.components[0].description}`);
} else {
print("Product 1 has no components.");
}
// Create Product 2 (a service)
let product2_name = "Cloud Backup Service - Pro Plan";
let product2_creation_time = 1672963200; // Example timestamp (Jan 6, 2023)
print(`\nCreating Product 2: ${product2_name}...`);
let product2 = new_product()
.name(product2_name)
.description("Unlimited cloud backup with 24/7 support.")
.price(19.99) // Monthly price
.type_(ProductTypeConstants::Service)
.category("Cloud Services")
.status(ProductStatusConstants::Available)
.active_till(1735689599) // Valid for a long time, or represents subscription cycle end
.set_base_created_at(product2_creation_time);
print("Saving Product 2...");
product2 = set_product(product2);
print("Product 2 saved.");
print(`\nRetrieving Product 2 (ID: ${product2.id})...`);
let retrieved_product2 = get_product_by_id(product2.id);
print(`Retrieved Product 2: ${retrieved_product2.name}, Category: ${retrieved_product2.category}, Status: ${retrieved_product2.status}`);
if retrieved_product2.components.len() > 0 {
print(`Product 2 has ${retrieved_product2.components.len()} components.`);
} else {
print("Product 2 has no components (as expected for a service).");
}
// --- Testing Sale Model ---
print("\n--- Testing Sale Model ---");
// Print SaleStatus constants
print("\n--- SaleStatus Constants ---");
print(`Sale Status Pending: ${SaleStatusConstants::Pending}`);
print(`Sale Status Completed: ${SaleStatusConstants::Completed}`);
print(`Sale Status Cancelled: ${SaleStatusConstants::Cancelled}`);
// Create SaleItem 1 (using product1 from above)
let sale_item1_product_id = product1.id; // Using product1.id after it's set // Using product1_id from product example
let sale_item1_name = retrieved_product1.name; // Using name from retrieved product1
let sale_item1_qty = 2;
let sale_item1_unit_price = retrieved_product1.price;
let sale_item1_subtotal = sale_item1_qty * sale_item1_unit_price;
print(`\nCreating SaleItem 1 for Product ID: ${sale_item1_product_id}, Name: ${sale_item1_name}...`);
let sale_item1 = new_sale_item(sale_item1_product_id, sale_item1_name, sale_item1_qty, sale_item1_unit_price, sale_item1_subtotal);
print(`SaleItem 1: Product ID ${sale_item1.product_id}, Qty: ${sale_item1.quantity}, Subtotal: ${sale_item1.subtotal}`);
// Create SaleItem 2 (using product2 from above)
let sale_item2_product_id = product2.id; // Using product2.id after it's set // Using product2_id from product example
let sale_item2_name = retrieved_product2.name;
let sale_item2_qty = 1;
let sale_item2_unit_price = retrieved_product2.price;
let sale_item2_subtotal = sale_item2_qty * sale_item2_unit_price;
print(`\nCreating SaleItem 2 for Product ID: ${sale_item2_product_id}, Name: ${sale_item2_name}...`);
let sale_item2 = new_sale_item(sale_item2_product_id, sale_item2_name, sale_item2_qty, sale_item2_unit_price, sale_item2_subtotal);
print(`SaleItem 2: Product ID ${sale_item2.product_id}, Qty: ${sale_item2.quantity}, Subtotal: ${sale_item2.subtotal}`);
// Create a Sale
let sale1_customer_id = company1.id; // Example: company1 is the customer
let sale1_date = 1673049600; // Example timestamp (Jan 7, 2023)
let sale1_total_amount = sale_item1.subtotal + sale_item2.subtotal;
print(`\nCreating Sale 1 for Customer ID: ${sale1_customer_id}...`);
let sale1 = new_sale(
sale1_customer_id, // for company_id_i64 in Rhai registration
"Temp Buyer Name", // for buyer_name in Rhai registration
"temp@buyer.com", // for buyer_email in Rhai registration
0.0, // for total_amount (will be overridden by builder)
SaleStatusConstants::Pending, // for status (will be overridden by builder)
0 // for sale_date (will be overridden by builder)
)
.customer_id(sale1_customer_id) // Actual field on Sale struct
.status(SaleStatusConstants::Pending)
.sale_date(sale1_date)
.add_item(sale_item1) // Add item one by one
.add_item(sale_item2)
// Alternatively, to set all items at once (if items were already in an array):
// .items([sale_item1, sale_item2])
.total_amount(sale1_total_amount)
.notes("First major sale of the year. Includes Advanced Gadget X and Cloud Backup Pro.")
.set_base_created_at(sale1_date)
.set_base_modified_at(sale1_date + 300) // 5 mins later
.add_base_comment(1) // Example comment ID
.add_base_comment(2);
print(`Sale 1 Created: ID ${sale1.id}, Customer ID: ${sale1.customer_id}, Status: ${sale1.status}, Total: ${sale1.total_amount}`);
print(`Sale 1 Notes: ${sale1.notes}`);
print(`Sale 1 Item Count: ${sale1.items.len()}`);
if sale1.items.len() > 0 {
print(`Sale 1 Item 1: ${sale1.items[0].name}, Qty: ${sale1.items[0].quantity}`);
}
if sale1.items.len() > 1 {
print(`Sale 1 Item 2: ${sale1.items[1].name}, Qty: ${sale1.items[1].quantity}`);
}
print(`Sale 1 Base Comments: ${sale1.comments}`);
// Save Sale 1 to database
print("\nSaving Sale 1 to database...");
sale1 = set_sale(sale1);
print("Sale 1 saved.");
// Retrieve Sale 1 from database
print(`\nRetrieving Sale 1 by ID (${sale1.id})...`);
let retrieved_sale1 = get_sale_by_id(sale1.id);
print(`Retrieved Sale 1: ID ${retrieved_sale1.id}, Customer: ${retrieved_sale1.customer_id}, Status: ${retrieved_sale1.status}`);
print(`Retrieved Sale 1 Total: ${retrieved_sale1.total_amount}, Notes: '${retrieved_sale1.notes}'`);
if retrieved_sale1.items.len() > 0 {
print(`Retrieved Sale 1 Item 1: ${retrieved_sale1.items[0].name} (Product ID: ${retrieved_sale1.items[0].product_id})`);
}
print("\nBiz Rhai example script finished.");