Skip to content

fix: assign an id when creating a product or collection - #90

Open
jsnapoli1 wants to merge 1 commit into
AJFrio:mainfrom
jsnapoli1:fix/product-create-missing-id
Open

fix: assign an id when creating a product or collection#90
jsnapoli1 wants to merge 1 commit into
AJFrio:mainfrom
jsnapoli1:fix/product-create-missing-id

Conversation

@jsnapoli1

Copy link
Copy Markdown

Problem

Creating a product silently does nothing. POST /api/admin/products returns 201, but the product never appears in the products tab, the storefront, or any listing.

KVManager.createProduct keys on product.id — but no caller supplies one. Not the admin UI, not the agent's create_product tool. So every create writes to the literal key product:undefined and pushes undefined into products:all.

Observed on a live deployment after three create attempts:

$ wrangler kv key list
 - product:undefined      ← one key, overwritten each time
 - products:all

$ wrangler kv key get products:all
[null,null,null]

The response body confirms it — 201, and no id field:

{"name":"Agent Flow Probe","price":20,"currency":"USD",
 "stripePriceId":"price_unlinked","stripeProductId":"prod_unlinked", ...}

Via the agent this is particularly bad. dispatch sees a 2xx and summarizeResult truthfully reports Created product "X" (undefined) — so the agent tells the user it worked while the products tab stays empty. That's how I found this: the agent claimed success and nothing appeared.

createCollection has the identical defect.

Fix

Generate a UUID when no id is supplied, and filter nulls out of the index on write so stores that already ran the broken path repair themselves on the next create instead of needing manual KV surgery.

crypto.randomUUID() rather than the uuid package — it's available in Workers and needs no dependency. Worth noting uuid is declared in package.json but imported nowhere in src/, which suggests id generation was lost in a refactor rather than never written.

Verification

Adds tests/integration/product-id.test.js (5 cases): id returned on create, created product appears in the listing, no product:undefined key is written, existing nulls are cleaned from the index, and collections get ids too.

All five fail against current main and pass with this change:

# without the fix
 Tests  5 failed (5)

# with the fix
 Tests  5 passed (5)

Full suite: 283 passed (24 files). npm run lint — 0 errors.

Note for existing stores

Any store that created products before this fix has a products:all containing nulls and a stale product:undefined key. The null-filtering makes the index self-heal on the next create; the orphaned product:undefined key is harmless but can be deleted:

wrangler kv key delete "product:undefined" --namespace-id <id> --remote

Products created before the fix are not recoverable — only the last one was ever stored, and without an id it can't be indexed.

https://claude.ai/code/session_015XLFFfHWNeazSuz6UcsQ4C

createProduct keyed on `product.id`, but no caller supplies one — not the
admin UI, not the agent's create_product tool. So every create wrote to
the literal key `product:undefined` and pushed `undefined` into
products:all.

The request still returned 201, with a body that had no id, and the
product was invisible everywhere afterwards. Via the agent this is
especially confusing: the tool sees a 2xx and truthfully reports
"Created product", while the products tab stays empty.

Observed on a live deployment: three creates left a single
`product:undefined` key and `products:all` = [null,null,null].

createCollection had the identical defect.

Both now generate a UUID when no id is supplied, and filter nulls out of
the index on write so stores that already ran the broken path repair
themselves on the next create rather than needing manual KV surgery.

The `uuid` package is a declared dependency that nothing imports, which
suggests id generation was lost in a refactor. This uses crypto.randomUUID
instead, which is available in Workers and needs no dependency.

Adds tests/integration/product-id.test.js (5 cases). All five fail
against the current code and pass with this change.

Claude-Session: https://claude.ai/code/session_015XLFFfHWNeazSuz6UcsQ4C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant