Gelana
A multi-branch retail and inventory management platform featuring an immutable double-entry stock movement ledger, automated Weighted Average Cost (WAC) tracking, deadlock-free POS checkout, and shift cash reconciliation.
Lead Systems Architect & Full-Stack Engineer
3 Months (Production Architecture & Deployment)
React 19 / Express 5 / Drizzle ORM / PostgreSQL / Neon
Zero Concurrency Deadlocks / 29 Composite B-Tree Indexes

Valuation Drift, Transfer Shrinkage, and POS Deadlocks
Multi-branch retail systems often mutate stock counters directly without an audit trail, leaving inventory counts and financial margins out of sync during supplier price changes, inter-branch transfers, and peak-hour checkouts.
Restocking at changing supplier prices without recalculating Weighted Average Cost (WAC) corrupts gross profit reporting.
Direct branch-to-branch updates hide goods lost or damaged in transit instead of isolating discrepancies.
Simultaneous checkouts locking inventory rows in arbitrary cart order trigger PostgreSQL AB/BA transaction aborts.
Modular Monolith with an Immutable Stock Ledger
Structured across 19 domain modules using strict PostgreSQL ACID transactions via Drizzle ORM. Stock events append to an immutable double-entry ledger, and Vercel Edge Rewrites proxy API calls to keep HttpOnly session cookies first-party.
Role-scoped owner and cashier views, split-tender checkout, Ctrl+K command palette, and SVG trend charts.
Same-origin API routing for HttpOnly cookies with per-request user status verification for instant revocation.
Append-only inventory_movements ledger, row-locked balance updates, automated WAC recalculation, and non-negative constraints.
Sargable range queries, indexed foreign keys, and integer cent-precision math to avoid floating-point drift.
Key Challenges & Trade-offs
Eliminating AB/BA Deadlocks Under Concurrent Checkouts
Concurrent POS carts locking inventory rows in arbitrary item order caused cyclic PostgreSQL lock waits and aborted checkouts.
Deduplicated and lexicographically sorted product IDs in SalesService.createSale before acquiring SELECT ... FOR UPDATE locks, enforcing a single global lock order.
Inspect snippet · apps/api/src/modules/sales/service.ts
// Deduplicate and lexicographically sort product IDs to guarantee total lock acquisition order
const sortedProductIds = Array.from(new Set(input.items.map((i) => i.productId))).sort();
const balanceMap = new Map<string, typeof inventoryBalances.$inferSelect>();
for (const pId of sortedProductIds) {
const [lockedBalance] = await tx
.select()
.from(inventoryBalances)
.where(and(
eq(inventoryBalances.branchId, input.branchId),
eq(inventoryBalances.productId, pId)
))
.for('update');
if (!lockedBalance) {
throw new AppError(400, `Product ${pId} balance record not found for branch`);
}
balanceMap.set(pId, lockedBalance);
}Cent-Exact Proportional Discount Apportionment
Splitting a sale-wide discount across multiple line items introduced fractional cent rounding errors between line totals and the grand total.
Tracked cumulative rounded discounts across line items and assigned any residual cent remainder to the highest-net item so journals always balance.
Inspect snippet · apps/api/src/modules/sales/service.ts
let allocatedSum = 0;
let highestIdx = 0;
let maxNet = -1;
for (let i = 0; i < lineTotals.length; i++) {
const proportion = lineTotals[i] / subtotal;
const allocated = round2(saleDiscount * proportion);
itemApportionedSaleDiscounts[i] = allocated;
allocatedSum = round2(allocatedSum + allocated);
if (lineTotals[i] > maxNet) {
maxNet = lineTotals[i];
highestIdx = i;
}
}
// Allocate remaining cent drift to highest-net line item
const remainder = round2(saleDiscount - allocatedSum);
if (Math.abs(remainder) > 0.0001) {
itemApportionedSaleDiscounts[highestIdx] = round2(
(itemApportionedSaleDiscounts[highestIdx] ?? 0) + remainder
);
}Preventing Return Refund Arbitrage on Discounted Sales
Naive return flows allowed items bought under a sale discount to be refunded at full catalog price.
Computed per-unit refund ceilings in ReturnsService net of both line-level and apportioned sale discounts.
Inspect snippet · apps/api/src/modules/returns/service.ts
// Compute maximum refund ceiling net of historical line and apportioned sale discounts
const netLineTotal = round2(saleItem.lineTotal - saleItem.apportionedSaleDiscount);
const unitRefundRate = round2(netLineTotal / saleItem.enteredQuantity);
const maxEligibleRefund = round2(returnedQuantity * unitRefundRate);
if (requestedRefund > maxEligibleRefund) {
throw new AppError(
400,
`Requested refund (${requestedRefund}) exceeds maximum eligible refund (${maxEligibleRefund}) net of historical discounts.`
);
}Auditable Multi-Branch Retail Operations in Production
Deployed across Vercel and Render with Neon PostgreSQL, Gelana keeps stock movements, shift cash closings, and valuations consistent under concurrent POS load, backed by 21 frontend integration test suites.