Skip to content

Commit

Permalink
Move 'pending' flow category as shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Dec 18, 2023
1 parent be18378 commit e1c9d02
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 40 deletions.
42 changes: 32 additions & 10 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ export class FlowSearchService {
models: Database,
filters: SearchFlowsArgs
): Promise<FlowSearchResult> {
const { limit, nextPageCursor, prevPageCursor, sortField, sortOrder } =
filters;
const {
limit,
nextPageCursor,
prevPageCursor,
sortField,
sortOrder,
pending: isPendingFlows,
} = filters;

const orderBy: FlowOrderBy = this.buildOrderBy(sortField, sortOrder);

Expand All @@ -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
Expand All @@ -91,7 +98,8 @@ export class FlowSearchService {
models,
orderBy,
limitComputed,
cursorCondition
cursorCondition,
isPendingFlows
);

// Remove the extra item used to check hasNextPage
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -632,12 +648,18 @@ export class FlowSearchService {
models: Database,
args: SearchFlowsArgsNonPaginated
): Promise<FlowSearchTotalAmountResult> {
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);
Expand Down
9 changes: 6 additions & 3 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export class SearchFlowsFilters {

@InputType()
export class FlowCategoryFilters {
@Field({ nullable: true })
pending: boolean;

@Field(() => [FlowCategory], { nullable: true })
categoryFilters: FlowCategory[];
}
Expand Down Expand Up @@ -83,6 +80,9 @@ export class SearchFlowsArgs extends PaginationArgs<FlowSortField> {

@Field({ nullable: true })
flowCategoryFilters: FlowCategoryFilters;

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

@ArgsType()
Expand All @@ -98,4 +98,7 @@ export class SearchFlowsArgsNonPaginated {

@Field({ nullable: true })
flowCategoryFilters: FlowCategoryFilters;

@Field({ nullable: true })
pending: boolean;
}
3 changes: 2 additions & 1 deletion src/domain-services/flows/strategy/flow-search-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface FlowSearchStrategy {
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
cursorCondition?: any,
filterByPendingFlows?: boolean
): Promise<FlowSearchStrategyResponse>;
}
9 changes: 7 additions & 2 deletions src/domain-services/flows/strategy/flowID-search-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ export interface FlowIDSearchStrategy {
search(
models: Database,
flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: FlowCategoryFilters
flowCategoryConditions: FlowCategoryFilters,
filterByPendingFlows?: boolean
): Promise<FlowIdSearchStrategyResponse>;

generateWhereClause(flowIds: FlowId[], conditions: any): any;
generateWhereClause(
flowIds: FlowId[],
conditions: any,
filterByPendingFlows?: boolean
): any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
cursorCondition?: any,
filterByPendingFlows?: boolean
): Promise<FlowSearchStrategyResponse> {
const flowConditionsMap = flowConditions.conditionsMap;
// Obtain flowObjects conditions
Expand All @@ -49,37 +50,35 @@ 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
const searchConditions = {
[Cond.AND]: [
flowEntityConditions ?? {},
cursorCondition ?? {},
searchFlowIdsStrategy.generateWhereClause(
flowIdsToFilter,
flowCategoryConditions
),
whereClauseFromStrategy ?? {},
],
};

Expand Down Expand Up @@ -111,24 +110,32 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {
// otherwise keep all flowIDs from the one that is not empty
determineStrategy(
flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: any
flowCategoryConditions: any,
filterByPendingFlows?: boolean
): any {
const isFlowObjectsConditionsIsDefined =
flowObjectsConditions !== undefined;
const isFlowCategoryConditionsIsDefined =
flowCategoryConditions !== undefined;
const isFilterByPendingFlowsIsDefined = filterByPendingFlows !== undefined;

const flowObjectsConditionsIsNotEmpty =
isFlowObjectsConditionsIsDefined && flowObjectsConditions.size;
const flowCategoryConditionsIsNotEmpty =
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ export class GetFlowIdsFromCategoryConditionsStrategyImpl
async search(
models: Database,
_flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: FlowCategoryFilters
flowCategoryConditions: FlowCategoryFilters,
filterByPendingFlows: boolean
): Promise<FlowIdSearchStrategyResponse> {
const whereClause = mapFlowCategoryConditionsToWhereClause(
filterByPendingFlows,
flowCategoryConditions
);

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class GetFlowIdsFromMixedConditionsStrategyImpl
async search(
models: Database,
flowObjectsConditions: Map<string, Map<string, number[]>>,
flowCategoryConditions: FlowCategoryFilters
flowCategoryConditions: FlowCategoryFilters,
filterByPendingFlows: boolean
): Promise<FlowIdSearchStrategyResponse> {
const { flowIDs: flowIdsFromFlowObjects }: FlowIdSearchStrategyResponse =
await this.getFlowIdsFromObjectConditionsStrategy.search(
Expand All @@ -35,7 +36,8 @@ export class GetFlowIdsFromMixedConditionsStrategyImpl
await this.getFlowIdsFromCategoryConditionsStrategy.search(
models,
flowObjectsConditions,
flowCategoryConditions
flowCategoryConditions,
filterByPendingFlows
);

const mergeFlowIDs: FlowId[] =
Expand Down
3 changes: 2 additions & 1 deletion src/domain-services/flows/strategy/impl/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit e1c9d02

Please sign in to comment.