feat(catalog): page and filter the storefront server-side (steps 1-2)
The real catalogue reaches ~16k products and GET /catalog/products had no
paging parameters at all: it returned every active product with its
variants, images, category, brand and collections in one response. The
apps' PAGE_SIZE = 20 was only a render window over that full array, so
scrolling made zero requests. The reference paged this server-side; the
port had moved it to the client (STUB-03), so this is a return to
reference behaviour.
Step 1 — indexes, which were not optional. Product carried NO index
whatsoever (not isActive, categoryId, brandId or createdAt) and
ProductVariant had none on productId, so every per-product include
scanned its whole table. Adding take/skip on top of that would have
paged the response without making the query faster, which is the exact
failure this change exists to avoid. Product_isActive_createdAt_idx is
composite so one index serves the filter, the default ordering and the
OFFSET walk.
CREATE INDEX, not CONCURRENTLY: the deploy image's migration runner
wraps each file in a transaction, which CONCURRENTLY cannot join (D15).
Every statement is IF NOT EXISTS because both migration paths run it.
Step 2 — the routes. Paging is opt-in, which is the whole
backward-compatibility story: with no query parameters the route returns
the same plain array from the same cache as before, so the admin panel,
the smoke scripts and installed app builds are untouched. Pass page,
limit or any filter and it becomes { rows, total, page, limit }.
The filters had to move too, not just the paging. The apps filtered
title, category, brand and gram range in the browser over the full
array; paging the response while leaving that in place would have made
search quietly WRONG — searching only the first page and reporting
"محصولی پیدا نشد" for products that exist. So every filter is built into
the Prisma query, and count uses the same where clause, so `total`
describes the set the app is actually scrolling.
Three routes added so the apps can stop fetching everything to find one
thing (wired up in step 3): ?ids=a,b,c for the cart, favourites and
order items; /:id for the product screen, which today downloads the
whole catalogue to find one row; /facets for the filter sheet's options,
which the apps currently derive by scanning the catalogue in the
browser.
Deliberately not cached: CatalogCacheService exists because the unpaged
query is expensive and every screen wanted it. A paged, filtered query
is neither — bounded by limit and index-served — and caching it would
need a key per filter combination, i.e. unbounded memory for cheap
queries plus a second place for stale rows to hide.
D27's two storefront rules (active, has an image) live in one shared
where clause, so the list, its count, the single-product route, by-ids
and the facets can never disagree about what is visible. limit and ids
are both capped at 100; an uncapped limit is just the old "give me
everything" query spelled differently.
17 new cases, 329/329 overall. All 17 passed on the first run, which is
not evidence by itself, so one filter was deliberately broken to confirm
the suite catches it (1 failed/16 passed) and then restored.
Not measured on real data — the database container was not running, so
16k-row behaviour is reasoned from the query plan, not timed. Worth one
EXPLAIN ANALYZE on a full dataset before step 4.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
bfbac92df5
commit
cbb93dec5d
@@ -0,0 +1,30 @@
|
||||
-- Catalogue paging indexes.
|
||||
--
|
||||
-- Owner, 2026-10-12: «دیتاها در اپ واقعی تا 16 هزار رکورد میرود و گرفتن همه یک
|
||||
-- جا اشتباه است، حتما باید پیج نیشن داشته باشد.» Step 1 of
|
||||
-- docs/CATALOG-PAGINATION-PLAN.md: without these, server-side paging would walk
|
||||
-- the same full table scans the unpaged query did.
|
||||
--
|
||||
-- `Product` carried NO index whatsoever, so every GET /catalog/products was a
|
||||
-- sequential scan, and so was each per-product include.
|
||||
--
|
||||
-- IF NOT EXISTS throughout: these run under BOTH migration paths (D15) — the
|
||||
-- Prisma CLI in development and the app's own runner in the deploy image.
|
||||
--
|
||||
-- CREATE INDEX (not CONCURRENTLY): the app's runner wraps each migration in a
|
||||
-- transaction, which CONCURRENTLY cannot join. On a 16k-row table this locks
|
||||
-- writes for a few hundred milliseconds, which is acceptable; a table large
|
||||
-- enough to matter would need the index built by hand outside a migration.
|
||||
|
||||
-- Matches the storefront list exactly: filter isActive, order by createdAt.
|
||||
CREATE INDEX IF NOT EXISTS "Product_isActive_createdAt_idx" ON "Product" ("isActive", "createdAt");
|
||||
|
||||
-- The category and brand filters of the search screen.
|
||||
CREATE INDEX IF NOT EXISTS "Product_categoryId_idx" ON "Product" ("categoryId");
|
||||
CREATE INDEX IF NOT EXISTS "Product_brandId_idx" ON "Product" ("brandId");
|
||||
|
||||
-- `include: { variants: { where: { isActive: true } } }`, once per product.
|
||||
CREATE INDEX IF NOT EXISTS "ProductVariant_productId_isActive_idx" ON "ProductVariant" ("productId", "isActive");
|
||||
|
||||
-- `where: { images: { some: {} } }` plus the gallery ordered by sortOrder.
|
||||
CREATE INDEX IF NOT EXISTS "ProductImage_productId_sortOrder_idx" ON "ProductImage" ("productId", "sortOrder");
|
||||
@@ -667,6 +667,18 @@ model Product {
|
||||
promotions Promotion[]
|
||||
allocationItems BranchAllocationItem[]
|
||||
favoritedBy CustomerFavorite[]
|
||||
|
||||
// Storefront paging (owner: the real catalogue reaches ~16k products, so
|
||||
// GET /catalog/products must page rather than return everything). This model
|
||||
// had NO index at all, so every storefront query was a full scan and paging
|
||||
// alone would not have been any faster.
|
||||
//
|
||||
// The composite matches the list's own shape: filter on isActive, order by
|
||||
// createdAt — Postgres can satisfy both from this one index, including the
|
||||
// OFFSET walk. The other two serve the category and brand filters.
|
||||
@@index([isActive, createdAt])
|
||||
@@index([categoryId])
|
||||
@@index([brandId])
|
||||
}
|
||||
|
||||
model ProductImage {
|
||||
@@ -684,6 +696,11 @@ model ProductImage {
|
||||
/// variants (one photo, several weights), which is why this is a plain
|
||||
/// one-to-many and not a join table.
|
||||
variants ProductVariant[]
|
||||
|
||||
// The storefront list filters on `images: { some: {} }` and includes the
|
||||
// gallery ordered by sortOrder, both per product. Unindexed, each one scanned
|
||||
// the whole table.
|
||||
@@index([productId, sortOrder])
|
||||
}
|
||||
|
||||
enum ProductImageRole {
|
||||
@@ -721,6 +738,10 @@ model ProductVariant {
|
||||
orderItems OrderItem[]
|
||||
|
||||
@@index([imageId])
|
||||
// Every storefront product carries its variants, so this include ran once per
|
||||
// product with no index to use. The composite covers the `isActive: true`
|
||||
// filter the list applies to them.
|
||||
@@index([productId, isActive])
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { CatalogService } from './catalog.service';
|
||||
import { DEFAULT_PAGE_SIZE } from './dto/list-products.dto';
|
||||
|
||||
/**
|
||||
* Server-side catalogue paging and filtering.
|
||||
*
|
||||
* Owner, 2026-10-12: «دیتاها در اپ واقعی تا 16 هزار رکورد میرود و گرفتن همه یک
|
||||
* جا اشتباه است، حتما باید پیج نیشن داشته باشد.»
|
||||
*
|
||||
* Two things these cases exist to protect:
|
||||
*
|
||||
* 1. The bare `GET /catalog/products` must keep returning the whole array from
|
||||
* the cache. The admin panel, the smoke scripts and already-installed app
|
||||
* builds all depend on that shape, and paging is opt-in precisely so none
|
||||
* of them break.
|
||||
* 2. A filter must reach the DATABASE. Paging the response while leaving the
|
||||
* filter in the client would make search quietly wrong — it would only ever
|
||||
* search the first page — which is worse than the slowness being fixed.
|
||||
*/
|
||||
describe('CatalogService — paging and filtering', () => {
|
||||
function setup(rowCount = 3) {
|
||||
const rows = Array.from({ length: rowCount }, (_, i) => ({
|
||||
id: `p${i}`,
|
||||
title: `محصول ${i}`,
|
||||
isActive: true,
|
||||
accountingStockWeight: null,
|
||||
accountingStockCount: null,
|
||||
accountingStockAt: null,
|
||||
variants: [{ id: `v${i}`, isActive: true, isPreOrder: false, qty: 5 }],
|
||||
images: [{ id: `i${i}`, sortOrder: 0 }],
|
||||
}));
|
||||
|
||||
const findMany = jest.fn().mockResolvedValue(rows);
|
||||
const count = jest.fn().mockResolvedValue(42);
|
||||
const prisma = {
|
||||
product: { findMany, count },
|
||||
category: { findMany: jest.fn().mockResolvedValue([{ id: 'c1', title: 'انگشتر' }]) },
|
||||
brand: { findMany: jest.fn().mockResolvedValue([{ id: 'b1', title: 'برند' }]) },
|
||||
collection: { findMany: jest.fn().mockResolvedValue([{ id: 'col1', title: 'مجموعه' }]) },
|
||||
productVariant: {
|
||||
aggregate: jest.fn().mockResolvedValue({
|
||||
_min: { rashWeightGrams: { toString: () => '1.5' } },
|
||||
_max: { rashWeightGrams: { toString: () => '12' } },
|
||||
}),
|
||||
},
|
||||
};
|
||||
// Nothing is out of stock in the accounting software for these cases.
|
||||
const accounting = { stockBlocker: jest.fn().mockResolvedValue(() => false) };
|
||||
const cache = { get: jest.fn((build: () => Promise<unknown>) => build()) };
|
||||
const service = new CatalogService(prisma as never, accounting as never, cache as never);
|
||||
return { service, findMany, count, prisma, cache };
|
||||
}
|
||||
|
||||
/** The `where` of the last product.findMany call. */
|
||||
const lastWhere = (findMany: jest.Mock) => findMany.mock.calls.at(-1)?.[0]?.where;
|
||||
const lastArgs = (findMany: jest.Mock) => findMany.mock.calls.at(-1)?.[0];
|
||||
|
||||
describe('backward compatibility', () => {
|
||||
it('returns a plain array through the cache when nothing is asked for', async () => {
|
||||
const { service, cache, findMany } = setup();
|
||||
const out = await service.listProducts();
|
||||
expect(Array.isArray(out)).toBe(true);
|
||||
expect(cache.get).toHaveBeenCalledTimes(1);
|
||||
// The original query: storefront rule only, no skip/take/orderBy.
|
||||
const args = lastArgs(findMany);
|
||||
expect(args.skip).toBeUndefined();
|
||||
expect(args.take).toBeUndefined();
|
||||
expect(args.where).toEqual({ isActive: true, images: { some: {} } });
|
||||
});
|
||||
|
||||
it('still applies the two storefront rules on a paged query', async () => {
|
||||
// Inactive products and products with no photo must never appear, however
|
||||
// the caller asks — D27.
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 1, limit: 10 });
|
||||
expect(lastWhere(findMany)).toMatchObject({ isActive: true, images: { some: {} } });
|
||||
});
|
||||
});
|
||||
|
||||
describe('paging', () => {
|
||||
it('translates page/limit into skip/take', async () => {
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 3, limit: 20 });
|
||||
expect(lastArgs(findMany)).toMatchObject({ skip: 40, take: 20 });
|
||||
});
|
||||
|
||||
it('defaults the page size when only a page is given', async () => {
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 2 });
|
||||
expect(lastArgs(findMany)).toMatchObject({ skip: DEFAULT_PAGE_SIZE, take: DEFAULT_PAGE_SIZE });
|
||||
});
|
||||
|
||||
it('reports the total of the FILTERED set, not of the page', async () => {
|
||||
// The app shows "N محصول" and stops scrolling by comparing against this,
|
||||
// so counting anything else would both lie and break the end of the list.
|
||||
const { service, count } = setup();
|
||||
const out = await service.findProducts({ page: 1, limit: 20, search: 'انگشتر' });
|
||||
expect(out.total).toBe(42);
|
||||
expect(count).toHaveBeenCalledWith({
|
||||
where: expect.objectContaining({ title: { contains: 'انگشتر', mode: 'insensitive' } }),
|
||||
});
|
||||
});
|
||||
|
||||
it('filters without paging when no page is given', async () => {
|
||||
const { service, findMany } = setup();
|
||||
const out = await service.findProducts({ search: 'x' });
|
||||
expect(lastArgs(findMany).skip).toBeUndefined();
|
||||
expect(lastArgs(findMany).take).toBeUndefined();
|
||||
// `limit` echoes the total so a caller cannot mistake it for a page size.
|
||||
expect(out.limit).toBe(out.total);
|
||||
});
|
||||
|
||||
it('orders newest first by default, and honours the other sorts', async () => {
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 1 });
|
||||
expect(lastArgs(findMany).orderBy).toEqual({ createdAt: 'desc' });
|
||||
await service.findProducts({ page: 1, sort: 'title' });
|
||||
expect(lastArgs(findMany).orderBy).toEqual({ title: 'asc' });
|
||||
await service.findProducts({ page: 1, sort: 'oldest' });
|
||||
expect(lastArgs(findMany).orderBy).toEqual({ createdAt: 'asc' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('filters reach the database', () => {
|
||||
it('passes category, brand and collection through', async () => {
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 1, categoryId: 'c1', brandId: 'b1', collectionId: 'col1' });
|
||||
expect(lastWhere(findMany)).toMatchObject({
|
||||
categoryId: 'c1',
|
||||
brandId: 'b1',
|
||||
collections: { some: { collectionId: 'col1' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('matches a gram range against the variants, as the app did', async () => {
|
||||
// A product qualifies when ANY of its variants is in range
|
||||
// (SearchView.vue:189-195), not when all of them are.
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 1, minGram: 2, maxGram: 8 });
|
||||
expect(lastWhere(findMany).variants).toEqual({
|
||||
some: { isActive: true, rashWeightGrams: { gte: 2, lte: 8 } },
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts an open-ended gram range', async () => {
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 1, minGram: 5 });
|
||||
expect(lastWhere(findMany).variants).toEqual({
|
||||
some: { isActive: true, rashWeightGrams: { gte: 5 } },
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores a blank search instead of searching for nothing', async () => {
|
||||
const { service, findMany } = setup();
|
||||
await service.findProducts({ page: 1, search: ' ' });
|
||||
expect(lastWhere(findMany).title).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('by ids — the cart, favourites and order items', () => {
|
||||
it('fetches only the named products and skips paging', async () => {
|
||||
const { service, findMany } = setup(2);
|
||||
const out = await service.findProducts({ ids: ['p0', 'p1'], page: 5, limit: 1 });
|
||||
expect(lastWhere(findMany)).toMatchObject({ id: { in: ['p0', 'p1'] } });
|
||||
expect(lastArgs(findMany).skip).toBeUndefined();
|
||||
expect(out.total).toBe(2);
|
||||
});
|
||||
|
||||
it('still hides an inactive or photo-less product asked for by id', async () => {
|
||||
const { service, findMany } = setup(1);
|
||||
await service.findProducts({ ids: ['p0'] });
|
||||
expect(lastWhere(findMany)).toMatchObject({ isActive: true, images: { some: {} } });
|
||||
});
|
||||
});
|
||||
|
||||
describe('one product by id', () => {
|
||||
it('returns it', async () => {
|
||||
const { service } = setup(1);
|
||||
await expect(service.findProductById('p0')).resolves.toMatchObject({ id: 'p0' });
|
||||
});
|
||||
|
||||
it('404s for a product that is not on the storefront', async () => {
|
||||
const { service, findMany } = setup(0);
|
||||
findMany.mockResolvedValue([]);
|
||||
await expect(service.findProductById('nope')).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('facets', () => {
|
||||
it('returns the filter options as small aggregates', async () => {
|
||||
// These used to be derived by scanning the whole catalogue in the browser,
|
||||
// which only works while the whole catalogue is in memory.
|
||||
const { service } = setup();
|
||||
const out = await service.facets();
|
||||
expect(out.categories).toEqual([{ id: 'c1', title: 'انگشتر' }]);
|
||||
expect(out.brands).toEqual([{ id: 'b1', title: 'برند' }]);
|
||||
expect(out.collections).toEqual([{ id: 'col1', title: 'مجموعه' }]);
|
||||
// Strings, like every other weight this API returns — Decimal, not float.
|
||||
expect(out.gramRange).toEqual({ min: '1.5', max: '12' });
|
||||
});
|
||||
|
||||
it('reports a zero range for an empty catalogue', async () => {
|
||||
const { service, prisma } = setup();
|
||||
prisma.productVariant.aggregate.mockResolvedValue({
|
||||
_min: { rashWeightGrams: null },
|
||||
_max: { rashWeightGrams: null },
|
||||
});
|
||||
await expect(service.facets()).resolves.toMatchObject({ gramRange: { min: '0', max: '0' } });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,58 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Query } from '@nestjs/common';
|
||||
import { CatalogService } from './catalog.service';
|
||||
import { Public } from '../auth/guards';
|
||||
import { ListProductsDto } from './dto/list-products.dto';
|
||||
|
||||
@Public() // pre-existing unauthenticated endpoint; see Phase 1 notes
|
||||
@Controller('catalog/products')
|
||||
export class CatalogController {
|
||||
constructor(private readonly catalog: CatalogService) {}
|
||||
|
||||
/**
|
||||
* The storefront list.
|
||||
*
|
||||
* Paging and filtering are OPT-IN, so this stays backward compatible: with no
|
||||
* query at all the caller gets the plain array it always got (the admin panel,
|
||||
* the smoke scripts and installed app builds all rely on that). Pass `page` or
|
||||
* `limit` and the answer becomes `{ rows, total, page, limit }`.
|
||||
*
|
||||
* Owner, 2026-10-12: the real catalogue reaches ~16k products, so a client
|
||||
* must be able to ask for one page — and the filters have to be applied here,
|
||||
* because a client cannot filter rows it was never sent.
|
||||
*/
|
||||
@Get()
|
||||
list() {
|
||||
return this.catalog.listProducts();
|
||||
list(@Query() query: ListProductsDto) {
|
||||
const asksForMore =
|
||||
query.page !== undefined ||
|
||||
query.limit !== undefined ||
|
||||
query.search !== undefined ||
|
||||
query.categoryId !== undefined ||
|
||||
query.brandId !== undefined ||
|
||||
query.collectionId !== undefined ||
|
||||
query.minGram !== undefined ||
|
||||
query.maxGram !== undefined ||
|
||||
query.sort !== undefined ||
|
||||
query.ids !== undefined;
|
||||
|
||||
// The bare call keeps its original shape — a plain array, from the cache.
|
||||
return asksForMore ? this.catalog.findProducts(query) : this.catalog.listProducts();
|
||||
}
|
||||
|
||||
/**
|
||||
* The filter options for the search sheet.
|
||||
*
|
||||
* Declared BEFORE `:id`: Nest matches in declaration order, so a literal
|
||||
* segment that could also read as a parameter has to come first or it is
|
||||
* never reached.
|
||||
*/
|
||||
@Get('facets')
|
||||
facets() {
|
||||
return this.catalog.facets();
|
||||
}
|
||||
|
||||
/** One product, for its own screen. */
|
||||
@Get(':id')
|
||||
byId(@Param('id') id: string) {
|
||||
return this.catalog.findProductById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { AccountingHooksService } from '../accounting-sync/accounting-hooks.service';
|
||||
import { CatalogCacheService } from './catalog-cache.service';
|
||||
import { DEFAULT_PAGE_SIZE, ListProductsDto, type ProductSort } from './dto/list-products.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CatalogService {
|
||||
@@ -11,17 +13,187 @@ export class CatalogService {
|
||||
private readonly catalogCache: CatalogCacheService,
|
||||
) {}
|
||||
|
||||
/** Served from CatalogCacheService; see there for what invalidates it. */
|
||||
/**
|
||||
* The whole active catalogue, unpaged.
|
||||
*
|
||||
* Kept exactly as it was for every existing caller (the admin panel, the smoke
|
||||
* scripts, installed app builds). New callers should use `findProducts` with a
|
||||
* page — the real catalogue reaches ~16k products, and this response carries
|
||||
* all of them with their variants, images and collections.
|
||||
*
|
||||
* Served from CatalogCacheService; see there for what invalidates it.
|
||||
*/
|
||||
listProducts() {
|
||||
return this.catalogCache.get(() => this.buildProductList());
|
||||
}
|
||||
|
||||
private async buildProductList() {
|
||||
/**
|
||||
* The storefront list, paged and filtered server-side.
|
||||
*
|
||||
* Owner, 2026-10-12: the catalogue reaches ~16k products, so returning it
|
||||
* whole is wrong. The filters live here rather than in the apps because a
|
||||
* client cannot filter data it has not been sent — paging the response while
|
||||
* leaving the filter on the client would silently search only the first page.
|
||||
*
|
||||
* Returns `{ rows, total, page, limit }`. With no `page`/`limit` the caller
|
||||
* gets everything that matches, so the filters are usable on their own.
|
||||
*
|
||||
* NOT cached, deliberately. CatalogCacheService holds one built response and
|
||||
* exists because the unpaged query is expensive and every screen asked for
|
||||
* it. A paged, filtered query is neither: it is bounded by `limit` and served
|
||||
* by Product_isActive_createdAt_idx. Caching it would mean a key per filter
|
||||
* combination — unbounded memory for queries that are already cheap, and a
|
||||
* second place for stale rows to hide. If this ever needs a cache it should
|
||||
* be a shared one (Redis), which is also what the single-instance note on
|
||||
* CatalogCacheService points at.
|
||||
*/
|
||||
async findProducts(query: ListProductsDto) {
|
||||
// Specific ids: the cart, favourites and order items each need a handful of
|
||||
// known products. Paging and the filters do not apply.
|
||||
if (query.ids?.length) {
|
||||
const rows = await this.buildProductList({ ids: query.ids });
|
||||
return { rows, total: rows.length, page: 1, limit: rows.length };
|
||||
}
|
||||
|
||||
const paged = query.page !== undefined || query.limit !== undefined;
|
||||
const limit = query.limit ?? DEFAULT_PAGE_SIZE;
|
||||
const page = query.page ?? 1;
|
||||
|
||||
const where = this.storefrontWhere(query);
|
||||
// Counted with the same `where`, so `total` describes the filtered set and
|
||||
// the app's "N محصول" and end-of-list detection stay truthful.
|
||||
const [total, rows] = await Promise.all([
|
||||
this.prisma.product.count({ where }),
|
||||
this.buildProductList({
|
||||
where,
|
||||
orderBy: this.orderFor(query.sort),
|
||||
...(paged ? { skip: (page - 1) * limit, take: limit } : {}),
|
||||
}),
|
||||
]);
|
||||
|
||||
return { rows, total, page: paged ? page : 1, limit: paged ? limit : total };
|
||||
}
|
||||
|
||||
/** One product for its own screen, instead of fetching the catalogue to find it. */
|
||||
async findProductById(id: string) {
|
||||
const [product] = await this.buildProductList({ where: { ...this.storefrontWhere({}), id } });
|
||||
if (!product) throw new NotFoundException('محصول یافت نشد');
|
||||
return product;
|
||||
}
|
||||
|
||||
/**
|
||||
* The filter options the search sheet offers.
|
||||
*
|
||||
* The apps derived these by scanning the whole catalogue in the browser
|
||||
* (SearchView.vue:167-181), which is only possible while the whole catalogue
|
||||
* is in memory. Each is a small aggregate here.
|
||||
*/
|
||||
async facets() {
|
||||
const where = this.storefrontWhere({});
|
||||
const [categories, brands, collections, gram] = await Promise.all([
|
||||
this.prisma.category.findMany({
|
||||
where: { products: { some: where } },
|
||||
select: { id: true, title: true },
|
||||
orderBy: { title: 'asc' },
|
||||
}),
|
||||
this.prisma.brand.findMany({
|
||||
where: { products: { some: where } },
|
||||
select: { id: true, title: true },
|
||||
orderBy: { title: 'asc' },
|
||||
}),
|
||||
this.prisma.collection.findMany({
|
||||
where: { products: { some: { product: where } } },
|
||||
select: { id: true, title: true },
|
||||
orderBy: { title: 'asc' },
|
||||
}),
|
||||
this.prisma.productVariant.aggregate({
|
||||
where: { isActive: true, product: where },
|
||||
_min: { rashWeightGrams: true },
|
||||
_max: { rashWeightGrams: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
categories,
|
||||
brands,
|
||||
collections,
|
||||
// Strings, like every other weight this API returns (Decimal, not float).
|
||||
gramRange: {
|
||||
min: gram._min.rashWeightGrams?.toString() ?? '0',
|
||||
max: gram._max.rashWeightGrams?.toString() ?? '0',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The two rules that decide whether a product is on the storefront at all,
|
||||
* plus the caller's filters. Shared so a filtered list, its count, the single
|
||||
* product route and the facets can never disagree about what is visible.
|
||||
*
|
||||
* D27 — a product with no image is never shown in the app. Products also
|
||||
* arrive from the accounting software, which carries no pictures; they appear
|
||||
* once an admin photographs them.
|
||||
*/
|
||||
private storefrontWhere(query: ListProductsDto): Prisma.ProductWhereInput {
|
||||
const where: Prisma.ProductWhereInput = { isActive: true, images: { some: {} } };
|
||||
|
||||
if (query.search?.trim()) {
|
||||
// `contains`, as the apps' own title filter did. Case-insensitive is a
|
||||
// no-op for Persian but matches how every other search in this API reads.
|
||||
where.title = { contains: query.search.trim(), mode: 'insensitive' };
|
||||
}
|
||||
if (query.categoryId) where.categoryId = query.categoryId;
|
||||
if (query.brandId) where.brandId = query.brandId;
|
||||
if (query.collectionId) where.collections = { some: { collectionId: query.collectionId } };
|
||||
|
||||
// A product is in range when at least ONE of its variants is, which is what
|
||||
// the app's `limit_gram` filter meant (SearchView.vue:189-195).
|
||||
if (query.minGram !== undefined || query.maxGram !== undefined) {
|
||||
where.variants = {
|
||||
some: {
|
||||
isActive: true,
|
||||
rashWeightGrams: {
|
||||
...(query.minGram !== undefined ? { gte: query.minGram } : {}),
|
||||
...(query.maxGram !== undefined ? { lte: query.maxGram } : {}),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return where;
|
||||
}
|
||||
|
||||
private orderFor(sort: ProductSort | undefined): Prisma.ProductOrderByWithRelationInput {
|
||||
switch (sort) {
|
||||
case 'oldest':
|
||||
return { createdAt: 'asc' };
|
||||
case 'title':
|
||||
return { title: 'asc' };
|
||||
default:
|
||||
// Newest first, and the half of Product_isActive_createdAt_idx the list
|
||||
// is indexed for.
|
||||
return { createdAt: 'desc' };
|
||||
}
|
||||
}
|
||||
|
||||
private async buildProductList(
|
||||
opts: {
|
||||
where?: Prisma.ProductWhereInput;
|
||||
orderBy?: Prisma.ProductOrderByWithRelationInput;
|
||||
skip?: number;
|
||||
take?: number;
|
||||
ids?: string[];
|
||||
} = {},
|
||||
) {
|
||||
const products = await this.prisma.product.findMany({
|
||||
// D27 — a product with no image is never shown in the app. Products now
|
||||
// also arrive from the accounting software, which carries no pictures;
|
||||
// they appear once an admin photographs them.
|
||||
where: { isActive: true, images: { some: {} } },
|
||||
// Defaults to the unfiltered storefront rule, so the unpaged
|
||||
// `listProducts()` above behaves exactly as before.
|
||||
where: opts.ids
|
||||
? { ...this.storefrontWhere({}), id: { in: opts.ids } }
|
||||
: (opts.where ?? { isActive: true, images: { some: {} } }),
|
||||
...(opts.orderBy ? { orderBy: opts.orderBy } : {}),
|
||||
...(opts.skip !== undefined ? { skip: opts.skip } : {}),
|
||||
...(opts.take !== undefined ? { take: opts.take } : {}),
|
||||
include: {
|
||||
// Only orderable variants reach the storefront. An inactive one used to
|
||||
// be listed and then rejected by getVariantOrThrow at checkout, so the
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { Transform, Type } from 'class-transformer';
|
||||
import { ArrayMaxSize, IsArray, IsIn, IsInt, IsNumber, IsOptional, IsString, Max, Min } from 'class-validator';
|
||||
|
||||
/** Page size when the caller asks for paging without naming one. */
|
||||
export const DEFAULT_PAGE_SIZE = 20;
|
||||
/**
|
||||
* Hard ceiling on `limit`.
|
||||
*
|
||||
* The real catalogue reaches ~16k products, so an unbounded `limit` would let
|
||||
* any caller ask for all of it — the exact query this whole change exists to
|
||||
* stop, just spelled differently. 100 is five of the apps' own pages.
|
||||
*/
|
||||
export const MAX_PAGE_SIZE = 100;
|
||||
/**
|
||||
* Ceiling on `ids`. The cart, favourites and an order's items are all small;
|
||||
* anything larger is a caller that should be paging instead.
|
||||
*/
|
||||
export const MAX_IDS = 100;
|
||||
|
||||
export type ProductSort = 'newest' | 'oldest' | 'title';
|
||||
|
||||
/**
|
||||
* Query for `GET /catalog/products`.
|
||||
*
|
||||
* Paging is OPT-IN: with neither `page` nor `limit` the route answers exactly as
|
||||
* it always did, with the whole catalogue, so the admin panel, the smoke scripts
|
||||
* and already-installed app builds keep working (plan §3-ب). The filters are
|
||||
* honoured either way.
|
||||
*/
|
||||
export class ListProductsDto {
|
||||
/** 1-based. Sending it (or `limit`) is what turns paging on. */
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
page?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@Max(MAX_PAGE_SIZE)
|
||||
limit?: number;
|
||||
|
||||
/** Free text over the product title. Matches the app's own client-side filter. */
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
search?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
categoryId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
brandId?: string;
|
||||
|
||||
/** Collection ("مجموعه") the product belongs to — the collection screen. */
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
collectionId?: string;
|
||||
|
||||
/**
|
||||
* Rash-weight range, matched against the product's VARIANTS: a product is in
|
||||
* range when at least one of its variants is, which is what the app's own
|
||||
* `limit_gram` filter did (SearchView.vue:189-195).
|
||||
*/
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
minGram?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
maxGram?: number;
|
||||
|
||||
/**
|
||||
* Specific ids, for the cart, favourites and order items — each needs a few
|
||||
* known products, not a page. Ignores paging and the filters above.
|
||||
*
|
||||
* Comma-separated so this stays a plain cacheable GET (`?ids=a,b,c`) rather
|
||||
* than needing a POST for a read. Capped, because the point of this parameter
|
||||
* is a handful of products: a caller that wants more should page.
|
||||
*/
|
||||
@IsOptional()
|
||||
@Transform(({ value }: { value: unknown }) =>
|
||||
typeof value === 'string'
|
||||
? value.split(',').map((s) => s.trim()).filter(Boolean).slice(0, MAX_IDS)
|
||||
: value,
|
||||
)
|
||||
@IsArray()
|
||||
@ArrayMaxSize(MAX_IDS)
|
||||
@IsString({ each: true })
|
||||
ids?: string[];
|
||||
|
||||
@IsOptional()
|
||||
@IsIn(['newest', 'oldest', 'title'])
|
||||
sort?: ProductSort;
|
||||
}
|
||||
Reference in New Issue
Block a user