Skip to content

Commit

Permalink
Add governingEntities 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 25f84fc commit 6e92a29
Show file tree
Hide file tree
Showing 5 changed files with 99 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 @@ -5,6 +5,7 @@ import { groupBy } from 'lodash';
import { Service } from 'typedi';
import Context from '../Context';
import { GlobalClusterService } from '../global-cluster/global-cluster-service';
import { GoverningEntityService } from '../governing-entity/governing-entity-service';
import { LocationService } from '../location/location-service';
import { OrganizationService } from '../organization/organization-service';
import { PlanService } from '../plans/plan-service';
Expand All @@ -15,6 +16,7 @@ import { UsageYearService } from '../usage-year/usage-year-service';
export class FlowObjectService {
constructor(
private globalClusterService: GlobalClusterService,
private governingEntityService: GoverningEntityService,
private locationService: LocationService,
private projectService: ProjectService,
private planService: PlanService,
Expand Down Expand Up @@ -49,6 +51,15 @@ export class FlowObjectService {
),
];
}
if (type === 'governingEntity') {
return [
'governingEntities',
await this.governingEntityService.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
Expand Up @@ -2,6 +2,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 GoverningEntity from '../../governing-entity/graphql/types';
import Location from '../../location/graphql/types';
import Organization from '../../organization/graphql/types';
import Plan from '../../plans/graphql/types';
Expand All @@ -13,6 +14,9 @@ export class FlowObjectsGroupedByType {
@Field(() => [GlobalCluster], { nullable: true })
globalClusters: GlobalCluster[];

@Field(() => [GoverningEntity], { nullable: true })
governingEntities: GoverningEntity[];

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

Expand Down
51 changes: 51 additions & 0 deletions src/domain-services/governing-entity/governing-entity-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { GoverningEntityId } from '@unocha/hpc-api-core/src/db/models/governingEntity';
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 GoverningEntityService {
async findById(
models: Database,
id: number
): Promise<InstanceDataOfModel<Database['governingEntity']>> {
const governingEntity = await models.governingEntity.get(
createBrandedValue(id)
);

if (!governingEntity) {
throw new Error(`Governing entity with ID ${id} does not exist`);
}

return governingEntity;
}

async findByIds(
models: Database,
ids: number[]
): Promise<{ id: GoverningEntityId; name?: string | null }[]> {
const governingEntities = await models.governingEntity.find({
where: {
id: {
[models.Op.IN]: ids.map((id) => createBrandedValue(id)),
},
},
});

const currentGoverningEntityVersions =
await models.governingEntityVersion.find({
where: {
governingEntityId: {
[models.Op.IN]: governingEntities.map((p) => p.id),
},
currentVersion: true,
},
});

return currentGoverningEntityVersions.map((gev) => ({
id: gev.governingEntityId,
name: gev.name,
}));
}
}
21 changes: 21 additions & 0 deletions src/domain-services/governing-entity/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 { GoverningEntityService } from '../governing-entity-service';
import GoverningEntity from './types';

@Service()
@Resolver(GoverningEntity)
export default class GoverningEntityResolver {
constructor(private fieldClusterService: GoverningEntityService) {}

@Query(() => GoverningEntity)
async fieldCluster(
@Arg('id') id: number,
@Ctx() context: Context
): Promise<InstanceDataOfModel<Database['governingEntity']>> {
return await this.fieldClusterService.findById(context.models, id);
}
}
12 changes: 12 additions & 0 deletions src/domain-services/governing-entity/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 GoverningEntity extends BaseType {
@Field(() => ID)
id: Brand<number, { readonly s: unique symbol }, 'Governing Entity ID'>;

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

0 comments on commit 6e92a29

Please sign in to comment.