From 25f84fc3c6d3ebf92acbb73c4a387447e2be0447 Mon Sep 17 00:00:00 2001 From: Clara Jordan Date: Mon, 19 Sep 2022 19:43:36 +0100 Subject: [PATCH] Add globalClusters to flowObjects response --- .../flow-object/flow-object-service.ts | 11 ++++++ src/domain-services/flow/graphql/types.ts | 4 +++ .../global-cluster/global-cluster-service.ts | 35 +++++++++++++++++++ .../global-cluster/graphql/resolver.ts | 21 +++++++++++ .../global-cluster/graphql/types.ts | 12 +++++++ 5 files changed, 83 insertions(+) create mode 100644 src/domain-services/global-cluster/global-cluster-service.ts create mode 100644 src/domain-services/global-cluster/graphql/resolver.ts create mode 100644 src/domain-services/global-cluster/graphql/types.ts diff --git a/src/domain-services/flow-object/flow-object-service.ts b/src/domain-services/flow-object/flow-object-service.ts index 9cc78d60..68fe44b5 100644 --- a/src/domain-services/flow-object/flow-object-service.ts +++ b/src/domain-services/flow-object/flow-object-service.ts @@ -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'; @@ -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, @@ -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', diff --git a/src/domain-services/flow/graphql/types.ts b/src/domain-services/flow/graphql/types.ts index cf4994a4..cb0d6bdd 100644 --- a/src/domain-services/flow/graphql/types.ts +++ b/src/domain-services/flow/graphql/types.ts @@ -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'; @@ -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[]; diff --git a/src/domain-services/global-cluster/global-cluster-service.ts b/src/domain-services/global-cluster/global-cluster-service.ts new file mode 100644 index 00000000..a82fef04 --- /dev/null +++ b/src/domain-services/global-cluster/global-cluster-service.ts @@ -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> { + 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[]> { + return await models.globalCluster.find({ + where: { + id: { + [models.Op.IN]: ids.map((id) => createBrandedValue(id)), + }, + }, + }); + } +} diff --git a/src/domain-services/global-cluster/graphql/resolver.ts b/src/domain-services/global-cluster/graphql/resolver.ts new file mode 100644 index 00000000..4cdd5ec4 --- /dev/null +++ b/src/domain-services/global-cluster/graphql/resolver.ts @@ -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> { + return await this.globalClusterService.findById(context.models, id); + } +} diff --git a/src/domain-services/global-cluster/graphql/types.ts b/src/domain-services/global-cluster/graphql/types.ts new file mode 100644 index 00000000..fe0cccff --- /dev/null +++ b/src/domain-services/global-cluster/graphql/types.ts @@ -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; + + @Field({ nullable: true }) + name?: string; +}