Skip to content

Commit aaa6b66

Browse files
committed
New style sorting/filtering for Orgs, Partners, Partnerships
1 parent e8b6f6d commit aaa6b66

File tree

3 files changed

+65
-48
lines changed

3 files changed

+65
-48
lines changed

src/components/organization/organization.repository.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { DtoRepository } from '~/core/database';
1111
import {
1212
ACTIVE,
1313
createNode,
14+
defineSorters,
15+
filter,
1416
matchProjectScopedRoles,
1517
matchProjectSens,
1618
matchProps,
@@ -19,11 +21,12 @@ import {
1921
paginate,
2022
rankSens,
2123
requestingUser,
22-
sorting,
24+
sortWith,
2325
} from '~/core/database/query';
2426
import {
2527
CreateOrganization,
2628
Organization,
29+
OrganizationFilters,
2730
OrganizationListInput,
2831
UpdateOrganization,
2932
} from './dto';
@@ -111,20 +114,12 @@ export class OrganizationRepository extends DtoRepository<
111114
);
112115
}
113116

114-
async list({ filter, ...input }: OrganizationListInput, session: Session) {
117+
async list(input: OrganizationListInput, session: Session) {
115118
const query = this.db
116119
.query()
117120
.matchNode('node', 'Organization')
118-
.match([
119-
...(filter?.userId && session.userId
120-
? [
121-
node('node'),
122-
relation('in', '', 'organization', ACTIVE),
123-
node('user', 'User', { id: filter.userId }),
124-
]
125-
: []),
126-
])
127121
.match(requestingUser(session))
122+
.apply(organizationFilters(input.filter))
128123
.apply(
129124
this.privileges.forUser(session).filterToReadable({
130125
wrapContext: (inner) => (query) =>
@@ -141,8 +136,18 @@ export class OrganizationRepository extends DtoRepository<
141136
.apply(oncePerProject(inner)),
142137
}),
143138
)
144-
.apply(sorting(Organization, input))
139+
.apply(sortWith(organizationSorters, input))
145140
.apply(paginate(input, this.hydrate(session)));
146141
return (await query.first())!; // result from paginate() will always have 1 row.
147142
}
148143
}
144+
145+
export const organizationFilters = filter.define(() => OrganizationFilters, {
146+
userId: filter.pathExists((id) => [
147+
node('node'),
148+
relation('in', '', 'organization', ACTIVE),
149+
node('', 'User', { id }),
150+
]),
151+
});
152+
153+
export const organizationSorters = defineSorters(Organization, {});

src/components/partner/partner.repository.ts

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
collect,
1717
createNode,
1818
createRelationships,
19+
defineSorters,
1920
filter as filters,
2021
matchProjectScopedRoles,
2122
matchProjectSens,
@@ -25,9 +26,15 @@ import {
2526
paginate,
2627
rankSens,
2728
requestingUser,
28-
sorting,
29+
sortWith,
2930
} from '~/core/database/query';
30-
import { CreatePartner, Partner, PartnerListInput, UpdatePartner } from './dto';
31+
import {
32+
CreatePartner,
33+
Partner,
34+
PartnerFilters,
35+
PartnerListInput,
36+
UpdatePartner,
37+
} from './dto';
3138

3239
@Injectable()
3340
export class PartnerRepository extends DtoRepository<
@@ -262,28 +269,12 @@ export class PartnerRepository extends DtoRepository<
262269
);
263270
}
264271

