Skip to content

Commit 0cad09b

Browse files
authored
Merge pull request #3042 from SeedCompany/agl/add-project-sorting
2 parents a82534f + f67c7cc commit 0cad09b

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/components/project/dto/project.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { stripIndent } from 'common-tags';
44
import { DateTime } from 'luxon';
55
import { keys as keysOf } from 'ts-transformer-keys';
66
import { MergeExclusive } from 'type-fest';
7+
import { sortingForEnumIndex } from '~/core/database/query';
78
import { abstractType, e } from '~/core/edgedb';
89
import { RegisterResource } from '~/core/resources';
910
import {
1011
DateInterval,
1112
DateTimeField,
1213
DbLabel,
14+
DbSort,
1315
DbUnique,
1416
ID,
1517
IntersectionType,
@@ -44,7 +46,7 @@ import { Postable } from '../../post/dto';
4446
import { ProjectChangeRequest } from '../../project-change-request/dto';
4547
import { ProjectMember } from '../project-member/dto';
4648
import { ProjectStatus } from './project-status.enum';
47-
import { SecuredProjectStep } from './project-step.enum';
49+
import { ProjectStep, SecuredProjectStep } from './project-step.enum';
4850
import { ProjectType } from './project-type.enum';
4951

5052
type AnyProject = MergeExclusive<TranslationProject, InternshipProject>;
@@ -114,10 +116,12 @@ class Project extends Interfaces {
114116
middleware: [parentIdMiddleware],
115117
})
116118
@DbLabel('ProjectStep')
119+
@DbSort(sortingForEnumIndex(ProjectStep))
117120
readonly step: SecuredProjectStep;
118121

119122
@Field(() => ProjectStatus)
120123
@DbLabel('ProjectStatus')
124+
@DbSort(sortingForEnumIndex(ProjectStatus))
121125
readonly status: ProjectStatus;
122126

123127
readonly primaryLocation: Secured<ID | null>;

src/components/project/project.repository.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ export class ProjectRepository extends CommonRepository {
286286
query
287287
.apply(matchProjectSens('node'))
288288
.return<{ sortValue: string }>('sensitivity as sortValue'),
289+
engagements: (query) =>
290+
query
291+
.match([
292+
node('node'),
293+
relation('out', '', 'engagement'),
294+
node('engagement', 'LanguageEngagement'),
295+
])
296+
.return<{ sortValue: number }>('count(engagement) as sortValue'),
289297
}),
290298
)
291299
.apply(paginate(input, this.hydrate(session.userId)))

src/core/database/query/cypher-functions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const apoc = {
6969
},
7070
coll: {
7171
flatten: fn1('apoc.coll.flatten'),
72+
indexOf: fn('apoc.coll.indexOf'),
7273
},
7374
convert: {
7475
/** Converts Neo4j node to object/map of the node's properties */

src/core/database/query/lists.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { identity } from 'rxjs';
33
import {
44
getDbSortTransformer,
55
ID,
6+
MadeEnum,
67
Order,
78
PaginatedListType,
89
PaginationInput,
910
Resource,
1011
ResourceShape,
1112
} from '~/common';
12-
import { collect } from './cypher-functions';
13+
import { apoc, collect } from './cypher-functions';
1314
import { ACTIVE } from './matching';
1415

1516
/**
@@ -97,7 +98,7 @@ export const sorting =
9798
return query.comment`sorting(${sort})`
9899
.subQuery('*', matcher)
99100
.with('*')
100-
.orderBy(sortTransformer('sortValue'), order);
101+
.orderBy(`${sortTransformer('sortValue')}`, order);
101102
};
102103

103104
const matchPropSort = (prop: string) => (query: Query) =>
@@ -112,6 +113,14 @@ const matchPropSort = (prop: string) => (query: Query) =>
112113
const matchBasePropSort = (prop: string) => (query: Query) =>
113114
query.return(`node.${prop} as sortValue`);
114115

116+
export const sortingForEnumIndex =
117+
<T extends string>(theEnum: MadeEnum<T>) =>
118+
(variable: string) =>
119+
apoc.coll.indexOf(
120+
[...theEnum.values].map((v) => `"${v}"`),
121+
variable,
122+
);
123+
115124
export const whereNotDeletedInChangeset = (changeset?: ID) => (query: Query) =>
116125
changeset
117126
? query.raw(

0 commit comments

Comments
 (0)