From e1c9d02da9a66a7dec60ef98ad70e00c5a2cb0d2 Mon Sep 17 00:00:00 2001 From: manelcecs Date: Fri, 15 Dec 2023 17:14:00 +0100 Subject: [PATCH] Move 'pending' flow category as shortcut --- .../flows/flow-search-service.ts | 42 +++++++++++++----- src/domain-services/flows/graphql/args.ts | 9 ++-- .../flows/strategy/flow-search-strategy.ts | 3 +- .../flows/strategy/flowID-search-strategy.ts | 9 +++- .../flow-object-conditions-strategy-impl.ts | 43 +++++++++++-------- ...-flow-category-conditions-strategy-impl.ts | 9 ++-- ...Ids-flow-mixed-conditions-strategy-impl.ts | 6 ++- .../flows/strategy/impl/utils.ts | 3 +- 8 files changed, 84 insertions(+), 40 deletions(-) diff --git a/src/domain-services/flows/flow-search-service.ts b/src/domain-services/flows/flow-search-service.ts index fbf8f946..b8bb18aa 100644 --- a/src/domain-services/flows/flow-search-service.ts +++ b/src/domain-services/flows/flow-search-service.ts @@ -62,8 +62,14 @@ export class FlowSearchService { models: Database, filters: SearchFlowsArgs ): Promise { - const { limit, nextPageCursor, prevPageCursor, sortField, sortOrder } = - filters; + const { + limit, + nextPageCursor, + prevPageCursor, + sortField, + sortOrder, + pending: isPendingFlows, + } = filters; const orderBy: FlowOrderBy = this.buildOrderBy(sortField, sortOrder); @@ -79,7 +85,8 @@ export class FlowSearchService { const { strategy, conditions } = this.determineStrategy( flowFilters, flowObjectFilters, - flowCategoryFilters + flowCategoryFilters, + isPendingFlows ); // Fetch one more item to check for hasNextPage @@ -91,7 +98,8 @@ export class FlowSearchService { models, orderBy, limitComputed, - cursorCondition + cursorCondition, + isPendingFlows ); // Remove the extra item used to check hasNextPage @@ -432,7 +440,8 @@ export class FlowSearchService { determineStrategy( flowFilters: SearchFlowsFilters, flowObjectFilters: FlowObjectFilters[], - flowCategoryFilters: FlowCategoryFilters + flowCategoryFilters: FlowCategoryFilters, + isFilterByPendingFlows: boolean ): { strategy: FlowSearchStrategy; conditions: any } { const isFlowFilterDefined = flowFilters !== undefined; const isFlowObjectFilterDefined = flowObjectFilters !== undefined; @@ -441,20 +450,27 @@ export class FlowSearchService { const isFlowCategoryFilterDefined = flowCategoryFilters !== undefined; + const isFilterByPendingFlowsDefined = isFilterByPendingFlows !== undefined; if ( (!isFlowFilterDefined && (!isFlowObjectFilterDefined || !isFlowObjectFiltersNotEmpty) && - !isFlowCategoryFilterDefined) || + !isFlowCategoryFilterDefined && + !isFilterByPendingFlowsDefined) || (isFlowFilterDefined && (!isFlowObjectFilterDefined || !isFlowObjectFiltersNotEmpty) && - !isFlowCategoryFilterDefined) + !isFlowCategoryFilterDefined && + !isFilterByPendingFlowsDefined) ) { const flowConditions = this.prepareFlowConditions(flowFilters); return { strategy: this.onlyFlowFiltersStrategy, conditions: flowConditions, }; - } else if (isFlowObjectFiltersNotEmpty || isFlowCategoryFilterDefined) { + } else if ( + isFlowObjectFiltersNotEmpty || + isFlowCategoryFilterDefined || + isFilterByPendingFlowsDefined + ) { const flowConditions = this.prepareFlowConditions(flowFilters); const flowObjectConditions = this.prepareFlowObjectConditions(flowObjectFilters); @@ -632,12 +648,18 @@ export class FlowSearchService { models: Database, args: SearchFlowsArgsNonPaginated ): Promise { - const { flowFilters, flowObjectFilters, flowCategoryFilters } = args; + const { + flowFilters, + flowObjectFilters, + flowCategoryFilters, + pending: isPendingFlows, + } = args; const { strategy, conditions } = this.determineStrategy( flowFilters, flowObjectFilters, - flowCategoryFilters + flowCategoryFilters, + isPendingFlows ); const { flows, count } = await strategy.search(conditions, models); diff --git a/src/domain-services/flows/graphql/args.ts b/src/domain-services/flows/graphql/args.ts index b58bfd69..f3241db1 100644 --- a/src/domain-services/flows/graphql/args.ts +++ b/src/domain-services/flows/graphql/args.ts @@ -31,9 +31,6 @@ export class SearchFlowsFilters { @InputType() export class FlowCategoryFilters { - @Field({ nullable: true }) - pending: boolean; - @Field(() => [FlowCategory], { nullable: true }) categoryFilters: FlowCategory[]; } @@ -83,6 +80,9 @@ export class SearchFlowsArgs extends PaginationArgs { @Field({ nullable: true }) flowCategoryFilters: FlowCategoryFilters; + + @Field({ nullable: true }) + pending: boolean; } @ArgsType() @@ -98,4 +98,7 @@ export class SearchFlowsArgsNonPaginated { @Field({ nullable: true }) flowCategoryFilters: FlowCategoryFilters; + + @Field({ nullable: true }) + pending: boolean; } diff --git a/src/domain-services/flows/strategy/flow-search-strategy.ts b/src/domain-services/flows/strategy/flow-search-strategy.ts index c2cb3a19..189a2758 100644 --- a/src/domain-services/flows/strategy/flow-search-strategy.ts +++ b/src/domain-services/flows/strategy/flow-search-strategy.ts @@ -14,6 +14,7 @@ export interface FlowSearchStrategy { models: Database, orderBy?: any, limit?: number, - cursorCondition?: any + cursorCondition?: any, + filterByPendingFlows?: boolean ): Promise; } diff --git a/src/domain-services/flows/strategy/flowID-search-strategy.ts b/src/domain-services/flows/strategy/flowID-search-strategy.ts index 4a721325..a8af57df 100644 --- a/src/domain-services/flows/strategy/flowID-search-strategy.ts +++ b/src/domain-services/flows/strategy/flowID-search-strategy.ts @@ -10,8 +10,13 @@ export interface FlowIDSearchStrategy { search( models: Database, flowObjectsConditions: Map>, - flowCategoryConditions: FlowCategoryFilters + flowCategoryConditions: FlowCategoryFilters, + filterByPendingFlows?: boolean ): Promise; - generateWhereClause(flowIds: FlowId[], conditions: any): any; + generateWhereClause( + flowIds: FlowId[], + conditions: any, + filterByPendingFlows?: boolean + ): any; } diff --git a/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy-impl.ts b/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy-impl.ts index 02653f11..d5ddf99a 100644 --- a/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy-impl.ts +++ b/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy-impl.ts @@ -32,7 +32,8 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { models: Database, orderBy?: any, limit?: number, - cursorCondition?: any + cursorCondition?: any, + filterByPendingFlows?: boolean ): Promise { const flowConditionsMap = flowConditions.conditionsMap; // Obtain flowObjects conditions @@ -49,26 +50,27 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { const searchFlowIdsStrategy: FlowIDSearchStrategy = this.determineStrategy( flowObjectsConditions, - flowCategoryConditions + flowCategoryConditions, + filterByPendingFlows ); const { flowIDs: flowIdsToFilter }: FlowIdSearchStrategyResponse = await searchFlowIdsStrategy.search( models, flowObjectsConditions, - flowCategoryConditions + flowCategoryConditions, + filterByPendingFlows ); + const whereClauseFromStrategy = searchFlowIdsStrategy.generateWhereClause( + flowIdsToFilter, + flowCategoryConditions, + filterByPendingFlows + ); + // Combine conditions from flowObjects FlowIDs and flow conditions const countConditions = { - [Cond.AND]: [ - flowEntityConditions ?? {}, - - searchFlowIdsStrategy.generateWhereClause( - flowIdsToFilter, - flowCategoryConditions - ), - ], + [Cond.AND]: [flowEntityConditions ?? {}, whereClauseFromStrategy ?? {}], }; // Combine cursor condition with flow conditions @@ -76,10 +78,7 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { [Cond.AND]: [ flowEntityConditions ?? {}, cursorCondition ?? {}, - searchFlowIdsStrategy.generateWhereClause( - flowIdsToFilter, - flowCategoryConditions - ), + whereClauseFromStrategy ?? {}, ], }; @@ -111,12 +110,14 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { // otherwise keep all flowIDs from the one that is not empty determineStrategy( flowObjectsConditions: Map>, - flowCategoryConditions: any + flowCategoryConditions: any, + filterByPendingFlows?: boolean ): any { const isFlowObjectsConditionsIsDefined = flowObjectsConditions !== undefined; const isFlowCategoryConditionsIsDefined = flowCategoryConditions !== undefined; + const isFilterByPendingFlowsIsDefined = filterByPendingFlows !== undefined; const flowObjectsConditionsIsNotEmpty = isFlowObjectsConditionsIsDefined && flowObjectsConditions.size; @@ -124,11 +125,17 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { isFlowCategoryConditionsIsDefined && Object.keys(flowCategoryConditions).length; - if (flowObjectsConditionsIsNotEmpty && flowCategoryConditionsIsNotEmpty) { + if ( + flowObjectsConditionsIsNotEmpty && + (flowCategoryConditionsIsNotEmpty || isFilterByPendingFlowsIsDefined) + ) { return this.getFlowIdsFromMixedConditions; } else if (flowObjectsConditionsIsNotEmpty) { return this.getFlowIdsFromObjectConditions; - } else if (flowCategoryConditionsIsNotEmpty) { + } else if ( + flowCategoryConditionsIsNotEmpty || + isFilterByPendingFlowsIsDefined + ) { return this.getFlowIdsFromCategoryConditions; } throw new Error( diff --git a/src/domain-services/flows/strategy/impl/get-flowIds-flow-category-conditions-strategy-impl.ts b/src/domain-services/flows/strategy/impl/get-flowIds-flow-category-conditions-strategy-impl.ts index 3e95bc83..b95b7523 100644 --- a/src/domain-services/flows/strategy/impl/get-flowIds-flow-category-conditions-strategy-impl.ts +++ b/src/domain-services/flows/strategy/impl/get-flowIds-flow-category-conditions-strategy-impl.ts @@ -21,9 +21,11 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl async search( models: Database, _flowObjectsConditions: Map>, - flowCategoryConditions: FlowCategoryFilters + flowCategoryConditions: FlowCategoryFilters, + filterByPendingFlows: boolean ): Promise { const whereClause = mapFlowCategoryConditionsToWhereClause( + filterByPendingFlows, flowCategoryConditions ); @@ -55,9 +57,10 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl generateWhereClause( flowIds: FlowId[], - flowCategoryConditions: FlowCategoryFilters + _conditions: any, + filterByPendingFlows: boolean ) { - const operation = flowCategoryConditions.pending ? Op.IN : Op.NOT_IN; + const operation = filterByPendingFlows === true ? Op.IN : Op.NOT_IN; return { id: { [operation]: flowIds, diff --git a/src/domain-services/flows/strategy/impl/get-flowIds-flow-mixed-conditions-strategy-impl.ts b/src/domain-services/flows/strategy/impl/get-flowIds-flow-mixed-conditions-strategy-impl.ts index 99ff6626..2882bde0 100644 --- a/src/domain-services/flows/strategy/impl/get-flowIds-flow-mixed-conditions-strategy-impl.ts +++ b/src/domain-services/flows/strategy/impl/get-flowIds-flow-mixed-conditions-strategy-impl.ts @@ -23,7 +23,8 @@ export class GetFlowIdsFromMixedConditionsStrategyImpl async search( models: Database, flowObjectsConditions: Map>, - flowCategoryConditions: FlowCategoryFilters + flowCategoryConditions: FlowCategoryFilters, + filterByPendingFlows: boolean ): Promise { const { flowIDs: flowIdsFromFlowObjects }: FlowIdSearchStrategyResponse = await this.getFlowIdsFromObjectConditionsStrategy.search( @@ -35,7 +36,8 @@ export class GetFlowIdsFromMixedConditionsStrategyImpl await this.getFlowIdsFromCategoryConditionsStrategy.search( models, flowObjectsConditions, - flowCategoryConditions + flowCategoryConditions, + filterByPendingFlows ); const mergeFlowIDs: FlowId[] = diff --git a/src/domain-services/flows/strategy/impl/utils.ts b/src/domain-services/flows/strategy/impl/utils.ts index caf23b31..b9d11f3d 100644 --- a/src/domain-services/flows/strategy/impl/utils.ts +++ b/src/domain-services/flows/strategy/impl/utils.ts @@ -38,11 +38,12 @@ export function mapFlowObjectConditionsToWhereClause( } export function mapFlowCategoryConditionsToWhereClause( + filterByPendingFlows: boolean, flowCategoryConditions: FlowCategoryFilters ) { let whereClause = {}; - if (flowCategoryConditions.pending !== undefined) { + if (filterByPendingFlows !== undefined) { whereClause = { group: 'inactiveReason', name: 'Pending review',