Skip to content

Commit 8432cc5

Browse files
authored
Merge pull request #3262 from SeedCompany/location-filters
2 parents f0c790e + 0bc2d41 commit 8432cc5

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

src/components/location/dto/list-locations.dto.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InputType, ObjectType } from '@nestjs/graphql';
1+
import { Field, InputType, ObjectType } from '@nestjs/graphql';
22
import {
33
FilterField,
44
ID,
@@ -10,14 +10,19 @@ import { Location } from './location.dto';
1010

1111
@InputType()
1212
export abstract class LocationFilters {
13+
@Field({
14+
nullable: true,
15+
})
16+
readonly name?: string;
17+
1318
readonly fundingAccountId?: ID;
1419
}
1520

1621
@InputType()
1722
export class LocationListInput extends SortablePaginationInput<keyof Location>({
1823
defaultSort: 'name',
1924
}) {
20-
@FilterField(() => LocationFilters, { internal: true })
25+
@FilterField(() => LocationFilters)
2126
readonly filter?: LocationFilters;
2227
}
2328

src/components/location/location.repository.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import {
99
Session,
1010
UnsecuredDto,
1111
} from '~/common';
12-
import { DtoRepository } from '~/core/database';
12+
import { DtoRepository, OnIndex } from '~/core/database';
1313
import {
1414
ACTIVE,
1515
createNode,
1616
createRelationships,
1717
defineSorters,
18+
filter,
19+
FullTextIndex,
1820
matchProps,
1921
merge,
2022
paginate,
@@ -25,6 +27,7 @@ import { FileId } from '../file/dto';
2527
import {
2628
CreateLocation,
2729
Location,
30+
LocationFilters,
2831
LocationListInput,
2932
UpdateLocation,
3033
} from './dto';
@@ -157,10 +160,11 @@ export class LocationRepository extends DtoRepository(Location) {
157160
);
158161
}
159162

160-
async list({ filter, ...input }: LocationListInput) {
163+
async list(input: LocationListInput) {
161164
const result = await this.db
162165
.query()
163166
.matchNode('node', 'Location')
167+
.apply(locationFilters(input.filter))
164168
.apply(sortWith(locationSorters, input))
165169
.apply(paginate(input, this.hydrate()))
166170
.first();
@@ -231,6 +235,35 @@ export class LocationRepository extends DtoRepository(Location) {
231235
.first();
232236
return !!result;
233237
}
238+
239+
@OnIndex('schema')
240+
private async createSchemaIndexes() {
241+
await this.db.query().apply(NameIndex.create()).run();
242+
}
234243
}
235244

236245
export const locationSorters = defineSorters(Location, {});
246+
247+
export const locationFilters = filter.define(() => LocationFilters, {
248+
name: filter.fullText({
249+
index: () => NameIndex,
250+
matchToNode: (q) =>
251+
q.match([
252+
node('node', 'Location'),
253+
relation('out', '', undefined, ACTIVE),
254+
node('match'),
255+
]),
256+
}),
257+
fundingAccountId: filter.pathExists((id) => [
258+
node('node'),
259+
relation('out', '', 'fundingAccount', ACTIVE),
260+
node('', 'FundingAccount', { id }),
261+
]),
262+
});
263+
264+
const NameIndex = FullTextIndex({
265+
indexName: 'LocationName',
266+
labels: 'LocationName',
267+
properties: 'value',
268+
analyzer: 'standard-folding',
269+
});

src/components/project/dto/list-projects.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Sensitivity,
1212
SortablePaginationInput,
1313
} from '~/common';
14+
import { LocationFilters } from '../../location/dto';
1415
import { PartnershipFilters } from '../../partnership/dto';
1516
import { ProjectStatus } from './project-status.enum';
1617
import { ProjectStep } from './project-step.enum';
@@ -103,6 +104,9 @@ export abstract class ProjectFilters {
103104

104105
@FilterField(() => PartnershipFilters)
105106
readonly primaryPartnership?: PartnershipFilters & {};
107+
108+
@FilterField(() => LocationFilters)
109+
readonly primaryLocation?: LocationFilters & {};
106110
}
107111

108112
@InputType()

src/components/project/project-filters.query.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
path,
77
variable,
88
} from '~/core/database/query';
9+
import { locationFilters } from '../location/location.repository';
910
import { partnershipFilters } from '../partnership/partnership.repository';
1011
import { ProjectFilters } from './dto';
1112
import { ProjectNameIndex } from './project.repository';
@@ -103,4 +104,13 @@ export const projectFilters = filter.define(() => ProjectFilters, {
103104
node('', 'Property', { value: variable('true') }),
104105
]),
105106
),
107+
primaryLocation: filter.sub(() => locationFilters)((sub) =>
108+
sub
109+
.with('node as project')
110+
.match([
111+
node('project'),
112+
relation('out', '', 'primaryLocation', ACTIVE),
113+
node('node', 'Location'),
114+
]),
115+
),
106116
});

0 commit comments

Comments
 (0)