2021-02-01 14:41:25 +02:00
|
|
|
import { ModelType } from '../../../BaseModel';
|
2020-11-11 13:52:47 +02:00
|
|
|
import { ModelFeedPage } from '../../../models/utils/paginatedFeed';
|
2020-11-17 11:53:50 +02:00
|
|
|
import { PaginationOrderDir } from '../../../models/utils/types';
|
2020-11-11 13:52:47 +02:00
|
|
|
import { Request } from '../Api';
|
|
|
|
import requestPaginationOptions from '../utils/requestPaginationOptions';
|
2021-02-01 14:41:25 +02:00
|
|
|
import requestFields from './requestFields';
|
2020-11-11 13:52:47 +02:00
|
|
|
|
2020-11-17 11:53:50 +02:00
|
|
|
// Note that this is inefficient pagination as it requires all the items to
|
|
|
|
// have been loaded first, even if not all of them are returned.
|
|
|
|
//
|
|
|
|
// It's however convenient for smaller lists as it reduces the need for
|
|
|
|
// building complex SQL queries.
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-02-01 14:41:25 +02:00
|
|
|
export default function(itemType: ModelType, items: any[], request: Request): ModelFeedPage {
|
|
|
|
const fields = requestFields(request, itemType);
|
2020-11-11 13:52:47 +02:00
|
|
|
const pagination = requestPaginationOptions(request);
|
|
|
|
const startIndex = (pagination.page - 1) * pagination.limit;
|
|
|
|
const itemCount = Math.min(items.length - startIndex, pagination.limit);
|
|
|
|
const hasMore = itemCount >= pagination.limit;
|
|
|
|
|
2020-11-17 11:53:50 +02:00
|
|
|
const sortBy = pagination.order[0].by;
|
|
|
|
const sortDir = pagination.order[0].dir;
|
|
|
|
const caseInsensitive = pagination.order[0].caseInsensitive;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-02-01 14:41:25 +02:00
|
|
|
const sortedItems = items.slice().map((item: any) => {
|
|
|
|
if (!fields.length) return item;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-02-01 14:41:25 +02:00
|
|
|
const newItem: any = {};
|
|
|
|
for (const k of Object.keys(item)) {
|
|
|
|
if (!fields.includes(k)) continue;
|
|
|
|
newItem[k] = item[k];
|
|
|
|
}
|
|
|
|
return newItem;
|
|
|
|
});
|
2020-11-17 11:53:50 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-17 11:53:50 +02:00
|
|
|
sortedItems.sort((a: any, b: any) => {
|
|
|
|
let v1 = a && (sortBy in a) ? a[sortBy] : '';
|
|
|
|
let v2 = b && (sortBy in b) ? b[sortBy] : '';
|
|
|
|
if (caseInsensitive && typeof v1 === 'string') v1 = v1.toLowerCase();
|
|
|
|
if (caseInsensitive && typeof v2 === 'string') v2 = v2.toLowerCase();
|
|
|
|
|
|
|
|
if (sortDir === PaginationOrderDir.ASC) {
|
|
|
|
if (v1 < v2) return -1;
|
|
|
|
if (v2 > v1) return +1;
|
|
|
|
} else {
|
|
|
|
if (v1 > v2) return -1;
|
|
|
|
if (v2 < v1) return +1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2020-11-11 13:52:47 +02:00
|
|
|
return {
|
2020-11-17 11:53:50 +02:00
|
|
|
items: sortedItems.slice(startIndex, startIndex + itemCount),
|
2020-11-11 13:52:47 +02:00
|
|
|
has_more: hasMore,
|
|
|
|
};
|
|
|
|
}
|