Skip to content

product.js: --file and --get, for games with more than one product - #210

Merged
fleon merged 2 commits into
mainfrom
pricing/product-file-and-get
Nov 23, 2026
Merged

fleon merged 2 commits into
mainfrom
pricing/product-file-and-get

Conversation

@fleon

@fleon fleon commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Switchlings now sells nine world products beside its full game (fleon/switchlings#556). Its price tool needed to read any product and send a whole product body, so it had copied this tool's auth and Publisher API calls into the game. This PR adds those two modes here instead, so the game can drop its copy.

  • --get <productId> prints the product as Play holds it (JSON), or {"missing": true}. It only reads.
  • --file <body.json> [--send|--verify] runs the existing plan, send and verify steps on a whole oneTimeProduct body, in place of listing.json's one product file. A product that already exists keeps its listings when the body has none. A new product with no listings is refused.

Without these flags the tool behaves as before. I checked it read-only on Switchlings: --get on an existing product and on a missing one, and --file producing a plan. product.test.js passes.

Comment thread Tools/Pricing/product.js Outdated
// --file: a whole product body of the game's own; --get: read one product.
const BODY_FILE = argument('--file');
const GET = argument('--get');
const PRODUCT_FILE = path.join(HERE, 'Monetization', 'iap', `${SOURCE.product.productId}.json`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--get and --file don't need listing.json's product, but this line still reads SOURCE.product.productId before either mode runs. For a game whose listing.json has only package (no single product), which is the case these modes are for, both crash right away with TypeError: Cannot read properties of undefined (reading 'productId'). I reproduced this with {"package": "com.example.game"} and both --get com.example.a and --file a.json. Only compute PRODUCT_FILE when neither flag is given (or use SOURCE.product?.productId), so the new modes depend on package alone.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5e4f733. The one product's file is now computed only when neither --get nor --file is given, so both modes need only package. New tests run both against a listing.json that holds only package.

@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Claude review (house rules)
Reviewed 2 files (Tools/Pricing/product.js, Tools/Pricing/README.md) against AGENTS.md and Tools/Pricing/README.md.
Left 1 inline comment.

Comment thread Tools/Pricing/product.js
Comment on lines +167 to +170
if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
else product.listings = existing.body.listings ?? [];
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crashes on a --file body that has no listings key, when the product is new. The !exists branch adds the problem but leaves product.listings undefined. The next line, product.listings.length (L172), then throws Cannot read properties of undefined (reading 'length'). So the run exits before it prints the problems list, and "a new product needs listings" never shows. I reproduced this with the stubbed Play from product.test.js: the output is Product … missing, would be created and then that TypeError. Nothing gets sent, but the refusal the PR describes never appears.

if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
else product.listings = existing.body.listings ?? [];
}
console.log(`Product ${PRODUCT_ID} on ${PACKAGE}: ${exists ? 'exists, would be updated' : 'missing, would be created'}`);
console.log(` listings: ${product.listings.length} languages (${product.listings.map((l) => l.languageCode).join(', ')})`);
console.log(` purchase option "${option.purchaseOptionId}": buy, legacy-compatible, one at a time, then activated`);

Suggested change
if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
else product.listings = existing.body.listings ?? [];
}
if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
product.listings = exists ? existing.body.listings ?? [] : [];
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5e4f733, with your suggestion. A new test sends a body with no listings: the run exits with 'a new product needs listings' and 'nothing was sent', and makes no PATCH.

…body without listings is refused, not a crash
@fleon
fleon merged commit de39dc2 into main Nov 23, 2026
11 checks passed
@fleon
fleon deleted the pricing/product-file-and-get branch November 23, 2026 19:17
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