Skip to content

Commit 396bcb4

Browse files
authored
Add sorting for progress summaries (#3306)
1 parent 4db4ce2 commit 396bcb4

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/components/progress-report/progress-report-extra-for-periodic-interface.repository.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import { Injectable } from '@nestjs/common';
2-
import { node, relation } from 'cypher-query-builder';
2+
import { mapEntries } from '@seedcompany/common';
3+
import { node, not, relation } from 'cypher-query-builder';
34
import {
45
CreateNodeOptions,
56
DefinedSorters,
67
defineSorters,
78
exp,
9+
path,
810
QueryFragment,
11+
SortCol,
912
SortFieldOf,
13+
SortMatcher,
1014
sortWith,
15+
variable,
1116
} from '~/core/database/query';
1217
import { engagementSorters } from '../engagement/engagement.repository';
1318
import { MergePeriodicReports } from '../periodic-report/dto';
19+
import { SummaryPeriod } from '../progress-summary/dto';
20+
import { progressSummarySorters } from '../progress-summary/progress-summary.repository';
1421
import { ProgressReport, ProgressReportStatus as Status } from './dto';
1522

1623
@Injectable()
@@ -52,4 +59,37 @@ export const progressReportExtrasSorters: DefinedSorters<
5259
node('node', 'LanguageEngagement'),
5360
])
5461
.apply(sortWith(engagementSorters, input)),
62+
...mapEntries(
63+
[
64+
{ field: 'cumulativeSummary', period: SummaryPeriod.Cumulative },
65+
{ field: 'fiscalYearSummary', period: SummaryPeriod.FiscalYearSoFar },
66+
{ field: 'periodSummary', period: SummaryPeriod.ReportPeriod },
67+
],
68+
({ field, period }) => {
69+
const periodVar = { period: variable(`"${period}"`) };
70+
const matcher: SortMatcher<string> = (query, input) =>
71+
query
72+
.with('node as report')
73+
.match([
74+
node('report'),
75+
relation('out', '', 'summary'),
76+
node('node', 'ProgressSummary', periodVar),
77+
])
78+
.apply(sortWith(progressSummarySorters, input))
79+
.union()
80+
.with('node')
81+
.with('node as report')
82+
.where(
83+
not(
84+
path([
85+
node('report'),
86+
relation('out', '', 'summary'),
87+
node('', 'ProgressSummary', periodVar),
88+
]),
89+
),
90+
)
91+
.return<SortCol>('null as sortValue');
92+
return [`${field}.*`, matcher];
93+
},
94+
).asRecord,
5595
});

src/components/progress-summary/dto/progress-summary.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { LinkTo } from '~/core/resources';
1313
export abstract class ProgressSummary {
1414
static readonly Props = keysOf<ProgressSummary>();
1515
static readonly SecuredProps = keysOf<SecuredProps<ProgressSummary>>();
16+
static readonly BaseNodeProps = ['planned', 'actual'];
1617

1718
@Field(() => Float)
1819
planned: number;

src/components/progress-summary/progress-summary.repository.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { Injectable } from '@nestjs/common';
2-
import { inArray, node, relation } from 'cypher-query-builder';
2+
import { mapValues } from '@seedcompany/common';
3+
import { inArray, node, Query, relation } from 'cypher-query-builder';
34
import { ID } from '~/common';
45
import { CommonRepository } from '~/core/database';
5-
import { ACTIVE, listConcat, merge } from '~/core/database/query';
6+
import {
7+
ACTIVE,
8+
defineSorters,
9+
listConcat,
10+
merge,
11+
SortCol,
12+
} from '~/core/database/query';
613
import { ProgressReport } from '../progress-report/dto';
714
import { FetchedSummaries, ProgressSummary, SummaryPeriod } from './dto';
815

@@ -80,3 +87,11 @@ export class ProgressSummaryRepository extends CommonRepository {
8087
.run();
8188
}
8289
}
90+
91+
export const progressSummarySorters = defineSorters(ProgressSummary, {
92+
...mapValues.fromList(
93+
['variance', 'scheduleStatus'],
94+
() => (query: Query) =>
95+
query.return<SortCol>('(node.actual - node.planned) as sortValue'),
96+
).asRecord,
97+
});

src/core/database/query/sorting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export interface SortCol {
171171
export type SortFieldOf<TResourceStatic extends ResourceShape<any>> =
172172
LiteralUnion<keyof TResourceStatic['prototype'] & string, string>;
173173

174-
type SortMatcher<Field extends string> = (
174+
export type SortMatcher<Field extends string> = (
175175
query: Query,
176176
input: Sort<Field> & SortInternals,
177177
) => Query<SortCol>;

0 commit comments

Comments
 (0)