@@ -11,7 +11,7 @@ import {
11
11
} from 'cypher-query-builder' ;
12
12
import { Direction } from 'cypher-query-builder/dist/typings/clauses/order-by' ;
13
13
import { AnyConditions } from 'cypher-query-builder/dist/typings/clauses/where-utils' ;
14
- import { isEmpty } from 'lodash' ;
14
+ import { isEmpty , keyBy } from 'lodash' ;
15
15
import { DateTime } from 'luxon' ;
16
16
import {
17
17
ID ,
@@ -174,7 +174,27 @@ export class FileRepository extends CommonRepository {
174
174
. where ( { node : hasLabel ( FileNodeType . Directory ) } )
175
175
. apply ( this . hydrateDirectory ( ) ) ,
176
176
)
177
- . return < { dto : FileNode } > ( 'dto' ) ;
177
+ . subQuery ( 'node' , ( sub ) =>
178
+ sub
179
+ . raw ( 'MATCH p=(node)-[:parent*0..]->(root:FileNode)' )
180
+ . return ( 'root' )
181
+ . orderBy ( 'length(p)' , 'DESC' )
182
+ . raw ( 'LIMIT 1' ) ,
183
+ )
184
+ . subQuery ( 'root' , ( sub ) =>
185
+ sub
186
+ . raw ( 'MATCH (resource:BaseNode)-[rel]->(root)' )
187
+ // Need to filter out FileNodes which are children of this dir
188
+ // (the schema was mistakenly pointing these relationships in the wrong direction)
189
+ // Also filter to ACTIVE, if applicable.
190
+ . raw (
191
+ 'WHERE NOT resource:FileNode AND coalesce(rel.active, true) <> false' ,
192
+ )
193
+ . return ( '[resource, type(rel)] as rootAttachedTo' ) ,
194
+ )
195
+ . return < { dto : FileNode } > (
196
+ merge ( 'dto' , keyBy ( [ 'root' , 'rootAttachedTo' ] ) ) . as ( 'dto' ) ,
197
+ ) ;
178
198
}
179
199
180
200
private hydrateFile ( ) {
0 commit comments