diff --git a/packages/tupaia-web-server/src/app/createApp.ts b/packages/tupaia-web-server/src/app/createApp.ts index 1b1394fc59..d3dbd688e1 100644 --- a/packages/tupaia-web-server/src/app/createApp.ts +++ b/packages/tupaia-web-server/src/app/createApp.ts @@ -40,6 +40,8 @@ import { EntitySearchRoute, EntitySearchRequest, RequestCountryAccessRequest, + EntityRoute, + EntityRequest, } from '../routes'; const { WEB_CONFIG_API_URL = 'http://localhost:8000/api/v1' } = process.env; @@ -69,6 +71,7 @@ export function createApp() { 'requestCountryAccess', handleWith(RequestCountryAccessRoute), ) + .get('entity/:projectCode/:entityCode', handleWith(EntityRoute)) .get('entities/:projectCode/:rootEntityCode', handleWith(EntitiesRoute)) .get('entitySearch/:projectCode', handleWith(EntitySearchRoute)) .get('entities/:projectCode/:rootEntityCode', handleWith(EntitiesRoute)) diff --git a/packages/tupaia-web-server/src/routes/EntityRoute.ts b/packages/tupaia-web-server/src/routes/EntityRoute.ts new file mode 100644 index 0000000000..7bc97492f4 --- /dev/null +++ b/packages/tupaia-web-server/src/routes/EntityRoute.ts @@ -0,0 +1,27 @@ +/** + * 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'; + +// TODO: WAITP-1278 Move this to types +export type EntityRequest = Request; + +const DEFAULT_FIELDS = ['parent_code', 'code', 'name', 'type']; + +export class EntityRoute extends Route { + public async buildResponse() { + const { params, query, ctx } = this.req; + const { projectCode, entityCode } = params; + + const entity = await ctx.services.entity.getEntity(projectCode, entityCode, { + fields: DEFAULT_FIELDS, + ...query, + }); + + return camelCaseKeys(entity); + } +} diff --git a/packages/tupaia-web-server/src/routes/index.ts b/packages/tupaia-web-server/src/routes/index.ts index 3fa1ae18da..66863d469b 100644 --- a/packages/tupaia-web-server/src/routes/index.ts +++ b/packages/tupaia-web-server/src/routes/index.ts @@ -4,6 +4,7 @@ */ export { DashboardsRequest, DashboardsRoute } from './DashboardsRoute'; +export { EntityRequest, EntityRoute } from './EntityRoute'; export { EntitiesRequest, EntitiesRoute } from './EntitiesRoute'; export { EntitySearchRequest, EntitySearchRoute } from './EntitySearchRoute'; export { EntityAncestorsRequest, EntityAncestorsRoute } from './EntityAncestorsRoute';