# 🚀 Panduan Migrasi ke Hosting (Online)

Aplikasi ini jalan lokal pakai **SQLite**. Untuk online (bisa diakses dari mana
saja), ada 2 jalur utama. Pilih salah satu sesuai kebutuhan.

| Jalur | Cocok untuk | Database | Foto bukti kurir |
|---|---|---|---|
| **A. Vercel + Postgres** (paling mudah) | Ingin cepat, gratis untuk mulai | WAJIB PostgreSQL (SQLite tidak jalan di Vercel) | WAJIB pindah ke S3/cloud storage |
| **B. VPS sendiri** (kontrol penuh) | Ingin murah jangka panjang, tetap boleh SQLite | SQLite atau PostgreSQL | Boleh tetap folder lokal |

> ⚠️ **PENTING sebelum online (berlaku untuk semua jalur):**
> 1. Ganti `JWT_SECRET` di `.env` dengan string acak panjang. Buat dengan:
>    `openssl rand -base64 48` (atau situs pembuat password acak).
> 2. Set `NODE_ENV="production"`.
> 3. **Foto bukti pengiriman** disimpan di folder lokal `/public/uploads`. Di
>    hosting serverless (Vercel) folder ini **hilang tiap deploy** — wajib pindah
>    ke S3/MinIO/Cloudinary (lihat bagian 4). Di VPS boleh tetap folder lokal.

---

## JALUR A — Vercel + PostgreSQL (Rekomendasi untuk pemula)

Vercel = tempat hosting yang paling ramah untuk Next.js. Karena Vercel tidak
menyimpan file secara permanen, **database wajib PostgreSQL** (cloud).

### Langkah 1 — Siapkan database PostgreSQL gratis
Pakai salah satu penyedia (semua punya paket gratis):
- **Neon** — <https://neon.tech>
- **Supabase** — <https://supabase.com>
- **Vercel Postgres** — dari dashboard Vercel langsung.

Setelah buat database, salin **Connection String**-nya, bentuknya seperti:
```
postgresql://user:password@host:5432/namadb?sslmode=require
```

### Langkah 2 — Ubah aplikasi agar pakai PostgreSQL
Ikuti **[03-PANDUAN-DATABASE.md](03-PANDUAN-DATABASE.md)** bagian "SQLite →
PostgreSQL" (ganti `provider` di `prisma/schema.prisma` + buat migrasi baru).
Commit & push perubahannya ke GitHub.

### Langkah 3 — Deploy ke Vercel
1. Daftar di <https://vercel.com> (login pakai akun GitHub).
2. **Add New → Project** → pilih repo `distribusi-telur`.
3. Di bagian **Environment Variables**, isi:
   | Nama | Isi |
   |---|---|
   | `DATABASE_URL` | connection string PostgreSQL dari Langkah 1 |
   | `JWT_SECRET` | string acak panjang |
   | `NODE_ENV` | `production` |
   | `STORAGE_DRIVER` | `s3` (kalau sudah siapkan S3) |
4. Klik **Deploy**. Tunggu selesai.

### Langkah 4 — Siapkan isi database pertama kali
Setelah deploy, database masih kosong. Dari komputer lokal Anda (dengan
`DATABASE_URL` diarahkan ke Postgres cloud), jalankan:
```bash
npm run db:migrate   # buat semua tabel
npm run db:seed      # isi data awal (cabang, akun, produk)
```
> Atau pakai fitur "Deploy Hook" / terminal Vercel bila tersedia.

Selesai — aplikasi bisa diakses di `https://nama-proyek.vercel.app`.

---

## JALUR B — VPS Sendiri (DigitalOcean / Contabo / Niagahoster VPS / dll)

Cocok kalau ingin kontrol penuh dan boleh tetap pakai SQLite.

### Langkah 1 — Siapkan server
- Sewa VPS (Ubuntu 22.04 disarankan).
- Login via SSH: `ssh root@ALAMAT-IP-SERVER`
- Pasang Node.js 18+:
  ```bash
  curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
  sudo apt-get install -y nodejs git
  ```

### Langkah 2 — Ambil & siapkan aplikasi
```bash
git clone https://github.com/FalahALL/distribusi-telur
cd distribusi-telur
npm install
cp .env.example .env
nano .env          # ganti JWT_SECRET, set NODE_ENV=production
npm run setup      # buat DB SQLite + seed  (atau pakai Postgres, lihat doc 03)
npm run build      # siapkan versi produksi
```

### Langkah 3 — Jalankan terus-menerus dengan PM2
`PM2` menjaga aplikasi tetap hidup walau terminal ditutup / server restart.
```bash
sudo npm install -g pm2
pm2 start "npm run start" --name telur
pm2 save
pm2 startup        # ikuti instruksi yang muncul (agar auto-start saat reboot)
```
Aplikasi kini jalan di `http://ALAMAT-IP:3000`.

### Langkah 4 — Domain + HTTPS (opsional tapi disarankan)
Pasang **Nginx** sebagai penerus + **Certbot** untuk SSL gratis:
```bash
sudo apt-get install -y nginx certbot python3-certbot-nginx
```
Buat konfigurasi Nginx yang meneruskan domain Anda ke `http://localhost:3000`,
lalu:
```bash
sudo certbot --nginx -d domainanda.com
```

Contoh isi `/etc/nginx/sites-available/telur`:
```nginx
server {
    server_name domainanda.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
```
Aktifkan: `sudo ln -s /etc/nginx/sites-available/telur /etc/nginx/sites-enabled/ && sudo nginx -t && sudo systemctl reload nginx`

### Update kode di VPS (nanti)
```bash
cd distribusi-telur
git pull
npm install
npm run db:migrate
npm run build
pm2 restart telur
```

---

## Alternatif Lain (sekilas)
- **Railway** (<https://railway.app>) — Postgres + Node otomatis, mirip Vercel.
- **Render** (<https://render.com>) — ada "Web Service" + "PostgreSQL" gratis.
  Langkahnya serupa Jalur A.

---

## 4. Foto Bukti Kurir → Cloud Storage (S3)

Di produksi, jangan simpan foto di folder lokal. Buka `src/lib/storage.ts` —
di sana sudah ada penanda `TODO` untuk S3/MinIO. Set variabel di `.env`:
```
STORAGE_DRIVER="s3"
S3_ENDPOINT="..."
S3_BUCKET="..."
S3_ACCESS_KEY="..."
S3_SECRET_KEY="..."
```
Penyedia S3 murah/gratis: **Cloudflare R2**, **MinIO** (self-host), **AWS S3**,
**Supabase Storage**.

---

## Ringkasan Checklist Sebelum Go-Live
- [ ] `JWT_SECRET` diganti string acak panjang
- [ ] `NODE_ENV=production`
- [ ] Database sudah PostgreSQL (kalau serverless) & sudah `migrate` + `seed`
- [ ] Foto bukti pindah ke S3 (kalau serverless)
- [ ] Domain + HTTPS aktif
- [ ] Ganti password akun demo / buat akun asli, nonaktifkan akun contoh
- [ ] Backup database terjadwal (lihat doc 03)
