diff --git a/src/domain-services/flows/flow-service.ts b/src/domain-services/flows/flow-service.ts index a4110c25..6f6d3753 100644 --- a/src/domain-services/flows/flow-service.ts +++ b/src/domain-services/flows/flow-service.ts @@ -1,5 +1,13 @@ import { Service } from 'typedi'; -import { FlowCategory, FlowSearchResult, FlowSortField } from './graphql/types'; +import { + FlowCategory, + FlowLocation, + FlowOrganization, + FlowPlan, + FlowSearchResult, + FlowSortField, + FlowUsageYear, +} from './graphql/types'; import { Database } from '@unocha/hpc-api-core/src/db/type'; import { Brand, createBrandedValue } from '@unocha/hpc-api-core/src/util/types'; import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; @@ -41,7 +49,49 @@ export class FlowService { const pagedData = flows.slice(afterIndex, afterIndex + first); const edges = await Promise.all( pagedData.map(async (flow) => { - const categories: FlowCategory[] = await this.getFlowCategories(flow, models); + const flowIdBranded = createBrandedValue(flow.id); + + const categories: FlowCategory[] = await this.getFlowCategories( + flow, + models + ); + + const flowObjects = await models.flowObject.find({ + where: { + flowID: flowIdBranded, + }, + }); + + const organizationsFO: any[] = []; + const locationsFO: any[] = []; + const plansFO: any[] = []; + const usageYearsFO: any[] = []; + + flowObjects.forEach((flowObject) => { + if (flowObject.objectType === 'organization') { + organizationsFO.push(flowObject); + } else if (flowObject.objectType === 'location') { + locationsFO.push(flowObject); + } else if (flowObject.objectType === 'plan') { + plansFO.push(flowObject); + } else if (flowObject.objectType === 'usageYear') { + usageYearsFO.push(flowObject); + } + }); + + const organizations: FlowOrganization[] = await this.getOrganizations( + organizationsFO, + models + ); + + const locations: FlowLocation[] = await this.getLocations( + locationsFO, + models + ); + + const plans = await this.getPlans(plansFO, models); + + const usageYears = await this.getUsageYears(usageYearsFO, models); return { node: { @@ -49,6 +99,10 @@ export class FlowService { amountUSD: flow.amountUSD.toString(), createdAt: flow.createdAt, categories: categories, + organizations: organizations, + locations: locations, + plans: plans, + usageYears: usageYears, }, cursor: flow.id.toString(), }; @@ -110,4 +164,94 @@ export class FlowService { group: cat.group, })); } + + private async getOrganizations( + organizationsFO: any[], + models: Database + ): Promise { + const organizations = await models.organization.find({ + where: { + id: { + [Op.IN]: organizationsFO.map((orgFO) => orgFO.objectID), + }, + }, + }); + + return organizations.map((org) => ({ + id: org.id, + refDirection: organizationsFO.find((orgFO) => orgFO.objectID === org.id) + .refDirection, + name: org.name, + })); + } + + private async getLocations( + locationsFO: any[], + models: Database + ): Promise { + const locations = await models.location.find({ + where: { + id: { + [Op.IN]: locationsFO.map((locFO) => locFO.objectID), + }, + }, + }); + + return locations.map((loc) => ({ + id: loc.id.valueOf(), + name: loc.name!, + })); + } + + private async getPlans( + plansFO: any[], + models: Database + ): Promise { + const plans = await models.plan.find({ + where: { + id: { + [Op.IN]: plansFO.map((planFO) => planFO.objectID), + }, + }, + }); + + const flowPlans: FlowPlan[] = []; + + for (const plan of plans) { + const planVersion = await models.planVersion.find({ + where: { + planId: plan.id, + currentVersion: true, + }, + }); + + flowPlans.push({ + id: plan.id.valueOf(), + name: planVersion[0].name, + }); + } + + return flowPlans; + } + + private async getUsageYears( + usageYearsFO: any[], + models: Database + ): Promise { + const usageYears = await models.usageYear.find({ + where: { + id: { + [Op.IN]: usageYearsFO.map((usageYearFO) => usageYearFO.objectID), + }, + }, + }); + + return usageYears.map((usageYear) => ({ + year: usageYear.year, + direction: usageYearsFO.find( + (usageYearFO) => usageYearFO.objectID === usageYear.id + ).refDirection, + })); + } + } diff --git a/src/domain-services/flows/graphql/types.ts b/src/domain-services/flows/graphql/types.ts index 9407db55..f8864e51 100644 --- a/src/domain-services/flows/graphql/types.ts +++ b/src/domain-services/flows/graphql/types.ts @@ -12,6 +12,45 @@ export class FlowCategory { group: string; } +@ObjectType() +export class FlowOrganization { + @Field({ nullable: false }) + id: number; + + @Field({ nullable: false }) + refDirection: string; + + @Field({ nullable: false }) + name: string; +} + +@ObjectType() +export class FlowLocation { + @Field({ nullable: false }) + id: number; + + @Field({ nullable: false }) + name: string; +} + +@ObjectType() +export class FlowPlan { + @Field({ nullable: false }) + id: number; + + @Field({ nullable: false }) + name: string; +} + +@ObjectType() +export class FlowUsageYear { + @Field({ nullable: false }) + year: string; + + @Field({ nullable: false }) + direction: string; +} + @ObjectType() export default class Flow { @Field({ nullable: false }) @@ -25,6 +64,18 @@ export default class Flow { @Field(() => [FlowCategory], { nullable: false }) categories: FlowCategory[]; + + @Field(() => [FlowOrganization], { nullable: false }) + organizations: FlowOrganization[]; + + @Field(() => [FlowLocation], { nullable: false }) + locations: FlowLocation[]; + + @Field(() => [FlowPlan], { nullable: false }) + plans: FlowPlan[]; + + @Field(() => [FlowUsageYear], { nullable: false }) + usageYears: FlowUsageYear[]; } @ObjectType()