Skip to content

Commit

Permalink
Add emergencies 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 6e92a29 commit 6969f0d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/domain-services/emergency/emergency-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 EmergencyService {
async findById(
models: Database,
id: number
): Promise<InstanceDataOfModel<Database['emergency']>> {
const emergency = await models.emergency.get(createBrandedValue(id));

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

return emergency;
}

async findByIds(
models: Database,
ids: number[]
): Promise<InstanceDataOfModel<Database['emergency']>[]> {
return await models.emergency.find({
where: {
id: {
[models.Op.IN]: ids.map((id) => createBrandedValue(id)),
},
},
});
}
}
21 changes: 21 additions & 0 deletions src/domain-services/emergency/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 { EmergencyService } from '../emergency-service';
import Emergency from './types';

@Service()
@Resolver(Emergency)
export default class EmergencyResolver {
constructor(private fieldClusterService: EmergencyService) {}

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

@Field({ nullable: true })
name?: string;
}
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 { EmergencyService } from '../emergency/emergency-service';
import { GlobalClusterService } from '../global-cluster/global-cluster-service';
import { GoverningEntityService } from '../governing-entity/governing-entity-service';
import { LocationService } from '../location/location-service';
Expand All @@ -15,6 +16,7 @@ import { UsageYearService } from '../usage-year/usage-year-service';
@Service()
export class FlowObjectService {
constructor(
private emergencyService: EmergencyService,
private globalClusterService: GlobalClusterService,
private governingEntityService: GoverningEntityService,
private locationService: LocationService,
Expand Down Expand Up @@ -42,6 +44,15 @@ export class FlowObjectService {
const typedObjects = await Promise.all(
Object.entries(groupBy(flowObjects, 'objectType')).map(
async ([type, flowObjects]) => {
if (type === 'emergency') {
return [
'emergencies',
await this.emergencyService.findByIds(
context.models,
flowObjects.map((fo) => fo.objectID)
),
];
}
if (type === 'globalCluster') {
return [
'globalClusters',
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 Emergency from '../../emergency/graphql/types';
import GlobalCluster from '../../global-cluster/graphql/types';
import GoverningEntity from '../../governing-entity/graphql/types';
import Location from '../../location/graphql/types';
Expand All @@ -11,6 +12,9 @@ import UsageYear from '../../usage-year/graphql/types';

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

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

Expand Down

0 comments on commit 6969f0d

Please sign in to comment.