Skip to content

fix(customer): return 404 (not 200+null) on master-path GET miss#242

Merged
CryptoJones merged 1 commit into
masterfrom
fix/customer-getbyid-404-on-non-existent-master-path
May 19, 2026
Merged

fix(customer): return 404 (not 200+null) on master-path GET miss#242
CryptoJones merged 1 commit into
masterfrom
fix/customer-getbyid-404-on-non-existent-master-path

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Closes #241.

Summary

Master-key GET /v1/customer/<nonexistent> returned 200 { customers: null } — a success status wrapping an empty body. Clients reading body.customers.custId crashed; SDK generators that branched on status marked it successful.

Fix: findAndRespond short-circuits to 404 when findByPk returns null. Both master and scoped paths now agree on the not-found shape.

Test plan

  • npm run lint clean
  • npm test — 686 → 687 (+1 master-path 404 test using the P5-M auth._setDbForTesting seam)

Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/

…sses on the master path

The master-key branch of `getCustomerById` called the `findAndRespond`
helper without first verifying the customer existed:

```js
if (isAuthKeyMasterKey) {
    return findAndRespond(customerId, res);
}
```

And `findAndRespond` unconditionally wrapped `findByPk(id)` in a 200
envelope:

```js
const customer = await Customer.findByPk(customerId);
return res.status(200).json({ ..., customers: customer });
```

So a master key requesting `/v1/customer/<nonexistent>` got back:

```json
{ "message": "Successfully retrieved...", "customers": null }
```

A success status code wrapping an empty body — any client reading
`body.customers.custId` crashes; SDK generators that branch on
HTTP status mark the request as successful and propagate the null
downstream. The scoped (non-master) branch never hit this path
because `GetCustomerCompanyId(id) === -1` short-circuits to 403 for
absent rows, but the master path skipped that guard.

Fix: have `findAndRespond` short-circuit to 404 when `findByPk`
returns null. Both paths now agree that "no such customer id" is
not-found, not a success. Soft-deleted rows (which `defaultScope`
filters at the model layer) correctly surface as 404 too.

Pinned in `tests/api/customer.test.js` with a master-path test that
drives `getCustomerById` directly through the controller export and
uses the P5-M `auth._setDbForTesting` seam to make `IsMaster`
resolve to `true`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit a29ca40 into master May 19, 2026
3 checks passed
@CryptoJones CryptoJones deleted the fix/customer-getbyid-404-on-non-existent-master-path branch May 19, 2026 12:04
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.

customer/getbyid: master path returns 200 + {customers: null} for non-existent ids

1 participant