# Idempotency

## Products API — `/api/ingest/products`

`shop + barcode` で UPSERT。同じ barcode を何回送っても安全です。

* 値に変更がなければ `unchanged`
* 値に変更があれば `updated` + `product_update_logs` に `changed_fields` を記録

## Inventory Snapshot — `/api/ingest/inventory-snapshot`

`shop + product_id + location_id` で UPSERT（絶対値）。同じ値の再送は在庫・日次とも重複せず、`inventory_update_logs` も**変更があった行のみ**追記されます（再送に冪等）。

## Inventory Available — `/api/ingest/inventory-available`

* **`mode=absolute`**: `available_qty = N` の上書きのため再送に冪等。
* **`mode=delta`**: 積算のため本質的に非冪等。`idempotency_key` を付与すると、同一キー × 同一 `(product, location)` の `available_delta` ログが既存なら適用を skip（`lines_deduped`）。**順次リトライを吸収する best-effort** であり、完全な並行重複保証ではないため、**イベント単位で一意な `idempotency_key`** を付与してください。

## Order Line Master — `/api/order-line-master/bulk-upsert`

`shop + source_system + transaction_id + transaction_line_id + line_instance_no` で UPSERT。同一行の再送は差分があれば更新（無ければ実質 no-op）、`order_line_master_update_logs` に記録します。

## Routing — `/api/routing/orders`

* **入口冪等**: `shop + sourceSystem + externalOrderRef` で routing order 1 行に収束。`status = dispatched` の注文を再 POST すると再ルーティングせず、既存の出荷指示を `idempotent: true` で返します（外部への再送なし）。
* **出口冪等**: 出荷指示は `shop + idempotencyKey`（省略時 `{sourceSystem}:{externalOrderRef}`）で UPSERT し、外部 fulfillment への二重送信を防ぎます。
* まだ dispatch されていない注文の再 POST は、前回の decision を破棄して再計算します。

## Routing 再引き当て — `/api/routing/reallocate`

* 売越し（`oversold`）decision を**同一出荷元の別メンバー**へ再引き当て。旧 decision は `superseded` にし後継へリンク（重複適用なし）。
* 出荷指示は `shop + idempotencyKey` で UPSERT されるため、再 dispatch は同一行を更新（外部への二重送信を防止）。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lmile.gitbook.io/lmile-docs/reference/idempotency.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
