argus-sdk-demo
Version:
Simple demo application for Argus SDK
198 lines (176 loc) • 6.41 kB
text/typescript
import Argus from 'argus-sdk';
import * as dotenv from 'dotenv';
// Load environment variables
dotenv.config();
// Preset transactions for demo - Realistic B2B finance scenarios
const transactions = [
{
name: "AWS Monthly Services",
amount: "3847.23",
currency: "USD",
payment_method: "card",
recipient_name: "Amazon Web Services",
merchant_category_code: "5734", // Computer Software Stores
metadata: { category: "cloud_infrastructure", recurring: true, vendor_id: "aws_prod_01" }
},
{
name: "Office Supplies Vendor",
amount: "892.47",
currency: "USD",
payment_method: "ach",
recipient_name: "Staples Business Advantage",
merchant_category_code: "5943", // Office Equipment/Supplies
metadata: { category: "office_supplies", po_number: "PO-2024-0187" }
},
{
name: "Software License Renewal",
amount: "4999.00",
currency: "USD",
payment_method: "card",
recipient_name: "Salesforce Inc",
merchant_category_code: "5734", // Computer Software Stores
metadata: { category: "software_licenses", license_type: "enterprise", seats: 50 }
},
{
name: "Suspicious Wire Transfer",
amount: "45000.00",
currency: "USD",
payment_method: "wire",
recipient_name: "Offshore Holdings LLC",
merchant_category_code: "6051", // Cryptocurrency/Digital Currency
recipient_type: "business",
metadata: { category: "investment", first_time_vendor: true, country: "Cayman Islands" }
},
{
name: "International Contractor Payment",
amount: "8500.00",
currency: "EUR",
payment_method: "wire",
recipient_name: "TechConsult GmbH",
recipient_type: "business",
metadata: { category: "professional_services", country: "Germany", tax_form: "W-8BEN-E" }
}
];
// Parse command line arguments
function parseArgs() {
const args = process.argv.slice(2);
const options: { apiKey?: string; baseURL?: string; iterations?: number } = {};
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case '--api-key':
case '-k':
options.apiKey = args[++i];
break;
case '--base-url':
case '-u':
options.baseURL = args[++i];
break;
case '--iterations':
case '-i':
options.iterations = parseInt(args[++i], 10);
break;
}
}
return options;
}
// Format decision with emoji
function formatDecision(decision: string): string {
switch (decision.toUpperCase()) {
case 'APPROVED':
return '✅ APPROVED';
case 'REJECTED':
return '❌ REJECTED';
case 'MANUAL_REVIEW':
return '⚠️ MANUAL REVIEW';
default:
return `❓ ${decision}`;
}
}
// Main demo function
async function runDemo() {
const options = parseArgs();
const apiKey = options.apiKey || process.env.ARGUS_API_KEY;
const baseURL = options.baseURL || process.env.ARGUS_BASE_URL || 'http://localhost:8000';
const iterations = options.iterations || 1;
if (!apiKey) {
console.error('❌ Error: API key is required');
console.error(' Set ARGUS_API_KEY environment variable or use --api-key flag');
process.exit(1);
}
console.log('🚀 Starting Argus SDK Demo - B2B Finance Operations');
console.log(`📍 API URL: ${baseURL}`);
console.log(`🔄 Running ${transactions.length} business transactions${iterations > 1 ? ` (${iterations} iterations)` : ''}...\n`);
// Initialize client
const client = new Argus({
apiKey,
baseURL,
});
// Track results
const results = { approved: 0, rejected: 0, manual_review: 0, total: 0 };
const timings: number[] = [];
try {
// Run iterations
for (let iter = 0; iter < iterations; iter++) {
if (iterations > 1) {
console.log(`\n🔄 Iteration ${iter + 1}/${iterations}`);
}
// Process each transaction
for (let i = 0; i < transactions.length; i++) {
const tx = transactions[i];
const startTime = Date.now();
try {
const response = await client.evaluate.transaction({
amount: tx.amount,
currency: tx.currency,
payment_method: tx.payment_method,
recipient_name: tx.recipient_name,
recipient_type: tx.recipient_type,
merchant_category_code: tx.merchant_category_code,
metadata: tx.metadata,
});
const duration = Date.now() - startTime;
timings.push(duration);
results.total++;
// Update counters
switch (response.decision.toUpperCase()) {
case 'APPROVED':
results.approved++;
break;
case 'REJECTED':
results.rejected++;
break;
case 'MANUAL_REVIEW':
results.manual_review++;
break;
}
// Display result
console.log(`${formatDecision(response.decision)} Transaction ${i + 1}: ${tx.name} (${tx.currency} ${tx.amount})`);
console.log(` Decision ID: ${response.decision_id}`);
if (response.failed_rule) {
console.log(` Failed Rule: ${response.failed_rule}`);
}
console.log(` Time: ${duration}ms\n`);
} catch (error) {
console.error(`❌ Error processing ${tx.name}:`, error instanceof Error ? error.message : error);
results.total++;
}
}
}
// Display summary
const avgTime = timings.length > 0 ? Math.round(timings.reduce((a, b) => a + b, 0) / timings.length) : 0;
const approvalRate = results.total > 0 ? Math.round((results.approved / results.total) * 100) : 0;
console.log('\n📊 Summary:');
console.log(` Total: ${results.total} transactions`);
console.log(` Approved: ${results.approved} (${approvalRate}%)`);
console.log(` Rejected: ${results.rejected} (${Math.round((results.rejected / results.total) * 100)}%)`);
console.log(` Manual Review: ${results.manual_review} (${Math.round((results.manual_review / results.total) * 100)}%)`);
if (timings.length > 0) {
console.log(` Avg Response Time: ${avgTime}ms`);
}
} catch (error) {
console.error('\n❌ Fatal error:', error instanceof Error ? error.message : error);
process.exit(1);
}
}
// Run the demo
runDemo().catch(console.error);