How dawoodmehmood/psxworth works
Purpose and runtime
PsxWorth is a Next.js 16 / React 19 App Router application for Pakistan Stock Exchange investors. It manages portfolios and transactions, calculates performance and allocation, surfaces payouts/corporate actions, and uses AI to parse broker or CDC transaction statements. The browser experience is responsive and installable as a PWA.
Entry routes are rooted in src/app/page.tsx, with portfolio workspace routes at src/app/portfolio/page.tsx and src/app/portfolio/[portfolioId]/page.tsx. Global composition and error handling live in src/app/layout.tsx and src/app/global-error.tsx.
Application boundary and access
The App Router owns page rendering, route layouts, loading/error states, metadata, sitemap, and the PWA manifest. Request-level protection or routing policy is likely centralized at src/proxy.ts. Authentication is provided by Clerk; app code should treat Clerk identity as the boundary between anonymous public pages and user-owned portfolio data.
Relevant paths: src/app/layout.tsx, src/proxy.ts, src/app/manifest.ts.
Portfolio workspace
The portfolio route is the primary authenticated product surface. It composes portfolio selection, creation, transaction entry/import, transaction tables, allocation charts, and performance views around a selected portfolioId. The last active portfolio is persisted through a cookie action, allowing navigation to restore context.
Relevant paths: src/app/portfolio/[portfolioId]/page.tsx, src/app/portfolio/[portfolioId]/components/PortfoliosSidebar/index.tsx, src/actions/cookies/lastActivePortfolio.ts.
Server-side domain operations
src/actions is the core server-side domain boundary. Portfolio and transaction actions mutate user records; stock, ETF, payout, and performance actions read market data and derive investor-facing views. Action middleware/errors provide a shared enforcement and failure-handling layer.
Primary flow: UI or React Query hook → action → database/query or market-data lookup → normalized domain result → UI invalidation/render.
Relevant paths: src/actions/portfolio/portfolioActions.ts, src/actions/transaction/TransactionFunctions.ts, src/actions/portfolioPerformance/portfolioPerformance.ts.
Dual database model
Drizzle ORM connects to two PostgreSQL domains: a user database for portfolios/transactions and a data database for public PSX market data. Their schemas are intentionally separated, as are migrations. This is a material boundary: user writes must not couple to market-data snapshot management.
Relevant paths: src/db/index.ts, src/db/userdb-schema.ts, src/db/datadb-schema.ts, migrations-userdb, migrations.
Market data, analytics, and entitlements
Portfolio calculations combine user transactions with prices, stock metadata, ETF constituents, and payout/corporate-action records. Historical returns is a self-contained feature with query/action/chart layers. Allocation and performance UI consume these derived outputs rather than directly owning persistence logic.
Relevant paths: src/features/historicalReturns/actions/historicalReturnsActions.ts, src/features/stockPrices/queries.ts, src/actions/payouts/index.ts.
AI import and client data flow
AI-assisted import is encapsulated in src/lib/ai: client integration, prompts, schemas, and parsing helpers should constrain untrusted statement input into validated transaction candidates before user review and transaction persistence. TanStack Query provides client fetching/cache coordination via the root provider; forms and tables are client interaction boundaries.
Relevant paths: src/lib/ai/aiClient.ts, src/lib/ai/schema.ts, src/components/QueryProvider/index.tsx.
Operations and external services
External services include Clerk authentication, PostHog product analytics, PostgreSQL, and an AI provider behind the AI client. Client/server instrumentation and PostHog integration are explicit. The app ships as a standalone Dockerized Next deployment; GitHub workflows cover verification, deployment, and market-data snapshot synchronization.
Relevant paths: instrumentation.js, src/app/posthog-server.js, Dockerfile, .github/workflows/sync-data-snapshot.yml.
Main entry points: page.tsx Portfolio workspace · index.ts Drizzle data access · aiClient.ts AI statement parser · layout.tsx App Router
Components 24
Next.js application
- App Router · Renders pages, layouts, metadata, and PWA surfaces.
src/app/layout.tsx - Request access policy · Applies routing and request-level protection.
src/proxy.ts - PWA manifest
src/app/manifest.ts - Portfolio workspace · Primary selected-portfolio product surface.
src/app/portfolio/[portfolioId]/page.tsx - Portfolio selector
src/app/portfolio/[portfolioId]/components/PortfoliosSidebar/index.tsx - Client query cache · Coordinates client fetching, cache, and invalidation.
src/components/QueryProvider/index.tsx
Domain services
- Portfolio actions · Mutates user-owned portfolio records.
src/actions/portfolio/portfolioActions.ts - Transaction actions · Persists reviewed transaction entries and imports.
src/actions/transaction/TransactionFunctions.ts - Performance calculations · Derives investor-facing portfolio performance.
src/actions/portfolioPerformance/portfolioPerformance.ts - Historical returns · Provides historical-return query and chart data.
src/features/historicalReturns/actions/historicalReturnsActions.ts - Payouts and actions
src/actions/payouts/index.ts - Last active portfolio · Restores portfolio context on navigation.
src/actions/cookies/lastActivePortfolio.ts - AI statement parser · Converts untrusted statements into validated transaction candidates.
src/lib/ai/aiClient.ts - Import validation schema
src/lib/ai/schema.ts
Data and integrations
- Drizzle data access · Separates user and market-data PostgreSQL access.
src/db/index.ts - User PostgreSQL domain · Owns portfolios and transactions.
src/db/userdb-schema.ts - Market-data PostgreSQL domain · Owns public prices, metadata, ETFs, and payouts.
src/db/datadb-schema.ts - Market price queries
src/features/stockPrices/queries.ts - Clerk · Identity boundary for user-owned data.
- AI provider
Delivery and observability
- PostHog instrumentation
src/app/posthog-server.js - Runtime instrumentation
instrumentation.js - Standalone deployment
Dockerfile - Market snapshot sync · Synchronizes market-data snapshots.
.github/workflows/sync-data-snapshot.yml
Connections 27
- App Router → Request access policy: requests pass through
- Request access policy → Clerk: authenticates
- App Router → PWA manifest: publishes
- App Router → Portfolio workspace: renders
- Portfolio workspace → Portfolio selector: composes
- Portfolio workspace → Client query cache: uses cache
- Portfolio selector → Last active portfolio: persists selection
- Client query cache → Portfolio actions: calls and invalidates
- Client query cache → Transaction actions: calls and invalidates
- Portfolio workspace → Performance calculations: loads views
- Portfolio workspace → Historical returns: loads returns
- Portfolio workspace → Payouts and actions: loads payouts
- Portfolio workspace → AI statement parser: imports statements
- AI statement parser → Import validation schema: validates candidates
- AI statement parser → AI provider: parses statement
- AI statement parser → Transaction actions: submits reviewed entries
- Portfolio actions → Drizzle data access: reads and writes
- Transaction actions → Drizzle data access: writes transactions
- Performance calculations → Drizzle data access: combines domains
- Historical returns → Market price queries: uses prices
- Payouts and actions → Drizzle data access: reads actions
- Market price queries → Drizzle data access: reads market data
- Drizzle data access → User PostgreSQL domain: user connection
- Drizzle data access → Market-data PostgreSQL domain: market-data connection
- Market snapshot sync → Market-data PostgreSQL domain: synchronizes snapshots
- Runtime instrumentation → PostHog instrumentation: reports telemetry
- Standalone deployment → App Router: deploys