Nano Store - Ecommerce Docs
Developer ResourcesProduct Gallery, Delete and Preview

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/.

FileRole
admin/product/product-restore-guard.tsNew. findDeletedReferent(executor, row) and DELETED_REFERENT_MESSAGES
admin/product/product-write.service.tsrestore() calls the guard after the not-deleted check and before the UPDATE
admin/product/product-bulk.service.tsbulkRestore calls the guard per row, inside its transaction
shared/product-response.builder.tsProductResponseRow.deletedAt; timestamps.deletedAt set inside the admin-surface block
shared/product-response-assembler.service.tsMaps deletedAt: row.deletedAt into the response row
admin/product/dto/product-response.dto.tsProductTimestampsDto.deletedAt?: Date | null
admin/product/product-admin.controller.tsSwagger @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:

  1. Category deleted: PRODUCT_CATEGORY_DELETED
  2. Brand set and deleted: PRODUCT_BRAND_DELETED
  3. 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:

OrderStatuserrorCodeMessage
1404PRODUCT_NOT_FOUNDno product has this public id
2409PRODUCT_NOT_DELETED"Product is not deleted."
3409PRODUCT_CATEGORY_DELETED"Category is deleted."
4409PRODUCT_BRAND_DELETED"Brand is deleted."
5409PRODUCT_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 with CATEGORY_PARENT_DELETED when the parent is deleted or missing, in addition to CATEGORY_NOT_DELETED, CATEGORY_NOT_FOUND and CATEGORY_RESTORE_MAX_DEPTH_EXCEEDED.
  • The category list's deletedOnly filter 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 SHARE select per table, then checks each row against that result.
  • The backend has no ErrorResponseDto, so the restore handler documents errors with plain @ApiResponse entries, matching updatePricing in 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

FileCovers
product-write.service.restore.spec.tsEach referent refused with its code, precedence, all-live restore, not-deleted never queries referents
product-bulk.service.restore.spec.tsMixed batch, precedence, unchanged PRODUCT_NOT_FOUND / PRODUCT_NOT_DELETED
product-restore-guard.int.spec.tsEight cases on the real database across the guard, single restore and bulk restore
product-response.builder.spec.ts, product-response-assembler.service.spec.tsAdmin 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