All academies
All projects

Inventory System

medium

Build an inventory management tool with products, stock movements, low-stock alerts, and CSV import. You will practice modeling an audit trail with union types for movement kinds, template literal types for SKUs, and validating untrusted CSV rows into well-typed records.

Next.jsTypeScriptTailwind CSSPrismaSQLite

Architecture

  • UI layer: product table with sorting and filtering, a stock-movement form, and an alerts panel
  • State layer: server components for reads plus a small client store only for in-flight form state
  • Domain layer: stock math as pure functions — applying a movement to a quantity, detecting low stock against thresholds
  • Service layer: an import pipeline that parses CSV rows as unknown, validates each field, and reports typed row errors
  • Persistence layer: Prisma models for products and movements, with repository functions returning domain types not Prisma types
  • Type layer: Product, StockMovement, and ImportResult types plus a Sku template literal type

Folder structure

src/
  app/
    page.tsx
    products/
      [id]/
        page.tsx
    import/
      page.tsx
    api/
      movements/
        route.ts
  components/
    ProductTable.tsx
    MovementForm.tsx
    AlertsPanel.tsx
    ImportReport.tsx
  lib/
    stock.ts
    csvImport.ts
    repository.ts
  types/
    inventory.ts

Domain type definitions

Start every project by defining its types — they become the contract for every component, service, and API call that follows.

export type Sku = string; // shaped like "WH-0001": prefix, dash, digits

export interface Product {
  id: string;
  sku: Sku;
  name: string;
  quantity: number;
  lowStockThreshold: number;
  unitPriceCents: number;
}

export type StockMovement =
  | { kind: "purchase"; productId: string; quantity: number; supplier: string; at: string }
  | { kind: "sale"; productId: string; quantity: number; orderId: string; at: string }
  | { kind: "adjustment"; productId: string; quantityDelta: number; reason: string; at: string };

export interface ImportRowError {
  row: number;
  field: string;
  message: string;
}

export interface ImportResult {
  imported: Product[];
  errors: ImportRowError[];
}

Build steps

  1. 1Define the domain types — Product, the StockMovement union with its three movement kinds, and the import result types.
  2. 2Write stock.ts: applyMovement computes the new quantity for any movement kind via an exhaustive switch, and isLowStock compares against the threshold.
  3. 3Set up Prisma models and repository.ts, mapping Prisma rows to domain types in one place.
  4. 4Build ProductTable as a server component with sortable columns and a low-stock filter.
  5. 5Build MovementForm — the fields change with the selected kind, mirroring the discriminated union.
  6. 6Implement the movements API route: validate the body into a StockMovement, apply it in a transaction, return the updated Product.
  7. 7Write csvImport.ts: parse rows as unknown, validate every field with narrowing, and collect ImportRowError entries instead of throwing.
  8. 8Build the import page with a file input and ImportReport showing imported counts and per-row errors.
  9. 9Build AlertsPanel listing low-stock products, sorted by how far below threshold they are.