Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datatrakWeb): Limit entity results to 100 #5847

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/api-client/src/connections/EntityApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,28 @@ export class EntityApi extends BaseApi {
field?: string;
fields?: string[];
filter?: any;
pageSize?: number;
},
includeRootEntity = false,
isPublic = false,
) {
return this.connection.get(`hierarchy/${hierarchyName}/${entityCode}/descendants`, {
...this.stringifyQueryParameters(queryOptions),
const { pageSize, ...otherQueryOptions } = queryOptions || {};
const params: {
pageSize?: number;
includeRootEntity: string;
isPublic: string;
field?: string;
fields?: string;
filter?: string;
} = {
...this.stringifyQueryParameters(otherQueryOptions),
includeRootEntity: `${includeRootEntity}`,
isPublic: `${isPublic}`,
});
};
if (pageSize) {
params.pageSize = pageSize;
}
return this.connection.get(`hierarchy/${hierarchyName}/${entityCode}/descendants`, params);
}

public async getDescendantsOfEntities(
Expand Down
23 changes: 15 additions & 8 deletions packages/database/src/modelClasses/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ export class EntityRecord extends DatabaseRecord {
return this.model.getAncestorsOfEntities(hierarchyId, [this.id], criteria);
}

async getDescendants(hierarchyId, criteria) {
return this.model.getDescendantsOfEntities(hierarchyId, [this.id], criteria);
async getDescendants(hierarchyId, criteria, options) {
return this.model.getDescendantsOfEntities(hierarchyId, [this.id], criteria, options);
}

async getRelatives(hierarchyId, criteria) {
Expand Down Expand Up @@ -442,9 +442,10 @@ export class EntityModel extends MaterializedViewLogDatabaseModel {
* @param {*} ancestorsOrDescendants
* @param {*} entityIds
* @param {*} criteria
* @param {*} options
* @returns {Promise<EntityRecord[]>}
*/
async getRelationsOfEntities(ancestorsOrDescendants, entityIds, criteria) {
async getRelationsOfEntities(ancestorsOrDescendants, entityIds, criteria, options) {
const cacheKey = this.getCacheKey(this.getRelationsOfEntities.name, arguments);
const [joinTablesOn, filterByEntityId] =
ancestorsOrDescendants === ENTITY_RELATION_TYPE.ANCESTORS
Expand All @@ -461,6 +462,7 @@ export class EntityModel extends MaterializedViewLogDatabaseModel {
joinWith: RECORDS.ANCESTOR_DESCENDANT_RELATION,
joinCondition: ['entity.id', joinTablesOn],
sort: ['generational_distance ASC'],
...options,
},
);
const relationData = await Promise.all(relations.map(async r => r.getData()));
Expand All @@ -477,11 +479,16 @@ export class EntityModel extends MaterializedViewLogDatabaseModel {
});
}

async getDescendantsOfEntities(hierarchyId, entityIds, criteria) {
return this.getRelationsOfEntities(ENTITY_RELATION_TYPE.DESCENDANTS, entityIds, {
entity_hierarchy_id: hierarchyId,
...criteria,
});
async getDescendantsOfEntities(hierarchyId, entityIds, criteria, options) {
return this.getRelationsOfEntities(
ENTITY_RELATION_TYPE.DESCENDANTS,
entityIds,
{
entity_hierarchy_id: hierarchyId,
...criteria,
},
options,
);
}

async getRelativesOfEntities(hierarchyId, entityIds, criteria) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class EntityDescendantsRoute extends Route<EntityDescendantsRequest> {
filter: { countryCode, projectCode, grandparentId, parentId, type, ...restOfFilter },
searchString,
fields = DEFAULT_FIELDS,
pageSize,
} = query;

if (isLoggedIn) {
Expand Down Expand Up @@ -108,6 +109,7 @@ export class EntityDescendantsRoute extends Route<EntityDescendantsRequest> {
{
fields,
filter,
pageSize,
},
false,
!isLoggedIn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const useSearchResults = (searchValue, filter, projectCode, disableSearch = fals
fields: ['id', 'parent_name', 'code', 'name', 'type'],
filter,
searchString: debouncedSearch,
pageSize: 100,
},
!disableSearch,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ describe('descendants', () => {
expect(entities).toBeArray();
expect(entities).toIncludeSameMembers(getEntitiesWithFields([], ['code', 'name', 'type']));
});

it('can limit by page size', async () => {
const { body: entities } = await app.get('hierarchy/redblue/KANTO/descendants', {
query: {
fields: 'code,name',
filter: 'type==city',
pageSize: 5,
},
});

expect(entities).toBeArray();
expect(entities.length).toBe(5);
});
});

describe('/hierarchy/:hierarchyName/descendants', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ export type DescendantsRequest = SingleEntityRequest<
SingleEntityRequestParams,
EntityResponse[],
RequestBody,
EntityRequestQuery & { includeRootEntity?: string }
EntityRequestQuery & { includeRootEntity?: string; pageSize?: number }
>;
export class EntityDescendantsRoute extends Route<DescendantsRequest> {
public async buildResponse() {
const { hierarchyId, entity, fields, field, filter } = this.req.ctx;
const { includeRootEntity: includeRootEntityString = 'false' } = this.req.query;
const { includeRootEntity: includeRootEntityString = 'false', pageSize } = this.req.query;
const includeRootEntity = includeRootEntityString?.toLowerCase() === 'true';
const descendants = await entity.getDescendants(hierarchyId, {
...filter,
});
const descendants = await entity.getDescendants(
hierarchyId,
{
...filter,
},
{
limit: pageSize,
},
);
const responseEntities = includeRootEntity ? [entity].concat(descendants) : descendants;

return formatEntitiesForResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/
import { NextFunction, Request, Response } from 'express';
import { PermissionsError } from '@tupaia/utils';
import { extractEntityFieldsFromQuery, extractEntityFieldFromQuery } from './fields';
import { CommonContext } from '../types';
import { extractEntityFieldsFromQuery, extractEntityFieldFromQuery } from './fields';

const throwNoAccessError = (hierarchyName: string) => {
throw new PermissionsError(`No access to requested hierarchy: ${hierarchyName}`);
Expand Down
1 change: 1 addition & 0 deletions packages/entity-server/src/routes/hierarchy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type CommonContext = {
fields: ExtendedEntityFieldName[];
filter: EntityFilter;
field?: FlattableEntityFieldName;
pageSize?: string;
};

export interface SingleEntityContext extends CommonContext {
Expand Down
6 changes: 5 additions & 1 deletion packages/server-boilerplate/src/models/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export type EntityFilter = DbFilter<EntityFilterFields>;
export interface EntityRecord extends Entity, BaseEntityRecord {
getChildren: (hierarchyId: string, criteria?: EntityFilter) => Promise<EntityRecord[]>;
getParent: (hierarchyId: string) => Promise<EntityRecord | undefined>;
getDescendants: (hierarchyId: string, criteria?: EntityFilter) => Promise<EntityRecord[]>;
getDescendants: (
hierarchyId: string,
criteria?: EntityFilter,
options?: Record<string, unknown>,
) => Promise<EntityRecord[]>;
getAncestors: (hierarchyId: string, criteria?: EntityFilter) => Promise<EntityRecord[]>;
getAncestorOfType: (hierarchyId: string, type: string) => Promise<EntityRecord | undefined>;
getRelatives: (hierarchyId: string, criteria?: EntityFilter) => Promise<EntityRecord[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export type ReqQuery = {
type?: string;
};
searchString?: string;
pageSize?: number;
};
Loading