Skip to content

Commit

Permalink
Definitive usage of search V2 method
Browse files Browse the repository at this point in the history
Removed old search and rename 'searchV2' to 'search'
  • Loading branch information
manelcecs committed Dec 29, 2023
1 parent bca2a88 commit 332accd
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 648 deletions.
411 changes: 30 additions & 381 deletions src/domain-services/flows/flow-search-service.ts

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FlowService {
models: Database,
dbConnection: Knex,
orderBy: FlowOrderBy,
limit: number
limit?: number
): Promise<FlowId[]> {
const entity = orderBy.subEntity ?? orderBy.entity;

Expand All @@ -58,8 +58,11 @@ export class FlowService {
.andWhere('objectType', entityCondKey)
.andWhere('refDirection', orderBy.direction!)
.orderByRaw(`array_position(ARRAY[${entityIDs.join(',')}], "objectID")`)
.orderBy('flowID', orderBy.order)
.limit(limit);
.orderBy('flowID', orderBy.order);

if (limit) {
query.limit(limit);
}

const flowIDs = await query;
return flowIDs.map((flowID) => flowID.flowID);
Expand Down
38 changes: 34 additions & 4 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@ import { ArgsType, Field, InputType } from 'type-graphql';
import { PaginationArgs } from '../../../utils/graphql/pagination';
import { type FlowSortField } from './types';

@InputType()
export class FlowStatusFilters {
@Field(() => Boolean, { nullable: true })
pending: boolean | null;

@Field(() => Boolean, { nullable: true })
commitment: boolean | null;

@Field(() => Boolean, { nullable: true })
paid: boolean | null;

@Field(() => Boolean, { nullable: true })
pledged: boolean | null;
}

@InputType()
export class FlowTypeFilters {
@Field(() => Boolean, { nullable: true })
carryover: boolean | null;

@Field(() => Boolean, { nullable: true })
parked: boolean | null;

@Field(() => Boolean, { nullable: true })
pass_through: boolean | null;

@Field(() => Boolean, { nullable: true })
standard: boolean | null;
}

@InputType()
export class SearchFlowsFilters {
@Field(() => [Number], { nullable: true })
Expand Down Expand Up @@ -80,8 +110,8 @@ export class SearchFlowsArgs extends PaginationArgs<FlowSortField> {
@Field(() => [FlowObjectFilters], { nullable: true })
flowObjectFilters: FlowObjectFilters[];

@Field({ nullable: true })
includeChildrenOfParkedFlows: boolean;
@Field({ name: 'includeChildrenOfParkedFlows', nullable: true })
shouldIncludeChildrenOfParkedFlows: boolean;

@Field(() => [FlowCategory], { nullable: true })
flowCategoryFilters: FlowCategory[];
Expand All @@ -98,8 +128,8 @@ export class SearchFlowsArgsNonPaginated {
@Field(() => [FlowObjectFilters], { nullable: true })
flowObjectFilters: FlowObjectFilters[];

@Field({ nullable: true })
includeChildrenOfParkedFlows: boolean;
@Field({ name: 'includeChildrenOfParkedFlows', nullable: true })
shouldIncludeChildrenOfParkedFlows: boolean;

@Field(() => [FlowCategory], { nullable: true })
flowCategoryFilters: FlowCategory[];
Expand Down
14 changes: 11 additions & 3 deletions src/domain-services/flows/graphql/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class FlowResolver {
@Args(() => SearchFlowsArgs, { validate: false })
args: SearchFlowsArgs
): Promise<FlowSearchResult> {
return await this.flowSearchService.searchV2(
return await this.flowSearchService.search(
context.models,
context.connection,
args
Expand All @@ -34,7 +34,11 @@ export default class FlowResolver {
@Args(() => SearchFlowsArgsNonPaginated, { validate: false })
args: SearchFlowsArgsNonPaginated
): Promise<FlowSearchTotalAmountResult> {
return await this.flowSearchService.searchTotalAmount(context.models, args);
return await this.flowSearchService.searchTotalAmount(
context.models,
context.connection,
args
);
}

@Query(() => FlowSearchResultNonPaginated)
Expand All @@ -45,6 +49,10 @@ export default class FlowResolver {
): Promise<FlowSearchResultNonPaginated> {
// Set default batch size to 1000
args.limit = args.limit > 0 ? args.limit : 1000;
return await this.flowSearchService.searchBatches(context.models, args);
return await this.flowSearchService.searchBatches(
context.models,
context.connection,
args
);
}
}
35 changes: 13 additions & 22 deletions src/domain-services/flows/strategy/flow-search-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,18 @@ export interface FlowSearchStrategyResponse {
count: number;
}

export interface FlowSearchStrategy {
search(
flowConditions:
| Map<string, any>
| { conditionsMap: Map<string, any>; flowCategoryFilters: any },
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any,
filterByPendingFlows?: boolean
): Promise<FlowSearchStrategyResponse>;
export interface FlowSearchArgs {
models: Database;
databaseConnection: Knex;
flowFilters: SearchFlowsFilters;
flowObjectFilters: FlowObjectFilters[];
flowCategoryFilters: FlowCategory[];
limit?: number;
orderBy?: any;
cursorCondition?: any;
searchPendingFlows?: boolean;
}

searchV2(
models: Database,
databaseConnection: Knex,
limit: number,
orderBy: any,
cursorCondition: any | undefined,
flowFilters: SearchFlowsFilters,
flowObjectFilters: FlowObjectFilters[],
flowCategoryFilters: FlowCategory[],
searchPendingFlows: boolean | undefined
): Promise<FlowSearchStrategyResponse>;
export interface FlowSearchStrategy {
search(args: FlowSearchArgs): Promise<FlowSearchStrategyResponse>;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { type Database } from '@unocha/hpc-api-core/src/db';
import { Cond } from '@unocha/hpc-api-core/src/db/util/conditions';
import type Knex from 'knex';
import { Service } from 'typedi';
import { FlowService } from '../../flow-service';
import {
type FlowCategory,
type FlowObjectFilters,
type SearchFlowsFilters,
} from '../../graphql/args';
import { type FlowOrderBy } from '../../model';
import {
type FlowSearchArgs,
type FlowSearchStrategy,
type FlowSearchStrategyResponse,
} from '../flow-search-strategy';
Expand All @@ -23,43 +16,8 @@ import {
export class OnlyFlowFiltersStrategy implements FlowSearchStrategy {
constructor(private readonly flowService: FlowService) {}

async search(
flowConditions: any,
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
): Promise<FlowSearchStrategyResponse> {
// Build conditions object
const searchConditions = {
[Cond.AND]: [flowConditions ?? {}, cursorCondition ?? {}],
};

// check and map orderBy to be from entity 'flow'
const orderByFlow = mapFlowOrderBy(orderBy);

const [flows, countRes] = await Promise.all([
this.flowService.getFlows(models, searchConditions, orderByFlow, limit),
this.flowService.getFlowsCount(models, flowConditions),
]);

// Map count result query to count object
const countObject = countRes[0] as { count: number };

return { flows, count: countObject.count };
}

async searchV2(
models: Database,
_databaseConnection: Knex,
limit: number,
orderBy: FlowOrderBy,
cursorCondition: any | undefined,
flowFilters: SearchFlowsFilters,
_flowObjectFilters: FlowObjectFilters[],
_flowCategoryFilters: FlowCategory[],
_searchPendingFlows: boolean | undefined
): Promise<FlowSearchStrategyResponse> {
async search(args: FlowSearchArgs): Promise<FlowSearchStrategyResponse> {
const { models, flowFilters, orderBy, limit, cursorCondition } = args;
// Map flowConditions to where clause
const flowConditions = prepareFlowConditions(flowFilters);

Expand Down
Loading

0 comments on commit 332accd

Please sign in to comment.