Skip to content

FieldRegion & FieldZone filtering & sorting #3503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/components/field-region/dto/list-field-region.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ import { InputType, ObjectType } from '@nestjs/graphql';
import {
FilterField,
type ID,
IdField,
PaginatedList,
SecuredList,
SortablePaginationInput,
} from '~/common';
import { FieldZoneFilters } from '../../field-zone/dto';
import { UserFilters } from '../../user/dto';
import { FieldRegion } from './field-region.dto';

@InputType()
export abstract class FieldRegionFilters {
readonly fieldZoneId?: ID;
@IdField({ optional: true })
readonly id?: ID<'FieldRegion'>;

@FilterField(() => UserFilters)
readonly director?: UserFilters & {};

@FilterField(() => FieldZoneFilters)
readonly fieldZone?: FieldZoneFilters & {};
}

@InputType()
Expand All @@ -19,7 +29,7 @@ export class FieldRegionListInput extends SortablePaginationInput<
>({
defaultSort: 'name',
}) {
@FilterField(() => FieldRegionFilters, { internal: true })
@FilterField(() => FieldRegionFilters)
readonly filter?: FieldRegionFilters;
}

Expand Down
56 changes: 53 additions & 3 deletions src/components/field-region/field-region.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ import {
ACTIVE,
createNode,
createRelationships,
defineSorters,
filter,
matchProps,
merge,
paginate,
sorting,
sortWith,
} from '~/core/database/query';
import {
fieldZoneFilters,
fieldZoneSorters,
} from '../field-zone/field-zone.repository';
import { userFilters, userSorters } from '../user/user.repository';
import {
type CreateFieldRegion,
FieldRegion,
FieldRegionFilters,
type FieldRegionListInput,
type UpdateFieldRegion,
} from './dto';
Expand Down Expand Up @@ -102,14 +110,15 @@ export class FieldRegionRepository extends DtoRepository(FieldRegion) {
);
}

async list({ filter, ...input }: FieldRegionListInput) {
async list(input: FieldRegionListInput) {
if (!this.privileges.can('read')) {
return SecuredList.Redacted;
}
const result = await this.db
.query()
.match(node('node', 'FieldRegion'))
.apply(sorting(FieldRegion, input))
.apply(fieldRegionFilters(input.filter))
.apply(sortWith(fieldRegionSorters, input))
.apply(paginate(input, this.hydrate()))
.first();
return result!; // result from paginate() will always have 1 row.
Expand All @@ -128,3 +137,44 @@ export class FieldRegionRepository extends DtoRepository(FieldRegion) {
.run();
}
}

export const fieldRegionFilters = filter.define(() => FieldRegionFilters, {
id: filter.baseNodeProp(),
director: filter.sub(() => userFilters)((sub) =>
sub.match([
node('outer'),
relation('out', '', 'director', ACTIVE),
node('node', 'User'),
]),
),
fieldZone: filter.sub(() => fieldZoneFilters)((sub) =>
sub.match([
node('outer'),
relation('out', '', 'zone', ACTIVE),
node('node', 'FieldZone'),
]),
),
});

export const fieldRegionSorters = defineSorters(FieldRegion, {
// eslint-disable-next-line @typescript-eslint/naming-convention
'director.*': (query, input) =>
query
.with('node as region')
.match([
node('region'),
relation('out', '', 'director', ACTIVE),
node('node', 'User'),
])
.apply(sortWith(userSorters, input)),
// eslint-disable-next-line @typescript-eslint/naming-convention
'fieldZone.*': (query, input) =>
query
.with('node as region')
.match([
node('region'),
relation('out', '', 'zone', ACTIVE),
node('node', 'FieldZone'),
])
.apply(sortWith(fieldZoneSorters, input)),
});
10 changes: 8 additions & 2 deletions src/components/field-zone/dto/list-field-zone.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import { InputType, ObjectType } from '@nestjs/graphql';
import {
FilterField,
type ID,
IdField,
PaginatedList,
SecuredList,
SortablePaginationInput,
} from '~/common';
import { UserFilters } from '../../user/dto';
import { FieldZone } from './field-zone.dto';

@InputType()
export abstract class FieldZoneFilters {
readonly fieldZoneId?: ID;
@IdField({ optional: true })
readonly id?: ID<'FieldZone'>;

@FilterField(() => UserFilters)
readonly director?: UserFilters & {};
}

@InputType()
Expand All @@ -19,7 +25,7 @@ export class FieldZoneListInput extends SortablePaginationInput<
>({
defaultSort: 'name',
}) {
@FilterField(() => FieldZoneFilters, { internal: true })
@FilterField(() => FieldZoneFilters)
readonly filter?: FieldZoneFilters;
}

Expand Down
35 changes: 32 additions & 3 deletions src/components/field-zone/field-zone.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ import {
ACTIVE,
createNode,
createRelationships,
defineSorters,
filter,
matchProps,
merge,
paginate,
sorting,
sortWith,
} from '~/core/database/query';
import { userFilters, userSorters } from '../user/user.repository';
import {
type CreateFieldZone,
FieldZone,
FieldZoneFilters,
type FieldZoneListInput,
type UpdateFieldZone,
} from './dto';
Expand Down Expand Up @@ -121,14 +125,15 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
await query.run();
}

