@@ -16,6 +16,8 @@ import {
16
16
collect ,
17
17
createNode ,
18
18
createRelationships ,
19
+ defineSorters ,
20
+ filter ,
19
21
filter as filters ,
20
22
matchProjectScopedRoles ,
21
23
matchProjectSens ,
@@ -25,9 +27,19 @@ import {
25
27
paginate ,
26
28
rankSens ,
27
29
requestingUser ,
28
- sorting ,
30
+ sortWith ,
29
31
} from '~/core/database/query' ;
30
- import { CreatePartner , Partner , PartnerListInput , UpdatePartner } from './dto' ;
32
+ import {
33
+ organizationFilters ,
34
+ organizationSorters ,
35
+ } from '../organization/organization.repository' ;
36
+ import {
37
+ CreatePartner ,
38
+ Partner ,
39
+ PartnerFilters ,
40
+ PartnerListInput ,
41
+ UpdatePartner ,
42
+ } from './dto' ;
31
43
32
44
@Injectable ( )
33
45
export class PartnerRepository extends DtoRepository <
@@ -262,28 +274,12 @@ export class PartnerRepository extends DtoRepository<
262
274
) ;
263
275
}
264
276
265
- async list ( { filter , ... input } : PartnerListInput , session : Session ) {
277
+ async list ( input : PartnerListInput , session : Session ) {
266
278
const result = await this . db
267
279
. query ( )
268
280
. 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
- ] )
280
281
. match ( requestingUser ( session ) )
281
- . apply (
282
- filters . builder ( filter ?? { } , {
283
- pinned : filters . isPinned ,
284
- userId : filters . skip , // already applied above
285
- } ) ,
286
- )
282
+ . apply ( partnerFilters ( input . filter ) )
287
283
. apply (
288
284
this . privileges . forUser ( session ) . filterToReadable ( {
289
285
wrapContext : ( inner ) => ( query ) =>
@@ -298,22 +294,52 @@ export class PartnerRepository extends DtoRepository<
298
294
. apply ( oncePerProject ( inner ) ) ,
299
295
} ) ,
300
296
)
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
- )
297
+ . apply ( sortWith ( partnerSorters , input ) )
315
298
. apply ( paginate ( input , this . hydrate ( session ) ) )
316
299
. first ( ) ;
317
300
return result ! ; // result from paginate() will always have 1 row.
318
301
}
319
302
}
303
+
304
+ export const partnerFilters = filters . define ( ( ) => PartnerFilters , {
305
+ pinned : filters . isPinned ,
306
+ userId : filters . pathExists ( ( id ) => [
307
+ node ( 'node' ) ,
308
+ relation ( 'out' , '' , 'organization' , ACTIVE ) ,
309
+ node ( '' , 'Organization' ) ,
310
+ relation ( 'in' , '' , 'organization' , ACTIVE ) ,
311
+ node ( '' , 'User' , { id } ) ,
312
+ ] ) ,
313
+ organization : filter . sub ( ( ) => organizationFilters ) ( ( sub ) =>
314
+ sub
315
+ . with ( 'node as partner' )
316
+ . match ( [
317
+ node ( 'partner' ) ,
318
+ relation ( 'out' , '' , 'organization' ) ,
319
+ node ( 'node' , 'Organization' ) ,
320
+ ] ) ,
321
+ ) ,
322
+ } ) ;
323
+
324
+ export const partnerSorters = defineSorters ( Partner , {
325
+ name : ( query ) =>
326
+ query
327
+ . match ( [
328
+ node ( 'node' ) ,
329
+ relation ( 'out' , '' , 'organization' , ACTIVE ) ,
330
+ node ( 'organization' , 'Organization' ) ,
331
+ relation ( 'out' , '' , 'name' , ACTIVE ) ,
332
+ node ( 'prop' , 'Property' ) ,
333
+ ] )
334
+ . return < { sortValue : string } > ( 'prop.value as sortValue' ) ,
335
+ // eslint-disable-next-line @typescript-eslint/naming-convention
336
+ 'organization.*' : ( query , input ) =>
337
+ query
338
+ . with ( 'node as partner' )
339
+ . match ( [
340
+ node ( 'partner' ) ,
341
+ relation ( 'out' , '' , 'organization' ) ,
342
+ node ( 'node' , 'Organization' ) ,
343
+ ] )
344
+ . apply ( sortWith ( organizationSorters , input ) ) ,
345
+ } ) ;
0 commit comments