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:
@@ -4,6 +4,7 @@ import {
|
||||
CategoryUpdateSchema,
|
||||
SupplierCreateSchema,
|
||||
SupplierUpdateSchema,
|
||||
PaginationSchema,
|
||||
} from '@forte/shared/schemas'
|
||||
import { CategoryService, SupplierService } from '../../services/inventory.service.js'
|
||||
|
||||
@@ -20,8 +21,9 @@ export const inventoryRoutes: FastifyPluginAsync = async (app) => {
|
||||
})
|
||||
|
||||
app.get('/categories', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const list = await CategoryService.list(app.db, request.companyId)
|
||||
return reply.send(list)
|
||||
const params = PaginationSchema.parse(request.query)
|
||||
const result = await CategoryService.list(app.db, request.companyId, params)
|
||||
return reply.send(result)
|
||||
})
|
||||
|
||||
app.get('/categories/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
@@ -61,15 +63,9 @@ export const inventoryRoutes: FastifyPluginAsync = async (app) => {
|
||||
})
|
||||
|
||||
app.get('/suppliers', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const list = await SupplierService.list(app.db, request.companyId)
|
||||
return reply.send(list)
|
||||
})
|
||||
|
||||
app.get('/suppliers/search', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { q } = request.query as { q?: string }
|
||||
if (!q) return reply.status(400).send({ error: { message: 'Query parameter q is required', statusCode: 400 } })
|
||||
const results = await SupplierService.search(app.db, request.companyId, q)
|
||||
return reply.send(results)
|
||||
const params = PaginationSchema.parse(request.query)
|
||||
const result = await SupplierService.list(app.db, request.companyId, params)
|
||||
return reply.send(result)
|
||||
})
|
||||
|
||||
app.get('/suppliers/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
|
||||
Reference in New Issue
Block a user