Skip to content

Commit

Permalink
Allow filter flow by multiples id
Browse files Browse the repository at this point in the history
Fix lint
  • Loading branch information
manelcecs authored and Pl217 committed Nov 17, 2023
1 parent b01eee2 commit f5b55d4
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 96 deletions.
18 changes: 9 additions & 9 deletions src/domain-services/categories/category-service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Database } from '@unocha/hpc-api-core/src/db';
import { type Database } from '@unocha/hpc-api-core/src/db';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { type InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
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';
import { type Category } from './graphql/types';

@Service()
export class CategoryService {
async getCategoriesForFlows(
flowLinks: Map<number, InstanceDataOfModel<Database['flowLink']>[]>,
flowLinks: Map<number, Array<InstanceDataOfModel<Database['flowLink']>>>,
models: Database
): Promise<Map<number, Category[]>> {
const flowLinksBrandedIds = [];
Expand All @@ -23,7 +23,7 @@ export class CategoryService {
return categoriesMap;
}

const categoriesRef: InstanceDataOfModel<Database['categoryRef']>[] =
const categoriesRef: Array<InstanceDataOfModel<Database['categoryRef']>> =
await models.categoryRef.find({
where: {
objectID: {
Expand All @@ -32,7 +32,7 @@ export class CategoryService {
},
});

const categories: InstanceDataOfModel<Database['category']>[] =
const categories: Array<InstanceDataOfModel<Database['category']>> =
await models.category.find({
where: {
id: {
Expand All @@ -42,7 +42,7 @@ export class CategoryService {
});

// Populate the map with categories for each flow
categoriesRef.forEach((catRef) => {
for (const catRef of categoriesRef) {
const flowId = catRef.objectID.valueOf();

if (!categoriesMap.has(flowId)) {
Expand All @@ -58,7 +58,7 @@ export class CategoryService {
}

categoriesPerFlow.push(this.mapCategoryToFlowCategory(category, catRef));
});
}

return categoriesMap;
}
Expand Down
96 changes: 52 additions & 44 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { Database } from '@unocha/hpc-api-core/src/db/type';
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { type Database } from '@unocha/hpc-api-core/src/db/type';
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 {
FlowObjectFilters,
SearchFlowsArgs,
SearchFlowsFilters,
} from './graphql/args';
import {
FlowPaged,
FlowParkedParentSource,
FlowSearchResult,
} from './graphql/types';
import { FlowSearchStrategy } from './strategy/flow-search-strategy';
import { OnlyFlowFiltersStrategy } from './strategy/impl/only-flow-conditions-strategy';
import { FlowObjectFiltersStrategy } from './strategy/impl/flow-object-conditions-strategy';
import { FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { CategoryService } from '../categories/category-service';
import { type Category } from '../categories/graphql/types';
import { ExternalReferenceService } from '../external-reference/external-reference-service';
import { FlowLinkService } from '../flow-link/flow-link-service';
import { FlowObjectService } from '../flow-object/flow-object-service';
import { type FlowObject } from '../flow-object/model';
import { type BaseLocation } from '../location/graphql/types';
import { LocationService } from '../location/location-service';
import { type Organization } from '../organizations/graphql/types';
import { OrganizationService } from '../organizations/organization-service';
import { type BasePlan } from '../plans/graphql/types';
import { PlanService } from '../plans/plan-service';
import { ReportDetailService } from '../report-details/report-detail-service';
import { type UsageYear } from '../usage-years/grpahql/types';
import { UsageYearService } from '../usage-years/usage-year-service';
import { FlowLinkService } from '../flow-link/flow-link-service';
import { FlowObject } from '../flow-object/model';
import { FlowObjectService } from '../flow-object/flow-object-service';
import { FlowEntity } from './model';
import { Category } from '../categories/graphql/types';
import { Organization } from '../organizations/graphql/types';
import { BaseLocation } from '../location/graphql/types';
import { BasePlan } from '../plans/graphql/types';
import { UsageYear } from '../usage-years/grpahql/types';
import {
type FlowObjectFilters,
type SearchFlowsArgs,
type SearchFlowsFilters,
} from './graphql/args';
import {
type FlowPaged,
type FlowParkedParentSource,
type FlowSearchResult,
} from './graphql/types';
import { type FlowEntity } from './model';
import { type FlowSearchStrategy } from './strategy/flow-search-strategy';
import { FlowObjectFiltersStrategy } from './strategy/impl/flow-object-conditions-strategy';
import { OnlyFlowFiltersStrategy } from './strategy/impl/only-flow-conditions-strategy';

@Service()
export class FlowSearchService {
Expand Down Expand Up @@ -144,14 +144,14 @@ export class FlowSearchService {

const items = await Promise.all(
flows.map(async (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 plans = plansMap.get(flow.id) || [];
const usageYears = usageYearsMap.get(flow.id) || [];
const externalReferences = externalReferencesMap.get(flow.id) || [];
const reportDetails = reportDetailsMap.get(flow.id) || [];
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 plans = plansMap.get(flow.id) ?? [];
const usageYears = usageYearsMap.get(flow.id) ?? [];
const externalReferences = externalReferencesMap.get(flow.id) ?? [];
const reportDetails = reportDetailsMap.get(flow.id) ?? [];

let parkedParentSource: FlowParkedParentSource[] = [];
if (flow.activeStatus && flowLink.length > 0) {
Expand Down Expand Up @@ -197,7 +197,7 @@ export class FlowSearchService {
hasNextPage: limit <= flows.length,
hasPreviousPage: afterCursor !== undefined,
startCursor: flows.length ? flows[0].id.valueOf() : 0,
endCursor: flows.length ? flows[flows.length - 1].id.valueOf() : 0,
endCursor: flows.length ? flows.at(-1)?.id.valueOf() ?? 0 : 0,
pageSize: flows.length,
sortField: orderBy.column,
sortOrder: orderBy.order,
Expand All @@ -209,11 +209,15 @@ export class FlowSearchService {
let flowConditions = {};

if (flowFilters) {
Object.entries(flowFilters).forEach(([key, value]) => {
for (const [key, value] of Object.entries(flowFilters)) {
if (value !== undefined) {
flowConditions = { ...flowConditions, [key]: value };
if (Array.isArray(value) && value.length !== 0) {
flowConditions = { ...flowConditions, [key]: { [Op.IN]: value } };
} else {
flowConditions = { ...flowConditions, [key]: value };
}
}
});
}
}

return flowConditions;
Expand All @@ -222,13 +226,16 @@ export class FlowSearchService {
prepareFlowObjectConditions(
flowObjectFilters: FlowObjectFilters[]
): Map<string, Map<string, number[]>> {
const flowObjectsConditions: Map<string, Map<string, number[]>> = new Map();
const flowObjectsConditions: Map<string, Map<string, number[]>> = new Map<
string,
Map<string, number[]>
>();

flowObjectFilters.forEach((flowObjectFilter) => {
for (const flowObjectFilter of flowObjectFilters) {
const { objectType, direction, objectID } = flowObjectFilter;

if (!flowObjectsConditions.has(objectType)) {
flowObjectsConditions.set(objectType, new Map());
flowObjectsConditions.set(objectType, new Map<string, number[]>());
}

const refDirectionMap = flowObjectsConditions.get(objectType);
Expand All @@ -245,7 +252,7 @@ export class FlowSearchService {
}

objectIDsArray!.push(objectID);
});
}

return flowObjectsConditions;
}
Expand All @@ -257,8 +264,8 @@ export class FlowSearchService {
let conditions = {};
if (
(!flowFilters &&
(!flowObjectFilters || flowObjectFilters.length === 0)) ||
(flowFilters && (!flowObjectFilters || flowObjectFilters.length === 0))
(!flowObjectFilters ?? flowObjectFilters.length === 0)) ??
(flowFilters && (!flowObjectFilters ?? flowObjectFilters.length === 0))
) {
const flowConditions = this.prepareFlowConditions(flowFilters);
conditions = { ...conditions, ...flowConditions };
Expand Down Expand Up @@ -302,14 +309,15 @@ export class FlowSearchService {
conditionsMap.set('flow', flowConditions);
return conditionsMap;
}

private mapFlowObjects(
flowObjects: FlowObject[],
organizationsFO: any[],
locationsFO: any[],
plansFO: any[],
usageYearsFO: any[]
) {
flowObjects.forEach((flowObject) => {
for (const flowObject of flowObjects) {
if (flowObject.objectType === 'organization') {
organizationsFO.push(flowObject);
} else if (flowObject.objectType === 'location') {
Expand All @@ -319,7 +327,7 @@ export class FlowSearchService {
} else if (flowObject.objectType === 'usageYear') {
usageYearsFO.push(flowObject);
}
});
}
}

private async getParketParents(
Expand Down
2 changes: 1 addition & 1 deletion src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Database } from '@unocha/hpc-api-core/src/db/type';
import { Service } from 'typedi';
import { Database } from '@unocha/hpc-api-core/src/db/type';

@Service()
export class FlowService {
Expand Down
6 changes: 3 additions & 3 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ArgsType, Field, InputType } from 'type-graphql';
import { FlowSortField } from './types';
import { PaginationArgs } from '../../../utils/graphql/pagination';
import { type FlowSortField } from './types';

@InputType()
export class SearchFlowsFilters {
@Field({ nullable: true })
id: number;
@Field(() => [Number], { nullable: true })
id: number[];

@Field({ nullable: true })
activeStatus: boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/domain-services/flows/graphql/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FlowPaged, FlowSearchResult, FlowSortField } from './types';
import { Args, Ctx, Query, Resolver } from 'type-graphql';
import { Service } from 'typedi';
import { Arg, Args, Ctx, Query, Resolver } from 'type-graphql';
import { FlowSearchService } from '../flow-search-service';
import Context from '../../Context';
import { FlowSearchService } from '../flow-search-service';
import { SearchFlowsArgs } from './args';
import { FlowPaged, FlowSearchResult } from './types';

@Service()
@Resolver(FlowPaged)
Expand Down
4 changes: 2 additions & 2 deletions src/domain-services/flows/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Field, ObjectType } from 'type-graphql';
import { IItemPaged, PageInfo } from '../../../utils/graphql/pagination';
import { BaseType } from '../../../utils/graphql/base-types';
import { PageInfo, type IItemPaged } from '../../../utils/graphql/pagination';
import { Category } from '../../categories/graphql/types';
import { BaseLocation } from '../../location/graphql/types';
import { Organization } from '../../organizations/graphql/types';
import { BasePlan } from '../../plans/graphql/types';
import { ReportDetail } from '../../report-details/graphql/types';
import { UsageYear } from '../../usage-years/grpahql/types';
import { BaseType } from '../../../utils/graphql/base-types';

@ObjectType()
export class FlowExternalReference {
Expand Down
2 changes: 1 addition & 1 deletion src/domain-services/location/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseType } from '../../../utils/graphql/base-types';
import { Brand } from '@unocha/hpc-api-core/src/util/types';
import { MaxLength } from 'class-validator';
import { Field, ID, Int, ObjectType, registerEnumType } from 'type-graphql';
import { BaseType } from '../../../utils/graphql/base-types';

export enum LocationStatus {
active = 'active',
Expand Down
14 changes: 7 additions & 7 deletions src/domain-services/location/location-service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type LocationId } from '@unocha/hpc-api-core/src/db/models/location';
import { type Database } from '@unocha/hpc-api-core/src/db/type';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { type InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
import { BaseLocation } from './graphql/types';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { LocationId } from '@unocha/hpc-api-core/src/db/models/location';
import { type BaseLocation } from './graphql/types';

@Service()
export class LocationService {
Expand All @@ -31,14 +31,14 @@ export class LocationService {
}

async getLocationsForFlows(
locationsFO: InstanceDataOfModel<Database['flowObject']>[],
locationsFO: Array<InstanceDataOfModel<Database['flowObject']>>,
models: Database
): Promise<Map<number, Set<BaseLocation>>> {
const locationObjectsIDs: LocationId[] = locationsFO.map((locFO) =>
createBrandedValue(locFO.objectID)
);

const locations: InstanceDataOfModel<Database['location']>[] =
const locations: Array<InstanceDataOfModel<Database['location']>> =
await models.location.find({
where: {
id: {
Expand All @@ -49,7 +49,7 @@ export class LocationService {

const locationsMap = new Map<number, Set<BaseLocation>>();

locationsFO.forEach((locFO) => {
for (const locFO of locationsFO) {
const flowId = locFO.flowID;
if (!locationsMap.has(flowId)) {
locationsMap.set(flowId, new Set<BaseLocation>());
Expand All @@ -61,7 +61,7 @@ export class LocationService {
}
const locationMapped = this.mapLocationsToFlowLocations(location, locFO);
locationsMap.get(flowId)!.add(locationMapped);
});
}
return locationsMap;
}

Expand Down
10 changes: 5 additions & 5 deletions src/domain-services/organizations/organization-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Database } from '@unocha/hpc-api-core/src/db';
import { Service } from 'typedi';
import { type Database } from '@unocha/hpc-api-core/src/db';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { Organization } from './graphql/types';
import { Service } from 'typedi';
import { type Organization } from './graphql/types';

@Service()
export class OrganizationService {
Expand All @@ -16,7 +16,7 @@ export class OrganizationService {

const organizationsMap = new Map<number, Organization[]>();

organizationsFO.forEach((orgFO) => {
for (const orgFO of organizationsFO) {
const flowId = orgFO.flowID;

if (!organizationsMap.has(flowId)) {
Expand All @@ -39,7 +39,7 @@ export class OrganizationService {
);

organizationsMap.get(flowId)!.push(organizationMapped);
});
}

return organizationsMap;
}
Expand Down
2 changes: 1 addition & 1 deletion src/domain-services/plans/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Brand } from '@unocha/hpc-api-core/src/util/types';
import { MaxLength } from 'class-validator';
import { Field, ID, Int, ObjectType } from 'type-graphql';
import PlanTag from '../../plan-tag/graphql/types';
import { BaseType } from '../../../utils/graphql/base-types';
import PlanTag from '../../plan-tag/graphql/types';

@ObjectType()
export class PlanCaseload {
Expand Down
Loading

0 comments on commit f5b55d4

Please sign in to comment.