112 lines
4.1 KiB
Plaintext
112 lines
4.1 KiB
Plaintext
// flow_script.rhai
|
|
// Example Rhai script for working with Flow models
|
|
|
|
// Constants for Flow status
|
|
const STATUS_DRAFT = "draft";
|
|
const STATUS_ACTIVE = "active";
|
|
const STATUS_COMPLETED = "completed";
|
|
const STATUS_CANCELLED = "cancelled";
|
|
|
|
// Create a new flow using builder pattern
|
|
let my_flow = new_flow(0, "flow-123");
|
|
name(my_flow, "Document Approval Flow");
|
|
status(my_flow, STATUS_DRAFT);
|
|
|
|
print(`Created flow: ${get_flow_name(my_flow)} (ID: ${get_flow_id(my_flow)})`);
|
|
print(`Status: ${get_flow_status(my_flow)}`);
|
|
|
|
// Create flow steps using builder pattern
|
|
let step1 = new_flow_step(0, 1);
|
|
description(step1, "Initial review by legal team");
|
|
status(step1, STATUS_DRAFT);
|
|
|
|
let step2 = new_flow_step(0, 2);
|
|
description(step2, "Approval by department head");
|
|
status(step2, STATUS_DRAFT);
|
|
|
|
let step3 = new_flow_step(0, 3);
|
|
description(step3, "Final signature by CEO");
|
|
status(step3, STATUS_DRAFT);
|
|
|
|
// Create signature requirements using builder pattern
|
|
let req1 = new_signature_requirement(0, get_flow_step_id(step1), "legal@example.com", "Please review this document");
|
|
signed_by(req1, "Legal Team");
|
|
status(req1, STATUS_DRAFT);
|
|
|
|
let req2 = new_signature_requirement(0, get_flow_step_id(step2), "dept@example.com", "Department approval needed");
|
|
signed_by(req2, "Department Head");
|
|
status(req2, STATUS_DRAFT);
|
|
|
|
let req3 = new_signature_requirement(0, get_flow_step_id(step3), "ceo@example.com", "Final approval required");
|
|
signed_by(req3, "CEO");
|
|
status(req3, STATUS_DRAFT);
|
|
|
|
print(`Created flow steps with signature requirements`);
|
|
|
|
// Add steps to the flow
|
|
let flow_with_steps = my_flow;
|
|
add_step(flow_with_steps, step1);
|
|
add_step(flow_with_steps, step2);
|
|
add_step(flow_with_steps, step3);
|
|
|
|
print(`Added steps to flow. Flow now has ${get_flow_steps(flow_with_steps).len()} steps`);
|
|
|
|
// Activate the flow
|
|
let active_flow = flow_with_steps;
|
|
status(active_flow, STATUS_ACTIVE);
|
|
print(`Updated flow status to: ${get_flow_status(active_flow)}`);
|
|
|
|
// Save the flow to the database
|
|
let saved_flow = db::save_flow(active_flow);
|
|
print(`Flow saved to database with ID: ${get_flow_id(saved_flow)}`);
|
|
|
|
// Save signature requirements to the database
|
|
let saved_req1 = db::save_signature_requirement(req1);
|
|
let saved_req2 = db::save_signature_requirement(req2);
|
|
let saved_req3 = db::save_signature_requirement(req3);
|
|
print(`Signature requirements saved to database with IDs: ${get_signature_requirement_id(saved_req1)}, ${get_signature_requirement_id(saved_req2)}, ${get_signature_requirement_id(saved_req3)}`);
|
|
|
|
// Retrieve the flow from the database
|
|
let retrieved_flow = db::get_flow_by_id(get_flow_id(saved_flow));
|
|
print(`Retrieved flow: ${get_flow_name(retrieved_flow)}`);
|
|
print(`It has ${get_flow_steps(retrieved_flow).len()} steps`);
|
|
|
|
// Complete the flow
|
|
let completed_flow = retrieved_flow;
|
|
status(completed_flow, STATUS_COMPLETED);
|
|
print(`Updated retrieved flow status to: ${get_flow_status(completed_flow)}`);
|
|
|
|
// Save the updated flow
|
|
db::save_flow(completed_flow);
|
|
print("Updated flow saved to database");
|
|
|
|
// List all flows in the database
|
|
let all_flows = db::list_flows();
|
|
print("\nListing all flows in database:");
|
|
let flow_count = 0;
|
|
for flow in all_flows {
|
|
print(` - Flow: ${get_flow_name(flow)} (ID: ${get_flow_id(flow)})`);
|
|
flow_count += 1;
|
|
}
|
|
print(`Total flows: ${flow_count}`);
|
|
|
|
// List all signature requirements
|
|
let all_reqs = db::list_signature_requirements();
|
|
print("\nListing all signature requirements in database:");
|
|
let req_count = 0;
|
|
for req in all_reqs {
|
|
print(` - Requirement for step ${get_signature_requirement_flow_step_id(req)} (ID: ${get_signature_requirement_id(req)})`);
|
|
req_count += 1;
|
|
}
|
|
print(`Total signature requirements: ${req_count}`);
|
|
|
|
// Clean up - delete the flow
|
|
db::delete_flow(get_flow_id(completed_flow));
|
|
print(`Deleted flow with ID: ${get_flow_id(completed_flow)}`);
|
|
|
|
// Clean up - delete signature requirements
|
|
db::delete_signature_requirement(get_signature_requirement_id(saved_req1));
|
|
db::delete_signature_requirement(get_signature_requirement_id(saved_req2));
|
|
db::delete_signature_requirement(get_signature_requirement_id(saved_req3));
|
|
print("Deleted all signature requirements");
|