Skip to content

Commit

Permalink
Fix nested sorting issue for 'planVersion'
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Apr 23, 2024
1 parent 44e15c2 commit 44760ef
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export class FlowService {
// using the flow-object relation if the entity is a flow-object
const entityCondKey = orderBy.entity as unknown;

const refDirection = orderBy.direction ?? 'source';

// Validate the variable using io-ts
const result = FlowObjectType.decode(entityCondKey);

Expand All @@ -171,21 +173,27 @@ export class FlowService {
let joinQuery = dbConnection
.queryBuilder()
.distinct('flowObject.flowID', 'flowObject.versionID')
.from('flowObject')
.where('objectType', entityCondKeyFlowObjectType);
.from('flowObject');

if (result._tag === 'Right') {
joinQuery = joinQuery
.join(entity, 'flowObject.objectID', `${entity}.id`)
.select(mappedOrderBy.column)
.select(`${entity}.${mappedOrderBy.column}`) // This is needed since we must select the column to order by
.where('flowObject.objectType', entityCondKeyFlowObjectType)
.andWhere('flowObject.refDirection', refDirection)
.orderBy(mappedOrderBy.column, mappedOrderBy.order);
} else {
// Collect fisrt part of the entity key by the fisrt Case letter
const entityKey = entity.slice(0, entity.search(/[A-Z]/));

joinQuery = joinQuery
.join(orderBy.entity, 'flowObject.objectID', `${entityKey}Id`)
.select(mappedOrderBy.column)
.join(
orderBy.entity,
'flowObject.objectID',
`${orderBy.entity}.${entityKey}Id`
)
.select(`${orderBy.entity}.${mappedOrderBy.column}`) // This is needed since we must select the column to order by
.where('flowObject.objectType', entityKey)
.andWhere('flowObject.refDirection', refDirection)
.orderBy(mappedOrderBy.column, mappedOrderBy.order);
}

Expand Down

0 comments on commit 44760ef

Please sign in to comment.