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

WAITP-1298 Add EntityAncestors route #4690

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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/:hierarchyName/: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