Refactor all list APIs for server-side pagination, search, and sort

All list endpoints now return paginated responses:
  { data: [...], pagination: { page, limit, total, totalPages } }

Query params: ?page=1&limit=25&q=search&sort=name&order=asc

Changes:
- Added PaginationSchema in @forte/shared for consistent param parsing
- Added pagination utils (withPagination, withSort, buildSearchCondition,
  paginatedResponse) in backend
- Refactored all services: AccountService, MemberService, CategoryService,
  SupplierService, ProductService, InventoryUnitService
- Merged separate /search endpoints into list endpoints via ?q= param
- Removed AccountSearchSchema and ProductSearchSchema (replaced by
  PaginationSchema)
- Added pagination test (5 items, page 1 limit 2, expect totalPages=3)
- Updated CLAUDE.md with API conventions
- 34 tests passing
This commit is contained in:
Ryan Moon
2026-03-27 19:53:59 -05:00
parent c34ad27b86
commit 750dcf4046
13 changed files with 448 additions and 265 deletions

View File

@@ -1,4 +1,4 @@
import { eq, and, or, ilike } from 'drizzle-orm'
import { eq, and, count } from 'drizzle-orm'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { products, inventoryUnits, priceHistory } from '../db/schema/inventory.js'
import type {
@@ -6,7 +6,14 @@ import type {
ProductUpdateInput,
InventoryUnitCreateInput,
InventoryUnitUpdateInput,
PaginationInput,
} from '@forte/shared/schemas'
import {
withPagination,
withSort,
buildSearchCondition,
paginatedResponse,
} from '../utils/pagination.js'
export const ProductService = {
async create(db: PostgresJsDatabase, companyId: string, input: ProductCreateInput) {
@@ -32,36 +39,42 @@ export const ProductService = {
return product ?? null
},
async list(db: PostgresJsDatabase, companyId: string) {
return db
.select()
.from(products)
.where(and(eq(products.companyId, companyId), eq(products.isActive, true)))
.limit(100)
async list(db: PostgresJsDatabase, companyId: string, params: PaginationInput) {
const baseWhere = and(eq(products.companyId, companyId), eq(products.isActive, true))
const searchCondition = params.q
? buildSearchCondition(params.q, [products.name, products.sku, products.upc, products.brand])
: undefined
const where = searchCondition ? and(baseWhere, searchCondition) : baseWhere
const sortableColumns: Record<string, typeof products.name> = {
name: products.name,
sku: products.sku,
brand: products.brand,
price: products.price,
created_at: products.createdAt,
}
let query = db.select().from(products).where(where).$dynamic()
query = withSort(query, params.sort, params.order, sortableColumns, products.name)
query = withPagination(query, params.page, params.limit)
const [data, [{ total }]] = await Promise.all([
query,
db.select({ total: count() }).from(products).where(where),
])
return paginatedResponse(data, total, params.page, params.limit)
},
async search(db: PostgresJsDatabase, companyId: string, query: string) {
const pattern = `%${query}%`
return db
.select()
.from(products)
.where(
and(
eq(products.companyId, companyId),
eq(products.isActive, true),
or(
ilike(products.name, pattern),
ilike(products.sku, pattern),
ilike(products.upc, pattern),
ilike(products.brand, pattern),
),
),
)
.limit(50)
},
async update(db: PostgresJsDatabase, companyId: string, id: string, input: ProductUpdateInput, changedBy?: string) {
// Log price changes before updating
async update(
db: PostgresJsDatabase,
companyId: string,
id: string,
input: ProductUpdateInput,
changedBy?: string,
) {
if (input.price !== undefined || input.minPrice !== undefined) {
const existing = await this.getById(db, companyId, id)
if (existing) {
@@ -129,13 +142,34 @@ export const InventoryUnitService = {
return unit ?? null
},
async listByProduct(db: PostgresJsDatabase, companyId: string, productId: string) {
return db
.select()
.from(inventoryUnits)
.where(
and(eq(inventoryUnits.companyId, companyId), eq(inventoryUnits.productId, productId)),
)
async listByProduct(
db: PostgresJsDatabase,
companyId: string,
productId: string,
params: PaginationInput,
) {
const where = and(
eq(inventoryUnits.companyId, companyId),
eq(inventoryUnits.productId, productId),
)
const sortableColumns: Record<string, typeof inventoryUnits.serialNumber> = {
serial_number: inventoryUnits.serialNumber,
status: inventoryUnits.status,
condition: inventoryUnits.condition,
created_at: inventoryUnits.createdAt,
}
let query = db.select().from(inventoryUnits).where(where).$dynamic()
query = withSort(query, params.sort, params.order, sortableColumns, inventoryUnits.createdAt)
query = withPagination(query, params.page, params.limit)
const [data, [{ total }]] = await Promise.all([
query,
db.select({ total: count() }).from(inventoryUnits).where(where),
])
return paginatedResponse(data, total, params.page, params.limit)
},
async update(