Skip to content

Commit

Permalink
Add sorting for progress summaries (#3306)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF authored Oct 9, 2024
1 parent 4db4ce2 commit 396bcb4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Injectable } from '@nestjs/common';
import { node, relation } from 'cypher-query-builder';
import { mapEntries } from '@seedcompany/common';
import { node, not, relation } from 'cypher-query-builder';
import {
CreateNodeOptions,
DefinedSorters,
defineSorters,
exp,
path,
QueryFragment,
SortCol,
SortFieldOf,
SortMatcher,
sortWith,
variable,
} from '~/core/database/query';
import { engagementSorters } from '../engagement/engagement.repository';
import { MergePeriodicReports } from '../periodic-report/dto';
import { SummaryPeriod } from '../progress-summary/dto';
import { progressSummarySorters } from '../progress-summary/progress-summary.repository';
import { ProgressReport, ProgressReportStatus as Status } from './dto';

@Injectable()
Expand Down Expand Up @@ -52,4 +59,37 @@ export const progressReportExtrasSorters: DefinedSorters<
node('node', 'LanguageEngagement'),
])
.apply(sortWith(engagementSorters, input)),
...mapEntries(
[
{ field: 'cumulativeSummary', period: SummaryPeriod.Cumulative },
{ field: 'fiscalYearSummary', period: SummaryPeriod.FiscalYearSoFar },
{ field: 'periodSummary', period: SummaryPeriod.ReportPeriod },
],
({ field, period }) => {
const periodVar = { period: variable(`"${period}"`) };
const matcher: SortMatcher<string> = (query, input) =>
query
.with('node as report')
.match([
node('report'),
relation('out', '', 'summary'),
node('node', 'ProgressSummary', periodVar),
])
.apply(sortWith(progressSummarySorters, input))
.union()
.with('node')
.with('node as report')
.where(
not(
path([
node('report'),
relation('out', '', 'summary'),
node('', 'ProgressSummary', periodVar),
]),
),
)
.return<SortCol>('null as sortValue');
return [`${field}.*`, matcher];
},
).asRecord,
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LinkTo } from '~/core/resources';
export abstract class ProgressSummary {
static readonly Props = keysOf<ProgressSummary>();
static readonly SecuredProps = keysOf<SecuredProps<ProgressSummary>>();
static readonly BaseNodeProps = ['planned', 'actual'];

@Field(() => Float)
planned: number;
Expand Down
19 changes: 17 additions & 2 deletions src/components/progress-summary/progress-summary.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Injectable } from '@nestjs/common';
import { inArray, node, relation } from 'cypher-query-builder';
import { mapValues } from '@seedcompany/common';
import { inArray, node, Query, relation } from 'cypher-query-builder';
import { ID } from '~/common';
import { CommonRepository } from '~/core/database';
import { ACTIVE, listConcat, merge } from '~/core/database/query';
import {
ACTIVE,
defineSorters,
listConcat,
merge,
SortCol,
} from '~/core/database/query';
import { ProgressReport } from '../progress-report/dto';
import { FetchedSummaries, ProgressSummary, SummaryPeriod } from './dto';

Expand Down Expand Up @@ -80,3 +87,11 @@ export class ProgressSummaryRepository extends CommonRepository {
.run();
}
}

export const progressSummarySorters = defineSorters(ProgressSummary, {
...mapValues.fromList(
['variance', 'scheduleStatus'],
() => (query: Query) =>
query.return<SortCol>('(node.actual - node.planned) as sortValue'),
).asRecord,
});
2 changes: 1 addition & 1 deletion src/core/database/query/sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export interface SortCol {
export type SortFieldOf<TResourceStatic extends ResourceShape<any>> =
LiteralUnion<keyof TResourceStatic['prototype'] & string, string>;

type SortMatcher<Field extends string> = (
export type SortMatcher<Field extends string> = (
query: Query,
input: Sort<Field> & SortInternals,
) => Query<SortCol>;
Expand Down

0 comments on commit 396bcb4

Please sign in to comment.