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:
@@ -1,4 +1,4 @@
|
||||
import { eq, and, ilike } from 'drizzle-orm'
|
||||
import { eq, and, count } from 'drizzle-orm'
|
||||
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
|
||||
import { categories, suppliers } from '../db/schema/inventory.js'
|
||||
import type {
|
||||
@@ -6,7 +6,14 @@ import type {
|
||||
CategoryUpdateInput,
|
||||
SupplierCreateInput,
|
||||
SupplierUpdateInput,
|
||||
PaginationInput,
|
||||
} from '@forte/shared/schemas'
|
||||
import {
|
||||
withPagination,
|
||||
withSort,
|
||||
buildSearchCondition,
|
||||
paginatedResponse,
|
||||
} from '../utils/pagination.js'
|
||||
|
||||
export const CategoryService = {
|
||||
async create(db: PostgresJsDatabase, companyId: string, input: CategoryCreateInput) {
|
||||
@@ -26,12 +33,31 @@ export const CategoryService = {
|
||||
return category ?? null
|
||||
},
|
||||
|
||||
async list(db: PostgresJsDatabase, companyId: string) {
|
||||
return db
|
||||
.select()
|
||||
.from(categories)
|
||||
.where(and(eq(categories.companyId, companyId), eq(categories.isActive, true)))
|
||||
.orderBy(categories.sortOrder)
|
||||
async list(db: PostgresJsDatabase, companyId: string, params: PaginationInput) {
|
||||
const baseWhere = and(eq(categories.companyId, companyId), eq(categories.isActive, true))
|
||||
|
||||
const searchCondition = params.q
|
||||
? buildSearchCondition(params.q, [categories.name])
|
||||
: undefined
|
||||
|
||||
const where = searchCondition ? and(baseWhere, searchCondition) : baseWhere
|
||||
|
||||
const sortableColumns: Record<string, typeof categories.name> = {
|
||||
name: categories.name,
|
||||
sort_order: categories.sortOrder,
|
||||
created_at: categories.createdAt,
|
||||
}
|
||||
|
||||
let query = db.select().from(categories).where(where).$dynamic()
|
||||
query = withSort(query, params.sort, params.order, sortableColumns, categories.sortOrder)
|
||||
query = withPagination(query, params.page, params.limit)
|
||||
|
||||
const [data, [{ total }]] = await Promise.all([
|
||||
query,
|
||||
db.select({ total: count() }).from(categories).where(where),
|
||||
])
|
||||
|
||||
return paginatedResponse(data, total, params.page, params.limit)
|
||||
},
|
||||
|
||||
async update(db: PostgresJsDatabase, companyId: string, id: string, input: CategoryUpdateInput) {
|
||||
@@ -71,22 +97,30 @@ export const SupplierService = {
|
||||
return supplier ?? null
|
||||
},
|
||||
|
||||
async list(db: PostgresJsDatabase, companyId: string) {
|
||||
return db
|
||||
.select()
|
||||
.from(suppliers)
|
||||
.where(and(eq(suppliers.companyId, companyId), eq(suppliers.isActive, true)))
|
||||
},
|
||||
async list(db: PostgresJsDatabase, companyId: string, params: PaginationInput) {
|
||||
const baseWhere = and(eq(suppliers.companyId, companyId), eq(suppliers.isActive, true))
|
||||
|
||||
async search(db: PostgresJsDatabase, companyId: string, query: string) {
|
||||
const pattern = `%${query}%`
|
||||
return db
|
||||
.select()
|
||||
.from(suppliers)
|
||||
.where(
|
||||
and(eq(suppliers.companyId, companyId), eq(suppliers.isActive, true), ilike(suppliers.name, pattern)),
|
||||
)
|
||||
.limit(50)
|
||||
const searchCondition = params.q
|
||||
? buildSearchCondition(params.q, [suppliers.name, suppliers.contactName, suppliers.email])
|
||||
: undefined
|
||||
|
||||
const where = searchCondition ? and(baseWhere, searchCondition) : baseWhere
|
||||
|
||||
const sortableColumns: Record<string, typeof suppliers.name> = {
|
||||
name: suppliers.name,
|
||||
created_at: suppliers.createdAt,
|
||||
}
|
||||
|
||||
let query = db.select().from(suppliers).where(where).$dynamic()
|
||||
query = withSort(query, params.sort, params.order, sortableColumns, suppliers.name)
|
||||
query = withPagination(query, params.page, params.limit)
|
||||
|
||||
const [data, [{ total }]] = await Promise.all([
|
||||
query,
|
||||
db.select({ total: count() }).from(suppliers).where(where),
|
||||
])
|
||||
|
||||
return paginatedResponse(data, total, params.page, params.limit)
|
||||
},
|
||||
|
||||
async update(db: PostgresJsDatabase, companyId: string, id: string, input: SupplierUpdateInput) {
|
||||
|
||||
Reference in New Issue
Block a user