Skip to content

Commit

Permalink
Fix incorrect query when multiple flowObject filter
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs authored and Pl217 committed Nov 17, 2023
1 parent f5b55d4 commit 216113c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ export class FlowSearchService {
flowObjectFilters: FlowObjectFilters[]
): { strategy: FlowSearchStrategy; conditions: any } {
let conditions = {};

if (
(!flowFilters &&
(!flowObjectFilters ?? flowObjectFilters.length === 0)) ??
(!flowObjectFilters || flowObjectFilters.length === 0)) ??
(flowFilters && (!flowObjectFilters ?? flowObjectFilters.length === 0))
) {
const flowConditions = this.prepareFlowConditions(flowFilters);
Expand Down Expand Up @@ -298,9 +299,7 @@ export class FlowSearchService {
};
}

throw new Error(
'Invalid combination of flowFilters and flowObjectFilters - temp: only provide flowFilters'
);
throw new Error('Invalid combination of flowFilters and flowObjectFilters');
}

private buildConditionsMap(flowConditions: any, flowObjectConditions: any) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Database } from '@unocha/hpc-api-core/src/db';
import { type Database } from '@unocha/hpc-api-core/src/db';
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { Service } from 'typedi';
import { FlowObjectService } from '../../../flow-object/flow-object-service';
import { FlowService } from '../../flow-service';
import { type FlowService } from '../../flow-service';
import {
FlowSearchStrategy,
FlowSearchStrategyResponse,
type FlowSearchStrategy,
type FlowSearchStrategyResponse,
} from '../flow-search-strategy';

@Service()
Expand All @@ -22,22 +23,23 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
cursorCondition: any,
models: Database
): Promise<FlowSearchStrategyResponse> {
// Obtain flowObjects conditions
const flowObjectsConditions: Map<
string,
Map<string, number[]>
> = flowConditions.get('flowObjects');
const flowEntityConditions = flowConditions.get('flow');
> = flowConditions.get('flowObjects') ?? new Map();

// Obtain flow conditions
const flowEntityConditions = flowConditions.get('flow') ?? new Map();

// Obtain where clause for flowObjects
const flowObjectWhere = this.mapFlowObjectConditionsToWhereClause(
flowObjectsConditions
);

// Obtain flowIDs based on provided flowObject conditions
const flowIDsFromFilteredFlowObjects =
await this.flowObjectService.getFlowIdsFromFlowObjects(
models,
flowObjectWhere
);
const flowIDsFromFilteredFlowObjects: FlowId[] =
await this.getFlowIDsFromFilteredFlowObjects(models, flowObjectWhere);

// Combine conditions from flowObjects FlowIDs and flow conditions
const mergedFlowConditions = {
Expand All @@ -62,6 +64,25 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
return { flows, count: countObject.count };
}

private async getFlowIDsFromFilteredFlowObjects(
models: Database,
flowObjectWhere: any[]
): Promise<FlowId[]> {
const flowIDsFromFilteredFlowObjects: FlowId[] = [];
const tempFlowIDs: FlowId[][] = await Promise.all(
flowObjectWhere.map((whereClause) =>
this.flowObjectService.getFlowIdsFromFlowObjects(models, whereClause)
)
);
// Flatten array of arrays keeping only values present in all arrays
const flowIDs = tempFlowIDs.reduce((a, b) =>
a.filter((c) => b.includes(c))
);
flowIDsFromFilteredFlowObjects.push(...flowIDs);

return flowIDsFromFilteredFlowObjects;
}

/*
* Map structure:
* {
Expand All @@ -74,12 +95,11 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
*/
private mapFlowObjectConditionsToWhereClause(
flowObjectConditions: Map<string, Map<string, number[]>>
): any {
let flowObjectWhere: any = {};
): any[] {
const whereClauses = [];
for (const [objectType, refDirectionMap] of flowObjectConditions) {
for (const [refDirection, objectIDs] of refDirectionMap) {
flowObjectWhere = {
...flowObjectWhere,
const whereClause = {
objectID: {
[Op.IN]: objectIDs,
},
Expand All @@ -90,9 +110,11 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
[Op.LIKE]: objectType,
},
};

whereClauses.push(whereClause);
}
}

return flowObjectWhere;
return whereClauses;
}
}

0 comments on commit 216113c

Please sign in to comment.