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:
@@ -84,11 +84,13 @@ describe('Account routes', () => {
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
const body = response.json()
|
||||
expect(body.length).toBe(2)
|
||||
expect(body.data.length).toBe(2)
|
||||
expect(body.pagination.total).toBe(2)
|
||||
expect(body.pagination.page).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /v1/accounts/search', () => {
|
||||
describe('GET /v1/accounts?q=', () => {
|
||||
it('searches by name', async () => {
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
@@ -105,14 +107,14 @@ describe('Account routes', () => {
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts/search?q=johnson',
|
||||
url: '/v1/accounts?q=johnson',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
const body = response.json()
|
||||
expect(body.length).toBe(1)
|
||||
expect(body[0].name).toBe('Johnson Family')
|
||||
expect(body.data.length).toBe(1)
|
||||
expect(body.data[0].name).toBe('Johnson Family')
|
||||
})
|
||||
|
||||
it('searches by phone', async () => {
|
||||
@@ -125,12 +127,35 @@ describe('Account routes', () => {
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts/search?q=867-5309',
|
||||
url: '/v1/accounts?q=867-5309',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
expect(response.json().length).toBe(1)
|
||||
expect(response.json().data.length).toBe(1)
|
||||
})
|
||||
|
||||
it('paginates results', async () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: `Account ${i}` },
|
||||
})
|
||||
}
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts?page=1&limit=2',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
const body = response.json()
|
||||
expect(body.data.length).toBe(2)
|
||||
expect(body.pagination.total).toBe(5)
|
||||
expect(body.pagination.totalPages).toBe(3)
|
||||
expect(body.pagination.page).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -180,7 +205,7 @@ describe('Account routes', () => {
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
expect(listRes.json().length).toBe(0)
|
||||
expect(listRes.json().data.length).toBe(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -274,7 +299,7 @@ describe('Member routes', () => {
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
expect(response.json().length).toBe(2)
|
||||
expect(response.json().data.length).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user