From b899075e88241f6fed63161023ea8e17adc859df Mon Sep 17 00:00:00 2001 From: manelcecs Date: Fri, 27 Oct 2023 12:22:57 +0200 Subject: [PATCH] WIP: refactor apply strategy pattern --- src/domain-services/flows/flow-service.ts | 117 +++--------------- .../location/location-service.ts | 20 +++ .../organizations/organization-service.ts | 24 ++++ src/domain-services/plans/plan-service.ts | 33 +++++ .../usage-years/usage-year-service.ts | 27 ++++ 5 files changed, 124 insertions(+), 97 deletions(-) create mode 100644 src/domain-services/organizations/organization-service.ts create mode 100644 src/domain-services/usage-years/usage-year-service.ts diff --git a/src/domain-services/flows/flow-service.ts b/src/domain-services/flows/flow-service.ts index 6f6d3753..0a63abe2 100644 --- a/src/domain-services/flows/flow-service.ts +++ b/src/domain-services/flows/flow-service.ts @@ -9,12 +9,20 @@ import { 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 { createBrandedValue } from '@unocha/hpc-api-core/src/util/types'; import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; -import { FlowId } from '@unocha/hpc-api-core/src/db/models/flow'; +import { OrganizationService } from '../organizations/organization-service'; +import { LocationService } from '../location/location-service'; +import { PlanService } from '../plans/plan-service'; +import { UsageYearService } from '../usage-years/usage-year-service'; @Service() export class FlowService { + constructor(private readonly organizationService: OrganizationService, + private readonly locationService: LocationService, + private readonly planService: PlanService, + private readonly usageYearService: UsageYearService) {} + async search( models: Database, first: number, @@ -49,18 +57,12 @@ export class FlowService { const pagedData = flows.slice(afterIndex, afterIndex + first); const edges = await Promise.all( pagedData.map(async (flow) => { - const flowIdBranded = createBrandedValue(flow.id); - const categories: FlowCategory[] = await this.getFlowCategories( flow, models ); - const flowObjects = await models.flowObject.find({ - where: { - flowID: flowIdBranded, - }, - }); + const flowObjects = await this.getFlowObjects(flow, models); const organizationsFO: any[] = []; const locationsFO: any[] = []; @@ -79,19 +81,19 @@ export class FlowService { } }); - const organizations: FlowOrganization[] = await this.getOrganizations( + const organizations: FlowOrganization[] = await this.organizationService.getFlowObjectOrganizations( organizationsFO, models ); - const locations: FlowLocation[] = await this.getLocations( + const locations: FlowLocation[] = await this.locationService.getFlowObjectLocations( locationsFO, models ); - const plans = await this.getPlans(plansFO, models); + const plans = await this.planService.getFlowObjectPlans(plansFO, models); - const usageYears = await this.getUsageYears(usageYearsFO, models); + const usageYears = await this.usageYearService.getFlowObjectUsageYears(usageYearsFO, models); return { node: { @@ -165,93 +167,14 @@ export class FlowService { })); } - 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({ + private async getFlowObjects(flow: any, models: Database): Promise { + const flowIdBranded = createBrandedValue(flow.id); + const flowObjects = await models.flowObject.find({ where: { - id: { - [Op.IN]: usageYearsFO.map((usageYearFO) => usageYearFO.objectID), - }, + flowID: flowIdBranded, }, }); - return usageYears.map((usageYear) => ({ - year: usageYear.year, - direction: usageYearsFO.find( - (usageYearFO) => usageYearFO.objectID === usageYear.id - ).refDirection, - })); + return flowObjects; } - } diff --git a/src/domain-services/location/location-service.ts b/src/domain-services/location/location-service.ts index fdd4a23a..fc9c6739 100644 --- a/src/domain-services/location/location-service.ts +++ b/src/domain-services/location/location-service.ts @@ -2,6 +2,8 @@ import { Database } from '@unocha/hpc-api-core/src/db/type'; import { Service } from 'typedi'; import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types'; import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model'; +import { FlowLocation } from '../flows/graphql/types'; +import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; @Service() export class LocationService { @@ -26,4 +28,22 @@ export class LocationService { where: { name: { [models.Op.ILIKE]: `%${name}%` } }, }); } + + async getFlowObjectLocations( + 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!, + })); + } } diff --git a/src/domain-services/organizations/organization-service.ts b/src/domain-services/organizations/organization-service.ts new file mode 100644 index 00000000..36e5a525 --- /dev/null +++ b/src/domain-services/organizations/organization-service.ts @@ -0,0 +1,24 @@ +import { Database } from "@unocha/hpc-api-core/src/db"; +import { Service } from "typedi"; +import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; + +@Service() +export class OrganizationService { + + async getFlowObjectOrganizations(organizationsFO: any[], models: Database){ + 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, + })); + } +} \ No newline at end of file diff --git a/src/domain-services/plans/plan-service.ts b/src/domain-services/plans/plan-service.ts index 354a75f2..e39283b1 100644 --- a/src/domain-services/plans/plan-service.ts +++ b/src/domain-services/plans/plan-service.ts @@ -1,8 +1,10 @@ import { PlanId } from '@unocha/hpc-api-core/src/db/models/plan'; import { Database } from '@unocha/hpc-api-core/src/db/type'; +import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; import { NotFoundError } from '@unocha/hpc-api-core/src/util/error'; import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types'; import { Service } from 'typedi'; +import { FlowPlan } from '../flows/graphql/types'; @Service() export class PlanService { @@ -44,4 +46,35 @@ export class PlanService { return years.map((y) => y.year); } + + async getFlowObjectPlans( + 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; + } } diff --git a/src/domain-services/usage-years/usage-year-service.ts b/src/domain-services/usage-years/usage-year-service.ts new file mode 100644 index 00000000..7712f235 --- /dev/null +++ b/src/domain-services/usage-years/usage-year-service.ts @@ -0,0 +1,27 @@ +import { Database } from '@unocha/hpc-api-core/src/db'; +import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; +import { Service } from 'typedi'; +import { FlowUsageYear } from '../flows/graphql/types'; + +@Service() +export class UsageYearService { + async getFlowObjectUsageYears( + 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, + })); + } +}