After the DC-005 module reorganization (41 files moved into src/ subdirs),
138 test suites failed because the refactor script's path-rewrite logic
missed three categories:
1. Files inside src/ doing 'require("./src/...")' — should be 'require("../...")'
2. Files in src/X/Y/ doing 'require("../../../src/...")' — should be 'require("../../...")'
3. Test files in __tests__/ with leftover 'require("../../../src/...")' paths
Root cause: the original refactor script ran before all files were moved,
so it computed relative paths against stale filesystem state.
Result:
- 30/30 test suites pass
- 879/879 tests pass (was: 18/30 suites, 614/687 tests)
Also fixed:
- routes/apps/restore.js: wrong responses import path
- routes/*/*.js: '../../src/utilities/X' → '../src/utilities/X' (depth 2 routes)
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
/**
|
|
* Pagination helper for list endpoints.
|
|
* Only paginates when ?page= or ?limit= query params are present (backward compat).
|
|
*
|
|
* Usage:
|
|
* const { paginate, parsePaginationParams } = require('./pagination');
|
|
* router.get('/items', asyncHandler(async (req, res) => {
|
|
* const items = await getAllItems();
|
|
* const params = parsePaginationParams(req.query);
|
|
* res.json({ success: true, ...paginate(items, params) });
|
|
* }));
|
|
*/
|
|
|
|
const DEFAULT_LIMIT = 50;
|
|
const MAX_LIMIT = 200;
|
|
|
|
/**
|
|
* Parse pagination params from query string.
|
|
* Returns null if no pagination requested (backward compat: return full list).
|
|
*/
|
|
function parsePaginationParams(query) {
|
|
if (!query.page && !query.limit) return null;
|
|
const page = Math.max(1, parseInt(query.page, 10) || 1);
|
|
const limit = Math.min(MAX_LIMIT, Math.max(1, parseInt(query.limit, 10) || DEFAULT_LIMIT));
|
|
return { page, limit };
|
|
}
|
|
|
|
/**
|
|
* Paginate an array of items.
|
|
* If params is null, returns { data: items } (no pagination metadata).
|
|
*/
|
|
function paginate(items, params) {
|
|
if (!params) return { data: items };
|
|
|
|
const { page, limit } = params;
|
|
const total = items.length;
|
|
const totalPages = Math.ceil(total / limit);
|
|
const start = (page - 1) * limit;
|
|
const data = items.slice(start, start + limit);
|
|
|
|
return {
|
|
data,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
totalPages,
|
|
hasMore: page < totalPages,
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = { paginate, parsePaginationParams, DEFAULT_LIMIT, MAX_LIMIT };
|