async list({ filter, ...input }: FieldZoneListInput) {
async list(input: FieldZoneListInput) {
if (!this.privileges.can('read')) {
return SecuredList.Redacted;
}
const result = await this.db
.query()
.match(node('node', 'FieldZone'))
.apply(sorting(FieldZone, input))
.apply(fieldZoneFilters(input.filter))
.apply(sortWith(fieldZoneSorters, input))
.apply(paginate(input, this.hydrate()))
.first();
return result!; // result from paginate() will always have 1 row.
Expand All @@ -147,3 +152,27 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
.run();
}
}

export const fieldZoneFilters = filter.define(() => FieldZoneFilters, {
id: filter.baseNodeProp(),
director: filter.sub(() => userFilters)((sub) =>
sub.match([
node('outer'),
relation('out', '', 'director', ACTIVE),
node('node', 'User'),
]),
),
});

export const fieldZoneSorters = defineSorters(FieldZone, {
// eslint-disable-next-line @typescript-eslint/naming-convention
'director.*': (query, input) =>
query
.with('node as zone')
.match([
node('zone'),
relation('out', '', 'director', ACTIVE),
node('node', 'User'),
])
.apply(sortWith(userSorters, input)),
});
4 changes: 4 additions & 0 deletions src/components/project/dto/list-projects.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SortablePaginationInput,
} from '~/common';
import { Transform } from '~/common/transform.decorator';
import { FieldRegionFilters } from '../../field-region/dto';
import { LocationFilters } from '../../location/dto';
import { PartnershipFilters } from '../../partnership/dto';
import { ProjectMemberFilters } from '../project-member/dto';
Expand Down Expand Up @@ -134,6 +135,9 @@ export abstract class ProjectFilters {

@FilterField(() => LocationFilters)
readonly primaryLocation?: LocationFilters & {};

@FilterField(() => FieldRegionFilters)
readonly fieldRegion?: FieldRegionFilters & {};
}

Object.defineProperty(ProjectFilters.prototype, 'mine', {
Expand Down
8 changes: 8 additions & 0 deletions src/components/project/project-filters.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
path,
variable,
} from '~/core/database/query';
import { fieldRegionFilters } from '../field-region/field-region.repository';
import { locationFilters } from '../location/location.repository';
import { partnershipFilters } from '../partnership/partnership.repository';
import { ProjectFilters } from './dto';
Expand Down Expand Up @@ -107,6 +108,13 @@ export const projectFilters = filter.define(() => ProjectFilters, {
node('node', 'Location'),
]),
),
fieldRegion: filter.sub(() => fieldRegionFilters)((sub) =>
sub.match([
node('outer'),
relation('out', '', 'fieldRegion', ACTIVE),
node('node', 'FieldRegion'),
]),
),
sensitivity: ({ value, query }) =>
query
.apply(matchProjectSens('node'))
Expand Down
18 changes: 18 additions & 0 deletions src/components/project/project.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
variable,
} from '~/core/database/query';
import { Privileges } from '../authorization';
import { fieldRegionSorters } from '../field-region/field-region.repository';
import { locationSorters } from '../location/location.repository';
import { partnershipSorters } from '../partnership/partnership.repository';
import {
Expand Down Expand Up @@ -456,4 +457,21 @@ export const projectSorters = defineSorters(IProject, {
.where(not(path(getPath(true))))
.return<SortCol>('null as sortValue');
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'fieldRegion.*': (query, input) => {
const getPath = (anon = false) => [
node('project'),
relation('out', '', 'fieldRegion', ACTIVE),
node(anon ? '' : 'node', 'FieldRegion'),
];
return query
.with('node as project')
.match(getPath())
.apply(sortWith(fieldRegionSorters, input))
.union()
.with('node')
.with('node as project')
.where(not(path(getPath(true))))
.return<SortCol>('null as sortValue');
},
});