Skip to content

Commit

Permalink
Temp wip
Browse files Browse the repository at this point in the history
refactor GQL types
  • Loading branch information
manelcecs committed Nov 9, 2023
1 parent e86d58a commit 6749651
Show file tree
Hide file tree
Showing 19 changed files with 433 additions and 278 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lint": "yarn lint-prettier && yarn lint-eslint"
},
"dependencies": {
"@unocha/hpc-api-core": "^6.0.0",
"@unocha/hpc-api-core": "https://github.com/UN-OCHA/hpc-api-core.git#7443efb9039a0eab7833ee9fa8393038e1c286a6",
"apollo-server-hapi": "^3.12.0",
"bunyan": "^1.8.15",
"class-validator": "^0.14.0",
Expand Down
46 changes: 26 additions & 20 deletions src/domain-services/categories/category-service.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
import { Database } from '@unocha/hpc-api-core/src/db';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
import { FlowCategory } from '../flows/graphql/types';
import { Category } from './graphql/types';
import { InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';

// TODO: add proper type for flowLinks
@Service()
export class CategoryService {
async getCategoriesForFlows(
flowLinks: Map<number, any[]>,
flowLinks: Map<number, InstanceDataOfModel<Database['flowLink']>[]>,
models: Database
): Promise<Map<number, FlowCategory[]>> {
): Promise<Map<number, Category[]>> {
const flowLinksBrandedIds = [];
for (const flowLink of flowLinks.keys()) {
flowLinksBrandedIds.push(createBrandedValue(flowLink));
}

const categoriesRef = await models.categoryRef.find({
where: {
objectID: {
[Op.IN]: flowLinksBrandedIds,
},
},
});
// Group categories by flow ID for easy mapping
const categoriesMap = new Map<number, Category[]>();

const categories = await models.category.find({
where: {
id: {
[Op.IN]: categoriesRef.map((catRef) => catRef.categoryID),
if (flowLinksBrandedIds.length === 0) {
return categoriesMap;
}

const categoriesRef: InstanceDataOfModel<Database['categoryRef']>[] =
await models.categoryRef.find({
where: {
objectID: {
[Op.IN]: flowLinksBrandedIds,
},
},
},
});
});

// Group categories by flow ID for easy mapping
const categoriesMap = new Map<number, FlowCategory[]>();
const categories: InstanceDataOfModel<Database['category']>[] =
await models.category.find({
where: {
id: {
[Op.IN]: categoriesRef.map((catRef) => catRef.categoryID),
},
},
});

// Populate the map with categories for each flow
categoriesRef.forEach((catRef) => {
Expand All @@ -61,7 +67,7 @@ export class CategoryService {
private mapCategoryToFlowCategory(
category: InstanceDataOfModel<Database['category']>,
categoryRef: InstanceDataOfModel<Database['categoryRef']>
): FlowCategory {
): Category {
return {
id: category.id,
name: category.name,
Expand Down
44 changes: 44 additions & 0 deletions src/domain-services/categories/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Field, ObjectType } from 'type-graphql';
import { BaseType } from '../../../utils/graphql/base-types';

@ObjectType()
export class CategoryRef extends BaseType {
@Field({ nullable: false })
objectID: number;

@Field({ nullable: false })
versionID: number;

@Field({ nullable: false })
objectType: string;

@Field({ nullable: false })
categoryID: number;
}

@ObjectType()
export class Category extends BaseType {
@Field({ nullable: true })
id: number;

@Field({ nullable: false })
name: string;

@Field({ nullable: false })
group: string;

@Field({ nullable: true })
description: string;

@Field({ nullable: true })
parentID: number;

@Field({ nullable: true })
code: string;

@Field({ nullable: true })
includeTotals: boolean;

@Field(() => CategoryRef, { nullable: true })
categoryRef: CategoryRef;
}
67 changes: 60 additions & 7 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Service } from 'typedi';
import { FlowSearchResult, FlowSortField } from './graphql/types';
import {
FlowParkedParentSource,
FlowSearchResult,
FlowSortField,
} from './graphql/types';
import { Database } from '@unocha/hpc-api-core/src/db/type';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { OrganizationService } from '../organizations/organization-service';
Expand Down Expand Up @@ -84,7 +88,7 @@ export class FlowSearchService {

const count = countRes[0] as { count: number };

const flowIds = flows.map((flow) => flow.id);
const flowIds: FlowId[] = flows.map((flow) => flow.id);

const organizationsFO: any[] = [];
const locationsFO: any[] = [];
Expand Down Expand Up @@ -131,17 +135,23 @@ export class FlowSearchService {
]);

const items = flows.map((flow) => {
const flowLink = flowLinksMap.get(flow.id) || [];
const categories = categoriesMap.get(flow.id) || [];
const organizations = organizationsMap.get(flow.id) || [];
const locations = locationsMap.get(flow.id) || [];
const locations = [...locationsMap.get(flow.id) || []] ;
const plans = plansMap.get(flow.id) || [];
const usageYears = usageYearsMap.get(flow.id) || [];
const externalReferences = externalReferencesMap.get(flow.id) || [];
const reportDetails = reportDetailsMap.get(flow.id) || [];

const childIDs: number[] = (flowLinksMap.get(flow.id) || []).map(
(flowLink) => flowLink.childID.valueOf()
) as number[];
const parkedParentSource: FlowParkedParentSource[] = [];
if (flow.activeStatus && flowLink.length > 0) {
this.getParketParents(flow, flowLink, models, parkedParentSource);
}

const childIDs: number[] = flowLinksMap
.get(flow.id)
?.map((flowLink) => flowLink.childID.valueOf()) as number[];

const parentIDs: number[] = flowLinksMap
.get(flow.id)
Expand All @@ -152,6 +162,7 @@ export class FlowSearchService {
id: flow.id.valueOf(),
versionID: flow.versionID,
amountUSD: flow.amountUSD.toString(),
createdAt: flow.createdAt.toISOString(),
updatedAt: flow.updatedAt.toISOString(),
activeStatus: flow.activeStatus,
restricted: flow.restricted,
Expand All @@ -167,7 +178,7 @@ export class FlowSearchService {
origCurrency: flow.origCurrency ? flow.origCurrency.toString() : '',
externalReferences,
reportDetails,
parkedParentSource: 'placeholder',
parkedParentSource,
// Paged item field
cursor: flow.id.valueOf(),
};
Expand Down Expand Up @@ -214,4 +225,46 @@ export class FlowSearchService {
}
});
}

private async getParketParents(
flow: any,
flowLink: any[],
models: Database,
parkedParentSource: FlowParkedParentSource[]
): Promise<any> {
const flowLinksDepth0 = flowLink.filter((flowLink) => flowLink.depth === 0);

const flowLinksParent = flowLinksDepth0.filter(
(flowLink) => flowLink.parentID === flow.id
);

const parentFlowIds = flowLinksParent.map((flowLink) =>
flowLink.parentID.valueOf()
);

const categories = await models.category.find({
where: {
group: 'flowType',
name: 'parked',
},
});

const categoriesIDs = categories.map((category) => category.id);

const categoryRef = await models.categoryRef.find({
where: {
categoryID: {
[Op.IN]: categoriesIDs,
},
versionID: flow.versionID,
},
});

const parentFlows = flowLinksParent.filter((flowLink) => {
return categoryRef.some(
(categoryRef) =>
categoryRef.objectID.valueOf() === flowLink.parentID.valueOf()
);
});
}
}
4 changes: 2 additions & 2 deletions src/domain-services/flows/graphql/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Flow, { FlowSearchResult, FlowSortField } from './types';
import { FlowPaged, FlowSearchResult, FlowSortField } from './types';
import { Service } from 'typedi';
import { Arg, Args, Ctx, Query, Resolver } from 'type-graphql';
import { FlowSearchService } from '../flow-search-service';
Expand All @@ -7,7 +7,7 @@ import { SearchFlowsFilters } from './args';
import { PaginationArgs } from '../../../utils/graphql/pagination';

@Service()
@Resolver(Flow)
@Resolver(FlowPaged)
export default class FlowResolver {
constructor(private flowSearchService: FlowSearchService) {}

Expand Down
Loading

0 comments on commit 6749651

Please sign in to comment.