Skip to content

Commit

Permalink
WAITP-1298 Add EntityAncestors route (#4690)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Caiger <caigertom@gmail.com>
  • Loading branch information
EMcQ-BES and tcaiger committed Jul 12, 2023
1 parent 4963921 commit 394fc22
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/tupaia-web-server/src/app/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
DashboardsRequest,
EntitiesRoute,
EntitiesRequest,
EntityAncestorsRoute,
EntityAncestorsRequest,
ReportRoute,
ReportRequest,
LegacyDashboardReportRoute,
Expand Down Expand Up @@ -66,6 +68,10 @@ export function createApp() {
handleWith(RequestCountryAccessRoute),
)
.get<EntitiesRequest>('entities/:projectCode/:rootEntityCode', handleWith(EntitiesRoute))
.get<EntityAncestorsRequest>(
'entityAncestors/:projectCode/:entityCode',
handleWith(EntityAncestorsRoute),
)
// TODO: Stop using get for logout, then delete this
.get<TempLogoutRequest>('logout', handleWith(TempLogoutRoute))
.build();
Expand Down
36 changes: 36 additions & 0 deletions packages/tupaia-web-server/src/routes/EntityAncestorsRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/

import { Request } from 'express';
import { Route } from '@tupaia/server-boilerplate';
import camelcaseKeys from 'camelcase-keys';

export type EntityAncestorsRequest = Request<any, any, any, any>;

const DEFAULT_FIELDS = ['parent_code', 'code', 'name', 'type'];

export class EntityAncestorsRoute extends Route<EntityAncestorsRequest> {
public async buildResponse() {
const { params, query, ctx } = this.req;
const { entityCode, projectCode } = params;
const { includeRootEntity = false, ...restOfQuery } = query;

const project = (
await ctx.services.central.fetchResources('projects', {
filter: { code: projectCode },
columns: ['entity_hierarchy.name'],
})
)[0];

const entities = await ctx.services.entity.getAncestorsOfEntity(
project['entity_hierarchy.name'],
entityCode,
{ fields: DEFAULT_FIELDS, ...restOfQuery },
includeRootEntity,
);

return camelcaseKeys(entities, { deep: true });
}
}
11 changes: 9 additions & 2 deletions packages/tupaia-web-server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

export { DashboardsRequest, DashboardsRoute } from './DashboardsRoute';
export { EntitiesRequest, EntitiesRoute } from './EntitiesRoute';
export { EntityAncestorsRequest, EntityAncestorsRoute } from './EntityAncestorsRoute';
export { ReportRequest, ReportRoute } from './ReportRoute';
export { LegacyDashboardReportRequest, LegacyDashboardReportRoute } from './LegacyDashboardReportRoute';
export { LegacyMapOverlayReportRequest, LegacyMapOverlayReportRoute } from './LegacyMapOverlayReportRoute';
export {
LegacyDashboardReportRequest,
LegacyDashboardReportRoute,
} from './LegacyDashboardReportRoute';
export {
LegacyMapOverlayReportRequest,
LegacyMapOverlayReportRoute,
} from './LegacyMapOverlayReportRoute';
export { MapOverlaysRequest, MapOverlaysRoute } from './MapOverlaysRoute';
export { UserRequest, UserRoute } from './UserRoute';
export { ProjectRequest, ProjectRoute } from './ProjectRoute';
Expand Down

0 comments on commit 394fc22

Please sign in to comment.