Skip to content

Commit 25f84fc

Browse files
czmjmanelcecs
authored andcommitted
Add globalClusters to flowObjects response
1 parent 0816cc7 commit 25f84fc

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
44
import { groupBy } from 'lodash';
55
import { Service } from 'typedi';
66
import Context from '../Context';
7+
import { GlobalClusterService } from '../global-cluster/global-cluster-service';
78
import { LocationService } from '../location/location-service';
89
import { OrganizationService } from '../organization/organization-service';
910
import { PlanService } from '../plans/plan-service';
@@ -13,6 +14,7 @@ import { UsageYearService } from '../usage-year/usage-year-service';
1314
@Service()
1415
export class FlowObjectService {
1516
constructor(
17+
private globalClusterService: GlobalClusterService,
1618
private locationService: LocationService,
1719
private projectService: ProjectService,
1820
private planService: PlanService,
@@ -38,6 +40,15 @@ export class FlowObjectService {
3840
const typedObjects = await Promise.all(
3941
Object.entries(groupBy(flowObjects, 'objectType')).map(
4042
async ([type, flowObjects]) => {
43+
if (type === 'globalCluster') {
44+
return [
45+
'globalClusters',
46+
await this.globalClusterService.findByIds(
47+
context.models,
48+
flowObjects.map((fo) => fo.objectID)
49+
),
50+
];
51+
}
4152
if (type === 'location') {
4253
return [
4354
'locations',

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

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Brand } from '@unocha/hpc-api-core/src/util/types';
22
import { Field, ID, ObjectType } from 'type-graphql';
33
import { BaseTypeWithSoftDelete } from '../../base-types';
4+
import GlobalCluster from '../../global-cluster/graphql/types';
45
import Location from '../../location/graphql/types';
56
import Organization from '../../organization/graphql/types';
67
import Plan from '../../plans/graphql/types';
@@ -9,6 +10,9 @@ import UsageYear from '../../usage-year/graphql/types';
910

1011
@ObjectType()
1112
export class FlowObjectsGroupedByType {
13+
@Field(() => [GlobalCluster], { nullable: true })
14+
globalClusters: GlobalCluster[];
15+
1216
@Field(() => [Location], { nullable: true })
1317
locations: Location[];
1418

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
4+
import { Service } from 'typedi';
5+
6+
@Service()
7+
export class GlobalClusterService {
8+
async findById(
9+
models: Database,
10+
id: number
11+
): Promise<InstanceDataOfModel<Database['globalCluster']>> {
12+
const globalCluster = await models.globalCluster.get(
13+
createBrandedValue(id)
14+
);
15+
16+
if (!globalCluster) {
17+
throw new Error(`Global cluster with ID ${id} does not exist`);
18+
}
19+
20+
return globalCluster;
21+
}
22+
23+
async findByIds(
24+
models: Database,
25+
ids: number[]
26+
): Promise<InstanceDataOfModel<Database['globalCluster']>[]> {
27+
return await models.globalCluster.find({
28+
where: {
29+
id: {
30+
[models.Op.IN]: ids.map((id) => createBrandedValue(id)),
31+
},
32+
},
33+
});
34+
}
35+
}
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 { GlobalClusterService } from '../global-cluster-service';
7+
import GlobalCluster from './types';
8+
9+
@Service()
10+
@Resolver(GlobalCluster)
11+
export default class GlobalClusterResolver {
12+
constructor(private globalClusterService: GlobalClusterService) {}
13+
14+
@Query(() => GlobalCluster)
15+
async globalCluster(
16+
@Arg('id') id: number,
17+
@Ctx() context: Context
18+
): Promise<InstanceDataOfModel<Database['globalCluster']>> {
19+
return await this.globalClusterService.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 GlobalCluster extends BaseType {
7+
@Field(() => ID)
8+
id: Brand<number, { readonly s: unique symbol }, 'Global cluster ID'>;
9+
10+
@Field({ nullable: true })
11+
name?: string;
12+
}

0 commit comments

Comments
 (0)