Catalog Integrity and Client IP Feature Guide
Delete protection for categories, brands and series, safe restore rules, constraint errors that no longer surface as 500, and the real client IP in audit and session records.
Feature Overview
Two fixes shipped together.
Catalog integrity. A category, brand or brand series can no longer be soft-deleted while live products still use it, and a category or series can no longer be restored under a deleted parent or brand. The rules hold under concurrent writes, including a running catalog import. Before this, a delete could leave live products pointing at a deleted category, brand or series, where they disappeared from every tree and filter.
Client IP. The admin activity log and the admin session record now hold the operator's real address instead of 127.0.0.1. Before this, the admin panel's calls to the API all arrived from the admin process itself.
This feature does not:
- Repair existing data. Live products that already sit under a deleted category, brand or series stay as they are, remain editable and deletable, and need no action.
- Change rate limit tiers or limits for existing routes.
IpThrottlerGuardnow reads the forwarded address when the token matches, and falls back toreq.ip. - Change any request or response shape. Only new
409codes and new refusals on existing routes.
Surface Matrix
| Surface | Prefix | Consumer | Auth |
|---|---|---|---|
| Catalog admin | /api/admin/catalog/categories, /api/admin/catalog/brands, /api/admin/catalog/brand-series | admin panel | JWT + Categories_*, Brands_*, BrandSeries_* |
| Product admin (write paths affected) | /api/admin/products | admin panel | JWT + Products_* |
| Activity log (where the IP is read) | /api/activity | admin panel | JWT + Activity_READ |
| Admin BFF auth proxies (IP now forwarded) | /api/admin/auth/*, /api/mobile/auth/* on the admin origin | browser | cookie session |
Business Rules
Deleting a category, brand or series (BLOCK policy)
- A delete is refused with
409while at least one product withdeleted_at IS NULLreferences the row. Draft and archived products count; only soft-deleted products do not. - Category:
CATEGORY_HAS_ACTIVE_PRODUCTS. Brand:BRAND_HAS_ACTIVE_PRODUCTS. Series (matched on brand and series together):BRAND_SERIES_HAS_ACTIVE_PRODUCTS. - The existing child checks run first. A category with live subcategories still answers
CATEGORY_HAS_ACTIVE_CHILDREN, and a brand with live series still answersBRAND_HAS_ACTIVE_SERIES, before any product count is taken. - Archived products must be set back to draft before they can be moved, so the user has to un-archive them first.
- Bulk delete applies the same rule per id. A blocked id does not fail the request; it appears in
failureswith itserrorCodeand the other ids proceed.
Locks and CATALOG_RESOURCE_BUSY
- A delete takes row locks so a product write cannot attach to the row in the middle of the delete. If those locks are not available within
CATALOG_DELETE_LOCK_TIMEOUT_MS(default 5000), the whole request answers409 CATALOG_RESOURCE_BUSY. A running catalog import is the usual cause. - This is transient. The user retries.
- In a bulk delete it fails the whole request, not individual ids.
Restoring
- Restoring a category whose parent is deleted, or missing, is refused with
409 CATEGORY_PARENT_DELETED. Restore the parent first. The same code is used when creating or moving a category under a deleted parent. - Restoring a series under a deleted brand, creating one under it, or updating one to move it to a deleted brand is refused with
BRAND_SERIES_BRAND_DELETED. Series bulk restore reports it per id. - Restoring a product still answers
PRODUCT_CATEGORY_DELETED,PRODUCT_BRAND_DELETEDorPRODUCT_SERIES_DELETEDwhen its referent is deleted. The check is now race-safe.
Legacy products stay editable
- A live product that already sits under a deleted category, brand or series can still be edited when the edit leaves that classification unchanged. The admin form re-sends all three ids on every save, so without this such a product could not be saved at all.
- Pointing a product at a different deleted referent is still refused. Create, import and restore never get this allowance.
Constraint errors are no longer 500
- A duplicate SKU, a series that belongs to a different brand, a missing thumbnail on a live product, a selling price above MRP and a
thumbnailAltwithoutthumbnailKeynow answer the intended409or400instead of an unmapped500. - This also applies to the product lifecycle and media write paths, and a duplicate tag slug answers
TAG_SLUG_ALREADY_EXISTS.
Client IP in audit and session records
- The address comes from the admin panel, not from the browser. The admin reads
X-Forwarded-Foras written by Nginx, countsTRUSTED_PROXY_HOPSentries in from the right, and sends the result to the API. - The API trusts that address only when the request also carries the shared
INTERNAL_CLIENT_IP_TOKEN. Without a matching token it records the direct peer, which behind the admin is127.0.0.1. A token mismatch is silent. - A visitor-supplied
X-Forwarded-Forvalue is never recorded. Nginx appends the real address and the admin takes the last entry. - The API stores the address in the activity record (
request.ip, returned asipAddress) and inadmin_sessions.ip_address. - Deployment order matters for security. The required order is in section 13.4 (and 13.5 for login throttling) of
docs/vps-deployment-runbook.md, and Backend Architecture describes the mechanism.
Rate Limits
No new limits on catalog routes. The bulk delete routes keep RATE_LIMITS.ADMIN_BULK_WRITE. Email login gains brute-force throttling, and mobile change-password is throttled; see Login Brute-Force Throttling. In production API_BIND_HOST is required and must be loopback or private.
Error UX Mapping
Copy comes from catalog-action-error.ts in the admin panel (app/(dashboard)/dashboard/catalog/_actions/). Unmapped codes fall back to generic copy, never to server text.
errorCode | HTTP | What the user sees |
|---|---|---|
CATEGORY_HAS_ACTIVE_PRODUCTS | 409 | Can't delete: products are still in this category. Move or delete them first — archived products must be set back to draft before they can be moved. |
BRAND_HAS_ACTIVE_PRODUCTS | 409 | Can't delete: products still use this brand. Move or delete them first — archived products must be set back to draft before they can be moved. |
BRAND_SERIES_HAS_ACTIVE_PRODUCTS | 409 | Can't delete: products still use this series. Move or delete them first — archived products must be set back to draft before they can be moved. |
CATALOG_RESOURCE_BUSY | 409 | Another change to the catalog is in progress (for example an import). Try again in a moment. |
CATEGORY_PARENT_DELETED | 409 | The parent category is deleted. Restore the parent first, or choose a different parent when creating or moving. |
BRAND_SERIES_BRAND_DELETED | 409 | Its brand is deleted. Restore the brand first. |
CATEGORY_HAS_ACTIVE_CHILDREN | 409 | This category still has subcategories. Delete or move them first. |
BRAND_HAS_ACTIVE_SERIES | 409 | This brand still has series. Delete them first, then delete the brand. |
See Also
Product Gallery, Delete and Preview API
Contract changes to the admin product restore, bulk restore and product response, with error codes and integration notes.
Catalog Integrity and Client IP Backend Architecture
Lock protocol, delete guards, constraint-error mapping and the client-IP resolution chain across the API and the admin BFF.