Skip to content

Commit

Permalink
WIP: query
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Oct 27, 2023
1 parent 26d3c22 commit 9e1bf3d
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 2 deletions.
148 changes: 146 additions & 2 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -41,14 +49,60 @@ 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: {
id: flow.id.valueOf(),
amountUSD: flow.amountUSD.toString(),
createdAt: flow.createdAt,
categories: categories,
organizations: organizations,
locations: locations,
plans: plans,
usageYears: usageYears,
},
cursor: flow.id.toString(),
};
Expand Down Expand Up @@ -110,4 +164,94 @@ export class FlowService {
group: cat.group,
}));
}

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({
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,
}));
}

}
51 changes: 51 additions & 0 deletions src/domain-services/flows/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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()
Expand Down

0 comments on commit 9e1bf3d

Please sign in to comment.