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,8 +1,14 @@
import { eq, and, or, ilike } from 'drizzle-orm'
import { eq, and, sql, count } from 'drizzle-orm'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { accounts, members } from '../db/schema/accounts.js'
import type { AccountCreateInput, AccountUpdateInput } from '@forte/shared/schemas'
import type { AccountCreateInput, AccountUpdateInput, PaginationInput } from '@forte/shared/schemas'
import { isMinor } from '@forte/shared/utils'
import {
withPagination,
withSort,
buildSearchCondition,
paginatedResponse,
} from '../utils/pagination.js'
export const AccountService = {
async create(db: PostgresJsDatabase, companyId: string, input: AccountCreateInput) {
@@ -52,39 +58,49 @@ export const AccountService = {
return account ?? null
},
async search(db: PostgresJsDatabase, companyId: string, query: string) {
const pattern = `%${query}%`
const results = await db
.select()
.from(accounts)
.where(
and(
eq(accounts.companyId, companyId),
eq(accounts.isActive, true),
or(
ilike(accounts.name, pattern),
ilike(accounts.email, pattern),
ilike(accounts.phone, pattern),
ilike(accounts.accountNumber, pattern),
),
),
)
.limit(50)
async list(db: PostgresJsDatabase, companyId: string, params: PaginationInput) {
const baseWhere = and(eq(accounts.companyId, companyId), eq(accounts.isActive, true))
return results
},
const searchCondition = params.q
? buildSearchCondition(params.q, [accounts.name, accounts.email, accounts.phone, accounts.accountNumber])
: undefined
async list(db: PostgresJsDatabase, companyId: string) {
return db
.select()
.from(accounts)
.where(and(eq(accounts.companyId, companyId), eq(accounts.isActive, true)))
.limit(100)
const where = searchCondition ? and(baseWhere, searchCondition) : baseWhere
const sortableColumns: Record<string, typeof accounts.name> = {
name: accounts.name,
email: accounts.email,
created_at: accounts.createdAt,
account_number: accounts.accountNumber,
}
let query = db.select().from(accounts).where(where).$dynamic()
query = withSort(query, params.sort, params.order, sortableColumns, accounts.name)
query = withPagination(query, params.page, params.limit)
const [data, [{ total }]] = await Promise.all([
query,
db.select({ total: count() }).from(accounts).where(where),
])
return paginatedResponse(data, total, params.page, params.limit)
},
}
export const MemberService = {
async create(db: PostgresJsDatabase, companyId: string, input: { accountId: string; firstName: string; lastName: string; dateOfBirth?: string; email?: string; phone?: string; notes?: string }) {
async create(
db: PostgresJsDatabase,
companyId: string,
input: {
accountId: string
firstName: string
lastName: string
dateOfBirth?: string
email?: string
phone?: string
notes?: string
},
) {
const minor = input.dateOfBirth ? isMinor(input.dateOfBirth) : false
const [member] = await db
@@ -115,14 +131,45 @@ export const MemberService = {
return member ?? null
},
async listByAccount(db: PostgresJsDatabase, companyId: string, accountId: string) {
return db
.select()
.from(members)
.where(and(eq(members.companyId, companyId), eq(members.accountId, accountId)))
async listByAccount(
db: PostgresJsDatabase,
companyId: string,
accountId: string,
params: PaginationInput,
) {
const where = and(eq(members.companyId, companyId), eq(members.accountId, accountId))
const sortableColumns: Record<string, typeof members.firstName> = {
first_name: members.firstName,
last_name: members.lastName,
created_at: members.createdAt,
}
let query = db.select().from(members).where(where).$dynamic()
query = withSort(query, params.sort, params.order, sortableColumns, members.lastName)
query = withPagination(query, params.page, params.limit)
const [data, [{ total }]] = await Promise.all([
query,
db.select({ total: count() }).from(members).where(where),
])
return paginatedResponse(data, total, params.page, params.limit)
},
async update(db: PostgresJsDatabase, companyId: string, id: string, input: { firstName?: string; lastName?: string; dateOfBirth?: string; email?: string; phone?: string; notes?: string }) {
async update(
db: PostgresJsDatabase,
companyId: string,
id: string,
input: {
firstName?: string
lastName?: string
dateOfBirth?: string
email?: string
phone?: string
notes?: string
},
) {
const updates: Record<string, unknown> = { ...input, updatedAt: new Date() }
if (input.dateOfBirth) {
updates.isMinor = isMinor(input.dateOfBirth)