Skip to content

Commit 0816cc7

Browse files
czmjmanelcecs
authored andcommitted
Add projects to flowObjects response
1 parent 5184827 commit 0816cc7

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import Context from '../Context';
77
import { LocationService } from '../location/location-service';
88
import { OrganizationService } from '../organization/organization-service';
99
import { PlanService } from '../plans/plan-service';
10+
import { ProjectService } from '../project/project-service';
1011
import { UsageYearService } from '../usage-year/usage-year-service';
1112

1213
@Service()
1314
export class FlowObjectService {
1415
constructor(
1516
private locationService: LocationService,
17+
private projectService: ProjectService,
1618
private planService: PlanService,
1719
private organizationService: OrganizationService,
1820
private usageYearService: UsageYearService
@@ -72,6 +74,15 @@ export class FlowObjectService {
7274
),
7375
];
7476
}
77+
if (type === 'project') {
78+
return [
79+
'projects',
80+
await this.projectService.findByIds(
81+
context.models,
82+
flowObjects.map((fo) => fo.objectID)
83+
),
84+
];
85+
}
7586

7687
return [];
7788
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BaseTypeWithSoftDelete } from '../../base-types';
44
import Location from '../../location/graphql/types';
55
import Organization from '../../organization/graphql/types';
66
import Plan from '../../plans/graphql/types';
7+
import Project from '../../project/graphql/types';
78
import UsageYear from '../../usage-year/graphql/types';
89

910
@ObjectType()
@@ -22,6 +23,9 @@ export class FlowObjectsGroupedByType {
2223

2324
@Field(() => [Plan], { nullable: true })
2425
plans: Plan[];
26+
27+
@Field(() => [Project], { nullable: true })
28+
projects: Project[];
2529
}
2630

2731
@ObjectType()
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 { ProjectService } from '../project-service';
7+
import Project from './types';
8+
9+
@Service()
10+
@Resolver(Project)
11+
export default class ProjectResolver {
12+
constructor(private projectService: ProjectService) {}
13+
14+
@Query(() => Project)
15+
async project(
16+
@Arg('id') id: number,
17+
@Ctx() context: Context
18+
): Promise<InstanceDataOfModel<Database['project']>> {
19+
return await this.projectService.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 Project extends BaseType {
7+
@Field(() => ID)
8+
id: Brand<number, { readonly s: unique symbol }, 'Project ID'>;
9+
10+
@Field({ nullable: true })
11+
name?: string;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { ProjectId } from '@unocha/hpc-api-core/src/db/models/project';
2+
import { Database } from '@unocha/hpc-api-core/src/db/type';
3+
import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
4+
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
5+
import { Service } from 'typedi';
6+
7+
@Service()
8+
export class ProjectService {
9+
async findById(
10+
models: Database,
11+
id: number
12+
): Promise<InstanceDataOfModel<Database['project']>> {
13+
const project = await models.project.get(createBrandedValue(id));
14+
15+
if (!project) {
16+
throw new Error(`Project with ID ${id} does not exist`);
17+
}
18+
19+
return project;
20+
}
21+
22+
async findByIds(
23+
models: Database,
24+
ids: number[]
25+
): Promise<{ id: ProjectId; name?: string | null }[]> {
26+
const projects = await models.project.find({
27+
where: {
28+
id: {
29+
[models.Op.IN]: ids.map((id) => createBrandedValue(id)),
30+
},
31+
},
32+
});
33+
34+
const currentProjectVersions = await models.projectVersion.find({
35+
where: {
36+
projectId: {
37+
[models.Op.IN]: projects.map((p) =>
38+
createBrandedValue(p.currentPublishedVersionId)
39+
),
40+
},
41+
},
42+
});
43+
44+
return currentProjectVersions.map((pv) => ({
45+
id: pv.projectId,
46+
name: pv.name,
47+
}));
48+
}
49+
}

0 commit comments

Comments
 (0)