From Manual Spreadsheets to GenAI: Architecting a B2G Logistics ERP with React and Gemini
- #PostgreSQL
- #TypeScript
- #React
- #Supabase
Business-to-Government (B2G) logistics is a complex, high-stakes environment. In Brazil, managing government contracts (known as Atas de Registro de Preços) and purchase orders (Notas de Empenho) involves parsing documents with dozens of pages, hundreds of items, and meticulously tracking partial deliveries over long periods.
Recently, I was tasked with solving this exact problem for a major logistics distributor in the healthcare sector. Their entire operation relied on disconnected, manual spreadsheets, leading to zero real-time visibility, typing errors, and communication gaps between the sales and purchasing departments. The solution was to build NEXUS: a modern, AI-powered ERP designed from the ground up to automate data entry and enforce strict business logic.
Here is a technical deep dive into how I architected this system, the challenges faced, and the solutions implemented.
The Architecture & Tech Stack
To ensure a fast, scalable, and maintainable application, I chose a modern hybrid stack:
- Frontend: React 18, Vite, and TypeScript for robust developer experience and type safety.
- UI/UX: Shadcn UI and Lucide Icons. As a Design Engineer, bringing my background in graphic design and prototyping into the development process drastically accelerated the frontend delivery. It allowed me to build an intuitive, low-friction interface tailored for operational users without needing constant hand-holding from a dedicated design team.
- Backend & Auth: Supabase (PostgreSQL, Auth, Storage, and Deno Edge Functions).
- Deployment: Vercel for automated CI/CD directly from the repository.
The "Brain": AI-Powered Data Extraction
The biggest bottleneck was data entry. A single government contract can span 29 pages and contain 174 distinct items, often with multiple competing companies listed on the same page. Typing this manually introduced severe errors.
I engineered a hybrid extraction pipeline using Generative AI:
- Client-Side Text Extraction: Using pdfjs-dist, the frontend reads the PDF directly in the browser. The text is chunked into logical blocks to respect LLM token limits.
- Semantic Interpretation via Edge Functions: Each text block is sent to a Supabase Edge Function that calls the Google Gemini API.
Overcoming Prompt Engineering Challenges:
- Vendor Isolation: The prompt was strictly tuned to halt extraction immediately if the AI detected a competitor's name, ensuring only our client's items were parsed into the JSON.
- Context Continuity: Because a vendor's header might be on page 3 but their items continue until page 12, I injected a "Global Context" into every subsequent chunk sent to the API, preventing data orphanhood.
Engineering Complex Business Logic
Extracting data is only half the battle; managing the lifecycle of an order requires deep business logic.
I developed an automated Auto-Match Engine. When a user uploads a delivery invoice (NF-e), the system parses the invoice items and automatically matches them with the original purchase order items using similarity algorithms and unit price validations. The balance is then dynamically decremented in PostgreSQL via RPC functions.
To handle real-world chaos, the system accounts for:
- The Box Factor (Fator Caixa): When items are delivered in sealed boxes (e.g., 100 units per box) that don't perfectly align with the requested quantity, the system automatically detects this pattern and marks the item as fulfilled, preventing residual "ghost" balances.
- Over-delivery Alerts (Sobrebaixa): If an invoice attempts to decrement more balance than available, the UI triggers a visual anomaly alert.
Zero-Trust Security with Row Level Security (RLS)
As the user base grew, data isolation became critical. I implemented a strict 4-tier Role-Based Access Control (RBAC) system:
Role
Access Level
DEV / SUP
Full access and technical administration capabilities.
ADM
Sector management and workload distribution.
OP (Operator)
Strictly isolated. Can only view records they created or were explicitly assigned to them.
Security wasn't handled in the frontend UI. Instead, I wrote over 40+ Row Level Security (RLS) policies directly in PostgreSQL. Even if a malicious user bypassed the client-side code, the database would return zero unauthorized records.
The Impact & Conclusion
To bridge the final communication gap, I built an integrated Purchasing Module. Instead of unrecorded phone calls between sales and purchasing, users can now request items directly within the app, triggering a tracked SLA workflow.
Crucially, this automated pipeline resulted in an 80% reduction in the time from order download to the start of the physical picking process, while simultaneously decreasing shipping errors by 60%—with a clear roadmap to reach a 100% error-free rate within the next 6 months.
Today, the project stands as a robust ERP with over 200 commits, seamless Vercel/Supabase deployments without dedicated infrastructure, and an architecture that saves countless hours of manual labor.




Building NEXUS reinforced a core engineering philosophy: the best developers don't just write code; they understand the business problem deeply and leverage technology—like GenAI and modern frameworks—to architect structural solutions.
About the Author
I am a product-minded Software Engineer with a strong background in UI/UX graphic design. I specialize in bridging the gap between visually intuitive interfaces and complex business logic, open to challenges in global tech teams. Let's connect!



