# 🚀 QUICK START — PRODUCTION DEPLOYMENT
**PT. Sarana Gemilang Finance System**

Panduan singkat untuk go-live production. Ikuti urut.

---

## ⚡ CARA TERCEPAT (One-Shot)

Jalankan satu command yang otomatis lakukan semua langkah:

```powershell
cd C:\Users\pdplb\sarana-gemilang-finance\backend
.\go-production.ps1
```

Script akan tanya konfirmasi di tiap step. Total waktu: ~10 menit.

---

## 📋 CARA MANUAL (Step-by-step)

Kalau lebih suka kontrol penuh:

### Step 1 — Backup Database
```powershell
cd C:\Users\pdplb\sarana-gemilang-finance\backend
$ts = Get-Date -Format "yyyyMMdd_HHmmss"
pg_dump $env:DATABASE_URL > "backup_pre_production_$ts.sql"
```

### Step 2 — Apply Migration Sprint 3
```powershell
npx prisma migrate deploy
npx prisma generate
```

### Step 3 — Clean Database
```powershell
node scripts/clean-database.js
# Ketik "RESET-PRODUCTION" saat diminta konfirmasi
```

### Step 4 — Setup Production (interaktif)
```powershell
node scripts/setup-production.js
# Wizard akan tanya:
#   - Domain frontend production (FRONTEND_URLS)
#   - Database URL (opsional, default dari .env)
#   - Password baru untuk 3 akun default
# Output: file .env.production siap pakai
```

### Step 5 — Verifikasi
```powershell
node scripts/sprint3-deploy.js
```

---

## 🎯 ALTERNATIF: Generate Credentials Tanpa Interaktif

Kalau tidak mau wizard, generate dulu credentials random:

```powershell
node scripts/generate-credentials.js > credentials.txt
# Buka credentials.txt, salin ke password manager
# Hapus credentials.txt setelah disimpan: Remove-Item credentials.txt
```

Lalu jalankan `setup-production.js` dan paste password dari password manager saat diminta.

---

## 📤 UPLOAD KE HOSTING

Setelah scripts selesai dan ada file `backend/.env.production`:

### 1. Upload file ke hosting
- `.env.production` → rename jadi `.env` di hosting
- Semua kode backend (jangan upload `.env` development!)

### 2. Di hosting, install dependencies
```bash
cd /path/to/backend
npm install --production
```

### 3. Apply migration di production DB
```bash
npx prisma migrate deploy
npx prisma generate
```

### 4. Restart aplikasi
```bash
# Pilih sesuai stack hosting Anda:
pm2 restart sarana-gemilang        # PM2
systemctl restart sarana-gemilang  # systemd
docker compose up -d --build       # Docker
```

### 5. Test aplikasi
```bash
# Health check
curl https://your-domain.com/health
# Expected: {"status":"ok",...}

curl https://your-domain.com/health/detailed
# Expected: {"status":"ok","database":{"connected":true,...},...}
```

### 6. Login dengan password baru
- Buka frontend production
- Login dengan password yang baru di-set di Step 4
- Pastikan bisa akses semua menu

---

## 📊 SETUP MONITORING

Setelah aplikasi live, setup monitoring (penting agar tahu kalau ada masalah):

Lihat file `MONITORING_SETUP.md` untuk panduan lengkap. Rekomendasi cepat:

1. Daftar di https://uptimerobot.com (gratis)
2. Add Monitor:
   - URL: `https://your-domain.com/health/detailed`
   - Interval: 5 menit
   - Keyword: `"status":"ok"`
3. Set alert email/Telegram

---

## ✅ CHECKLIST FINAL

Sebelum mengabarkan ke klien bahwa sistem live:

- [ ] Backup database production tersimpan di tempat aman
- [ ] Migration Sprint 3 applied (`scripts/sprint3-deploy.js` pass)
- [ ] Data testing sudah di-clean (`scripts/clean-database.js`)
- [ ] 3 password default sudah diganti
- [ ] JWT_SECRET baru sudah di-set (128 char hex)
- [ ] NODE_ENV=production di hosting
- [ ] FRONTEND_URLS sesuai domain production
- [ ] HTTPS aktif (SSL certificate valid)
- [ ] Health check `/health/detailed` return 200 OK
- [ ] Monitoring (UptimeRobot atau setara) aktif
- [ ] Tim IT punya akses password manager untuk credentials
- [ ] Dokumentasi user (cara login, fitur dasar) sudah disiapkan

---

## 🆘 TROUBLESHOOTING

**Q: Migration gagal — "constraint already exists"**
- Kemungkinan migration sudah pernah di-apply
- Solusi: `npx prisma migrate resolve --applied <migration_name>`

**Q: Login error 401 setelah upload .env.production**
- JWT_SECRET berbeda antara saat token dibuat vs saat verifikasi
- Semua user perlu login ulang (sudah otomatis di-handle, force logout dilakukan setup-production.js)

**Q: CORS error di browser**
- FRONTEND_URLS di .env tidak match dengan domain frontend
- Periksa di .env.production: `FRONTEND_URLS="https://exact-domain.com"`

**Q: Health check return 503**
- Database tidak connect — cek DATABASE_URL
- Atau memory critical — restart aplikasi, cek log

**Q: Password baru tidak diterima saat login**
- Pastikan password copy-paste dari password manager (tidak ada spasi/karakter tersembunyi)
- Cek case-sensitive: A vs a beda

---

## 📞 SUPPORT FILES

- `SPRINT3_DEPLOYMENT.md` — detail teknis Sprint 3 fixes
- `MONITORING_SETUP.md` — panduan monitoring lengkap
- `backend/scripts/clean-database.js` — reset data
- `backend/scripts/setup-production.js` — wizard setup
- `backend/scripts/sprint3-deploy.js` — verifikasi
- `backend/scripts/generate-credentials.js` — generate random credentials
- `backend/go-production.ps1` — one-shot wizard

---

**Status: Sistem siap untuk go-live production** ✅

Estimasi total waktu setup: **15-30 menit** termasuk testing.
