# DeployHub

Internal build distribution platform (Flybuilds / Diawi replacement). Upload APK, AAB, or IPA builds; apps are created automatically by package name or bundle ID. Binaries are stored in the user's Google Drive; install links and QR codes point to your domain.

## Architecture

- **Frontend:** Next.js 14 (React) + Tailwind CSS
- **Backend:** Node.js + NestJS (REST API)
- **Auth:** Google OAuth 2.0, JWT for API
- **Storage:** User's Google Drive (`drive.file` scope only)
- **Database:** PostgreSQL (metadata only)

## Prerequisites

- Node.js 18+
- PostgreSQL 13+
- Google Cloud project with OAuth 2.0 credentials and Drive API enabled

## Setup

### 1. Google Cloud

1. Create a project at [Google Cloud Console](https://console.cloud.google.com/).
2. Enable **Google Drive API**.
3. Create **OAuth 2.0 Client ID** (Web application):
   - Authorized redirect URI: `http://localhost:4000/api/auth/google/callback` (or your backend URL + `/api/auth/google/callback`).
4. Note **Client ID** and **Client Secret**.

### 2. Database

Create a database and run migrations:

```bash
cd backend
npm install
# Set DB_* in .env, then:
npx typeorm migration:run -d src/data-source.ts
```

If the TypeORM CLI fails, create tables manually (see `backend/src/migrations/` for SQL). Ensure `gen_random_uuid()` is available (PostgreSQL 13+).

### 3. Backend

```bash
cd backend
cp .env.example .env
# Edit .env: DB_*, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_CALLBACK_URL, JWT_SECRET, FRONTEND_URL, PUBLIC_BASE_URL
npm install
npm run start:dev
```

Runs at `http://localhost:4000`.

### 4. Frontend

```bash
cd frontend
# Create .env.local with NEXT_PUBLIC_API_URL=http://localhost:4000 (and NEXT_PUBLIC_APP_URL if different)
npm install
npm run dev
```

Runs at `http://localhost:3000`.

## Usage

1. Sign in with Google (grants Drive access for uploads).
2. **Upload build:** use the global "Upload build" button; choose `.apk`, `.aab`, or `.ipa`.
3. Apps are created/grouped automatically by **package name** (Android) or **bundle identifier** (iOS).
4. Each build gets an install URL: `https://yourdomain.com/download?build_id=<uuid>`.
5. Use **Install** or **QR** on a build to share the link; the download page detects platform and shows "Install APK" or "Tap to Install" (OTA for iOS).

## API Overview

| Endpoint | Auth | Description |
|----------|------|-------------|
| `GET /api/auth/google` | No | Redirect to Google OAuth |
| `GET /api/auth/google/callback` | No | OAuth callback, redirects to frontend with `?token=JWT` |
| `GET /api/users/me` | JWT | Current user profile |
| `GET /api/apps` | JWT | List apps and builds for the user |
| `POST /api/builds/upload` | JWT | Multipart upload (file: APK/AAB/IPA) |
| `GET /api/install/info?build_id=` | No | Install info (platform, installUrl, app name, version) |
| `GET /api/install/redirect?build_id=` | No | 302 redirect to Drive or itms-services URL |

## Security & Compliance

- **HTTPS only** in production.
- **drive.file** scope only: access only to files the app creates.
- Backend never exposes raw Drive credentials; install links use public "anyone with link" URLs stored in DB.
- OAuth refresh tokens are stored and used to refresh access tokens when uploading to Drive.
- Internal distribution only (no TestFlight / Play Store); Ad-Hoc or Enterprise provisioning required for iOS.

## Limitations

- **One user = one workspace** (single organization).
- **No manual app creation:** apps exist only after at least one build is uploaded.
- **AAB metadata:** Android App Bundle (`.aab`) metadata extraction is best-effort (manifest inside AAB is protobuf); for full metadata, prefer APK or use bundletool externally.
- **iOS:** OTA install requires a signed IPA with Ad-Hoc or Enterprise provisioning; the install link uses a dynamically generated `manifest.plist` hosted on the user's Drive.
- If the user deletes the file from Google Drive, the install link will fail (by design).

## Project structure

```
DeployHub/
├── backend/          # NestJS API
│   ├── src/
│   │   ├── entities/       # User, App, Build
│   │   ├── migrations/
│   │   └── modules/        # auth, users, apps, builds, drive, install
│   └── .env.example
├── frontend/         # Next.js app
│   └── src/
│       ├── app/            # pages (/, /download)
│       ├── components/     # Login, Dashboard, UploadButton, AppCard
│       └── lib/            # api, auth helpers
└── README.md
```

## License

MIT (or your choice).
