# Discount Edit API Requirements (POS Cart)

Dokumen ini untuk sinkronisasi BE dengan UI POS terbaru:

- di tabel cart ada tombol **Edit** pada kolom Discount
- klik tombol membuka modal **Edit Discount**
- kasir bisa set:
  - `Percentage (%)`
  - `Nominal (Rp)`

## Status Saat Ini

FE sudah bisa hitung discount sementara di client untuk UX.

Agar jadi **real API** dan konsisten perhitungan backend, BE perlu endpoint validasi/perhitungan discount.

## Endpoint yang Dibutuhkan

## 1) Price Preview per Cart Item

`POST /api/v1/pos/cart/price-preview`

Request contoh:

```json
{
  "items": [
    {
      "product_id": 1513,
      "qty": 1,
      "discount_type": "percentage",
      "discount_value": 10
    }
  ]
}
```

Response contoh:

```json
{
  "items": [
    {
      "product_id": 1513,
      "unit_price": 289000,
      "discount_amount": 28900,
      "subtotal": 260100
    }
  ],
  "summary": {
    "subtotal": 289000,
    "discount_total": 28900,
    "grand_total": 260100
  }
}
```

## 2) Checkout Wajib Recalculate

`POST /api/v1/pos/checkout`

Backend harus:

- ignore total dari FE bila ada
- hitung ulang harga/discount di server
- validasi discount agar tidak > subtotal item

## Rule Validasi BE

- `discount_type` hanya: `percentage` atau `fixed`
- `discount_value >= 0`
- jika `percentage`, clamp maksimal 100
- jika `fixed`, clamp maksimal subtotal item

## File BE yang Perlu Diupdate

- `internal/modules/pos/handler.go` (buat bila belum ada)
- `internal/modules/pos/service.go`
- `internal/modules/pos/repository.go`
- `internal/server/http.go`
  - register route:
    - `POST /api/v1/pos/cart/price-preview`
    - `POST /api/v1/pos/checkout`

## Catatan Integrasi FE

FE sudah menampilkan modal discount edit dan menyimpan state:

- `discount_type`
- `discount_value`

Setelah endpoint preview aktif, FE dapat call API preview setiap save discount untuk nilai final yang authoritative dari BE.

