db/heromodels/examples/flow_rhai/flow.rs
timurgordon 2a2d69dafb fix
2025-05-27 11:34:44 +03:00

108 lines
4.3 KiB
Rust

// Hero Models - Flow Rhai Example
print("Hero Models - Flow Rhai Example");
print("=============================");
// Helper to format Option<String> (Dynamic in Rhai: String or ()) for printing
fn format_optional(val, placeholder) {
if val == () {
placeholder
} else {
val
}
}
// The database instance is now implicitly passed to DB functions.
print("DB instance will be implicitly passed.");
// --- Test Flow Model ---
print("\n--- Testing Flow Model ---");
// Create a new flow using the constructor and builder methods
print("Creating a new flow (ID: 1, UUID: flow-uuid-001)...");
let flow1 = new_flow(1, "flow-uuid-001")
.name("Document Approval Workflow")
.status("Active");
print("Flow object created: " + flow1);
print("Flow ID: " + flow1.id);
print("Flow UUID: " + flow1.flow_uuid);
print("Flow Name: " + flow1.name);
print("Flow Status: " + flow1.status);
// Save the flow to the database
set_flow(flow1);
print("Flow saved to database.");
// Retrieve the flow
let retrieved_flow = get_flow_by_id(1);
print("Retrieved Flow by ID (1): " + retrieved_flow.name + ", Status: " + retrieved_flow.status);
// --- Test FlowStep Model (as part of Flow) ---
print("\n--- Testing FlowStep Model (as part of Flow) ---");
// Create FlowSteps
print("Creating flow steps and adding to flow...");
let step1 = new_flow_step(101, 1) // id, step_order
.description("Initial Review by Manager")
.status("Pending");
let step2 = new_flow_step(102, 2) // id, step_order. Note: FlowStep ID 102 will be used for sig_req1 & sig_req2
.description("Legal Team Sign-off")
.status("Pending");
// Add steps to the flow created earlier
flow1 = flow1.add_step(step1);
flow1 = flow1.add_step(step2);
print("Flow now has " + flow1.steps.len() + " steps.");
print("First step description: " + format_optional(flow1.steps[0].description, "[No Description]"));
// Re-save the flow with its steps
set_flow(flow1);
print("Flow with steps saved to database.");
// Retrieve the flow and check its steps
let retrieved_flow_with_steps = get_flow_by_id(1);
print("Retrieved Flow by ID (1) has " + retrieved_flow_with_steps.steps.len() + " step(s).");
if retrieved_flow_with_steps.steps.len() > 0 {
print("First step of retrieved flow: " + format_optional(retrieved_flow_with_steps.steps[0].description, "[No Description]"));
}
// --- Test SignatureRequirement Model ---
print("\n--- Testing SignatureRequirement Model ---");
// Create SignatureRequirements (referencing FlowStep ID 102, which is step2)
print("Creating signature requirements for step with ID 102...");
let sig_req1 = new_signature_requirement(201, 102, "pubkey_legal_lead", "Legal Lead: Approve terms.")
.status("Required");
let sig_req2 = new_signature_requirement(202, 102, "pubkey_general_counsel", "General Counsel: Final Approval.")
.status("Required"); // signed_by and signature will default to None (Rust) / () (Rhai)
print("SigReq 1: " + sig_req1.message + " for PubKey: " + sig_req1.public_key + " (Status: " + sig_req1.status + ")");
if sig_req2.signed_by == () {
print("SigReq 2: " + sig_req2.message + " for PubKey: " + sig_req2.public_key + " (Status: " + sig_req2.status + ", Not signed yet)");
} else {
print("SigReq 2: " + sig_req2.message + " for PubKey: " + sig_req2.public_key + " (Status: " + sig_req2.status + ", Signed by: " + format_optional(sig_req2.signed_by, "[Not Signed Yet]") + ")");
}
// Save signature requirements
set_signature_requirement(sig_req1);
set_signature_requirement(sig_req2);
print("SignatureRequirements saved to database.");
// Retrieve a signature requirement
let retrieved_sig_req = get_signature_requirement_by_id(201);
print("Retrieved SignatureRequirement by ID (201): " + retrieved_sig_req.message);
// --- Test updating a SignatureRequirement ---
print("\n--- Testing Update for SignatureRequirement ---");
let updated_sig_req = retrieved_sig_req
.status("Signed")
.signed_by("pubkey_legal_lead_actual_signer_id")
.signature("base64_encoded_signature_data_here");
print("Updated SigReq 1 - Status: " + updated_sig_req.status + ", Signed By: " + format_optional(updated_sig_req.signed_by, "[Not Signed Yet]") + ", Signature: " + format_optional(updated_sig_req.signature, "[No Signature]"));
set_signature_requirement(updated_sig_req); // Save updated
print("Updated SignatureRequirement saved.");
print("\nFlow Rhai example script finished.");