Edge Functions
Relevant source files
The following files were used as context for generating this wiki page:
Edge Functions in the DRX CRM ecosystem serve as the server-side compute layer, primarily utilized for tasks that require elevated privileges, complex business logic, or AI-powered processing. These functions reside within the Supabase infrastructure and handle critical operations such as credit risk assessment, cashflow analysis, and AI-driven business insights.
The architecture leverages these functions to maintain a "privacy-first" approach by processing sensitive data within a secure environment before returning results to the frontend. They are integral to the system's Advanced Features, including AI intelligence for pricing recommendations and inventory forecasting.
Sources: README.md:12-25, testsprite_tests/standard_prd.json:8-15
AI and Intelligence Functions
The system utilizes specialized functions to provide "AI Intelligence" for business decision-making. These functions process historical data to generate recommendations for product pricing and inventory management.
AI Insights and Credit Risk
The AI engine evaluates customer creditworthiness by analyzing several data points:
- Purchase History: 30-day, 90-day, and all-time trends.
- Payment Behavior: On-time payment ratios and current utilization.
- Inventory Metrics: Coverage ratios to determine risk levels.
The following diagram illustrates the data flow for an AI-powered credit decision:
The logic for these decisions is captured in the ai_credit_decisions audit trail for compliance and transparency.
Sources: README.md:104-109, app/(app)/admin/page.tsx:43-52, testsprite_tests/standard_prd.json:15-20
Financial and Cashflow Processing
Edge Functions handle the deterministic rules required for financial accuracy and cashflow monitoring. This includes calculating total sales, average sale values, and outstanding credit across the platform.
Metrics Calculation Logic
The system processes sales data to generate real-time metrics for dashboards. This logic is strictly validated to ensure no unlogged financial changes occur.
| Metric | Calculation Logic | Derived From |
|---|---|---|
| Total Sales | sum(total_amount) | sales table |
| Average Sale | total_sales / count(sales) | sales table |
| Credit Outstanding | sum(current_balance) | credit_limits table |
| Cost Per Unit | total_cost / quantity_received | inventory_intakes table |
Sources: tests/event-desk-data-processing.unit.test.ts:26-55, tests/data-processing-logic.test.ts:105-115, testsprite_tests/standard_prd.json:28-35
Inventory and Intake Automation
A core component of the backend logic is the Inventory Intake workflow. This system automates the calculation of costs and requires administrative approval for significant deviations in pricing.
Cost Deviation and Approvals
Functions monitor the "Cost per Gram" or "Cost per Ounce" during intake. If a new intake's cost deviates significantly (e.g., >50%) from the historical average, the system triggers a requirement for Manager approval.
Key Intake Functions
calculateCostPerGram: Determines base cost for weight-based products.get_avg_cost_per_gram: Retrieves historical pricing for deviation detection.is_manager: Validates permissions for approving intakes.
Sources: test-inventory-intake.sh:60-115, tests/data-processing-logic.test.ts:74-95, README.md:98-102
Administrative and System Management
The system includes administrative functions to manage the health of the application, visible through the Admin Console. These functions monitor system-wide statistics and failures.
System Auditing
The system_audit_log and ai_insights_failures tables are populated by edge functions to track:
- Tenant Isolation: Ensuring data from one business profile is never accessible to another.
- AI Failures: Logging instances where the AI engine fails to generate insights for troubleshooting.
- RBAC Enforcement: Server-side validation of user roles (Super Admin, Manager, Supervisor, Cashier).
Sources: app/(app)/admin/page.tsx:43-52, testsprite_tests/standard_prd.json:30-45, CHANGELOG.md:85-95
Conclusion
Edge Functions provide the computational backbone for DRX CRM, enabling complex AI-driven credit decisions, strictly controlled inventory intake workflows, and robust financial reporting. By isolating these processes in the backend, the system ensures high data integrity, adheres to role-based access controls, and maintains the privacy-first standards required for modern retail operations.