Skip to content

Commit

Permalink
WIP: refactor apply strategy pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Oct 27, 2023
1 parent 9e1bf3d commit b899075
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 97 deletions.
117 changes: 20 additions & 97 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[] = [];
Expand All @@ -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: {
Expand Down Expand Up @@ -165,93 +167,14 @@ export class FlowService {
}));
}

private async getOrganizations(
organizationsFO: any[],
models: Database
): Promise<FlowOrganization[]> {
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<FlowLocation[]> {
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<FlowPlan[]> {
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<FlowUsageYear[]> {
const usageYears = await models.usageYear.find({
private async getFlowObjects(flow: any, models: Database): Promise<any[]> {
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;
}

}
20 changes: 20 additions & 0 deletions src/domain-services/location/location-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,4 +28,22 @@ export class LocationService {
where: { name: { [models.Op.ILIKE]: `%${name}%` } },
});
}

async getFlowObjectLocations(
locationsFO: any[],
models: Database
): Promise<FlowLocation[]> {
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!,
}));
}
}
24 changes: 24 additions & 0 deletions src/domain-services/organizations/organization-service.ts
Original file line number Diff line number Diff line change
@@ -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,
}));
}
}
33 changes: 33 additions & 0 deletions src/domain-services/plans/plan-service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -44,4 +46,35 @@ export class PlanService {

return years.map((y) => y.year);
}

async getFlowObjectPlans(
plansFO: any[],
models: Database
): Promise<FlowPlan[]> {
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;
}
}
27 changes: 27 additions & 0 deletions src/domain-services/usage-years/usage-year-service.ts
Original file line number Diff line number Diff line change
@@ -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<FlowUsageYear[]> {
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,
}));
}
}

0 comments on commit b899075

Please sign in to comment.