Skip to content

Commit

Permalink
Add globalClusters to flowObjects response
Browse files Browse the repository at this point in the history
  • Loading branch information
czmj authored and manelcecs committed Nov 16, 2023
1 parent 0816cc7 commit 25f84fc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/domain-services/flow-object/flow-object-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { groupBy } from 'lodash';
import { Service } from 'typedi';
import Context from '../Context';
import { GlobalClusterService } from '../global-cluster/global-cluster-service';
import { LocationService } from '../location/location-service';
import { OrganizationService } from '../organization/organization-service';
import { PlanService } from '../plans/plan-service';
Expand All @@ -13,6 +14,7 @@ import { UsageYearService } from '../usage-year/usage-year-service';
@Service()
export class FlowObjectService {
constructor(
private globalClusterService: GlobalClusterService,
private locationService: LocationService,
private projectService: ProjectService,
private planService: PlanService,
Expand All @@ -38,6 +40,15 @@ export class FlowObjectService {
const typedObjects = await Promise.all(
Object.entries(groupBy(flowObjects, 'objectType')).map(
async ([type, flowObjects]) => {
if (type === 'globalCluster') {
return [
'globalClusters',
await this.globalClusterService.findByIds(
context.models,
flowObjects.map((fo) => fo.objectID)
),
];
}
if (type === 'location') {
return [
'locations',
Expand Down
4 changes: 4 additions & 0 deletions src/domain-services/flow/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Brand } from '@unocha/hpc-api-core/src/util/types';
import { Field, ID, ObjectType } from 'type-graphql';
import { BaseTypeWithSoftDelete } from '../../base-types';
import GlobalCluster from '../../global-cluster/graphql/types';
import Location from '../../location/graphql/types';
import Organization from '../../organization/graphql/types';
import Plan from '../../plans/graphql/types';
Expand All @@ -9,6 +10,9 @@ import UsageYear from '../../usage-year/graphql/types';

@ObjectType()
export class FlowObjectsGroupedByType {
@Field(() => [GlobalCluster], { nullable: true })
globalClusters: GlobalCluster[];

@Field(() => [Location], { nullable: true })
locations: Location[];

Expand Down
35 changes: 35 additions & 0 deletions src/domain-services/global-cluster/global-cluster-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Database } from '@unocha/hpc-api-core/src/db/type';
import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';

@Service()
export class GlobalClusterService {
async findById(
models: Database,
id: number
): Promise<InstanceDataOfModel<Database['globalCluster']>> {
const globalCluster = await models.globalCluster.get(
createBrandedValue(id)
);

if (!globalCluster) {
throw new Error(`Global cluster with ID ${id} does not exist`);
}

return globalCluster;
}

async findByIds(
models: Database,
ids: number[]
): Promise<InstanceDataOfModel<Database['globalCluster']>[]> {
return await models.globalCluster.find({
where: {
id: {
[models.Op.IN]: ids.map((id) => createBrandedValue(id)),
},
},
});
}
}
21 changes: 21 additions & 0 deletions src/domain-services/global-cluster/graphql/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Database } from '@unocha/hpc-api-core/src/db/type';
import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
import { Arg, Ctx, Query, Resolver } from 'type-graphql';
import { Service } from 'typedi';
import Context from '../../Context';
import { GlobalClusterService } from '../global-cluster-service';
import GlobalCluster from './types';

@Service()
@Resolver(GlobalCluster)
export default class GlobalClusterResolver {
constructor(private globalClusterService: GlobalClusterService) {}

@Query(() => GlobalCluster)
async globalCluster(
@Arg('id') id: number,
@Ctx() context: Context
): Promise<InstanceDataOfModel<Database['globalCluster']>> {
return await this.globalClusterService.findById(context.models, id);
}
}
12 changes: 12 additions & 0 deletions src/domain-services/global-cluster/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Brand } from '@unocha/hpc-api-core/src/util/types';
import { Field, ID, ObjectType } from 'type-graphql';
import { BaseType } from '../../base-types';

@ObjectType()
export default class GlobalCluster extends BaseType {
@Field(() => ID)
id: Brand<number, { readonly s: unique symbol }, 'Global cluster ID'>;

@Field({ nullable: true })
name?: string;
}

0 comments on commit 25f84fc

Please sign in to comment.