diff --git a/packages/tupaia-web-server/src/app/createApp.ts b/packages/tupaia-web-server/src/app/createApp.ts index 7d76ab264a..f3db76dfc2 100644 --- a/packages/tupaia-web-server/src/app/createApp.ts +++ b/packages/tupaia-web-server/src/app/createApp.ts @@ -18,6 +18,8 @@ import { DashboardsRequest, EntitiesRoute, EntitiesRequest, + EntityAncestorsRoute, + EntityAncestorsRequest, ReportRoute, ReportRequest, LegacyDashboardReportRoute, @@ -66,6 +68,10 @@ export function createApp() { handleWith(RequestCountryAccessRoute), ) .get('entities/:projectCode/:rootEntityCode', handleWith(EntitiesRoute)) + .get( + 'entityAncestors/:projectCode/:entityCode', + handleWith(EntityAncestorsRoute), + ) // TODO: Stop using get for logout, then delete this .get('logout', handleWith(TempLogoutRoute)) .build(); diff --git a/packages/tupaia-web-server/src/routes/EntityAncestorsRoute.ts b/packages/tupaia-web-server/src/routes/EntityAncestorsRoute.ts new file mode 100644 index 0000000000..d74ad9a7c6 --- /dev/null +++ b/packages/tupaia-web-server/src/routes/EntityAncestorsRoute.ts @@ -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; + +const DEFAULT_FIELDS = ['parent_code', 'code', 'name', 'type']; + +export class EntityAncestorsRoute extends Route { + 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 }); + } +} diff --git a/packages/tupaia-web-server/src/routes/index.ts b/packages/tupaia-web-server/src/routes/index.ts index 0c094b5141..68a30c8611 100644 --- a/packages/tupaia-web-server/src/routes/index.ts +++ b/packages/tupaia-web-server/src/routes/index.ts @@ -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';