Product Gallery, Delete and Preview Backend Architecture
Backend changes for the feature — the restore referent guard, the admin-only deletedAt timestamp, and their error contract.
Audience: Backend developers and reviewers Scope: The two backend changes in
apps/api/src/modules/products. The storefront gallery and the admin screens are documented on the features page.
Product Gallery, Delete and Preview Backend
1. Module Scope
The change touches the products module only. There is no schema change, no migration, no new error code and no new endpoint. It did not change delete semantics. The later catalog-integrity-and-client-ip change made category, brand and series delete refuse while live products remain (see Catalog Integrity and Client IP).
2. Composition
All paths are under apps/api/src/modules/products/.
| File | Role |
|---|---|
admin/product/product-restore-guard.ts | New. findDeletedReferent(executor, row) and DELETED_REFERENT_MESSAGES |
admin/product/product-write.service.ts | restore() calls the guard after the not-deleted check and before the UPDATE |
admin/product/product-bulk.service.ts | bulkRestore calls the guard per row, inside its transaction |
shared/product-response.builder.ts | ProductResponseRow.deletedAt; timestamps.deletedAt set inside the admin-surface block |
shared/product-response-assembler.service.ts | Maps deletedAt: row.deletedAt into the response row |
admin/product/dto/product-response.dto.ts | ProductTimestampsDto.deletedAt?: Date | null |
admin/product/product-admin.controller.ts | Swagger @ApiResponse entries for the restore 404 and 409 |
3. Runtime Rules and Domain Invariants
Restore refuses on a soft-deleted referent
A restored product points at the category, brand and series it had when it was deleted. Restoring while one is soft-deleted would publish the product under a parent the storefront hides, so restore returns 409 with the same codes create and update already use.
findDeletedReferent runs one query: categories LEFT JOIN brands LEFT JOIN brand_series, joined on the product row's internal ids. It does not reuse the live-only public-id lookups, because those answer "not found" for exactly the deleted row it must see. It returns the first match in this order, so one error is reported per call:
- Category deleted:
PRODUCT_CATEGORY_DELETED - Brand set and deleted:
PRODUCT_BRAND_DELETED - Series set and deleted:
PRODUCT_SERIES_DELETED
If a referent row is missing (impossible under the FK restrict), the guard throws a plain Error, which surfaces as a 500, rather than read the absent row as live.
A refused restore writes nothing and triggers no cache invalidation.
Admin-only deletedAt
timestamps.deletedAt is null for a live product and the deletion time for a soft-deleted one. It is assigned only inside the surface === "admin" branch of the response builder. ProductCustomerDetailShape omits it by type (Omit<..., "deletedAt">), so the storefront response never carries the key.
4. Error Contract
Restore (POST /api/admin/products/:publicId/restore), in the order evaluated:
| Order | Status | errorCode | Message |
|---|---|---|---|
| 1 | 404 | PRODUCT_NOT_FOUND | no product has this public id |
| 2 | 409 | PRODUCT_NOT_DELETED | "Product is not deleted." |
| 3 | 409 | PRODUCT_CATEGORY_DELETED | "Category is deleted." |
| 4 | 409 | PRODUCT_BRAND_DELETED | "Brand is deleted." |
| 5 | 409 | PRODUCT_SERIES_DELETED | "Brand series is deleted." |
Bulk restore does not throw for these. Each refused row becomes { publicId, errorCode } in failures and the other rows still restore.
Variants of a deleted product are 404
ProductVariantAdminService.list calls resolveProduct (admin/variant/product-variant-admin.service.ts:93), which filters isNull(products.deletedAt) (:419) and throws NotFoundException with PRODUCT_NOT_FOUND (:423-426). A deleted product's variants are therefore unreachable by design. The admin detail page skips the variants and options calls for a deleted product and explains why; the backend is unchanged.
5. Known Residuals
- Category restore (
catalog/admin/category/catalog-category-write.service.ts,restore) now refuses withCATEGORY_PARENT_DELETEDwhen the parent is deleted or missing, in addition toCATEGORY_NOT_DELETED,CATEGORY_NOT_FOUNDandCATEGORY_RESTORE_MAX_DEPTH_EXCEEDED. - The category list's
deletedOnlyfilter returns one tree level per call, scoped to the parent. - The referent check and the restore UPDATE now run in one transaction with the referents locked
FOR KEY SHARE(product-restore-guard.ts), so a referent deleted in between is no longer accepted. Single and bulk restore both lock. - Bulk restore pre-locks the batch's referents with one
ORDER BY id ... FOR KEY SHAREselect per table, then checks each row against that result. - The backend has no
ErrorResponseDto, so the restore handler documents errors with plain@ApiResponseentries, matchingupdatePricingin the same controller.
6. Rollback
Revert the commit. There is nothing to migrate back; the admin treats an absent deletedAt as not deleted.
7. Tests
| File | Covers |
|---|---|
product-write.service.restore.spec.ts | Each referent refused with its code, precedence, all-live restore, not-deleted never queries referents |
product-bulk.service.restore.spec.ts | Mixed batch, precedence, unchanged PRODUCT_NOT_FOUND / PRODUCT_NOT_DELETED |
product-restore-guard.int.spec.ts | Eight cases on the real database across the guard, single restore and bulk restore |
product-response.builder.spec.ts, product-response-assembler.service.spec.ts | Admin deletedAt is null or a Date; the customer response has no deletedAt key |
All of these pass. Run the integration spec on its own against the _test database with pnpm --filter api test:int --testPathPatterns product-restore-guard. A bare positional pattern is ignored by the script's --testPathIgnorePatterns and would run every integration spec.
See Also
Product Gallery, Delete and Preview Features
Storefront gallery arrows, admin product delete with undo, the deleted-products view with restore, list filters, and the customer preview.
Product Gallery, Delete and Preview API
Contract changes to the admin product restore, bulk restore and product response, with error codes and integration notes.