Skip to content

Commit

Permalink
Allow no sorting from client
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Jan 31, 2024
1 parent dd38861 commit 19134fe
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 49 deletions.
38 changes: 6 additions & 32 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ export class FlowSearchService {
// Categories Map follows the structure:
// flowID: { versionID: [categories]}
// So we need to get the categories for the flow version
const categories =
categoriesMap.get(flow.id)!.get(flow.versionID) ?? [];
const categories = categoriesMap.get(flow.id);
const categoriesByVersion = categories?.get(flow.versionID) ?? [];
const organizations = organizationsMap.get(flow.id) ?? [];
const locations = locationsMap.get(flow.id) ?? [];
const plans = plansMap.get(flow.id) ?? [];
Expand Down Expand Up @@ -253,7 +253,7 @@ export class FlowSearchService {

return this.buildFlowDTO(
flow,
categories,
categoriesByVersion,
organizations,
locations,
plans,
Expand All @@ -267,27 +267,6 @@ export class FlowSearchService {
})
);

// const isOrderByForFlows = orderBy.entity === 'flow';
// const firstItem = items[0];
// const prevPageCursorEntity = isOrderByForFlows
// ? firstItem
// : firstItem[orderBy.entity as keyof typeof firstItem];
// const prevPageCursorValue = prevPageCursorEntity
// ? prevPageCursorEntity[
// orderBy.column as keyof typeof prevPageCursorEntity
// ] ?? ''
// : '';

// const lastItem = items.at(-1);
// const nextPageCursorEntity = isOrderByForFlows
// ? lastItem
// : lastItem![orderBy.entity as keyof typeof lastItem];
// const nextPageCursorValue = nextPageCursorEntity
// ? nextPageCursorEntity[
// orderBy.column as keyof typeof nextPageCursorEntity
// ]?.toString() ?? ''
// : '';

return {
flows: items,
hasNextPage: limit <= flows.length,
Expand Down Expand Up @@ -343,7 +322,7 @@ export class FlowSearchService {
operation: filter.flag ? Op.IN : Op.NOT_IN,
}));

return shortcutFilters;
return shortcutFilters.length > 0 ? shortcutFilters : null;
}

determineStrategy(
Expand All @@ -360,7 +339,8 @@ export class FlowSearchService {
// If there are no sortByEntity (orderBy.entity === 'flow')
// but flowFilters only
// use onlyFlowFiltersStrategy
const isOrderByEntityFlow = orderBy?.entity === 'flow';
const isOrderByEntityFlow =
orderBy === undefined || orderBy?.entity === 'flow';
const isFlowFiltersDefined = flowFilters !== undefined;
const isFlowObjectFiltersDefined = flowObjectFilters !== undefined;
const isFlowCategoryFiltersDefined = flowCategoryFilters !== undefined;
Expand Down Expand Up @@ -764,12 +744,6 @@ export class FlowSearchService {
}

// Validate the shortcut filters
// There must be only one shortcut filter
// if only one is defined
// return an object like
// { category: 'Parked', operation: 'IN' }
// if more than one is defined
// throw an error
const shortcutFilter = this.mapShortcutFilters(
isPendingFlows,
isCommitmentFlows,
Expand Down
23 changes: 14 additions & 9 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ export class FlowService {

// Get the entity list
const mappedOrderBy = mapFlowOrderBy(orderBy);
const entityList = await dbConnection
.queryBuilder()
.select(
orderBy.subEntity ? `${orderBy.subEntity}Id` : 'id',
'flowID',
'versionID'
)
.from(entity)
.orderBy(mappedOrderBy.column, mappedOrderBy.order);

let mapFlowsToUniqueFlowEntities;

Expand All @@ -89,6 +80,13 @@ export class FlowService {
const result = FlowObjectType.decode(entityCondKey);

if (result._tag === 'Right') {
const entityList = await dbConnection
.queryBuilder()
.select('id')
.from(entity)
.orderBy(mappedOrderBy.column, mappedOrderBy.order)
.orderBy('id', mappedOrderBy.order);

const entityIDs = entityList.map((entity) => entity.id);
const entityCondKeyFlowObjectType = entityCondKey as FlowObjectType;
let query = dbConnection
Expand Down Expand Up @@ -116,6 +114,13 @@ export class FlowService {
versionID: flowID.versionID,
}));
} else {
const entityList = await dbConnection
.queryBuilder()
.select(`${orderBy.subEntity}Id`, 'flowID', 'versionID')
.from(entity)
.orderBy(mappedOrderBy.column, mappedOrderBy.order)
.orderBy('id', mappedOrderBy.order);

mapFlowsToUniqueFlowEntities = entityList.map((entity) => ({
id: entity.flowID,
versionID: entity.versionID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Cond } from '@unocha/hpc-api-core/src/db/util/conditions';
import { Service } from 'typedi';
import { FlowService } from '../../flow-service';
import { type SearchFlowsFilters } from '../../graphql/args';
import { type FlowEntity, type UniqueFlowEntity } from '../../model';
import {
type FlowSearchArgs,
Expand Down Expand Up @@ -219,9 +220,9 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
);

// Once the list of elements is reduced, we need to build the conditions
const searchConditions = this.buildConditions(reducedFlows);
const searchConditions = this.buildConditions(reducedFlows, flowFilters);

const orderByForFlow = mapFlowOrderBy(orderBy);
const orderByForFlow = { column: 'updatedAt', order: 'DESC' };

const flows = await this.flowService.getFlows({
models,
Expand All @@ -232,11 +233,21 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
return { flows, count };
}

buildConditions(uniqueFlowEntities: UniqueFlowEntity[]): any {
buildConditions(
uniqueFlowEntities: UniqueFlowEntity[],
flowFilters: SearchFlowsFilters
): any {
const whereClauses = uniqueFlowEntities.map((flow) => ({
[Cond.AND]: [{ id: flow.id }, { versionID: flow.versionID }],
}));

if (flowFilters) {
const flowConditions = prepareFlowConditions(flowFilters);
return {
[Cond.AND]: [flowConditions, { [Cond.OR]: whereClauses }],
};
}

return {
[Cond.OR]: whereClauses,
};
Expand Down
31 changes: 26 additions & 5 deletions src/domain-services/flows/strategy/impl/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,37 @@ export function mergeFlowIDsFromFilteredFlowObjectsAndFlowCategories(
);
}

export function mapFlowOrderBy(orderBy: any) {
export const sortingColumnMapping: Map<string, string> = new Map<
string,
string
>([
['reporterRefCode', 'refCode'],
['sourceID', 'sourceID'],
]);

export function mapFlowOrderBy(orderBy: any): {
column: string;
order: string;
} {
if (!orderBy) {
return { column: 'updatedAt', order: 'DESC' };
}
let orderByForFlow = { column: orderBy.column, order: orderBy.order };
if (orderBy.entity !== 'flow') {
orderByForFlow = { column: 'updatedAt', order: 'DESC' };

if (orderBy.entity === 'flow') {
return { column: orderBy.column, order: orderBy.order };
}

let columnToSort: string;
if (sortingColumnMapping.has(orderBy.column)) {
// I don't like this but the compiler is complaining
// that columnToSort might be undefined if I don't do this
// but it's already checked that the column exists in the map
columnToSort = sortingColumnMapping.get(orderBy.column) ?? 'updatedAt';
} else {
columnToSort = orderBy.column;
}

return orderByForFlow;
return { column: columnToSort, order: orderBy.order };
}

export function prepareFlowConditions(flowFilters: SearchFlowsFilters): any {
Expand Down

0 comments on commit 19134fe

Please sign in to comment.