# Job-Info-Prod

Production-ready job information management system for field workers.

## Features

- **Dual-Token Authentication**: PocketBase + Microsoft Graph tokens
- **Always-Warm Cache**: Background refresh loop keeps tokens fresh
- **Offline-First**: Works without internet using cached data
- **Zero Re-Auth**: Users never need to login again after initial auth

## Tech Stack

- **Runtime**: Bun
- **Backend**: Hono
- **Auth**: PocketBase + Microsoft OAuth
- **Cache**: Redis/Valkey via ioredis
- **Frontend**: TypeScript + Tailwind CSS

## Quick Start

```bash
# Install dependencies
bun install

# Copy environment file
cp .env.example .env

# Edit .env with your configuration
nano .env

# Start development server
bun run dev
```

## Environment Variables

See `.env.example` for all required variables:

- `PORT` - Server port (default: 3000)
- `POCKETBASE_URL` - PocketBase instance URL
- `MS_CLIENT_ID` - Microsoft OAuth client ID
- `MS_CLIENT_SECRET` - Microsoft OAuth client secret
- `MS_TENANT_ID` - Microsoft tenant ID
- `REDIS_HOST` - Redis/Valkey host
- `REDIS_PORT` - Redis/Valkey port

## Architecture

```
┌─────────────────┐     ┌──────────────────┐
│   Frontend      │────▶│   Hono Backend   │
│  (TypeScript)   │◀────│                  │
└─────────────────┘     └────────┬─────────┘
                                 │
        ┌────────────────────────┼────────────────────────┐
        ▼                        ▼                        ▼
┌───────────────┐      ┌─────────────────┐      ┌─────────────────┐
│   PocketBase  │      │  Redis/Valkey   │      │ Microsoft Graph │
│   (Auth+Data) │      │  (Token Cache)  │      │   (SharePoint)  │
└───────────────┘      └─────────────────┘      └─────────────────┘
```

## Token Refresh Flow

1. User completes OAuth flow → tokens stored in localStorage + cache
2. Backend starts 30-min refresh loop per user
3. Frontend checks backend for fresh tokens every 25 min
4. If backend unreachable, frontend uses localStorage cache
5. Stale tokens returned as fallback (field workers can't wait)

## Scripts

- `bun run dev` - Start development server with watch
- `bun run start` - Start production server
- `bun run build:css` - Build Tailwind CSS
- `bun run typecheck` - Run TypeScript type checking
- `bun run test` - Run tests

## Folder Structure

```
Job-Info-Prod/
├── backend/
│   └── src/
│       ├── config/       # Environment and constants
│       ├── middleware/   # Auth, logging, error handling
│       ├── routes/       # API route handlers
│       ├── services/     # Business logic
│       └── index.ts      # Server entry point
├── frontend/
│   ├── public/           # Static HTML files
│   └── src/
│       ├── auth/         # Auth client
│       ├── components/   # UI components
│       ├── services/     # API clients
│       ├── styles/       # Tailwind CSS
│       └── app.ts        # Main app entry
└── shared/
    ├── constants/        # Shared constants
    └── types/            # TypeScript types
```

## License

MIT