265-
async list({ filter, ...input }: PartnerListInput, session: Session) {
272+
async list(input: PartnerListInput, session: Session) {
266273
const result = await this.db
267274
.query()
268275
.matchNode('node', 'Partner')
269-
.match([
270-
...(filter?.userId && session.userId
271-
? [
272-
node('node'),
273-
relation('out', '', 'organization', ACTIVE),
274-
node('', 'Organization'),
275-
relation('in', '', 'organization', ACTIVE),
276-
node('user', 'User', { id: filter.userId }),
277-
]
278-
: []),
279-
])
280276
.match(requestingUser(session))
281-
.apply(
282-
filters.builder(filter ?? {}, {
283-
pinned: filters.isPinned,
284-
userId: filters.skip, // already applied above
285-
}),
286-
)
277+
.apply(partnerFilters(input.filter))
287278
.apply(
288279
this.privileges.forUser(session).filterToReadable({
289280
wrapContext: (inner) => (query) =>
@@ -298,22 +289,33 @@ export class PartnerRepository extends DtoRepository<
298289
.apply(oncePerProject(inner)),
299290
}),
300291
)
301-
.apply(
302-
sorting(Partner, input, {
303-
name: (query) =>
304-
query
305-
.match([
306-
node('node'),
307-
relation('out', '', 'organization', ACTIVE),
308-
node('organization', 'Organization'),
309-
relation('out', '', 'name', ACTIVE),
310-
node('prop', 'Property'),
311-
])
312-
.return<{ sortValue: string }>('prop.value as sortValue'),
313-
}),
314-
)
292+
.apply(sortWith(partnerSorters, input))
315293
.apply(paginate(input, this.hydrate(session)))
316294
.first();
317295
return result!; // result from paginate() will always have 1 row.
318296
}
319297
}
298+
299+
export const partnerFilters = filters.define(() => PartnerFilters, {
300+
pinned: filters.isPinned,
301+
userId: filters.pathExists((id) => [
302+
node('node'),
303+
relation('out', '', 'organization', ACTIVE),
304+
node('', 'Organization'),
305+
relation('in', '', 'organization', ACTIVE),
306+
node('', 'User', { id }),
307+
]),
308+
});
309+
310+
export const partnerSorters = defineSorters(Partner, {
311+
name: (query) =>
312+
query
313+
.match([
314+
node('node'),
315+
relation('out', '', 'organization', ACTIVE),
316+
node('organization', 'Organization'),
317+
relation('out', '', 'name', ACTIVE),
318+
node('prop', 'Property'),
319+
])
320+
.return<{ sortValue: string }>('prop.value as sortValue'),
321+
});

src/components/partnership/partnership.repository.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
coalesce,
2121
createNode,
2222
createRelationships,
23+
defineSorters,
24+
filter,
2325
INACTIVE,
2426
matchChangesetAndChangedProps,
2527
matchProps,
@@ -28,7 +30,7 @@ import {
2830
oncePerProject,
2931
paginate,
3032
requestingUser,
31-
sorting,
33+
sortWith,
3234
whereNotDeletedInChangeset,
3335
} from '~/core/database/query';
3436
import { FileService } from '../file';
@@ -37,6 +39,7 @@ import {
3739
CreatePartnership,
3840
Partnership,
3941
PartnershipAgreementStatus,
42+
PartnershipFilters,
4043
PartnershipListInput,
4144
UpdatePartnership,
4245
} from './dto';
@@ -233,12 +236,13 @@ export class PartnershipRepository extends DtoRepository<
233236
),
234237
)
235238
.match(requestingUser(session))
239+
.apply(partnershipFilters(input.filter))
236240
.apply(
237241
this.privileges.forUser(session).filterToReadable({
238242
wrapContext: oncePerProject,
239243
}),
240244
)
241-
.apply(sorting(Partnership, input))
245+
.apply(sortWith(partnershipSorters, input))
242246
.apply(paginate(input, this.hydrate(session, viewOfChangeset(changeset))))
243247
.first();
244248
return result!; // result from paginate() will always have 1 row.
@@ -398,3 +402,9 @@ export class PartnershipRepository extends DtoRepository<
398402
};
399403
}
400404
}
405+
406+
export const partnershipFilters = filter.define(() => PartnershipFilters, {
407+
projectId: filter.skip,
408+
});
409+
410+
export const partnershipSorters = defineSorters(Partnership, {});

0 commit comments

Comments
 (0)