> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sertifikasitrainer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# DB baru + Scalev resync

> Neon fresh database, kredensial Scalev baru, dan sinkron produk checkout.

Panduan ini dipakai saat **database di-reset** (Neon baru / wipe) atau **ganti akun Scalev** — supaya batch di admin tidak masih mengarah ke store/variant lama.

## Kapan perlu resync?

| Situasi                       | Gejala                                | Solusi                      |
| ----------------------------- | ------------------------------------- | --------------------------- |
| Ganti `SCALEV_API_KEY` / toko | `ensure-scalev` error **Not Found**   | Resync semua batch          |
| Restore DB backup lama        | Tier punya `store_TJH...` (toko lama) | Reset store + resync        |
| Neon fresh + batch baru       | Belum ada produk di Scalev            | Resync setelah batch dibuat |

Tier menyimpan `scalevStoreUniqueId` per baris — **mengalahkan** `SCALEV_STORE_UNIQUE_ID` di env. DB lama + key baru = mismatch.

## Env Scalev (shared `.env`)

```bash theme={null}
PAYMENT_PROVIDER=scalev
SCALEV_API_KEY=sk_...
SCALEV_STORE_UNIQUE_ID=store_...    # dari GET /v3/stores/simplified
SCALEV_BUSINESS_ID=TUQ34D2CUIL8ILFX  # opsional, referensi dashboard
SCALEV_BASE_URL=https://api.scalev.com
SCALEV_WEBHOOK_SECRET=...           # webhook production
```

Cek key + store:

```bash theme={null}
curl -s -H "Authorization: Bearer $SCALEV_API_KEY" \
  "https://api.scalev.com/v3/stores/simplified" | jq '.data[] | {name, unique_id}'
```

## Alur: Neon DB baru (production)

<Warning>
  `neon-fresh-setup.sh` **menghapus semua data** di `DATABASE_URL` yang aktif di `apps/api/.env`.
</Warning>

<Steps>
  <Step title="1. Isi .env Scalev">
    Pastikan API key dan `SCALEV_STORE_UNIQUE_ID` sudah benar di `.env` shared.
  </Step>

  <Step title="2. Wipe + migrate + seed">
    ```bash theme={null}
    ./thub env merge --prod
    ./scripts/fresh-neon-and-scalev.sh
    ```

    Atau hanya DB: `./scripts/neon-fresh-setup.sh`
  </Step>

  <Step title="3. Push secrets ke Cloudflare">
    ```bash theme={null}
    ./thub secrets generate
    ./thub secrets push
    ```
  </Step>

  <Step title="4. Deploy (jika perlu)">
    ```bash theme={null}
    ./thub deploy
    ```

    Atau workflow **Deploy Cloudflare** di GitHub Actions.
  </Step>

  <Step title="5. Buat batch & tier di admin">
    Seed hanya demo batch — batch production buat lewat `/admin/batches` + tier.
    Lihat [Admin playbook](/guides/admin-playbook).
  </Step>

  <Step title="6. Resync Scalev otomatis">
    ```bash theme={null}
    PW_BASE_URL=https://app.sertifikasitrainer.com ./scripts/pw-login.sh admin
    ./thub scalev resync
    ```

    Atau: `./scripts/resync-all-scalev.sh`

    Script ini per batch:

    1. Clear `scalevProductId` batch
    2. Set tier → `SCALEV_STORE_UNIQUE_ID` dari `.env`, clear variant lama
    3. `POST resync-scalev` → buat produk + variant di Scalev
  </Step>

  <Step title="7. Verifikasi">
    * Admin: **Scalev health** → "Siap checkout"
    * Test: `POST /api/public/register` → dapat `providerOrderCode` + URL bayar
  </Step>
</Steps>

## Diagram

```mermaid theme={null}
flowchart TD
  env[".env Scalev baru"] --> merge["thub env merge --prod"]
  merge --> neon["neon-fresh-setup\nwipe + migrate + seed"]
  neon --> admin["Admin: batch + tier"]
  admin --> resync["thub scalev resync"]
  resync --> scalev["Produk + variant di Scalev"]
  scalev --> order["/public/register → order"]
```

## Resync manual (satu batch)

Di admin → detail batch → panel Scalev:

1. **Ensure Scalev**
2. **Resync** per tier

Kalau masih gagal, pastikan tier tidak masih pakai **Store ID lama** — edit tier atau jalankan `./thub scalev resync`.

## Dev lokal

Dev pakai `PAYMENT_PROVIDER=manual` di `.env.development.local` — resync Scalev **tidak wajib** untuk laptop.

Untuk tes Scalev lokal: override sementara `PAYMENT_PROVIDER=scalev` di merge dev + jalankan resync ke API lokal (jarang dipakai).

## Halaman terkait

* [Tutorial Scalev](/external/scalev)
* [Alur batch & tier](/architecture/alur-batch-tier)
* [Alur pembelian](/architecture/alur-pembelian)
* [Admin playbook](/guides/admin-playbook)
