-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WAITP-1298 Add EntityAncestors route (#4690)
Co-authored-by: Tom Caiger <caigertom@gmail.com>
- Loading branch information
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/tupaia-web-server/src/routes/EntityAncestorsRoute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters