Skip to content

Commit

Permalink
WIP:unit tests strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Nov 12, 2023
1 parent fa06bb0 commit 56718c2
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 148 deletions.
5 changes: 4 additions & 1 deletion src/domain-services/flows/flow-link-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export class FlowLinkService {
});

// Group flowLinks by flow ID for easy mapping
const flowLinksMap = new Map<number, InstanceOfModel<Database['flowLink']>[]>();
const flowLinksMap = new Map<
number,
InstanceOfModel<Database['flowLink']>[]
>();

// Populate the map with flowLinks for each flow
flowLinks.forEach((flowLink) => {
Expand Down
74 changes: 35 additions & 39 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import {
SearchFlowsArgs,
SearchFlowsFilters,
} from './graphql/args';
import {
FlowSearchResult
} from './graphql/types';
import { FlowSearchResult } from './graphql/types';
import { FlowSearchStrategy } from './strategy/flow-search-strategy';
import { OnlyFlowFiltersStrategy } from './strategy/impl/only-flow-conditions-strategy';

@Service()
export class FlowSearchService {
constructor(
private readonly onlyFlowFiltersStrategy: OnlyFlowFiltersStrategy
) { }
) {}

async search(
models: Database,
Expand Down Expand Up @@ -45,39 +43,28 @@ export class FlowSearchService {
}

const orderBy = {
column: sortField??'updatedAt',
column: sortField ?? 'updatedAt',
order: sortOrder ?? 'desc',
};

const { flowFilters, flowObjectFilters } = filters;

let onlyFlowFilters = false;
let onlyFlowObjectFilters = false;
let bothFlowFilters = false;

if (
(!flowFilters && !flowObjectFilters) ||
(flowFilters && !flowObjectFilters)
) {
onlyFlowFilters = true;
} else if (!flowFilters && flowObjectFilters) {
onlyFlowObjectFilters = true;
} else if (flowFilters && flowObjectFilters) {
bothFlowFilters = true;
}

let conditions: any = { ...cursorCondition };
const strategy = this.determineStrategy(flowFilters, flowObjectFilters, conditions);

return await strategy.search(conditions, orderBy, limit, cursorCondition, models);

const { strategy, conditions } = this.determineStrategy(
flowFilters,
flowObjectFilters,
cursorCondition
);

return await strategy.search(
conditions,
orderBy,
limit,
cursorCondition,
models
);
}





private prepareFlowObjectConditions(
prepareFlowObjectConditions(
flowObjectFilters: FlowObjectFilters[]
): Map<string, Map<string, number[]>> {
const flowObjectConditions = new Map<string, Map<string, number[]>>();
Expand Down Expand Up @@ -108,25 +95,33 @@ export class FlowSearchService {
return flowObjectConditions;
}

private prepareFlowConditions(flowFilters: SearchFlowsFilters): Map<string, any> {
const flowConditions = new Map<string, any>();
prepareFlowConditions(flowFilters: SearchFlowsFilters): any {
let flowConditions = {};

if (flowFilters) {
Object.entries(flowFilters).forEach(([key, value]) => {
if (value !== undefined) {
flowConditions.set(key, value);
flowConditions = { ...flowConditions, [key]: value };
}
});
}

return flowConditions;
}

private determineStrategy(flowFilters: SearchFlowsFilters, flowObjectFilters: FlowObjectFilters[], conditions: any): FlowSearchStrategy {
if ((!flowFilters && (!flowObjectFilters || flowObjectFilters.length === 0)) || (flowFilters && (!flowObjectFilters || flowObjectFilters.length === 0))) {
determineStrategy(
flowFilters: SearchFlowsFilters,
flowObjectFilters: FlowObjectFilters[],
conditions: any
): { strategy: FlowSearchStrategy; conditions: any } {
if (
(!flowFilters &&
(!flowObjectFilters || flowObjectFilters.length === 0)) ||
(flowFilters && (!flowObjectFilters || flowObjectFilters.length === 0))
) {
const flowConditions = this.prepareFlowConditions(flowFilters);
conditions = { ...conditions, ...flowConditions }
return this.onlyFlowFiltersStrategy;
conditions = { ...conditions, ...flowConditions };
return { strategy: this.onlyFlowFiltersStrategy, conditions };
}
// else if (!flowFilters && flowObjectFilters.length !== 0) {
// const flowObjectConditions = this.prepareFlowObjectConditions(flowObjectFilters);
Expand All @@ -139,7 +134,8 @@ export class FlowSearchService {
// return new BothFlowFiltersStrategy(this);
// }

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

}
4 changes: 1 addition & 3 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ export class FlowObjectFilters {
}

@InputType()
export class FlowCategory{

export class FlowCategory {
@Field({ nullable: false })
id: number;

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

}

@ArgsType()
Expand Down
2 changes: 1 addition & 1 deletion src/domain-services/flows/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Flow extends BaseFlow {
reportDetails: ReportDetail[];

@Field(() => [FlowParkedParentSource], { nullable: true })
parkedParentSource: FlowParkedParentSource[];
parkedParentSource: FlowParkedParentSource[] | null;
}

@ObjectType()
Expand Down
16 changes: 11 additions & 5 deletions src/domain-services/flows/strategy/flow-search-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Database } from "@unocha/hpc-api-core/src/db";
import { FlowSearchResult } from "../graphql/types";
import { Database } from '@unocha/hpc-api-core/src/db';
import { FlowSearchResult } from '../graphql/types';

export interface FlowSearchStrategy{
search(flowConditions: Map<string, any>, orderBy: any, limit: number, cursorCondition: any, models: Database): Promise<FlowSearchResult>;
}
export interface FlowSearchStrategy {
search(
flowConditions: Map<string, any>,
orderBy: any,
limit: number,
cursorCondition: any,
models: Database
): Promise<FlowSearchResult>;
}
Loading

0 comments on commit 56718c2

Please sign in to comment.