Skip to content

Commit 6e92a29

Browse files
czmjmanelcecs
authored andcommitted
Add governingEntities to flowObjects response
1 parent 25f84fc commit 6e92a29

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

src/domain-services/flow-object/flow-object-service.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { groupBy } from 'lodash';
55
import { Service } from 'typedi';
66
import Context from '../Context';
77
import { GlobalClusterService } from '../global-cluster/global-cluster-service';
8+
import { GoverningEntityService } from '../governing-entity/governing-entity-service';
89
import { LocationService } from '../location/location-service';
910
import { OrganizationService } from '../organization/organization-service';
1011
import { PlanService } from '../plans/plan-service';
@@ -15,6 +16,7 @@ import { UsageYearService } from '../usage-year/usage-year-service';
1516
export class FlowObjectService {
1617
constructor(
1718
private globalClusterService: GlobalClusterService,
19+
private governingEntityService: GoverningEntityService,
1820
private locationService: LocationService,
1921
private projectService: ProjectService,
2022
private planService: PlanService,
@@ -49,6 +51,15 @@ export class FlowObjectService {
4951
),
5052
];
5153
}
54+
if (type === 'governingEntity') {
55+
return [
56+
'governingEntities',
57+
await this.governingEntityService.findByIds(
58+
context.models,
59+
flowObjects.map((fo) => fo.objectID)
60+
),
61+
];
62+
}
5263
if (type === 'location') {
5364
return [
5465
'locations',

src/domain-services/flow/graphql/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Brand } from '@unocha/hpc-api-core/src/util/types';
22
import { Field, ID, ObjectType } from 'type-graphql';
33
import { BaseTypeWithSoftDelete } from '../../base-types';
44
import GlobalCluster from '../../global-cluster/graphql/types';
5+
import GoverningEntity from '../../governing-entity/graphql/types';
56
import Location from '../../location/graphql/types';
67
import Organization from '../../organization/graphql/types';
78
import Plan from '../../plans/graphql/types';
@@ -13,6 +14,9 @@ export class FlowObjectsGroupedByType {
1314
@Field(() => [GlobalCluster], { nullable: true })
1415
globalClusters: GlobalCluster[];
1516

17+
@Field(() => [GoverningEntity], { nullable: true })
18+
governingEntities: GoverningEntity[];
19+
1620
@Field(() => [Location], { nullable: true })
1721
locations: Location[];
1822

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { GoverningEntityId } from '@unocha/hpc-api-core/src/db/models/governingEntity';
2+
import { Database } from '@unocha/hpc-api-core/src/db/type';
3+
import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
4+
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
5+
import { Service } from 'typedi';
6+
7+
@Service()
8+
export class GoverningEntityService {
9+
async findById(
10+
models: Database,
11+
id: number
12+
): Promise<InstanceDataOfModel<Database['governingEntity']>> {
13+
const governingEntity = await models.governingEntity.get(
14+
createBrandedValue(id)
15+
);
16+
17+
if (!governingEntity) {
18+
throw new Error(`Governing entity with ID ${id} does not exist`);
19+
}
20+
21+
return governingEntity;
22+
}
23+
24+
async findByIds(
25+
models: Database,
26+
ids: number[]
27+
): Promise<{ id: GoverningEntityId; name?: string | null }[]> {
28+
const governingEntities = await models.governingEntity.find({
29+
where: {
30+
id: {
31+
[models.Op.IN]: ids.map((id) => createBrandedValue(id)),
32+
},
33+
},
34+
});
35+
36+
const currentGoverningEntityVersions =
37+
await models.governingEntityVersion.find({
38+
where: {
39+
governingEntityId: {
40+
[models.Op.IN]: governingEntities.map((p) => p.id),
41+
},
42+
currentVersion: true,
43+
},
44+
});
45+
46+
return currentGoverningEntityVersions.map((gev) => ({
47+
id: gev.governingEntityId,
48+
name: gev.name,
49+
}));
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Database } from '@unocha/hpc-api-core/src/db/type';
2+
import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
3+
import { Arg, Ctx, Query, Resolver } from 'type-graphql';
4+
import { Service } from 'typedi';
5+
import Context from '../../Context';
6+
import { GoverningEntityService } from '../governing-entity-service';
7+
import GoverningEntity from './types';
8+
9+
@Service()
10+
@Resolver(GoverningEntity)
11+
export default class GoverningEntityResolver {
12+
constructor(private fieldClusterService: GoverningEntityService) {}
13+
14+
@Query(() => GoverningEntity)
15+
async fieldCluster(
16+
@Arg('id') id: number,
17+
@Ctx() context: Context
18+
): Promise<InstanceDataOfModel<Database['governingEntity']>> {
19+
return await this.fieldClusterService.findById(context.models, id);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Brand } from '@unocha/hpc-api-core/src/util/types';
2+
import { Field, ID, ObjectType } from 'type-graphql';
3+
import { BaseType } from '../../base-types';
4+
5+
@ObjectType()
6+
export default class GoverningEntity extends BaseType {
7+
@Field(() => ID)
8+
id: Brand<number, { readonly s: unique symbol }, 'Governing Entity ID'>;
9+
10+
@Field({ nullable: true })
11+
name?: string;
12+
}

0 commit comments

Comments
 (0)